如何为 PyAudio 使用外部麦克风而不是内置麦克风?
How do I use an external microphone for PyAudio instead of the in-built microphone?
pyAudio.PyAudio().open() 函数接受一个称为 "input_device_index" 的参数,其中,如果我给出了代表我想要的麦克风的特定索引号,它就会使用它。此外,另一个称为 "Input" 的参数也必须是 "True"。
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
抱歉,重新启动编译器后,代码终于可以运行了。我粘贴了一段 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.
pyAudio.PyAudio().open() 函数接受一个称为 "input_device_index" 的参数,其中,如果我给出了代表我想要的麦克风的特定索引号,它就会使用它。此外,另一个称为 "Input" 的参数也必须是 "True"。
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
抱歉,重新启动编译器后,代码终于可以运行了。我粘贴了一段 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.