Find all the microphone names and device index in Python using PyAudio
If you want to work with a microphone in your Python program, then you must have to know the device id of your mic. In this python tutorial, I will make it easy to get the mic id or index in Python. We will find mic device names and index attached to our machine in our python program.
Let’s get started.
Python program to find the mic names id in PyAudio
To work with the microphone or audio input device in your Python program, you have to enter the device name or index to specify which device you want to use.
To find the mic device attached to your computer you will need to have the following packages installed on your python.
Both of them are available in pip to install.
The commands are:
pip install SpeechRecognition pip install pyaudio
For Mac users install the following packages first:
brew install portaudio pip install pyaudio
Linux Users may use this:
$ sudo apt-get install python-pyaudio python3-pyaudio
If this does not work, then the possible reason is that you are running a virtual environment. In that case, install pyaudio using pip.
That’s It. Now you are all set to get the mic list attached to your device.
- Get voice input with microphone in Python using PyAudio and SpeechRecognition
- Voice Command Calculator in Python using speech recognition and PyAudio
Import speech_recognition module to use microphone class
Using the below code we can import the package.
import speech_recognition as s_r
The following line will return all the mic devices attached to your PC.
Now what you have to do is just simply print the output to get the microphones.
Print all the mic connected to your machine in Python
import speech_recognition as s_r print(s_r.Microphone.list_microphone_names()) #print all the microphones connected to your machine
['Microsoft Sound Mapper - Input', 'Microphone (Realtek High Defini', 'Microsoft Sound Mapper - Output', 'Speaker/HP (Realtek High Defini']
In my case, this is the output. The output might be different from mine, as it depends on the attached devices.
Now the question arises which device will work properly and how to use these microphones.
Find mic device index
As you are a programmer you know in an array index starts with zero. Thus the very first device listed in the above array holds the index 0. The second one holds the index 1 and so on.
In my case, ‘Microphone (Realtek High Defini’ works properly on my machine as a mic. So I will pick this one.
So how to pick it?
mic = s_r.Microphone(device_index=1)
To work further with microphone follow the next tutorial,