1

By running the following code i get all my available microphone:

import speech_recognition as sr

for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print(f'{index}, {name}')

These are all my microphones (and other things) that i have:

0, Microsoft Sound Mapper - Input
1, Microphone (Realtek(R) Audio)
2, Stereo Mix (Realtek(R) Audio)
3, Microsoft Sound Mapper - Output
4, Speakers (Realtek(R) Audio)
5, Primary Sound Capture Driver
6, Microphone (Realtek(R) Audio)
7, Stereo Mix (Realtek(R) Audio)
8, Primary Sound Driver
9, Speakers (Realtek(R) Audio)
10, Realtek ASIO
11, Speakers (Realtek(R) Audio)
12, Stereo Mix (Realtek(R) Audio)
13, Microphone (Realtek(R) Audio)
14, Speakers 1 (Realtek HD Audio output with SST)
15, Speakers 2 (Realtek HD Audio output with SST)
16, PC Speaker (Realtek HD Audio output with SST)
17, Microphone 1 (Realtek HD Audio Mic input with SST)
18, Microphone 2 (Realtek HD Audio Mic input with SST)
19, Microphone 3 (Realtek HD Audio Mic input with SST)
20, Stereo Mix (Realtek HD Audio Stereo input)

how can I select a specific microphone for speech recognition, I need to swap between the microphone at index 1 and 2 for testing purposes how can I do that.

Code for reference:

import speech_recognition as sr

r = sr.Recognizer()
with sr.Microphone() as source:
    print("Speak Anything!")
    audio = r.listen(source)

    r.energy_threshold = 300
    r.pause_threshold = 1

    try:
        text = r.recognize_google(audio)
        print("You said : {}".format(text))
    except:
        print("Sorry could not recognize what you said!")

And is there a way to print the microphone that is currently being used?

1

1 Answer 1

3

Use

mic = Microphone(device_index=1)

The full code:

import speech_recognition as sr

r = sr.Recognizer()
mic = Microphone(device_index=1)
with mic as source:
    print("Speak Anything!")
    audio = r.listen(source)

1
  • Did you mean 'sr.Microphone(device_index=1)'? I just did 'with sr.Microphone(device_index=1) as source:'.
    – Kovy Jacob
    Commented Sep 12, 2023 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.