我正在尝试选择要在Python中使用PyAudio库进行录制的设备,但我不知道该怎么做。我在网上找到了此代码,其中显示了所有可用的输入设备:
import pyaudio
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
print("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i))
但是,这如何工作,如何从该列表中选择要使用的设备?我似乎无法在网上找到有关选择要使用的设备的信息,因此,如果有人可以帮助我,那将是非常棒的,谢谢。
列出设备后(通过打印代码,如问题代码中所示,可以选择要使用的设备的索引。
即它可能会打印出来
('Input Device id ', 2, ' - ', u'USB Sound Device: Audio (hw:1,0)')
('Input Device id ', 3, ' - ', u'sysdefault')
('Input Device id ', 11, ' - ', u'spdif')
('Input Device id ', 12, ' - ', u'default')
然后从该特定设备开始记录,您需要打开一个PyAudio流:
# Open stream with the index of the chosen device you selected from your initial code
stream = p.open(format=p.get_format_from_width(width=2),
channels=1,
output=True,
rate=OUTPUT_SAMPLE_RATE,
input_device_index=INDEX_OF_CHOSEN_INPUT_DEVICE, # This is where you specify which input device to use
stream_callback=callback)
# Start processing and do whatever else...
stream.start_stream()
有关流选项的更多信息,请看PyAudio's official documentation上指定的配置。
如果您需要更多脚本的帮助,我建议您查看使用PyAudio进行无阻塞音频的简单示例,available on their documentation.