From 126c10b5ce1b6c662b392c546b3438ccf20697b3 Mon Sep 17 00:00:00 2001 From: Ruben van de Ven Date: Fri, 17 May 2019 20:47:33 +0200 Subject: [PATCH] Stop google stream after finish (or rather, prevent google stream from restarting after finish) - Fix #29 --- hugvey/speech/google.py | 28 ++++++++++++++++++++++++++-- hugvey/story.py | 2 ++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/hugvey/speech/google.py b/hugvey/speech/google.py index e1d6249..60fa481 100644 --- a/hugvey/speech/google.py +++ b/hugvey/speech/google.py @@ -37,6 +37,7 @@ class GoogleVoiceClient(object): # Create a thread-safe buffer of audio data self.buffer = queue.Queue() self.isRunning = threading.Event() + self.isStarted = threading.Event() self.toBeShutdown = False self.target_rate = 16000 self.cv_laststate = None @@ -57,6 +58,25 @@ class GoogleVoiceClient(object): def resume(self): self.buffer = queue.Queue() # have a clear queue when resuming self.isRunning.set() + + def start(self): + """ + Pause/resume are used within a story to create a break/restart in the recording + eacht time hugvey speaks. however, this caused issues with the mics that stay on after + a story finishes (it cause Google to resume). Hence, we add another layer to its running + state: start/stop. This is not influenced by the resume and triggered by starting and + stopping the story only. + A bit messy but it should work ;-) + """ + self.logger.info("Start STT") + self.resume() + self.isStarted.set() + + + def stop(self): + self.logger.info("Stop STT") + self.pause() + self.isStarted.clear() def generator(self): self.logger.debug('start generator') @@ -207,12 +227,16 @@ class GoogleVoiceClient(object): return self.subsequentMutedFrames = 0 + + if not self.isStarted.is_set(): + # Stopped state, so resume is not triggered + return + # self.logger.debug("We have mic!") if not self.isRunning.is_set(): self.logger.info("Resume voice") self.resume() - if not self.isRunning.is_set(): # logger.log(LOG_BS, "Don't put to queue if google is paused") return @@ -228,7 +252,7 @@ class GoogleVoiceClient(object): self.hugvey = None def triggerStart(self): - pass + self.start() def __del__(self): self.logger.warn("Destroyed google object") diff --git a/hugvey/story.py b/hugvey/story.py index 916213e..e719021 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -1659,6 +1659,8 @@ class Story(object): self.stop() self.finish_time = time.time() self.timer.pause() + if self.hugvey.google: + self.hugvey.google.stop() def calculateFinishesForMsg(self, msgId, depth = 0, checked = []): # if msgId in checked: