Reputation:
I'm using the speech_recognition module in Python. I have it use adjust_for_ambient_noise()
before recording audio with listen()
. The problem is sometimes it just tries to record seemingly forever, and then when it finally finishes it eats bandwidth transfering the large audio file it recorded to Google for transcription.
Is there a way for me to specify a maximum record time of like 5 seconds?
Setting timeout=5
is only the amount of time it will wait to detect a starting audio signal.
import speech_recognition
def listen(rec, mic):
with mic as source:
print('adjusting mic for ambient noise..')
rec.adjust_for_ambient_noise(source)
print('listening..')
# timeout is not max record time but max time it will wait for audio to start
audio = rec.listen(source, timeout=5)
try:
print('sending audio to google..')
outputString = rec.recognize_google(audio).lower()
except speech_recognition.UnknownValueError:
print('google was not able to transcribe the audio')
return None
except speech_recognition.RequestError:
print('google API unreachable')
return None
except speech_recognition.WaitTimeoutError:
print('timout expired while waiting for audio command to start')
return None
print('transcription \'' + outputString + '\'')
return outputString
rec = sr.Recognizer()
mic = sr.Microphone()
outputString = listen(rec, mic)
Upvotes: 1
Views: 1481
Reputation: 26
You can use (source, phrase_time_limit=5)
instead of (source, timeout=5)
Upvotes: 1