From e7521edaccf742b4d623328dae167bde69c04149 Mon Sep 17 00:00:00 2001 From: Johan Euphrosine Date: Tue, 27 Mar 2018 17:37:25 +0900 Subject: [PATCH] google-assistant-sdk/pushtotalk: only start recording when we get audio data Change-Id: Icfd2e3a01ddbb35e17e54b53f26091580bf31f0f --- .../googlesamples/assistant/grpc/audio_helpers.py | 14 ++++++++++++++ .../googlesamples/assistant/grpc/pushtotalk.py | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/google-assistant-sdk/googlesamples/assistant/grpc/audio_helpers.py b/google-assistant-sdk/googlesamples/assistant/grpc/audio_helpers.py index cad78c0..6d62fc1 100644 --- a/google-assistant-sdk/googlesamples/assistant/grpc/audio_helpers.py +++ b/google-assistant-sdk/googlesamples/assistant/grpc/audio_helpers.py @@ -269,9 +269,12 @@ def __init__(self, source, sink, iter_size, sample_width): self._volume_percentage = 50 self._stop_recording = threading.Event() self._source_lock = threading.RLock() + self._recording = False + self._playing = False def start_recording(self): """Start recording from the audio source.""" + self._recording = True self._stop_recording.clear() self._source.start() @@ -280,15 +283,26 @@ def stop_recording(self): self._stop_recording.set() with self._source_lock: self._source.stop() + self._recording = False def start_playback(self): """Start playback to the audio sink.""" + self._playing = True self._sink.start() def stop_playback(self): """Stop playback from the audio sink.""" self._sink.flush() self._sink.stop() + self._playing = False + + @property + def recording(self): + return self._recording + + @property + def playing(self): + return self._playing @property def volume_percentage(self): diff --git a/google-assistant-sdk/googlesamples/assistant/grpc/pushtotalk.py b/google-assistant-sdk/googlesamples/assistant/grpc/pushtotalk.py index 01561ee..c508fc3 100644 --- a/google-assistant-sdk/googlesamples/assistant/grpc/pushtotalk.py +++ b/google-assistant-sdk/googlesamples/assistant/grpc/pushtotalk.py @@ -139,9 +139,10 @@ def iter_log_assist_requests(): logging.info('Transcript of user request: "%s".', ' '.join(r.transcript for r in resp.speech_results)) - self.conversation_stream.start_playback() - logging.info('Playing assistant response.') if len(resp.audio_out.audio_data) > 0: + if not self.conversation_stream.playing: + self.conversation_stream.start_playback() + logging.info('Playing assistant response.') self.conversation_stream.write(resp.audio_out.audio_data) if resp.dialog_state_out.conversation_state: conversation_state = resp.dialog_state_out.conversation_state