def listen_translate():
while(True):
# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone(sample_rate=8000) as source:
print("Say something!")
# print(5),
# time.sleep(1)
# print(4),
# time.sleep(1)
# print(3),
# time.sleep(1)
# print(2),
# time.sleep(1)
# print(1),
# time.sleep(1)
audio = r.listen(source)#,timeout=5,phrase_time_limit=0.05
# r = sr.Recognizer()
# with sr.AudioFile('./english.wav') as source:
# audio = r.record(source) # read the entire audio file
# write audio to a WAV file ``
with open("microphone-results.wav", "wb") as f:
f.write(audio.get_wav_data())
# recognize speech using Sphinx
try:
print("Sphinx thinks you said :" + r.recognize_sphinx(audio))
except sr.UnknownValueError:
print("Sphinx could not understand audio")
except sr.RequestError as e:
print("Sphinx error; {0}".format(e))
```