Audio routing between apps

60 Views Asked by At

I have a Python voice-enabled chatbot that plays and records audio. However, the chatbot is also recognizing its own audio output as input. Is there a way to prevent this? Is there a specific app that can be used to route audio from one app to another?

def play_audio(file_name):
    audio_path = os.path.join(path, file_name)
    data, fs = sf.read(audio_path)
    sd.play(data, fs)
    sd.wait()


def rec_audio():
    recog = sr.Recognizer()

    with sr.Microphone() as source:
        print("Listening... From Site Rec..")
        audio = recog.listen(source)

    data = ""

    try:
        data = recog.recognize_google(audio)
        print("User: " + data)

    except sr.UnknownValueError:
        print("Machine coudn't process..")
        play_audio("voice break.mp3")
    except sr.RequestError as ex:
        print("Google side" + str(ex))
        play_audio("RequestError.mp3")

    return data

both of these code run simultaneously using threading. is there any way to route the audios??

I wanted the machine not to recognize its own playback audio but to observe the audio only from the microphone..

1

There are 1 best solutions below

0
Kovy Jacob On

Use this to see all the inputs that you have on your device - sr.Microphone() encompasses many inputs.

import speech_recognition as sr

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

For example, I got:

0, Microsoft Sound Mapper - Input
1, Microphone (Realtek High Defini
2, Microsoft Sound Mapper - Output
3, Speakers (Realtek High Definiti
4, Primary Sound Capture Driver
5, Microphone (Realtek High Definition Audio)
6, Primary Sound Driver
7, Speakers (Realtek High Definition Audio)
8, Speakers (Realtek High Definition Audio)
9, Microphone (Realtek High Definition Audio)
10, Speakers (Realtek HD Audio output)
11, Microphone (Realtek HD Audio Mic input)
12, Stereo Mix (Realtek HD Audio Stereo input)

Now, use this code to listen to ONLY the input that you want:

import speech_recognition as sr

r = sr.Recognizer()
#choose the number next to the input that you want to use

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

As you didn't post which modules you are using or the full code for testing I can't verify that this will solve your problem, but I believe it should.