From 06c5b0d36dda694ec7da2e0e7786f7103c7764db Mon Sep 17 00:00:00 2001 From: Kris Gesling Date: Thu, 25 Jun 2020 15:48:08 +0930 Subject: [PATCH] ensure volume restored after mic recording --- mycroft/audio/audioservice.py | 39 ++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/mycroft/audio/audioservice.py b/mycroft/audio/audioservice.py index 52c26cb3839b..565720bb800d 100644 --- a/mycroft/audio/audioservice.py +++ b/mycroft/audio/audioservice.py @@ -298,22 +298,41 @@ def _restore_volume(self, message=None): def _restore_volume_after_record(self, message=None): """ Restores the volume when Mycroft is done recording. - If no utterance detected, restores immediately. - Else keeps volume low until Mycroft responds to the utterance. + If no utterance detected, restore immediately. + If no response is made in reasonable time, then also restore. Args: message: message bus message, not used but required """ def __restore_volume(): LOG.debug('restoring volume') - self.volume_is_low = False - if not self.volume_is_low: - self.current.restore_volume() - self.bus.remove('recognizer_loop:speech.recognition.unknown', - __restore_volume) - - if self.current: - self.bus.on('recognizer_loop:speech.recognition.unknown', + self.current.restore_volume() + + def wait_for_speak(bus, timeout=8): + """Wait for a speak Message on the bus. + + Arguments: + bus (Mycroft MessageBus): Bus instance to listen on + timeout (int): how long to wait for the messagem, defaults to 8 sec. + """ + self.speak_msg_detected = False + + def detected_speak(message=None): + self.speak_msg_detected = True + self.bus.on('speak', detected_speak) + time.sleep(timeout) + self.bus.remove('speak', detected_speak) + return self.speak_msg_detected + + if self.current is None: + LOG.debug("No audio service to restore volume of") + return None + self.bus.on('recognizer_loop:speech.recognition.unknown', + __restore_volume) + speak_msg_detected = wait_for_speak(self.bus) + if not speak_msg_detected: + __restore_volume() + self.bus.remove('recognizer_loop:speech.recognition.unknown', __restore_volume) def play(self, tracks, prefered_service, repeat=False):