Skip to content

Commit

Permalink
feat:g2p (#109)
Browse files Browse the repository at this point in the history
* feat:g2p

move the G2P plugin from TTS base class into ovos-audio

it was only there originally as a way to deploy it to classic mycroft-core when they didn't review PRs

* feat:g2p

move the G2P plugin from TTS base class into ovos-audio

it was only there originally as a way to deploy it to classic mycroft-core when they didn't review PRs

* feat:g2p

move the G2P plugin from TTS base class into ovos-audio

it was only there originally as a way to deploy it to classic mycroft-core when they didn't review PRs

* Update ovos_audio/playback.py

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
JarbasAl and coderabbitai[bot] authored Oct 23, 2024
1 parent 248b0aa commit c782ad0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
51 changes: 25 additions & 26 deletions ovos_audio/playback.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import random
from queue import Empty
from threading import Thread, Event
from typing import Optional

from ovos_audio.transformers import TTSTransformersService
from ovos_bus_client.message import Message
from ovos_bus_client.util import get_message_lang
from ovos_config import Configuration
from ovos_plugin_manager.g2p import OVOSG2PFactory
from ovos_plugin_manager.templates.g2p import OutOfVocabulary, Grapheme2PhonemePlugin
from ovos_plugin_manager.templates.tts import TTS
from ovos_utils.log import LOG, log_deprecation
from ovos_utils.sound import play_audio
from queue import Empty
from threading import Thread, Event
from time import time


Expand All @@ -27,6 +32,13 @@ def __init__(self, queue=TTS.queue, bus=None):
self._now_playing = None
self._started = Event()
self.tts_transform = TTSTransformersService(self.bus)
self.g2p = None
if Configuration().get("g2p", {}).get("module"):
LOG.debug("Loading Grapheme2Phoneme plugin for mouth movements")
try:
self.g2p: Optional[Grapheme2PhonemePlugin] = OVOSG2PFactory.create()
except: # not mission critical
pass

@property
def is_running(self):
Expand Down Expand Up @@ -117,6 +129,16 @@ def _play(self):
data, message.context = self.tts_transform.transform(data, message.context)

self.p = play_audio(data)

if not visemes and self.g2p is not None:
try:
visemes = self.g2p.utterance2visemes(message.data["utterance"],
get_message_lang(message))
except OutOfVocabulary:
pass
except:
# this one is unplanned, let devs know all the info so they can fix it
LOG.exception(f"Unexpected failure in G2P plugin: {self.g2p}")
if visemes:
self.show_visemes(visemes)
if self.p:
Expand Down Expand Up @@ -155,30 +177,7 @@ def run(self, cb=None):
while not self._terminated:
self._do_playback.wait()
try:
# HACK: we do these check to account for direct usages of TTS.queue singletons
speech_data = self.queue.get(timeout=2)
if len(speech_data) == 5 and isinstance(speech_data[-1], Message):
data, visemes, listen, tts_id, message = speech_data
else:
log_deprecation(
"Direct modification of TTS.queue is not recommended!\n"
"expected=(data, visemes, listen, tts_id, message)",
"0.1.0")
if len(speech_data) == 6:
# old ovos backwards compat
_, data, visemes, ident, listen, tts_id = speech_data
elif len(speech_data) == 5:
# mycroft style
tts_id = None
_, data, visemes, ident, listen = speech_data
else:
# old mycroft style TODO can this be deprecated? its very very old
listen = False
tts_id = None
_, data, visemes, ident = speech_data

message = Message("speak", context={"session": {"session_id": ident}})

data, visemes, listen, tts_id, message = self.queue.get(timeout=2)
self._now_playing = (data, visemes, listen, tts_id, message)
self._play()
except Empty:
Expand Down
6 changes: 3 additions & 3 deletions ovos_audio/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,9 @@ def shutdown(self):
Stop any playing audio and make sure threads are joined correctly.
"""
self.status.set_stopping()
if self.tts.playback:
self.tts.playback.shutdown()
self.tts.playback.join()
if self.playback_thread:
self.playback_thread.shutdown()
self.playback_thread.join()
if self.audio:
self.audio.shutdown()

Expand Down

0 comments on commit c782ad0

Please sign in to comment.