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.


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,

Get voice input with microphone in Python using PyAudio and SpeechRecognition

13 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.

  2. Lakhan Raj Saini says:

    Will it work with wireless mic too ? How could I work with wireless mic ?

    • Saruque Ahamed Mollick says:

      Yes, It will work for the wireless mic too. You just need to connect your device first. Then find the device ID with the same process. It will show you the microphone device name.

  3. amila says:

    I had an issue with the mic and error was not defining the device_index
    Solved it thanks to you. I was searching everywhere but did not find a clear answer Your explanation is crystal clear thank you so much again ….!!!!

  4. Loga says:

    thanks for your clear explanation. I have pyaudio and SpeechRecogntion installed. I have my laptop microphone as well an external microphone connected. I could see that they are working in the input device under sound settings. But still the above command is giving me an empty list as if no microphones are available. Can you pls help me with this.

  5. James Kulu says:

    I got OSError: [Errno -9985] Device unavailable error. Please help me

  6. Abdulaziz says:

    sir here my program works but stuck on listenning… and didnot proceed further.

    r = sr.Recognizer()
    with sr.Microphone(device_index=22) as source:

    audio = r.listen(source)
    query = r.recognize_google(audio, language=’en-in’) # Using google for voice recognition.
    print(f”User said: {query}\n”) # User query will be printed.

    except Exception as e:
    # print(e)
    speak(“Say that again please…”) # Say that again will be printed in case of improper voice
    return “None” # None string will be returned
    return query

  7. Mono game developer says:

    same problem but i have internet connectivity active plus the code access the mic but it is stuck on listening. the code is down below:

    import pyttsx3
    import speech_recognition as sr
    import datetime
    import os
    import time
    import requests
    import playsound
    import pyaudio

    engine = pyttsx3.init(‘sapi5’)
    voices = engine.getProperty(‘voices’)
    engine.setProperty(‘voice’, voices[1].id)
    mic = sr.Microphone(device_index=1)

    def speak(audio):

    def wishMe():
    hour = int(
    if hour>= 0 and hour= 12 and hour= 18 and hour<=24:
    speak("Good job Sir")

    def takeCommand():

    r = sr.Recognizer()

    with sr.Microphone() as source:

    r.pause_threshold = 1
    audio = r.listen(source)
    print('Listened : '+ r.recognize_google(audio))

    said = r.recognize_google(audio, language ='en-in')
    print(f"User said: {said}\n")

    except Exception as e:
    print("Unable to Recognize your voice.")
    print("Exception: " + str(e))
    speak("Unable to Recognize your voice.")
    return "None"

    return said

    if __name__ == '__main__':
    clear = lambda: os.system('cls')


Leave a Reply

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