def converse(self): """Send a voice request to the Assistant and playback the response. Returns: True if conversation should continue. """ continue_conversation = False self.conversation_stream.start_recording() logging.info('Recording audio request.') def iter_converse_requests(): for c in self.gen_converse_requests(): assistant_helpers.log_converse_request_without_audio(c) yield c self.conversation_stream.start_playback() # This generator yields ConverseResponse proto messages # received from the gRPC Google Assistant API. for resp in self.assistant.Converse(iter_converse_requests(), self.deadline): assistant_helpers.log_converse_response_without_audio(resp) if resp.error.code != code_pb2.OK: logging.error('server error: %s', resp.error.message) break if resp.event_type == END_OF_UTTERANCE: logging.info('End of audio request detected') self.conversation_stream.stop_recording() if resp.result.spoken_request_text: logging.info('Transcript of user request: "%s".', resp.result.spoken_request_text) logging.info('Playing assistant response.') if len(resp.audio_out.audio_data) > 0: self.conversation_stream.write(resp.audio_out.audio_data) if resp.result.spoken_response_text: logging.info( 'Transcript of TTS response ' '(only populated from IFTTT): "%s".', resp.result.spoken_response_text) if resp.result.conversation_state: self.conversation_state = resp.result.conversation_state if resp.result.volume_percentage != 0: self.conversation_stream.volume_percentage = ( resp.result.volume_percentage ) if resp.result.microphone_mode == DIALOG_FOLLOW_ON: continue_conversation = True logging.info('Expecting follow-on query from user.') elif resp.result.microphone_mode == CLOSE_MICROPHONE: continue_conversation = False logging.info('Finished playing assistant response.') self.conversation_stream.stop_playback() return continue_conversation
def assist(self): # Configure audio source and sink. self.audio_device = None self.audio_source = self.audio_device = ( self.audio_device or audio_helpers.SoundDeviceStream( sample_rate=self.audio_sample_rate, sample_width=self.audio_sample_width, block_size=self.audio_block_size, flush_size=self.audio_flush_size)) self.audio_sink = self.audio_device = ( self.audio_device or audio_helpers.SoundDeviceStream( sample_rate=self.audio_sample_rate, sample_width=self.audio_sample_width, block_size=self.audio_block_size, flush_size=self.audio_flush_size)) # Create conversation stream with the given audio source and sink. self.conversation_stream = audio_helpers.ConversationStream( source=self.audio_source, sink=self.audio_sink, iter_size=self.audio_iter_size, sample_width=self.audio_sample_width) restart = False continue_conversation = True try: while continue_conversation: continue_conversation = False subprocess.Popen( ["aplay", "/home/pi/GassistPi/sample-audio-files/Fb.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.conversation_stream.start_recording() GPIO.output(5, GPIO.HIGH) led.ChangeDutyCycle(100) self.logger.info('Recording audio request.') def iter_converse_requests(): for c in self.gen_converse_requests(): assistant_helpers.log_converse_request_without_audio(c) yield c self.conversation_stream.start_playback() # This generator yields ConverseResponse proto messages # received from the gRPC Google Assistant API. for resp in self.assistant.Converse(iter_converse_requests(), self.grpc_deadline): assistant_helpers.log_converse_response_without_audio(resp) if resp.error.code != code_pb2.OK: self.logger.error('server error: %s', resp.error.message) break if resp.event_type == END_OF_UTTERANCE: self.logger.info('End of audio request detected') GPIO.output(5, GPIO.LOW) led.ChangeDutyCycle(0) self.conversation_stream.stop_recording() if resp.result.spoken_request_text: usrcmd = resp.result.spoken_request_text if 'trigger' in str(usrcmd).lower(): Action(str(usrcmd).lower()) return continue_conversation if 'play'.lower() in str(usrcmd).lower(): YouTube(str(usrcmd).lower()) return continue_conversation if 'stop'.lower() in str(usrcmd).lower(): stop() return continue_conversation if 'tune into'.lower() in str(usrcmd).lower(): radio(str(usrcmd).lower()) return continue_conversation if 'wireless'.lower() in str(usrcmd).lower(): ESP(str(usrcmd).lower()) return continue_conversation else: continue self.logger.info('Transcript of user request: "%s".', resp.result.spoken_request_text) GPIO.output(5, GPIO.LOW) GPIO.output(6, GPIO.HIGH) led.ChangeDutyCycle(50) self.logger.info('Playing assistant response.') if len(resp.audio_out.audio_data) > 0: self.conversation_stream.write( resp.audio_out.audio_data) if resp.result.spoken_response_text: self.logger.info( 'Transcript of TTS response ' '(only populated from IFTTT): "%s".', resp.result.spoken_response_text) if resp.result.conversation_state: self.conversation_state_bytes = resp.result.conversation_state if resp.result.volume_percentage != 0: volume_percentage = resp.result.volume_percentage self.logger.info('Volume should be set to %s%%', volume_percentage) if resp.result.microphone_mode == DIALOG_FOLLOW_ON: continue_conversation = True GPIO.output(6, GPIO.LOW) GPIO.output(5, GPIO.HIGH) led.ChangeDutyCycle(100) self.logger.info( 'Expecting follow-on query from user.') self.logger.info('Finished playing assistant response.') GPIO.output(6, GPIO.LOW) GPIO.output(5, GPIO.LOW) led.ChangeDutyCycle(0) self.conversation_stream.stop_playback() except Exception as e: self._create_assistant() self.logger.exception('Skipping because of connection reset') restart = True try: self.conversation_stream.close() if restart: self.assist() except Exception: self.logger.error('Failed to close conversation_stream.')
def assist(self): # Configure audio source and sink. self.audio_device = None self.audio_source = self.audio_device = ( self.audio_device or audio_helpers.SoundDeviceStream( sample_rate=self.audio_sample_rate, sample_width=self.audio_sample_width, block_size=self.audio_block_size, flush_size=self.audio_flush_size ) ) self.audio_sink = self.audio_device = ( self.audio_device or audio_helpers.SoundDeviceStream( sample_rate=self.audio_sample_rate, sample_width=self.audio_sample_width, block_size=self.audio_block_size, flush_size=self.audio_flush_size ) ) # Create conversation stream with the given audio source and sink. self.conversation_stream = audio_helpers.ConversationStream( source=self.audio_source, sink=self.audio_sink, iter_size=self.audio_iter_size, sample_width=self.audio_sample_width ) restart = False continue_dialog = True try: while continue_dialog: continue_dialog = False self.conversation_stream.start_recording() self.logger.info('Recording audio request.') def iter_converse_requests(): for c in self.gen_converse_requests(): assistant_helpers.log_converse_request_without_audio(c) yield c self.conversation_stream.start_playback() # This generator yields ConverseResponse proto messages # received from the gRPC Google Assistant API. for resp in self.assistant.Converse(iter_converse_requests(), self.grpc_deadline): assistant_helpers.log_converse_response_without_audio(resp) if resp.error.code != code_pb2.OK: self.logger.error('server error: %s', resp.error.message) break if resp.event_type == END_OF_UTTERANCE: self.logger.info('End of audio request detected') self.conversation_stream.stop_recording() if resp.result.spoken_request_text: self.logger.info('Transcript of user request: "%s".', resp.result.spoken_request_text) self.logger.info('Playing assistant response.') if len(resp.audio_out.audio_data) > 0: self.conversation_stream.write(resp.audio_out.audio_data) if resp.result.spoken_response_text: self.logger.info( 'Transcript of TTS response ' '(only populated from IFTTT): "%s".', resp.result.spoken_response_text) if resp.result.conversation_state: self.conversation_state_bytes = resp.result.conversation_state if resp.result.volume_percentage != 0: volume_percentage = resp.result.volume_percentage self.logger.info('Volume should be set to %s%%', volume_percentage) if resp.result.microphone_mode == DIALOG_FOLLOW_ON: continue_dialog = True self.logger.info('Expecting follow-on query from user.') self.logger.info('Finished playing assistant response.') self.conversation_stream.stop_playback() except Exception as e: self._create_assistant() self.logger.exception('Skipping because of connection reset') restart = True try: self.conversation_stream.close() if restart: self.assist() except Exception: self.logger.error('Failed to close conversation_stream.')
def assist(self): # Configure audio source and sink. self.audio_device = None self.audio_source = self.audio_device = ( self.audio_device or audio_helpers.SoundDeviceStream( sample_rate=self.audio_sample_rate, sample_width=self.audio_sample_width, block_size=self.audio_block_size, flush_size=self.audio_flush_size)) self.audio_sink = self.audio_device = ( self.audio_device or audio_helpers.SoundDeviceStream( sample_rate=self.audio_sample_rate, sample_width=self.audio_sample_width, block_size=self.audio_block_size, flush_size=self.audio_flush_size)) # Create conversation stream with the given audio source and sink. self.conversation_stream = audio_helpers.ConversationStream( source=self.audio_source, sink=self.audio_sink, iter_size=self.audio_iter_size, sample_width=self.audio_sample_width) restart = False continue_conversation = True try: while continue_conversation: continue_conversation = False subprocess.Popen( ["aplay", "/home/pi/GassistPi/sample-audio-files/Fb.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.conversation_stream.start_recording() self.logger.info('Recording audio request.') def iter_converse_requests(): for c in self.gen_converse_requests(): assistant_helpers.log_converse_request_without_audio(c) yield c self.conversation_stream.start_playback() # This generator yields ConverseResponse proto messages # received from the gRPC Google Assistant API. for resp in self.assistant.Converse(iter_converse_requests(), self.grpc_deadline): assistant_helpers.log_converse_response_without_audio(resp) if resp.error.code != code_pb2.OK: self.logger.error('server error: %s', resp.error.message) break if resp.event_type == END_OF_UTTERANCE: self.logger.info('End of audio request detected') self.conversation_stream.stop_recording() if resp.result.spoken_request_text: usr = resp.result.spoken_request_text if 'trigger' in str(usr): if 'shut down'.lower() in str(usr).lower(): subprocess.Popen([ "aplay", "/home/pi/GassistPi/sample-audio-files/Pi-Close.wav" ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) time.sleep(10) os.system("sudo shutdown -h now") break else: for num, name in enumerate(var): if name.lower() in str(usr).lower(): pinout = gpio[num] if 'on'.lower() in str(usr).lower(): GPIO.output(pinout, 1) subprocess.Popen([ "aplay", "/home/pi/GassistPi/sample-audio-files/Device-On.wav" ], stdin=subprocess. PIPE, stdout=subprocess. PIPE, stderr=subprocess. PIPE) elif 'off'.lower() in str(usr).lower(): GPIO.output(pinout, 0) subprocess.Popen([ "aplay", "/home/pi/GassistPi/sample-audio-files/Device-Off.wav" ], stdin=subprocess. PIPE, stdout=subprocess. PIPE, stderr=subprocess. PIPE) return continue_conversation else: continue self.logger.info('Transcript of user request: "%s".', resp.result.spoken_request_text) self.logger.info('Playing assistant response.') if len(resp.audio_out.audio_data) > 0: self.conversation_stream.write( resp.audio_out.audio_data) if resp.result.spoken_response_text: self.logger.info( 'Transcript of TTS response ' '(only populated from IFTTT): "%s".', resp.result.spoken_response_text) if resp.result.conversation_state: self.conversation_state_bytes = resp.result.conversation_state if resp.result.volume_percentage != 0: volume_percentage = resp.result.volume_percentage self.logger.info('Volume should be set to %s%%', volume_percentage) if resp.result.microphone_mode == DIALOG_FOLLOW_ON: continue_conversation = True self.logger.info( 'Expecting follow-on query from user.') self.logger.info('Finished playing assistant response.') self.conversation_stream.stop_playback() except Exception as e: self._create_assistant() self.logger.exception('Skipping because of connection reset') restart = True try: self.conversation_stream.close() if restart: self.assist() except Exception: self.logger.error('Failed to close conversation_stream.')