Browse Source

fix dynamic loading of TTS module

nico 8 years ago
parent
commit
29c82316d6

+ 2 - 1
core/ConfigurationManager/ConfigurationManager.py

@@ -131,7 +131,8 @@ class ConfigurationManager:
         logging.debug("Settings file content: %s" % texts_to_speech)
         # get args
         args = find(texts_to_speech, tts_name)
-        logging.debug("Args for %s STT: %s" % (tts_name, args))
+        logging.debug("Args for %s TTS: %s" % (tts_name, args))
+        print args
         return args
 
     @classmethod

+ 27 - 2
neurons/Neurone.py

@@ -3,6 +3,14 @@ import importlib
 from core import ConfigurationManager
 
 
+class TTSModuleNotFound(Exception):
+    pass
+
+
+class TTSNotInstantiable(Exception):
+    pass
+
+
 class Neurone:
     def __init__(self, tts=None):
         # get the name of the plugin
@@ -13,10 +21,27 @@ class Neurone:
             self.tts = ConfigurationManager.get_default_text_to_speech()
         # get tts args
         self.tts_args = ConfigurationManager.get_tts_args(self.tts)
+        self.tts = self.tts.capitalize()
+        print "tts args: %s" % str(self.tts_args)
+
+        # instantiate the TTS
+        self.tts_instance = self._get_tts_instance()
 
     def say(self, message):
         # here we use the tts to make jarvis talk
         # the module is imported on fly, depending on the selected tts from settings
-        tts_backend = importlib.import_module("tts." + self.tts)
-        tts_backend.say(words=message, **self.tts_args)
+        self.tts_instance.say(words=message, **(self.tts_args if self.tts_args is not None else {}))
+
+    def _get_tts_instance(self):
+        print "Import TTS module named %s " % self.tts
+        mod = __import__('tts', fromlist=[self.tts])
+        try:
+            klass = getattr(mod, self.tts)
+        except ImportError, e:
+            raise TTSModuleNotFound("The TTS not found: %s" % e)
 
+        if klass is not None:
+            # run the plugin
+            return klass()
+        else:
+            raise TTSNotInstantiable("TTS module %s not instantiable" % self.tts)

+ 1 - 3
test.py

@@ -9,6 +9,4 @@ import logging
 
 from core import ShellGui
 
-
-
-shelgui = ShellGui()
+Say(message="bonjour", tts="pico2wave")

+ 1 - 0
tts/__init__.py

@@ -1,2 +1,3 @@
 from TTS import TTS
 from voxygen import Voxygen
+from pico2wave import Pico2wave

+ 1 - 1
tts/pico2wave/__init__.py

@@ -1 +1 @@
-from pico2wave import *
+from pico2wave import Pico2wave

+ 11 - 10
tts/pico2wave/pico2wave.py

@@ -1,17 +1,18 @@
 import subprocess
 import os
 from core import AudioPlayer
+from tts import TTS
 
-AUDIO_FREQUENCY = 16000
-AUDIO_SIZE = -16
-AUDIO_CHANNEL = 1
-AUDIO_BUFFER = 2048
 
+class Pico2wave(TTS):
 
-def say(words=None, language=None):
-    temp_file = "/tmp/temp.wav"
-    devnull = open("/dev/null", "w")
-    subprocess.check_output(["/usr/bin/pico2wave", "-l=%s" % language, "-w=%s" % temp_file, words], stderr=devnull)
-    AudioPlayer.play_audio(temp_file)
-    devnull.close()
+    def __init__(self, audio_player_type=None):
+        TTS.__init__(self, audio_player_type)
+
+    def say(self, words=None, language=None):
+        temp_file = "/tmp/temp.wav"
+        devnull = open("/dev/null", "w")
+        subprocess.check_output(["/usr/bin/pico2wave", "-l=%s" % language, "-w=%s" % temp_file, words], stderr=devnull)
+        self.play_audio(temp_file)
+        devnull.close()