Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

prevent audio playing between mic record and utt handling #2621

Merged
merged 3 commits into from
Jun 29, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions mycroft/audio/audioservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ def load_services_callback(self):
self.bus.on('recognizer_loop:audio_output_start', self._lower_volume)
self.bus.on('recognizer_loop:record_begin', self._lower_volume)
self.bus.on('recognizer_loop:audio_output_end', self._restore_volume)
self.bus.on('recognizer_loop:record_end', self._restore_volume)
self.bus.on('recognizer_loop:record_end',
self._restore_volume_after_record)

def track_start(self, track):
"""Callback method called from the services to indicate start of
Expand Down Expand Up @@ -294,6 +295,46 @@ def _restore_volume(self, message=None):
if not self.volume_is_low:
self.current.restore_volume()

def _restore_volume_after_record(self, message=None):
"""
Restores the volume when Mycroft is done recording.
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():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this need the __ it's local so it won't need to be made anonymous?

LOG.debug('restoring volume')
self.current.restore_volume()

def wait_for_speak(bus, timeout=8):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bus doesn't seem to be used (instead the one associated with the class is used)? can the argument be removed?

"""Wait for a speak Message on the bus.

Arguments:
bus (Mycroft MessageBus): Bus instance to listen on
timeout (int): how long to wait, defaults to 8 sec
"""
self.speak_msg_detected = False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like this can be a local variable instead of an object variable to keep things encapsulated.
speak_msg_detected = False here

and in detected_speak have a line
nonlocal speak_message_detected

feels like otherwise there could be issues with re-entrancy.


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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably a personal opinion but perhaps invert this logic for clarity

        if self.current:
            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)
        else:
            LOG.debug("No audio service to restore volume of")

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):
"""
play starts playing the audio on the prefered service if it
Expand Down Expand Up @@ -440,4 +481,5 @@ def shutdown(self):
self.bus.remove('recognizer_loop:record_begin', self._lower_volume)
self.bus.remove('recognizer_loop:audio_output_end',
self._restore_volume)
self.bus.remove('recognizer_loop:record_end', self._restore_volume)
self.bus.remove('recognizer_loop:record_end',
self._restore_volume_after_record)