3232from time import time , sleep
3333
3434import requests
35- from phoneme_guesser .exceptions import FailedToGuessPhonemes
36-
35+ from ovos_plugin_manager .g2p import OVOSG2PFactory
3736from ovos_plugin_manager .utils .tts_cache import TextToSpeechCache , hash_sentence
3837from ovos_utils import resolve_resource_file
3938from ovos_utils .configuration import read_mycroft_config
4039from ovos_utils .enclosure .api import EnclosureAPI
4140from ovos_utils .file_utils import get_cache_directory
42- from ovos_utils .lang .phonemes import get_phonemes
4341from ovos_utils .lang .visimes import VISIMES
4442from ovos_utils .log import LOG
4543from ovos_utils .messagebus import Message , FakeBus as BUS
@@ -243,6 +241,7 @@ def __init__(self, lang="en-us", config=None, validator=None,
243241 self .config , tts_id , self .audio_ext
244242 )
245243 self .cache .curate ()
244+ self .g2p = OVOSG2PFactory .create (config_core )
246245 self .handle_metric ({"metric_type" : "tts.init" })
247246
248247 def handle_metric (self , metadata = None ):
@@ -441,7 +440,13 @@ def _execute(self, sentence, ident, listen, **kwargs):
441440 else : # synth + cache
442441 audio_file , phonemes = self ._synth (sentence , sentence_hash , ** kwargs )
443442
444- viseme = self .viseme (phonemes ) if phonemes else None
443+ # get visemes/mouth movements
444+ if phonemes :
445+ viseme = self .viseme (phonemes )
446+ else :
447+ lang = self ._get_lang (kwargs )
448+ viseme = self .g2p .utterance2visemes (sentence , lang )
449+
445450 audio_ext = self ._determine_ext (audio_file )
446451 self .queue .put (
447452 (audio_ext , str (audio_file ), viseme , ident , l )
@@ -459,11 +464,7 @@ def _determine_ext(self, audio_file):
459464 except :
460465 return self .audio_ext
461466
462- def _synth (self , sentence , sentence_hash = None , ** kwargs ):
463- self .handle_metric ({"metric_type" : "tts.synth.start" })
464- sentence_hash = sentence_hash or hash_sentence (sentence )
465- audio = self .cache .define_audio_file (sentence_hash )
466-
467+ def _get_lang (self , kwargs ):
467468 # parse requested language for this TTS request
468469 # NOTE: this is ovos only functionality, not in mycroft-core!
469470 lang = kwargs .get ("lang" )
@@ -474,7 +475,16 @@ def _synth(self, sentence, sentence_hash=None, **kwargs):
474475 kwargs ["message" ].context .get ("lang" )
475476 except : # not a mycroft message object
476477 pass
477- kwargs ["lang" ] = lang or self .lang
478+ return lang or self .lang
479+
480+ def _synth (self , sentence , sentence_hash = None , ** kwargs ):
481+ self .handle_metric ({"metric_type" : "tts.synth.start" })
482+ sentence_hash = sentence_hash or hash_sentence (sentence )
483+ audio = self .cache .define_audio_file (sentence_hash )
484+
485+ # parse requested language for this TTS request
486+ # NOTE: this is ovos only functionality, not in mycroft-core!
487+ kwargs ["lang" ] = self ._get_lang (kwargs )
478488
479489 # filter kwargs per plugin, different plugins expose different options
480490 # mycroft-core -> no kwargs
@@ -494,11 +504,11 @@ def _synth(self, sentence, sentence_hash=None, **kwargs):
494504 def _cache_phonemes (self , sentence , phonemes = None , sentence_hash = None ):
495505 sentence_hash = sentence_hash or hash_sentence (sentence )
496506 if not phonemes :
497- try : # TODO debug why get_phonemes fails in the first place
498- phonemes = get_phonemes (sentence )
499- self .handle_metric ({"metric_type" : "tts.phonemes.guess " })
500- except ( ImportError , FailedToGuessPhonemes ) :
501- pass
507+ try :
508+ phonemes = self . g2p . utterance2arpa (sentence , self . lang )
509+ self .handle_metric ({"metric_type" : "tts.phonemes.g2p " })
510+ except Exception as e :
511+ self . handle_metric ({ "metric_type" : "tts.phonemes.g2p.error" , "error" : str ( e )})
502512 if phonemes :
503513 return self .save_phonemes (sentence_hash , phonemes )
504514 return None
0 commit comments