def begin_interaction(stream): global finished, Paused, userID, witResultOverride flacrecord.calibrate_input_threshold(stream) googleTTS("Greetings! After the tone, please speak clearly towards my face. Don't forget to say Hello!") Paused = False finished = False while not finished and not rospy.is_shutdown(): while (Paused): pass flac_file = listen_for_block_of_speech(stream) if finished: break if not flac_file == []: if finished: break override = witResultOverride witResultOverride = None if override: pub_speech.publish(str(override)) responseString, finished = responseprocess.messageResponse(override, userID, stream) print responseString googleTTS(responseString) else: hypothesis = stt_google_wav(flac_file) if hypothesis: pub_speech.publish(hypothesis) if hypothesis.lower() == "what does the fox say": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/fox.wav')) elif hypothesis.lower() == "do you have a license": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/mate.wav')) elif hypothesis.lower() == "do you work out": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/work.wav')) elif hypothesis.lower() == "something strange in the neighborhood": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/ghost.wav')) elif hypothesis.lower() == "attention": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/attention.wav')) elif hypothesis.lower() == "i'm talking to you": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/talking.wav')) elif hypothesis.lower() == "i don't want you": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/want.wav')) elif hypothesis.lower() == "how did we meet": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/met.wav')) elif hypothesis.lower() == "do you have any popcorn": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/popcorn.wav')) elif hypothesis.lower() == "how did you get to work today": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/underground.wav')) elif hypothesis.lower() == "which man are you": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/tetris.wav')) elif hypothesis.lower() == "what are you": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/blue.wav')) elif hypothesis.lower() == "the dogs are out": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/dogs.wav')) elif hypothesis.lower() == "what did she say": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/love.wav')) elif hypothesis.lower() == "is there a house in new orleans": play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/orleans.wav')) else : witResult = witLookup(hypothesis) if witResult != []: responseString, finished = responseprocess.messageResponse(witResult, userID, stream) else: responseString = responseprocess.randomNegative() flacrecord.calibrate_input_threshold(stream) googleTTS(responseString) else: responseString = responseprocess.randomNegative() flacrecord.calibrate_input_threshold(stream) googleTTS(responseString) else: responseString = responseprocess.randomNegative() flacrecord.calibrate_input_threshold(stream) googleTTS(responseString)
def begin_interaction(stream): global finished, Paused, userID, witResultOverride flacrecord.calibrate_input_threshold(stream) googleTTS( "Greetings! After the tone, please speak clearly towards my face. Don't forget to say Hello!" ) Paused = False finished = False while not finished and not rospy.is_shutdown(): while (Paused): pass flac_file = listen_for_block_of_speech(stream) if finished: break if not flac_file == []: if finished: break override = witResultOverride witResultOverride = None if override: pub_speech.publish(str(override)) responseString, finished = responseprocess.messageResponse( override, userID, stream) print responseString googleTTS(responseString) else: hypothesis = stt_google_wav(flac_file) if hypothesis: pub_speech.publish(hypothesis) if hypothesis.lower() == "what does the fox say": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/fox.wav')) elif hypothesis.lower() == "do you have a license": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/mate.wav')) elif hypothesis.lower() == "do you work out": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/work.wav')) elif hypothesis.lower( ) == "something strange in the neighborhood": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/ghost.wav')) elif hypothesis.lower() == "attention": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/attention.wav')) elif hypothesis.lower() == "i'm talking to you": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/talking.wav')) elif hypothesis.lower() == "i don't want you": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/want.wav')) elif hypothesis.lower() == "how did we meet": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/met.wav')) elif hypothesis.lower() == "do you have any popcorn": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/popcorn.wav')) elif hypothesis.lower() == "how did you get to work today": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/underground.wav')) elif hypothesis.lower() == "which man are you": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/tetris.wav')) elif hypothesis.lower() == "what are you": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/blue.wav')) elif hypothesis.lower() == "the dogs are out": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/dogs.wav')) elif hypothesis.lower() == "what did she say": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/love.wav')) elif hypothesis.lower( ) == "is there a house in new orleans": play_wav( os.path.join( os.path.dirname(os.path.realpath(__file__)), 'easter_eggs/orleans.wav')) else: witResult = witLookup(hypothesis) if witResult != []: responseString, finished = responseprocess.messageResponse( witResult, userID, stream) else: responseString = responseprocess.randomNegative() flacrecord.calibrate_input_threshold(stream) googleTTS(responseString) else: responseString = responseprocess.randomNegative() flacrecord.calibrate_input_threshold(stream) googleTTS(responseString) else: responseString = responseprocess.randomNegative() flacrecord.calibrate_input_threshold(stream) googleTTS(responseString)
def listen_for_block_of_speech(stream): global interaction_cancelled global recording_override_start global recording_override_stop interaction_cancelled = False recording_override_start = False recording_override_stop = False full_recording = [] max_recording_chunks = 8 * globalvariables.CHUNKS_PER_SECOND face_controller = FaceController() calibrate_input_threshold() recording_started = False beep_start = time.time() stream.start_stream() play_wav( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'raw/soundstart.wav')) beep_end = time.time() print "Removing first " + str( int(globalvariables.SAMPLERATE * (beep_end - beep_start))) + " samples from recording" stream.read(int(globalvariables.SAMPLERATE * (beep_end - beep_start))) prev_visual_talkingness = 0 listening_start_time = time.time() face_controller.update(message="Speak now...") while True: if interaction_cancelled: print "-----------Recording cancelled-----------" full_recording = [] break try: chunk_data = stream.read(globalvariables.SAMPLES_PER_CHUNK) except IOError, e: if e.args[1] == pyaudio.paInputOverflowed: chunk_data = '\x00' * globalvariables.SAMPLES_PER_CHUNK else: raise full_recording.append(chunk_data) print "Visual talkingness: " + str(visual_talkingness) if recording_started: if not recording_override_start: if visual_talkingness <= visual_talkingness_passive_avg: print "-----------Stopped recording due to stopped speaking-----------" break else: if visual_talkingness > visual_talkingness_passive_max or recording_override_start: print("starting to record") face_controller.update(message="I'm listening...") recording_started = True else: if len(full_recording) > globalvariables.PRE_CHUNKS: full_recording.pop(0) if time.time() - listening_start_time > 5: print "--------Stopped recording as nothing heard--------" full_recording = [] break if recording_override_stop: print "-----------Stopped recording due to override-----------" break if len(full_recording) >= max_recording_chunks: print "-----------Stopped recording due to max recording length-----------" break
print "--------Stopped recording as nothing heard--------" full_recording = [] break if recording_override_stop: print "-----------Stopped recording due to override-----------" break if len(full_recording) >= max_recording_chunks: print "-----------Stopped recording due to max recording length-----------" break stream.stop_stream() face_controller.update(message="Hang on a moment...") play_wav( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'raw/soundstop.wav')) if len(full_recording): wav_filename = save_speech(full_recording, p) flac_filename = convert_wav_to_flac(wav_filename) return flac_filename else: return "" def save_speech(data, p): filename = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'output_' + str(int(time.time()))) # write data to WAVE file data = ''.join(data)
def listen_for_block_of_speech(stream): global interaction_cancelled global recording_override_start global recording_override_stop interaction_cancelled = False recording_override_start = False recording_override_stop = False full_recording = [] max_recording_chunks = 8 * globalvariables.CHUNKS_PER_SECOND face_controller = FaceController() calibrate_input_threshold() recording_started = False beep_start = time.time() stream.start_stream() play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'raw/soundstart.wav')) beep_end = time.time() print "Removing first " + str(int(globalvariables.SAMPLERATE*(beep_end - beep_start))) + " samples from recording" stream.read(int(globalvariables.SAMPLERATE*(beep_end - beep_start))) prev_visual_talkingness = 0 listening_start_time = time.time() face_controller.update(message="Speak now...") while True: if interaction_cancelled: print "-----------Recording cancelled-----------" full_recording = [] break try: chunk_data = stream.read(globalvariables.SAMPLES_PER_CHUNK) except IOError, e: if e.args[1] == pyaudio.paInputOverflowed: chunk_data = '\x00'*globalvariables.SAMPLES_PER_CHUNK else: raise full_recording.append(chunk_data) print "Visual talkingness: " + str(visual_talkingness) if recording_started: if not recording_override_start: if visual_talkingness <= visual_talkingness_passive_avg: print "-----------Stopped recording due to stopped speaking-----------" break else: if visual_talkingness > visual_talkingness_passive_max or recording_override_start: print("starting to record") face_controller.update(message="I'm listening...") recording_started = True else: if len(full_recording) > globalvariables.PRE_CHUNKS: full_recording.pop(0) if time.time() - listening_start_time > 5: print "--------Stopped recording as nothing heard--------" full_recording = [] break if recording_override_stop: print "-----------Stopped recording due to override-----------" break if len(full_recording) >= max_recording_chunks: print "-----------Stopped recording due to max recording length-----------" break
if time.time() - listening_start_time > 5: print "--------Stopped recording as nothing heard--------" full_recording = [] break if recording_override_stop: print "-----------Stopped recording due to override-----------" break if len(full_recording) >= max_recording_chunks: print "-----------Stopped recording due to max recording length-----------" break stream.stop_stream() face_controller.update(message="Hang on a moment...") play_wav(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'raw/soundstop.wav')) if len(full_recording): wav_filename = save_speech(full_recording,p) flac_filename = convert_wav_to_flac(wav_filename) return flac_filename else: return "" def save_speech(data, p): filename = os.path.join(os.path.dirname(os.path.realpath(__file__)),'output_'+str(int(time.time()))) # write data to WAVE file data = ''.join(data) wf = wave.open(filename+'.wav', 'wb') wf.setnchannels(1)