通过PyAudio录制音频时,如何指定要使用的确切输入设备?
我的电脑有两个麦克风,一个内置,一个通过USB,我想用USB麦克风录音。 this有input_device_index
用于选择设备,但目前还不清楚该索引与设备的关联方式。例如,我如何知道哪个设备索引0引用?如果我不得不猜测,我会说0指的是内置设备,而1指的是USB设备,但我想找一些程序化的方法来确认这一点。在Linux上,有没有办法获取这些索引及其引用的设备的列表?
答案 0 :(得分:17)
你可以使用: get_device_info_by_host_api_device_index。 例如:
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')
答案 1 :(得分:2)
我没有看过pyaudio
,但我也很少使用sounddevice。
以下是列出可用input
和output
audio devices
的示例代码。
import sounddevice as sd
print sd.query_devices()
从下面的输出可以看出,当我将耳机放到麦克风插孔时,Index 1
可用作输入。
1 Jack Mic (IDT High Definition A, MME (2 in, 0 out)
默认笔记本电脑音频麦克风显示为index 2
2 Microphone Array (IDT High Defi, MME (2 in, 0 out)
<强>输出强>
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
0 Microsoft Sound Mapper - Input, MME (2 in, 0 out)
> 1 Jack Mic (IDT High Definition A, MME (2 in, 0 out)
2 Microphone Array (IDT High Defi, MME (2 in, 0 out)
3 Microsoft Sound Mapper - Output, MME (0 in, 2 out)
< 4 Speakers / Headphones (IDT High, MME (0 in, 2 out)
5 Communication Headphones (IDT H, MME (0 in, 2 out)
6 Primary Sound Capture Driver, Windows DirectSound (2 in, 0 out)
7 Jack Mic (IDT High Definition Audio CODEC), Windows DirectSound (2 in, 0 out)
8 Microphone Array (IDT High Definition Audio CODEC), Windows DirectSound (2 in, 0 out)
9 Primary Sound Driver, Windows DirectSound (0 in, 2 out)
10 Speakers / Headphones (IDT High Definition Audio CODEC), Windows DirectSound (0 in, 2 out)
11 Communication Headphones (IDT High Definition Audio CODEC), Windows DirectSound (0 in, 2 out)
12 Communication Headphones (IDT High Definition Audio CODEC), Windows WASAPI (0 in, 2 out)
13 Speakers / Headphones (IDT High Definition Audio CODEC), Windows WASAPI (0 in, 2 out)
14 Jack Mic (IDT High Definition Audio CODEC), Windows WASAPI (2 in, 0 out)
15 Microphone Array (IDT High Definition Audio CODEC), Windows WASAPI (2 in, 0 out)
16 Headset Microphone (Bluetooth Hands-free Audio), Windows WDM-KS (1 in, 0 out)
17 Headphones (Bluetooth Hands-free Audio), Windows WDM-KS (0 in, 2 out)
18 Headphones (HpOut), Windows WDM-KS (0 in, 2 out)
19 Microphone Array (MicIn2), Windows WDM-KS (2 in, 0 out)
20 Jack Mic (MuxedIn), Windows WDM-KS (2 in, 0 out)
21 Dock Mic (MuxedIn), Windows WDM-KS (2 in, 0 out)
22 Rec. Playback (MuxedIn), Windows WDM-KS (2 in, 0 out)
23 Speakers (Speaker/HP), Windows WDM-KS (0 in, 2 out)
答案 2 :(得分:0)
我不了解PyAudio,但是使用sounddevice模块就是这样:
python3 -m sounddevice
答案 3 :(得分:0)
只需使用arecord -l
列出所有可用的输入设备。
答案 4 :(得分:0)
在PyAudio Documentation中,您可以定义一个input_device_index
。
要了解该设备索引是什么,可以遵循此Github Gist中提供的代码,也可以遵循Raspberry Pi Forum中提供的代码(提供输出代码示例)中的代码。
答案 5 :(得分:0)
您可以使用 PulseAudio 选择输入设备。