From 8027bda8ef23f234794ee12c5f57db464b78ede1 Mon Sep 17 00:00:00 2001 From: JarbasAI <33701864+JarbasAl@users.noreply.github.com> Date: Tue, 19 Sep 2023 21:16:38 +0100 Subject: [PATCH] feat/sounds_playback in ovos-audio (#129) --- ovos_workshop/skills/base.py | 11 ---- ovos_workshop/skills/mycroft_skill.py | 14 +++++ ovos_workshop/skills/ovos.py | 74 +++++++++++++++++++++------ 3 files changed, 71 insertions(+), 28 deletions(-) diff --git a/ovos_workshop/skills/base.py b/ovos_workshop/skills/base.py index 83184236..f1ba7c04 100644 --- a/ovos_workshop/skills/base.py +++ b/ovos_workshop/skills/base.py @@ -1878,17 +1878,6 @@ def speak_dialog(self, key: str, data: Optional[dict] = None, ) self.speak(key, expect_response, wait, {}) - @staticmethod - def acknowledge(): - """ - Acknowledge a successful request. - - This method plays a sound to acknowledge a request that does not - require a verbal response. This is intended to provide simple feedback - to the user that their request was handled successfully. - """ - return play_acknowledge_sound() - # method named init_dialog in mycroft-core def load_dialog_files(self, root_directory: Optional[str] = None): """ diff --git a/ovos_workshop/skills/mycroft_skill.py b/ovos_workshop/skills/mycroft_skill.py index c0d493ac..cd1a2e1d 100644 --- a/ovos_workshop/skills/mycroft_skill.py +++ b/ovos_workshop/skills/mycroft_skill.py @@ -21,6 +21,7 @@ from ovos_bus_client import MessageBusClient, Message from ovos_utils.log import LOG, log_deprecation, deprecated from ovos_workshop.skills.base import BaseSkill, is_classic_core +from ovos_utils.sound import play_acknowledge_sound class _SkillMetaclass(ABCMeta): @@ -166,6 +167,19 @@ def _init_settings_manager(self): except ImportError: pass # standalone skill, skip backwards compat property + @staticmethod + def acknowledge(): + """ + Acknowledge a successful request. + + This method plays a sound to acknowledge a request that does not + require a verbal response. This is intended to provide simple feedback + to the user that their request was handled successfully. + """ + # DEPRECATED - note that this is a staticmethod and uses the old endpoint + # the OVOSSkill class does things properly + return play_acknowledge_sound() + def _init_settings(self): """Setup skill settings.""" if is_classic_core(): diff --git a/ovos_workshop/skills/ovos.py b/ovos_workshop/skills/ovos.py index 82c655b7..dfa2e19f 100644 --- a/ovos_workshop/skills/ovos.py +++ b/ovos_workshop/skills/ovos.py @@ -33,6 +33,40 @@ def __instancecheck__(self, instance): issubclass(instance.__class__, MycroftSkill) +def _can_play_audio(instant=False): + """ helper method to ensure skills keep working on old versions of ovos/mycroft""" + # TODO - completely drop support for older core versions in 0.1.0 release + try: + import mycroft + except ImportError: + # skills don't require core anymore, running standalone + return True + + if instant: + try: + from ovos_core.version import OVOS_VERSION_BUILD, \ + OVOS_VERSION_MINOR, OVOS_VERSION_MAJOR + if OVOS_VERSION_MAJOR >= 1 or \ + OVOS_VERSION_MINOR > 0 or \ + OVOS_VERSION_BUILD >= 8: + return True # min version of ovos-core - 0.0.8 + except ImportError: + pass + else: + try: + # feature introduced before ovos_core module + from mycroft.version import OVOS_VERSION_BUILD, \ + OVOS_VERSION_MINOR, OVOS_VERSION_MAJOR + if OVOS_VERSION_MAJOR >= 1 or \ + OVOS_VERSION_MINOR > 0 or \ + OVOS_VERSION_BUILD >= 4: + return True # min version of ovos-core - 0.0.4 + except ImportError: + pass + + return False + + class OVOSSkill(BaseSkill, metaclass=_OVOSSkillMetaclass): """ New features: @@ -140,28 +174,34 @@ def deactivate(self): """ self._deactivate() - def play_audio(self, filename: str): + def acknowledge(self): + """ + Acknowledge a successful request. + + This method plays a sound to acknowledge a request that does not + require a verbal response. This is intended to provide simple feedback + to the user that their request was handled successfully. + """ + audio_file = self.config_core.get('sounds', {}).get('acknowledge', + 'snd/acknowledge.mp3') + self.play_audio(audio_file, instant=True) + + def play_audio(self, filename: str, instant: bool = False): """ Queue and audio file for playback @param filename: File to play + @param instant: if True audio will be played instantly instead of queued with TTS """ - core_supported = False - if not is_classic_core(): - try: - from mycroft.version import OVOS_VERSION_BUILD, \ - OVOS_VERSION_MINOR, OVOS_VERSION_MAJOR - if OVOS_VERSION_MAJOR >= 1 or \ - OVOS_VERSION_MINOR > 0 or \ - OVOS_VERSION_BUILD >= 4: - core_supported = True # min version of ovos-core - except ImportError: - # skills don't require core anymore, running standalone - core_supported = True - - if core_supported: + if _can_play_audio(instant): message = dig_for_message() or Message("") - self.bus.emit(message.forward("mycroft.audio.queue", - {"filename": filename})) + if instant: + self.bus.emit(message.forward("mycroft.audio.play_sound", + {"uri": filename})) + else: + self.bus.emit(message.forward("mycroft.audio.queue", + {"filename": filename, # TODO - deprecate filename in ovos-audio + "uri": filename # new namespace + })) else: LOG.warning("self.play_audio requires ovos-core >= 0.0.4a45, " "falling back to local skill playback")