Exemplo n.º 1
0
    def __init__(self):
        if self.__instance is not None:
            raise Exception("Singleton can't be created twice !")

        # Init thread class
        threading.Thread.__init__(self)
        self._stopevent = threading.Event()

        self.configuration = ConfigManagerSingleton.get().getConfiguration()

        self.queue = Queue([])
        self.lang = "en-EN"
        if self.configuration.has_key('lang'):
            self.lang = self.configuration['lang']
        if self.configuration.has_key("tts") == False or self.configuration["tts"].lower() == "pico"or self.configuration["tts"].lower() == "picotts":
            self.engine = "pico"
            self.ext = "wav"
        elif self.configuration["tts"].lower() == "voicerss" and "voicerss_key" in self.configuration:
            self.engine = "voicerss"
            self.ext = "ogg"
            self.voicerss_key = self.configuration["voicerss_key"]
        else:
            player.play_block("error_conf")
            return

        # Init pre-synthetized utterances
        self._init_sys_utterance()

        # Start thread
        threading.Thread.start(self)
Exemplo n.º 2
0
    def run(self):
        """
        Recorder main loop
        """
        # Thread loop
        while not self._stopevent.isSet():
            # Wait queue
            if self.queue.empty():
                sleep(.1)
                continue

            # Get message
            data = self.queue.get()
            filename = soundpath + soundfile + "." + self.ext

            # System utterances
            if _utterances.has_key(data):
                # Randomize a weight
                weight = randint(1, sum((msg[0] for msg in _utterances[data])))
                for i, msg in enumerate(_utterances[data]):
                    weight = weight - msg[0]
                    if weight <= 0:
                        break

                # Create filename
                filename = "%s%s_%s_%d.%s" % (soundpath, self.engine, data, i, self.ext.lower())

            # Pico TTS
            elif self.engine == "pico":
                call(['/usr/bin/pico2wave', '-w', filename, '-l', self.lang, '"'+ data + '"'])

            # VoiceRSS
            elif self.engine == "voicerss":
                url = urlopen("http://api.voicerss.org/?%s" % urlencode({"r": 1, "c": self.ext.upper(),
                                                                         "f": "16khz_16bit_mono",
                                                                         "key": self.voicerss_key,
                                                                         "src": data.encode('UTF-8'),
                                                                         "hl": self.lang}))
                with open(os.path.basename(filename), "wb") as f:
                    f.write(url.read())

            # Play synthetized file
            if os.path.exists(filename):
                log.msg(_("Playing generated TTS").encode('utf8'))
                player.play_block(sound = filename, path = soundpath, ext = self.ext)
            else:
                log.msg(_("There was an error creating the output file %(filename)s" % {'filename': str(filename)}).encode('utf8'))

            # Remove message from queue
            self.queue.task_done()