diff --git a/mycroft/audio/speech.py b/mycroft/audio/speech.py index 08a910314a..3b772e2091 100644 --- a/mycroft/audio/speech.py +++ b/mycroft/audio/speech.py @@ -30,6 +30,7 @@ config = None tts = None tts_hash = None lock = Lock() +mimic_fallback_obj = None _last_stop_signal = 0 @@ -130,11 +131,15 @@ def mute_and_speak(utterance, ident): def mimic_fallback_tts(utterance, ident): + global mimic_fallback_obj # fallback if connection is lost config = Configuration.get() tts_config = config.get('tts', {}).get("mimic", {}) lang = config.get("lang", "en-us") - tts = Mimic(lang, tts_config) + if not mimic_fallback_obj: + mimic_fallback_obj = Mimic(lang, tts_config) + tts = mimic_fallback_obj + LOG.debug("Mimic fallback, utterance : " + str(utterance)) tts.init(bus) tts.execute(utterance, ident) diff --git a/mycroft/tts/__init__.py b/mycroft/tts/__init__.py index aad0507f08..73735ab775 100644 --- a/mycroft/tts/__init__.py +++ b/mycroft/tts/__init__.py @@ -177,6 +177,7 @@ class TTS: self.playback.start() self.clear_cache() self.spellings = self.load_spellings() + self.tts_name = type(self).__name__ def load_spellings(self): """Load phonetic spellings of words as dictionary""" @@ -209,7 +210,7 @@ class TTS: self.bus.emit(Message("recognizer_loop:audio_output_end")) # Clean the cache as needed - cache_dir = mycroft.util.get_cache_directory("tts") + cache_dir = mycroft.util.get_cache_directory("tts/" + self.tts_name) mycroft.util.curate_cache(cache_dir, min_free_percent=100) # This check will clear the "signal" @@ -313,8 +314,9 @@ class TTS: for sentence in chunks: key = str(hashlib.md5( sentence.encode('utf-8', 'ignore')).hexdigest()) - wav_file = os.path.join(mycroft.util.get_cache_directory("tts"), - key + '.' + self.audio_ext) + wav_file = os.path.join( + mycroft.util.get_cache_directory("tts/" + self.tts_name), + key + '.' + self.audio_ext) if os.path.exists(wav_file): LOG.debug("TTS cache hit") @@ -341,11 +343,16 @@ class TTS: """ Remove all cached files. """ if not os.path.exists(mycroft.util.get_cache_directory('tts')): return - for f in os.listdir(mycroft.util.get_cache_directory("tts")): - file_path = os.path.join(mycroft.util.get_cache_directory("tts"), - f) - if os.path.isfile(file_path): - os.unlink(file_path) + for d in os.listdir(mycroft.util.get_cache_directory("tts")): + dir_path = os.path.join(mycroft.util.get_cache_directory("tts"), d) + if os.path.isdir(dir_path): + for f in os.listdir(dir_path): + file_path = os.path.join(dir_path, f) + if os.path.isfile(file_path): + os.unlink(file_path) + # If no sub-folders are present, check if it is a file & clear it + elif os.path.isfile(dir_path): + os.unlink(dir_path) def save_phonemes(self, key, phonemes): """ @@ -355,8 +362,7 @@ class TTS: key: Hash key for the sentence phonemes: phoneme string to save """ - - cache_dir = mycroft.util.get_cache_directory("tts") + cache_dir = mycroft.util.get_cache_directory("tts/" + self.tts_name) pho_file = os.path.join(cache_dir, key + ".pho") try: with open(pho_file, "w") as cachefile: @@ -372,8 +378,9 @@ class TTS: Args: Key: Key identifying phoneme cache """ - pho_file = os.path.join(mycroft.util.get_cache_directory("tts"), - key + ".pho") + pho_file = os.path.join( + mycroft.util.get_cache_directory("tts/" + self.tts_name), + key + ".pho") if os.path.exists(pho_file): try: with open(pho_file, "r") as cachefile: diff --git a/mycroft/tts/mimic2_tts.py b/mycroft/tts/mimic2_tts.py index 97cbe83500..97eded7802 100644 --- a/mycroft/tts/mimic2_tts.py +++ b/mycroft/tts/mimic2_tts.py @@ -266,8 +266,7 @@ class Mimic2(TTS): key: Hash key for the sentence phonemes: phoneme string to save """ - - cache_dir = get_cache_directory("tts") + cache_dir = get_cache_directory("tts/" + self.tts_name) pho_file = os.path.join(cache_dir, key + ".pho") try: with open(pho_file, "w") as cachefile: @@ -282,7 +281,8 @@ class Mimic2(TTS): Args: Key: Key identifying phoneme cache """ - pho_file = os.path.join(get_cache_directory("tts"), key + ".pho") + pho_file = os.path.join(get_cache_directory("tts/" + self.tts_name), + key + ".pho") if os.path.exists(pho_file): try: with open(pho_file, "r") as cachefile: