如何使用 PyAudio 选择特定的输入设备 [英] How to select a specific input device with PyAudio
问题描述
通过 PyAudio 录制音频时,如何指定要使用的确切输入设备?
When recording audio via PyAudio, how do you specify the exact input device to use?
我的电脑有两个麦克风,一个是内置的,一个是通过 USB 的,我想用 USB 麦克风录音.流类有一个input_device_index
用于选择设备,但尚不清楚该指数与设备的相关性.例如,我怎么知道哪个设备索引 0 指的是?如果我不得不猜测,我会说 0 指的是内置设备,而 1 指的是 USB 设备,但我想找到一些程序化的方式来确认这一点.在 Linux 上,有没有办法获取这些索引及其引用的设备的列表?
My computer has two microphones, one built-in and one via USB, and I want to record using the USB mic. The Stream class has an input_device_index
for selecting the device, but it's unclear how this index correlates to the devices. For example, how do I know which device index 0 refers to? If I had to guess, I'd say 0 refers to the built-in device while 1 refers to the USB device, but I'd like to find some programmatic way of confirming this. On Linux, is there a way to get a list of these indexes and the devices they refer to?
推荐答案
您可以使用:get_device_info_by_host_api_device_index.例如:
you can use: get_device_info_by_host_api_device_index. For instance:
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).get('name')
这篇关于如何使用 PyAudio 选择特定的输入设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!