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

feat/handle_hex_audiodata #34

Merged
merged 7 commits into from
Oct 18, 2023
Merged
Changes from 4 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
44 changes: 35 additions & 9 deletions ovos_audio/service.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import binascii
import os
import os.path
import time
from hashlib import md5
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 +20,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,31 +420,52 @@ 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):
JarbasAl marked this conversation as resolved.
Show resolved Hide resolved
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")
audio_ext = "wav"

audio_file = f"{gettempdir()}/{fname}.{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}")
JarbasAl marked this conversation as resolved.
Show resolved Hide resolved
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((audio_ext, str(audio_file), viseme, sess_id, listen, message))
TTS.queue.put((str(audio_file), viseme, sess_id, listen, message))
JarbasAl marked this conversation as resolved.
Show resolved Hide resolved

def handle_instant_play(self, message):
""" play a sound file immediately (may play over TTS) """
if 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")
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}")
JarbasAl marked this conversation as resolved.
Show resolved Hide resolved
audio_file = self._resolve_sound_uri(audio_file)
Expand Down
Loading