Selaa lähdekoodia

Cache optimization

Unknown 8 vuotta sitten
vanhempi
commit
f9240fd33a
4 muutettua tiedostoa jossa 23 lisäystä ja 12 poistoa
  1. 13 7
      core/Cache.py
  2. 3 0
      tts/TTS.py
  3. 2 1
      tts/pico2wave/pico2wave.py
  4. 5 4
      tts/voxygen/voxygen.py

+ 13 - 7
core/Cache.py

@@ -13,19 +13,20 @@ DEFAULT_VOICE = "default"
 logging.basicConfig()
 logger = logging.getLogger("jarvis")
 
-class Cache:
 
+class Cache:
     def __init__(self, module_name=DEFAULT_MODULE_NAME, cache_path=DEFAULT_CACHE_PATH,
-                 cache_extension=DEFAULT_CACHE_EXTENSION):
+                 cache_extension=None):
+        if cache_extension is None:
+            cache_extension = DEFAULT_CACHE_EXTENSION
+
         self._module_name = module_name
         self._cache_path = cache_path
         self._cache_extension = cache_extension
 
-    def get_audio_file_cache_path(self, words, voice, language):
+    def get_audio_file_cache_path(self, words, language=DEFAULT_LANGUAGE, voice=DEFAULT_VOICE):
         # fix UnicodeEncodeError: 'ascii' codec can't encode character X in position Y
-        if isinstance(words, unicode):
-            words = words.encode('utf-8')
-        md5 = hashlib.md5(words).hexdigest()
+        md5 = self.generate_md5_from_words(words)
         filename = voice + "." + md5 + "." + self._cache_extension
         cache_directory = os.path.join(self._cache_path, self._module_name, language)
         file_path = os.path.join(cache_directory, filename)
@@ -33,8 +34,13 @@ class Cache:
         logger.debug("Cache directory %s exists and File path for audio is: %s", cache_directory, file_path)
         return file_path
 
+    @staticmethod
+    def generate_md5_from_words(words):
+        if isinstance(words, unicode):
+            words = words.encode('utf-8')
+        return hashlib.md5(words).hexdigest()
+
     @staticmethod
     def remove_audio_file(file_path, cache):
         if not cache:
             FileManager.remove_file(file_path)
-

+ 3 - 0
tts/TTS.py

@@ -10,3 +10,6 @@ class TTS:
     def play_audio(self, music_file, cache=False):
         self.audio_player.play_audio(music_file)
         self.cache.remove_audio_file(music_file, cache)
+
+    def unify_key(self, key):
+        return key.lower()

+ 2 - 1
tts/pico2wave/pico2wave.py

@@ -19,14 +19,15 @@ class Pico2wave(TTS):
         TTS.__init__(self, audio_player_type, "wav")
 
     def say(self, words=None, language=PICO2WAVE_LANGUAGES_DEFAULT, cache=False):
+        file_path = self.cache.get_audio_file_cache_path(words, language)
         language = self.get_voice(language)
-        file_path = self.cache.get_audio_file_cache_path(words, language=language, voice="default")
 
         self.get_audio(words, language, file_path)
         self.play_audio(file_path, cache=cache)
 
     def get_voice(self, language):
         logger.debug("Pico2wave asked lang: %s" % language)
+        language = self.unify_key(language)
 
         if language in self.PICO2WAVE_LANGUAGES:
             return self.PICO2WAVE_LANGUAGES[language]

+ 5 - 4
tts/voxygen/voxygen.py

@@ -31,18 +31,19 @@ class Voxygen(TTS):
     VOXYGEN_TIMEOUT_SEC = 30
 
     def __init__(self):
-        TTS.__init__(self, AudioPlayer.PLAYER_MP3, cache_extension="tts")
+        TTS.__init__(self, AudioPlayer.PLAYER_MP3)
 
     def say(self, words=None, voice=None, language=VOXYGEN_LANGUAGES_DEFAULT, cache=True):
         voice = self.get_voice(voice, language)
 
-        file_path = self.cache.get_audio_file_cache_path(words, voice, language)
+        file_path = self.cache.get_audio_file_cache_path(words, language, voice)
 
         if self.get_audio(voice, words, file_path, cache):
-            self.play_audio(file_path)
-            self.cache.remove_audio_file(file_path, cache)
+            self.play_audio(file_path,cache)
 
     def get_voice(self, voice, language):
+        language = self.unify_key(language)
+        voice = self.unify_key(voice)
         if language in self.VOXYGEN_LANGUAGES and voice in self.VOXYGEN_LANGUAGES[language]:
             return self.VOXYGEN_LANGUAGES[language][voice]