Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a specific input device with PyAudio

Tags:

When recording audio via PyAudio, how do you specify the exact input device to use?

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?

like image 258
Cerin Avatar asked Apr 27 '16 15:04

Cerin


People also ask

How do you use PyAudio?

To use PyAudio, first instantiate PyAudio using pyaudio. PyAudio() (1), which sets up the portaudio system. To record or play audio, open a stream on the desired device with the desired audio parameters using pyaudio. PyAudio.

How do I choose an input device for Windows?

Select Start (Windows logo Start button) > Settings (Gear-shaped Settings icon) > System > Sound. In Sound settings, go to Input > Choose your input device, and then select the microphone or recording device you want to use.


2 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') 
like image 138
slegroux Avatar answered Oct 16 '22 05:10

slegroux


I havent looked at pyaudio but I've used sounddevice as well on few occasions.

Here is an example code that lists available input and output audio devices.

import sounddevice as sd print sd.query_devices()  

As you can see from below output, when I put my headset to mic jack , Index 1is available as input. 1 Jack Mic (IDT High Definition A, MME (2 in, 0 out)

While default laptop audio microphone comes up as index 2

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) 
like image 37
Anil_M Avatar answered Oct 16 '22 04:10

Anil_M