Skip to content

Commit

Permalink
audio service ducking
Browse files Browse the repository at this point in the history
  • Loading branch information
emphasize committed Nov 8, 2023
1 parent 1181bcd commit d9c31bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ovos_audio/playback.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ def clear_queue(self):
def begin_audio(self, message=None):
"""Perform beginning of speech actions."""
if self.bus:
if not self.tts.config.get("pulse_duck", False):
self.bus.emit(Message("ovos.common_play.duck"))
message = message or Message("speak")
self.bus.emit(message.forward("recognizer_loop:audio_output_start"))
else:
Expand All @@ -125,6 +127,8 @@ def end_audio(self, listen, message=None):
listen (bool): True if listening event should be emitted
"""
if self.bus:
if not self.tts.config.get("pulse_duck", False):
self.bus.emit(Message("ovos.common_play.unduck"))
# Send end of speech signals to the system
message = message or Message("speak")
self.bus.emit(message.forward("recognizer_loop:audio_output_end"))
Expand Down
15 changes: 12 additions & 3 deletions ovos_audio/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,19 @@ def handle_instant_play(self, message):
if self.validate_source and not validate_message_context(message):
LOG.debug("ignoring playback, message is not from a native source")
return

audio_file = message.data.get("uri")
hex_audio = message.data.get("binary_data")
audio_ext = message.data.get("audio_ext")
ensure_volume = message.data.get("force_unmute", False)
if hex_audio:
audio_file = self._path_from_hexdata(hex_audio, audio_ext)
if not audio_file:
raise ValueError(f"message.data needs to provide 'uri' or 'binary_data': {message.data}")
audio_file = self._resolve_sound_uri(audio_file)

# volume handling and audio service ducking
ensure_volume = message.data.get("force_unmute", False)
duck_pulse_handled = bool(self.tts and self.tts.config.get("pulse_duck"))
if ensure_volume:
volume_poll: Message = self.bus.wait_for_response(Message("mycroft.volume.get"))
volume = volume_poll.data.get("percent", 0) if volume_poll else 80
Expand All @@ -487,16 +491,21 @@ def handle_instant_play(self, message):
volume_changed = True
elif muted:
self.bus.emit(Message("mycroft.volume.unmute"))

if self.audio.current and not duck_pulse_handled:
self.audio.current.lower_volume()

play_audio(audio_file).wait()

# return to previous state
if self.audio.current and not duck_pulse_handled:
self.audio.current.restore_volume()
if ensure_volume:
if volume_changed:
self.bus.emit(Message("mycroft.volume.set", {"percent": volume,
"play_sound": False}))
if muted:
self.bus.emit(Message("mycroft.volume.mute"))

self.bus.emit(message.response({}))

def handle_get_languages_tts(self, message):
Expand Down

0 comments on commit d9c31bf

Please sign in to comment.