예제 #1
0
    def on_gui_message(self, payload):
        try:
            msg = json.loads(payload)
            if self.debug:
                LOG.debug("Msg: " + str(payload))
            msg_type = msg.get("type")
            if msg_type == "mycroft.session.set":
                skill = msg.get("namespace")
                self.skill = self.skill or skill
                data = msg.get("data")
                if skill not in self.vars:
                    self.vars[skill] = {}
                for d in data:
                    self.vars[skill][d] = data[d]
                self.on_new_gui_data(data)
            elif msg_type == "mycroft.session.list.insert":
                # Insert new namespace
                self.skill = msg['data'][0]['skill_id']
                self.loaded.insert(0, [self.skill, []])
            elif msg_type == "mycroft.gui.list.insert":
                # Insert a page in an existing namespace
                self.page = msg['data'][0]['url']
                pos = msg.get('position')
                # TODO sometimes throws IndexError: list index out of range
                # not invalid json, seems like either pos is out of range or
                # "mycroft.session.list.insert" message was missed
                # NOTE: only happened once with wiki skill, cant replicate
                self.loaded[0][1].insert(pos, self.page)
                #self.skill = self.loaded[0][0]
            elif msg_type == "mycroft.session.list.move":
                # Move the namespace at "pos" to the top of the stack
                pos = msg.get('from')
                self.loaded.insert(0, self.loaded.pop(pos))
            elif msg_type == "mycroft.session.list.remove":
                pos = msg.get('position')
                skill = msg.get("namespace")
                if self.skill == skill:
                    self.skill = None
                self.loaded.pop(pos)
            elif msg_type == "mycroft.events.triggered":
                # Switch selected page of namespace
                skill = msg['namespace']
                self.skill = self.skill or skill
                pos = msg['data']['number']
                for n in self.loaded:
                    if n[0] == skill:
                        # TODO sometimes pos throws
                        #  IndexError: list index out of range
                        # ocasionally happens with weather skill
                        # LOGS:
                        #   05:38:29.363 - __main__:on_gui_message:56 - DEBUG - Msg: {"type": "mycroft.events.triggered", "namespace": "mycroft-weather.mycroftai", "event_name": "page_gained_focus", "data": {"number": 1}}
                        #   05:38:29.364 - __main__:on_gui_message:90 - ERROR - list index out of range
                        self.page = n[1][pos]

            self._draw_buffer()
            self.on_message(msg)
        except Exception as e:
            if self.debug:
                LOG.exception(e)
                LOG.error("Invalid JSON: " + str(payload))
예제 #2
0
 def run(self, start_scene=None):
     if not self.connected:
         self.connect()
     try:
         Screen.wrapper(self._run, arguments=[start_scene])
     except ResizeScreenError as e:
         self.run(e.scene)
     except Exception as e:
         LOG.exception(e)
         sys.exit(0)
예제 #3
0
    def _stop(self):
        """Stop and close an open stream."""
        try:
            if not self.stream.is_stopped():
                self.stream.stop_stream()
            self.stream.close()
        except Exception:
            LOG.exception('Failed to stop mic input stream')
            # Let's pretend nothing is wrong...

        self.stream = None
        self.audio.terminate()
예제 #4
0
 def reload(self):
     """Reload configuration and restart consumer and producer."""
     self.stop()
     for hw in self.hotword_engines:
         try:
             self.hotword_engines[hw]["engine"].stop()
         except Exception as e:
             LOG.exception(e)
     # load config
     self._load_config()
     # restart
     self.start_async()
예제 #5
0
    def save_phonemes(self, key, phonemes):
        """Cache phonemes

        Arguments:
            key:        Hash key for the sentence
            phonemes:   phoneme string to save
        """
        pho_file = os.path.join(self.cache_dir, key + ".pho")
        try:
            with open(pho_file, "w") as cachefile:
                cachefile.write(phonemes)
        except Exception:
            LOG.exception("Failed to write {} to cache".format(pho_file))
            pass
예제 #6
0
 def create():
     try:
         config = CONFIGURATION.get("stt", {})
         module = config.get("module", "google")
         clazz = STTFactory.CLASSES.get(module)
         return clazz()
     except Exception as e:
         # The STT backend failed to start. Report it and fall back to
         # default.
         LOG.exception('The selected STT backend could not be loaded, '
                       'falling back to default...')
         if module != 'google':
             return GoogleSTT()
         else:
             raise
예제 #7
0
    def save_phonemes(self, key, phonemes):
        """
            Cache phonemes

            Args:
                key:        Hash key for the sentence
                phonemes:   phoneme string to save
        """

        cache_dir = get_cache_directory("tts")
        pho_file = os.path.join(cache_dir, key + ".pho")
        try:
            with open(pho_file, "w") as cachefile:
                cachefile.write(json.dumps(phonemes))
        except Exception:
            LOG.exception("Failed to write {} to cache".format(pho_file))
예제 #8
0
 def initialize():
     nonlocal instance, complete
     try:
         clazz = HotWordFactory.CLASSES[module]
         instance = clazz(hotword, config, lang=lang)
     except TriggerReload:
         complete.set()
         sleep(0.5)
         loop.reload()
     except NoModelAvailable:
         LOG.warning('Could not found find model for {} on {}.'.format(
             hotword, module
         ))
         instance = None
     except Exception:
         LOG.exception(
             'Could not create hotword. Falling back to default.')
         instance = None
     complete.set()
예제 #9
0
    def run(self):
        """Thread main loop. Get audio and extra data from queue and play.

        The queue messages is a tuple containing
        snd_type: 'mp3' or 'wav' telling the loop what format the data is in
        data: path to temporary audio data
        videmes: list of visemes to display while playing
        listen: if listening should be triggered at the end of the sentence.

        Playback of audio is started and the visemes are sent over the bus
        the loop then wait for the playback process to finish before starting
        checking the next position in queue.

        If the queue is empty the tts.end_audio() is called possibly triggering
        listening.
        """
        while not self._terminated:
            try:
                (snd_type, data,
                 visemes, ident, listen) = self.queue.get(timeout=2)
                if not self._processing_queue:
                    self._processing_queue = True
                    self.tts.begin_audio()

                if snd_type == 'wav':
                    self.p = play_wav(data)
                elif snd_type == 'mp3':
                    self.p = play_mp3(data)

                self.p.communicate()
                self.p.wait()

                if self.queue.empty():
                    self.tts.end_audio()
                    self._processing_queue = False
            except Empty:
                pass
            except Exception as e:
                LOG.exception(e)
                if self._processing_queue:
                    self.tts.end_audio()
                    self._processing_queue = False
예제 #10
0
파일: alsa.py 프로젝트: j1nx/jarbas_utils
 def get_mixer(self, control="Master"):
     if self._mixer is None:
         try:
             mixer = Mixer(control)
         except Exception as e:
             try:
                 mixer = Mixer(control)
             except Exception as e:
                 try:
                     if control != "Master":
                         LOG.warning("could not allocate requested mixer, "
                                     "falling back to 'Master'")
                         mixer = Mixer("Master")
                     else:
                         raise
                 except Exception as e:
                     LOG.error("Couldn't allocate mixer")
                     LOG.exception(e)
                     raise
         self._mixer = mixer
     return self.mixer
예제 #11
0
 def run(self):
     restart_attempts = 0
     with self.mic as source:
         self.recognizer.adjust_for_ambient_noise(source)
         while self.state.running:
             try:
                 audio = self.recognizer.listen(source, self.emitter,
                                                self.stream_handler)
                 if audio is not None:
                     self.queue.put((AUDIO_DATA, audio))
                 else:
                     LOG.warning("Audio contains no data.")
             except IOError as e:
                 # IOError will be thrown if the read is unsuccessful.
                 # If self.recognizer.overflow_exc is False (default)
                 # input buffer overflow IOErrors due to not consuming the
                 # buffers quickly enough will be silently ignored.
                 LOG.exception('IOError Exception in AudioProducer')
                 if e.errno == pyaudio.paInputOverflowed:
                     pass  # Ignore overflow errors
                 elif restart_attempts < MAX_MIC_RESTARTS:
                     # restart the mic
                     restart_attempts += 1
                     LOG.info('Restarting the microphone...')
                     source.restart()
                     LOG.info('Restarted...')
                 else:
                     LOG.error('Restarting mic doesn\'t seem to work. '
                               'Stopping...')
                     raise
             except Exception:
                 LOG.exception('Exception in AudioProducer')
                 raise
             else:
                 # Reset restart attempt counter on sucessful audio read
                 restart_attempts = 0
             finally:
                 if self.stream_handler is not None:
                     self.stream_handler.stream_stop()
예제 #12
0
    def _normalized_numbers(self, sentence):
        """normalized numbers to word equivalent.

        Args:
            sentence (str): setence to speak

        Returns:
            stf: normalized sentences to speak
        """
        try:
            from lingua_franca.format import pronounce_number
            numbers = re.findall(r'-?\d+', sentence)
            normalized_num = [
                (num, pronounce_number(int(num)))
                for num in numbers
            ]
            for num, norm_num in normalized_num:
                sentence = sentence.replace(num, norm_num, 1)
        except TypeError:
            LOG.exception("type error in mimic2_tts.py _normalized_numbers()")
        except ImportError:
            LOG.warning("lingua_franca not installed, can not normalize numbers")
        return sentence
예제 #13
0
    def run(self):
        """Start and reload mic and STT handling threads as needed.

        Wait for KeyboardInterrupt and shutdown cleanly.
        """
        try:
            self.start_async()
        except Exception:
            LOG.exception('Starting producer/consumer threads for listener '
                          'failed.')
            return

        # Handle reload of consumer / producer if config changes
        while self.state.running:
            try:
                time.sleep(1)
            except KeyboardInterrupt as e:
                LOG.error(e)
                self.stop()
                raise  # Re-raise KeyboardInterrupt
            except Exception:
                LOG.exception('Exception in RecognizerLoop')
                raise
예제 #14
0
def handle_speak(message):
    utt = message.data["utterance"]
    utt = translate_text(utt, "pt")  # source lang is auto detected
    print("MYCROFT:", utt)


bus = listen_for_message("speak", handle_speak)

print("Write in any language and mycroft will answer in {lang}".format(
    lang=OUTPUT_LANG))

while True:
    try:
        utt = input("YOU:")
        lang = detect_lang(
            "utt")  # source lang is auto detected, this is optional
        if lang != MYCROFT_LANG:
            utt = translate_text(utt)
        send_message(
            "recognizer_loop:utterance",
            {"utterances": [utt]},
            bus=bus  # re-utilize the bus connection
        )
    except KeyboardInterrupt:
        break
    except Exception as e:
        LOG.exception(e)

bus.remove_all_listeners("speak")
bus.close()