Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audio service ducking #42

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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