Pārlūkot izejas kodu

TTS mothere class now handle the file generation

nico 8 gadi atpakaļ
vecāks
revīzija
2d5032fa0b
2 mainītis faili ar 24 papildinājumiem un 22 dzēšanām
  1. 22 9
      core/TTS/TTSModule.py
  2. 2 13
      tts/pico2wave/pico2wave.py

+ 22 - 9
core/TTS/TTSModule.py

@@ -11,6 +11,10 @@ logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
 
+class TtsGenerateAudioFunctionNotFound(Exception):
+    pass
+
+
 class TTSModule(object):
 
     def __init__(self, **kwargs):
@@ -39,22 +43,31 @@ class TTSModule(object):
                                                                                              self.language,
                                                                                              self.voice)
 
-    def set_words(self, words):
+    def play_audio(self):
         """
-        Give the sentence to pronounce to the TTS module so it can generate the file path
-        :param words: string that contain words to give the the TTS engine
+        Play the audio file
         :return:
         """
+        Mplayer.play(self.file_path)
+
+    def generate_and_play(self, words, generate_audio_function_from_child=None):
+        if generate_audio_function_from_child is None:
+            raise TtsGenerateAudioFunctionNotFound
+
         self.words = words
         # we can generate the file path from info we have
         self.file_path = self._get_path_to_store_audio()
 
-    def play_audio(self):
-        """
-        Play the audio file
-        :return:
-        """
-        Mplayer.play(self.file_path)
+        if not self.cache:
+            # no cache, we need to generate the file
+            generate_audio_function_from_child()
+        else:
+            # we check if the file already exist. If not we generate it with the TTS engine
+            if not self.is_file_already_in_cache():
+                generate_audio_function_from_child()
+
+        # then play the generated audio file
+        self.play_audio()
 
     def _get_path_to_store_audio(self):
         """

+ 2 - 13
tts/pico2wave/pico2wave.py

@@ -15,19 +15,8 @@ class Pico2wave(TTSModule):
         super(Pico2wave, self).__init__(**kwargs)
 
     def say(self, words):
-        # the mother class TTSModule needs to know what we will generate to check the cache et generate the file path
-        self.set_words(words)
-
-        if not self.cache:
-            # no cache, we need to generate the file
-            self._generate_audio_file()
-        else:
-            # we check if the file already exist. If not we generate it with the TTS engine
-            if not self.is_file_already_in_cache():
-                self._generate_audio_file()
-
-        # then play the generated audio file
-        self.play_audio()
+
+        self.generate_and_play(words, self._generate_audio_file)
 
     def _generate_audio_file(self):
         pico2wave_exec_path = ["/usr/bin/pico2wave"]