def save_phonemes(self, key, phonemes): """Cache phonemes Arguments: key: Hash key for the sentence phonemes: phoneme string to save """ 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: cachefile.write(json.dumps(phonemes)) except Exception: LOG.exception("Failed to write {} to cache".format(pho_file))
def __init__(self, tts_config, tts_name, audio_file_type): self.config = tts_config self.tts_name = tts_name if "preloaded_cache" in self.config: self.persistent_cache_dir = Path(self.config["preloaded_cache"]) else: self.persistent_cache_dir = None self.temporary_cache_dir = Path(get_cache_directory("tts/" + tts_name)) self.audio_file_type = audio_file_type self.resource_dir = Path(__file__).parent.parent.joinpath("res") self.cached_sentences = dict() ensure_directory_exists(str(self.persistent_cache_dir), permissions=0o755) ensure_directory_exists(str(self.temporary_cache_dir), permissions=0o755)
def load_phonemes(self, key): """Load phonemes from cache file. Arguments: Key: Key identifying phoneme cache """ 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: phonemes = json.load(cachefile) return phonemes except Exception as e: LOG.error("Failed to read .PHO from cache ({})".format(e)) return None