Python Forum

Full Version: PyAudio delay
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So i'm currently making a voice assistant thingy as a school project and i have quite a problem with pyaudio.
while True:

        chunk = 256
        
        # get audio from the microphone                                                                       
        r = sr.Recognizer()                                                                                   
        with sr.Microphone() as source:                                                                       
            print("Ready")                                                                                   
            audio = r.listen(source)   
 
        try:
            print("You said " + r.recognize_google(audio))
            if r.recognize_google(audio) == "hello":
                f = wave.open("hal1.wav","rb")
                p = pyaudio.PyAudio()
                stream = p.open(format = p.get_format_from_width(f.getsampwidth()), channels = f.getnchannels(), rate = f.getframerate(), output = True)
                data = f.readframes(chunk)
                while data:
                    stream.write(data)
                    data = f.readframes(chunk)
                stream.stop_stream()
                stream.close()
                p.terminate()
        except sr.UnknownValueError:
            print("Could not understand audio")
        except sr.RequestError as e:
            print("Could not request results; {0}".format(e))
The problem is in the title, there is a MASSIVE delay before the wav file is actually played. (sometimes it can take up to 20 seconds to get it played back)
And for a voice assistant that is unacceptable.
I don't know where the problem is exactly, but i'm pretty sure it's not the speech recognition part.
Does anybody have a clue why this is happening?
I am not familiar with using Google speech recognition in Python. But first thing that comes to mind is you could place print statements in various places in your code that you suspect take long to execute. That way you could see which part(s) of code is causing the delay.