How to Select a Specific Input Device with Pyaudio

How to select which device to record with (Python PyAudio)

After you have listed the devices out (by printing them as you have shown in the code from the question) you can choose which index of the devices you want to use.

i.e. it may print out

('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')

And then to start recording from that specific device, you need to open a PyAudio stream:

# 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()

For more information regarding the stream options, take a look at the config specified on PyAudio's official documentation.

If you need help with more of the script I recommend looking at the simple example for non-blocking audio with PyAudio, available on their documentation.

Record mic with pyaudio

You can try to see whether you are using the right input device. Add the input_device_index={the right input device} argument to the audio.open.

You can check the ids of your devices like so: How to select a specific input device with 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).get('name')

Isolating Input Device Audio and Output Device Audio with PyAudio

Windows actually offers a native way to record your systems audio without having to install to use other solutions. I primarily used this method because I had no idea how to install the PyAudio Fork mentioned here.

If you go to your system's sound settings, assuming its Windows, you will find a recording tab in the upper left hand corner. Upon clicking this, you'll also find a
"Stereo Mix" icon which is normally disabled.Sound Settings with Stereo Mix shown2

If you enable this, this channel will capture all the devices output audio without needing to set up anything else. Therefore, you can set input_device_index with whatever the index of Stereo Mix on your computer is.

PyAudio cannot find any output devices

Its likely a broken file that needs replacement.

  1. Search for "/usr/./libstdc++.so.6" where pip installed it.

  2. Search for "/usr/lib/x86_64-linux-gnu/libstdc++.so.6" and copy to location at (1). This should fix it.

  3. If you have Anaconda version 3 - 4.3.0 or 4.4.0 or earlier installed then check at "anaconda3/lib/libstdc++.so.6" and replance with (2).

Alternatively A) rename the file and place the file from (2) where you renamed the old libstdc++.so.6 file or B) upgrade to Anaconda to 5.0.1 version and run conda update --all. And if you do "B" don't forget to remove the eggs or whls manually first to be sure there is no chance installing old software from there again.

This should fix it for you.

Enjoy ;-)



Related Topics



Leave a reply



Submit