1

I want to record and print something from input microphone. I have a built in microphone that is working. but when I call recogniser.listen it never stops listening. I have checked on the internet and someone said to add to the params timeout=10 so I did, and it stops but I get:

raise WaitTimeoutError("listening timed out while waiting for phrase to start")
speech_recognition.WaitTimeoutError: listening timed out while waiting for phrase to start

even though I am speaking. my code:

import pyaudio
import wave
import speech_recognition as sr
# OBATINED THE AUDIO
def play_audio(filename):
    chunk = 1024
    wf = wave.open(filename, "rb")
    pa = pyaudio.PyAudio()

# OPENING THE FILE WITH PYAUDIO.

stream = pa.open(
     format=pa.get_format_from_width(wf.getsampwidth()),  #GETTING THE FORMAT
     channels=wf.getnchannels(),  #GETTING THE CHANEL
     rate=wf.getframerate(), #GETTING THE FRAME RATE
     output=True)

data_stream = wf.readframes(chunk) #CREATING A DATA STREAM, WITH CHUNK SIZE

# WHILE THERE IS SOME DATA LEFT
while data_stream:
    #WRITING THE DATA
    stream.write(data_stream)
    #CONTINUE READING
    data_stream = wf.readframes(chunk)

#closing the stream
stream.close()
#terminating the pyaudio
pa.terminate()

def init_speech():
    print("Listening...")
    play_audio('sounds/when.wav')
    r = sr.Recognizer()
    # OPENING THE MICROPHONE AS source
    with sr.Microphone() as source:
        print("Say something")
        #listening with the recognizer object, and passing it the sorce.
        r.adjust_for_ambient_noise(source)
        audio = r.listen(source=source,timeout=10)


play_audio('sounds/fill.wav')
command = ""

try:
    command = r.recognize_sphinx(audio)
except:
    print("Couldn't understand you")

print("Your Command",command)

init_speech()

I tried to do r.record(source), it doesn't crashed, but it always goes to the except, and print "Couldn't Understand you"

ItsMeNaira
  • 360
  • 1
  • 12
pythwan
  • 99
  • 9

0 Answers0