pyAudio.PyAudio()。open()函数接受一个名为“input_device_index”的参数,其中,如果我给出了一个代表我所需麦克风的索引号,它就会使用它。此外,另一个称为“输入”的参数也必须为“真”。
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 48000
RECORD_SECONDS = 2
WAVE_OUTPUT_FILENAME = "output.wav"
INPUT_DEVICE_INDEX = 3
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index= INPUT_DEVICE_INDEX,
frames_per_buffer=CHUNK)
我使用以下方法确定了我所需设备的索引:
get_device_info_by_index(i)
其中i是表示特定索引号的整数。对于这种情况,我的'i'是3.哪个输出:
{'defaultHighInputLatency': 0.1,
'defaultHighOutputLatency': 0.0126875,
'defaultLowInputLatency': 0.01,
'defaultLowOutputLatency': 0.0033541666666666668,
'defaultSampleRate': 48000.0,
'hostApi': 0,
'index': 3,
'maxInputChannels': 2,
'maxOutputChannels': 2,
'name': 'Scarlett 2i2 USB',
'structVersion': 2}
但是,在open()函数中输入“input_device_index”并运行音频记录功能后,我遇到了错误:
File "<ipython-input-145-166f13e76ff1>", line 1, in <module>
record()
File "/Users/aaron.yong/python_main/audio_test_v5.py", line 56, in record
frames_per_buffer=CHUNK)
File "/anaconda/lib/python3.6/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/anaconda/lib/python3.6/site-packages/pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
OSError: [Errno -9986] Internal PortAudio error
答案 0 :(得分:0)
我很抱歉,在重新启动编译器之后,代码终究可行了。我已经粘贴了一段pyaudio.py的初始函数,它显示了它是如何工作的。
谢谢!
def __init__(self,
PA_manager,
rate,
channels,
format,
input=False,
output=False,
input_device_index=None,
output_device_index=None,
frames_per_buffer=1024,
start=True,
input_host_api_specific_stream_info=None,
output_host_api_specific_stream_info=None,
stream_callback=None):
"""
Initialize a stream; this should be called by
:py:func:`PyAudio.open`. A stream can either be input, output,
or both.
:param PA_manager: A reference to the managing :py:class:`PyAudio`
instance
:param rate: Sampling rate
:param channels: Number of channels
:param format: Sampling size and format. See |PaSampleFormat|.
:param input: Specifies whether this is an input stream.
Defaults to ``False``.
:param output: Specifies whether this is an output stream.
Defaults to ``False``.
:param input_device_index: Index of Input Device to use.
Unspecified (or ``None``) uses default device.
Ignored if `input` is ``False``.
:param output_device_index:
Index of Output Device to use.
Unspecified (or ``None``) uses the default device.
Ignored if `output` is ``False``.
:param frames_per_buffer: Specifies the number of frames per buffer.
:param start: Start the stream running immediately.
Defaults to ``True``. In general, there is no reason to set
this to ``False``.
:param input_host_api_specific_stream_info: Specifies a host API
specific stream information data structure for input.