nico 8 роки тому
батько
коміт
d6eedc9c1c
3 змінених файлів з 44 додано та 16 видалено
  1. 6 0
      core/TTS/TTSModule.py
  2. 1 1
      settings.yml
  3. 37 15
      tts/voxygen/voxygen.py

+ 6 - 0
core/TTS/TTSModule.py

@@ -11,12 +11,18 @@ logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
 
+class MissingTTSParameter(Exception):
+    pass
+
+
 class TtsGenerateAudioFunctionNotFound(Exception):
     pass
 
+
 class FailToLoadSoundFile(Exception):
     pass
 
+
 class TTSModule(object):
 
     def __init__(self, **kwargs):

+ 1 - 1
settings.yml

@@ -62,7 +62,7 @@ text_to_speech:
       cache: True
   - voxygen:
       language: "fr"
-      voice: "Emma"
+      voice: "Fabienne"
       cache: True
   - acapela:
       language: "sonid15"

+ 37 - 15
tts/voxygen/voxygen.py

@@ -1,31 +1,53 @@
 import logging
 
+import requests
+
 from core import AudioPlayer
+from core import FileManager
+from core.TTS.TTSModule import TTSModule, MissingTTSParameter, FailToLoadSoundFile
 from tts import TTS
 
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
+TTS_URL = "https://www.voxygen.fr/sites/all/modules/voxygen_voices/assets/proxy/index.php"
+TTS_TIMEOUT_SEC = 30
+TTS_CONTENT_TYPE = "audio/mpeg"
+
+
+class Voxygen(TTSModule):
+
+    def __init__(self, **kwargs):
+        super(Voxygen, self).__init__(**kwargs)
+
+        self.voice = kwargs.get('voice', None)
+        if self.voice is None:
+            raise MissingTTSParameter("voice parameter is required by the Voxygen TTS")
 
-class Voxygen(TTS):
-    TTS_VOICE_DEFAULT = "Michel"
-    TTS_LANGUAGES_DEFAULT = "default"
-    TTS_URL = "https://www.voxygen.fr/sites/all/modules/voxygen_voices/assets/proxy/index.php"
+    def say(self, words):
+        self.generate_and_play(words, self._generate_audio_file)
 
-    def __init__(self):
-        TTS.__init__(self)
+    def _generate_audio_file(self):
+        payload = self.get_payload(self.voice, self.words)
 
-    def say(self, words=None, voice=TTS_VOICE_DEFAULT, language=TTS_LANGUAGES_DEFAULT, cache=True):
-        self.say_generic(cache, language, words, self.get_audio_voxygen, AudioPlayer.PLAYER_MP3, AudioPlayer.AUDIO_MP3_FREQUENCY, voice)
+        # getting the mp3
+        r = requests.get(TTS_URL, params=payload, stream=True, timeout=TTS_TIMEOUT_SEC)
+        content_type = r.headers['Content-Type']
 
-    def get_audio_voxygen(self, **kwargs):
-        words = kwargs.get('words', None)
-        cache = kwargs.get('cache', None)
-        file_path = kwargs.get('file_path', None)
-        voice = kwargs.get('voice', None)
-        payload = Voxygen.get_payload(voice, words)
+        logger.debug("Voxygen : Trying to get url: %s response code: %s and content-type: %s",
+                     r.url,
+                     r.status_code,
+                     content_type)
 
-        return TTS.get_audio(file_path, cache, payload, self.TTS_URL)
+        try:
+            if r.status_code == requests.codes.ok and content_type == TTS_CONTENT_TYPE:
+                return FileManager.write_in_file(self.file_path, r.content)
+            else:
+                return False
+        except IOError as e:
+            logger.error("I/O error(%s): %s", e.errno, e.strerror)
+        except ValueError:
+            logger.error("Could not convert data to an integer.")
 
     @staticmethod
     def get_payload(voice, words):