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.

  • SpeechRecognition
  • PyAudio

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.

Also, learn:

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.

s_r.Microphone.list_microphone_names()

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

Output:

['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,

Get voice input with microphone in Python using PyAudio and SpeechRecognition

2 responses to “Find all the microphone names and device index in Python using PyAudio”

  1. Dave Kimble says:

    If your audience needs to be told the index starts from 0, then they also need to be told to: import speechrecognition as sr
    import pyaudio
    Why is speechrecognition involved at all?
    Can’t it be done with pyaudio alone?

    • Saruque Ahamed Mollick says:

      Yeah, you are right.
      This has been told just to make sure they have the right device index. They will be able to test the device index through this.

Leave a Reply

Your email address will not be published. Required fields are marked *