How to choose a specific input device using PyAudio

When recording audio through PyAudio, how do you indicate the exact input device to use?

My computer has two microphones, one built-in and one via USB, and I want to record using a USB microphone. The stream class has input_device_index for device selection, but it is unclear how this index correlates with devices. For example, how do I know which device index is 0? If I were to guess, I would say that 0 refers to the built-in device, and 1 refers to the USB device, but I would like to find some software way to confirm this. On Linux, is there a way to get a list of these indexes and the devices to which they refer?

+8
source share
5 answers

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') 
+18
source

I haven’t looked at pyaudio but I used sounddevice as well as a few cases.

Here is a sample code that lists the available input and output audio devices .

 import sounddevice as sd print sd.query_devices() 

As you can see from the downstream output, when I install the headset on the microphone jack, Index 1 is available as an input. 1 Jack Mic (IDT High Definition A, MME (2 in, 0 out)

While the sound microphone for a laptop looks like index 2 by default

2 Microphone Array (IDT High Defi, MME (2 in, 0 out)

Output

 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
source

I do not know about PyAudio, but with the sounddevice module it looks like this:

 python3 -m sounddevice 
0
source

Just use arecord -l to get a list of all available input devices.

0
source

The PyAudio documentation says that you can define input_device_index .

To find out what the device index is, you can follow the code provided in this Github Gist or at the next Raspberry Pi forum, where an example of the code displayed is provided.

0
source

All Articles