From fb455ce8956ed73234eaa28e4c399e5bd7afde47 Mon Sep 17 00:00:00 2001 From: JarbasAi Date: Tue, 17 Oct 2023 00:19:27 +0100 Subject: [PATCH 1/7] feat/handle_hex_audiodata --- ovos_audio/service.py | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/ovos_audio/service.py b/ovos_audio/service.py index 9fb18d6..db4e091 100644 --- a/ovos_audio/service.py +++ b/ovos_audio/service.py @@ -1,12 +1,12 @@ +import binascii import os import os.path import time from os.path import exists -from ovos_audio.audio import AudioService -from ovos_audio.playback import PlaybackThread -from ovos_audio.transformers import DialogTransformersService -from ovos_audio.tts import TTSFactory -from ovos_audio.utils import report_timing, validate_message_context +from queue import Queue +from tempfile import gettempdir +from threading import Thread, Lock + from ovos_bus_client import Message, MessageBusClient from ovos_bus_client.session import SessionManager from ovos_config.config import Configuration @@ -19,8 +19,12 @@ from ovos_utils.metrics import Stopwatch from ovos_utils.process_utils import ProcessStatus, StatusCallbackMap from ovos_utils.sound import play_audio -from queue import Queue -from threading import Thread, Lock + +from ovos_audio.audio import AudioService +from ovos_audio.playback import PlaybackThread +from ovos_audio.transformers import DialogTransformersService +from ovos_audio.tts import TTSFactory +from ovos_audio.utils import report_timing, validate_message_context def on_ready(): @@ -415,6 +419,18 @@ def _resolve_sound_uri(uri: str): raise FileNotFoundError(f"{audio_file} does not exist") return audio_file + @staticmethod + def _path_from_hexdata(hex_audio, audio_ext=None): + bindata = binascii.unhexlify(hex_audio) + if not audio_ext: + LOG.warning("audio extension not sent, assuming wav") + audio_ext = "wav" + + audio_file = f"{gettempdir()}/{hex_audio}.{audio_ext}" + with open(audio_file, "wb") as f: + f.write(bindata) + return audio_file + def handle_queue_audio(self, message): """ Queue a sound file to play in speech thread ensures it doesnt play over TTS """ @@ -422,9 +438,13 @@ def handle_queue_audio(self, message): LOG.debug("ignoring playback, message is not from a native source") return viseme = message.data.get("viseme") - audio_ext = message.data.get("audio_ext") # unused ? audio_file = message.data.get("uri") or \ message.data.get("filename") # backwards compat + hex_audio = message.data.get("binary_data") + audio_ext = message.data.get("audio_ext") + if hex_audio: + audio_file = self._path_from_hexdata(hex_audio, audio_ext) + if not audio_file: raise ValueError(f"'uri' missing from message.data: {message.data}") audio_file = self._resolve_sound_uri(audio_file) @@ -440,6 +460,10 @@ def handle_instant_play(self, 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") + if hex_audio: + audio_file = self._path_from_hexdata(hex_audio, audio_ext) if not audio_file: raise ValueError(f"'uri' missing from message.data: {message.data}") audio_file = self._resolve_sound_uri(audio_file) From 20d726db146dbc601b262996bfd87a4789a28250 Mon Sep 17 00:00:00 2001 From: JarbasAi Date: Tue, 17 Oct 2023 01:53:34 +0100 Subject: [PATCH 2/7] filename --- ovos_audio/service.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ovos_audio/service.py b/ovos_audio/service.py index db4e091..98ab7c9 100644 --- a/ovos_audio/service.py +++ b/ovos_audio/service.py @@ -2,6 +2,7 @@ import os import os.path import time +from hashlib import md5 from os.path import exists from queue import Queue from tempfile import gettempdir @@ -421,12 +422,13 @@ def _resolve_sound_uri(uri: str): @staticmethod def _path_from_hexdata(hex_audio, audio_ext=None): + fname = md5(hex_audio).hexdigest() bindata = binascii.unhexlify(hex_audio) if not audio_ext: LOG.warning("audio extension not sent, assuming wav") audio_ext = "wav" - audio_file = f"{gettempdir()}/{hex_audio}.{audio_ext}" + audio_file = f"{gettempdir()}/{fname}.{audio_ext}" with open(audio_file, "wb") as f: f.write(bindata) return audio_file From adeb408b8ecc57d73a13f7e98f73217abd3f9ccc Mon Sep 17 00:00:00 2001 From: JarbasAi Date: Tue, 17 Oct 2023 01:58:04 +0100 Subject: [PATCH 3/7] filename --- ovos_audio/service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ovos_audio/service.py b/ovos_audio/service.py index 98ab7c9..9b63f9a 100644 --- a/ovos_audio/service.py +++ b/ovos_audio/service.py @@ -422,7 +422,7 @@ def _resolve_sound_uri(uri: str): @staticmethod def _path_from_hexdata(hex_audio, audio_ext=None): - fname = md5(hex_audio).hexdigest() + fname = md5(hex_audio.encode("utf-8")).hexdigest() bindata = binascii.unhexlify(hex_audio) if not audio_ext: LOG.warning("audio extension not sent, assuming wav") From 1a1bc14cfe475816258eb57d5cd1652badf16927 Mon Sep 17 00:00:00 2001 From: JarbasAi Date: Tue, 17 Oct 2023 02:13:34 +0100 Subject: [PATCH 4/7] warning log --- ovos_audio/service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ovos_audio/service.py b/ovos_audio/service.py index 9b63f9a..777ed77 100644 --- a/ovos_audio/service.py +++ b/ovos_audio/service.py @@ -454,7 +454,7 @@ def handle_queue_audio(self, message): listen = message.data.get("listen", False) sess_id = SessionManager.get(message).session_id - TTS.queue.put((audio_ext, str(audio_file), viseme, sess_id, listen, message)) + TTS.queue.put((str(audio_file), viseme, sess_id, listen, message)) def handle_instant_play(self, message): """ play a sound file immediately (may play over TTS) """ From 706eb90ddeb0eafebaca1cd7917fd7fcfb195ad1 Mon Sep 17 00:00:00 2001 From: JarbasAi Date: Tue, 17 Oct 2023 22:49:33 +0100 Subject: [PATCH 5/7] fix/validate_source --- ovos_audio/service.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ovos_audio/service.py b/ovos_audio/service.py index 777ed77..b7589ff 100644 --- a/ovos_audio/service.py +++ b/ovos_audio/service.py @@ -436,7 +436,7 @@ def _path_from_hexdata(hex_audio, audio_ext=None): def handle_queue_audio(self, message): """ Queue a sound file to play in speech thread ensures it doesnt play over TTS """ - if not validate_message_context(message): + if self.validate_source and not validate_message_context(message): LOG.debug("ignoring playback, message is not from a native source") return viseme = message.data.get("viseme") @@ -458,7 +458,7 @@ def handle_queue_audio(self, message): def handle_instant_play(self, message): """ play a sound file immediately (may play over TTS) """ - if not validate_message_context(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") From 9816b19a3d846984085d4ffbc73677022820920d Mon Sep 17 00:00:00 2001 From: JarbasAi Date: Tue, 17 Oct 2023 23:44:41 +0100 Subject: [PATCH 6/7] docstrs --- ovos_audio/service.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ovos_audio/service.py b/ovos_audio/service.py index b7589ff..86d2577 100644 --- a/ovos_audio/service.py +++ b/ovos_audio/service.py @@ -422,6 +422,11 @@ def _resolve_sound_uri(uri: str): @staticmethod def _path_from_hexdata(hex_audio, audio_ext=None): + """ hex_audio contains hex string encoded bytes + audio_ext if not provided assumed to be wav + + recommended encoding via binascii.hexlify(byte_data).decode('utf-8') + """ fname = md5(hex_audio.encode("utf-8")).hexdigest() bindata = binascii.unhexlify(hex_audio) if not audio_ext: @@ -448,7 +453,7 @@ def handle_queue_audio(self, message): audio_file = self._path_from_hexdata(hex_audio, audio_ext) if not audio_file: - raise ValueError(f"'uri' missing from message.data: {message.data}") + raise ValueError(f"message.data needs to provide 'uri' or 'binary_data': {message.data}") audio_file = self._resolve_sound_uri(audio_file) audio_ext = audio_ext or audio_file.split(".")[-1] listen = message.data.get("listen", False) @@ -467,7 +472,7 @@ def handle_instant_play(self, message): if hex_audio: audio_file = self._path_from_hexdata(hex_audio, audio_ext) if not audio_file: - raise ValueError(f"'uri' missing from message.data: {message.data}") + raise ValueError(f"message.data needs to provide 'uri' or 'binary_data': {message.data}") audio_file = self._resolve_sound_uri(audio_file) play_audio(audio_file) From 6ddc910e4e99ac0564e80387e60d57d2c32ba4b9 Mon Sep 17 00:00:00 2001 From: JarbasAi Date: Wed, 18 Oct 2023 14:59:58 +0100 Subject: [PATCH 7/7] fix --- ovos_audio/service.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ovos_audio/service.py b/ovos_audio/service.py index 86d2577..4c487a3 100644 --- a/ovos_audio/service.py +++ b/ovos_audio/service.py @@ -455,11 +455,12 @@ def handle_queue_audio(self, message): 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) - audio_ext = audio_ext or audio_file.split(".")[-1] + listen = message.data.get("listen", False) - sess_id = SessionManager.get(message).session_id - TTS.queue.put((str(audio_file), viseme, sess_id, listen, message)) + # expected queue contents: (data, visemes, listen, tts_id, message) + # a sound does not have a tts_id, assign that to "sounds" + TTS.queue.put((str(audio_file), viseme, listen, "sounds", message)) def handle_instant_play(self, message): """ play a sound file immediately (may play over TTS) """