Skip to content

Commit

Permalink
feat/handle_hex_audiodata
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Oct 16, 2023
1 parent e201593 commit fb455ce
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions ovos_audio/service.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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():
Expand Down Expand Up @@ -415,16 +419,32 @@ 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 """
if not validate_message_context(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)
Expand All @@ -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)
Expand Down

0 comments on commit fb455ce

Please sign in to comment.