示例#1
0
 def __init__(self, **kwargs):
     super(RFIDReader, self).__init__(**kwargs)
     Thread.__init__(self, name=RFIDReader)
     Utils.print_info('[NFC_Reader] Starting manager ...')
     self.synapse_list = list(super(RFIDReader, self).get_list_synapse())
     if self.synapse_list is None:
         self.synapse_list = list()
示例#2
0
    def __init__(self, **kwargs):
        super(Editor, self).__init__(**kwargs)
        # the args from the neuron configuration
        listen_ip = kwargs.get('listen_ip', '0.0.0.0')
        port = kwargs.get('port', 8000)
        ignore_pattern = kwargs.get('ignore_pattern', None)
        dir_first = kwargs.get('dir_first', False)
        hide_hidden = kwargs.get('hide_hidden', False)
        page_title = kwargs.get('page_title', "Kalliope Editor")
        stop_server = kwargs.get('stop_server', False)

        if stop_server:
            self.stop_http_server()
            Utils.print_info("[ Editor ] Editor stopped")
        else:
            global IGNORE_PATTERN, DIRSFIRST, HIDEHIDDEN, PAGE_TITLE

            IGNORE_PATTERN = ignore_pattern
            DIRSFIRST = dir_first
            HIDEHIDDEN = hide_hidden
            PAGE_TITLE = page_title

            if self.stop_http_server():
                server = EditorThread(listen_ip, int(port))
                server.daemon = True
                server.start()
                Cortex.save('EditorServerThread', server)
示例#3
0
    def downloadPreciseEngine(self):
        import json
        import requests
        import tarfile

        Utils.print_info("[Preicse] Precise engine not present, starting download now")
        url = "https://api.github.com/repos/MycroftAI/mycroft-precise/releases/latest"
        response = requests.get(url)
        if response.status_code == 200:
            download_url = None
            arch = self.settings.machine
            for asset in response.json()["assets"]:
                if arch in asset.get("name"):
                    if asset.get("name").startswith("precise-engine") and asset.get("name").endswith(".tar.gz"):
                        download_name = asset.get("name")
                        download_url = asset.get('browser_download_url')

            filepath = os.path.join(TOP_DIR, download_name)
            if download_url:
                Utils.print_info("[Precise] Downloading %s this can take a moment" % download_name)
                file = requests.get(download_url)
                if file.status_code == 200:
                    with open(filepath, 'wb') as f:
                        f.write(file.content)

                    with tarfile.open(filepath) as tar:
                        tar.extractall(path=TOP_DIR)
                    os.remove(filepath)
                    return True
        return False
示例#4
0
 def ReadListItems(self, list):
     for l in self.keep.all():
         if l.title == list:
             return l.text
             break
     else:
         Utils.print_info('[Gnote] List %s not found' % list)
         return False
示例#5
0
 def stop_http_server(self):
     running_server = Cortex.get_from_key("EditorServerThread")
     if running_server:
         Utils.print_info("[ Editor ] Editor is running, stopping now...")
         running_server.shutdown_server()
         while not running_server.is_down:
             time.sleep(0.1)
     return True
示例#6
0
 def __init__(self, listen_ip, port):
     super(EditorThread, self).__init__()
     self.is_down = False
     server_address = (listen_ip, port)
     self.httpd = SimpleServer(server_address, RequestHandler)
     Utils.print_info(
         ('[ Editor ] Listening on: http://%s:%s') %
         (self.httpd.server_address[0], self.httpd.server_address[1]))
示例#7
0
文件: Utils.py 项目: navet56/kalliope
 def run(self):
     """
     Start the thread that listen the microphone and then give the audio to the callback method
     """
     Utils.print_info("Say something!")
     self.stop_thread = self.recognizer.listen_in_background(self.microphone, self.callback)
     while not self.kill_yourself:
         sleep(0.1)
     logger.debug("kill the speech recognition process")
     self.stop_thread()
 def google_engine(self, question):
     result = None
     g_a = GoogleAnswer(question)
     result = g_a.get_answer()
     if result:
         Utils.print_info('Found answer on Google')
         result = self.format_result(result[0])
     else:
         Utils.print_info('No answer found on Google')
     return result
示例#9
0
    def run(self):
        """
         Creates an input audio stream, initializes wake word detection (Porcupine) object, and monitors the audio
         stream for occurrences of the wake word(s).
         """

        num_keywords = len(self._keyword_file_paths)

        keyword_names =\
            [os.path.basename(x).replace('.ppn', '').replace('_compressed', '').split('_')[0] for x in self._keyword_file_paths]

        for keyword_name, sensitivity in zip(keyword_names,
                                             self._sensitivities):
            logger.debug('Listening for %s with sensitivity of %s' %
                         (keyword_name, sensitivity))

        self.porcupine = Porcupine(library_path=self._library_path,
                                   model_file_path=self._model_file_path,
                                   keyword_file_paths=self._keyword_file_paths,
                                   sensitivities=self._sensitivities)

        self.pa = pyaudio.PyAudio()
        self.audio_stream = self.pa.open(
            rate=self.porcupine.sample_rate,
            channels=1,
            format=pyaudio.paInt16,
            input=True,
            frames_per_buffer=self.porcupine.frame_length,
            input_device_index=self._input_device_index)

        logger.debug("[Porcupine] detecting...")

        while not self.kill_received:
            if not self.paused:
                callback = None
                pcm = self.audio_stream.read(self.porcupine.frame_length)
                pcm = struct.unpack_from("h" * self.porcupine.frame_length,
                                         pcm)
                result = self.porcupine.process(pcm)

                if num_keywords == 1 and result:
                    message = "Keyword " + str(keyword_names[0]) + " detected"
                    callback = self.detected_callback

                elif num_keywords > 1 and result >= 0:
                    message = "Keyword " + str(
                        keyword_names[result]) + " detected"
                    callback = self.detected_callback

                if callback is not None:
                    logger.debug(message)
                    Utils.print_info(message)
                    callback()
                    logger.debug("[Porcupine] detect voice break")
                    break
    def ddg_engine(self, question):
        result = None

        ddg = DuckDuckGo(self.translate_text(question, is_question=True))
        result = ddg.get_answer()
        if result:
            result = self.format_result(self.translate_text(result))
            Utils.print_info('Found answer on DuckDuckGo')
        else:
            Utils.print_info('No answer found on DuckDuckGo')

        return result
示例#11
0
    def __init__(self):
        super(Order, self).__init__()
        Utils.print_info('Starting voice order manager')
        # load settings and brain from singleton
        sl = SettingLoader()
        self.settings = sl.settings
        self.brain = BrainLoader().get_brain()

        # keep in memory the order to process
        self.order_to_process = None

        # get the player instance
        self.player_instance = PlayerLauncher.get_player(settings=self.settings)

        # save an instance of the trigger
        self.trigger_instance = None
        self.trigger_callback_called = False
        self.is_trigger_muted = False

        # save the current order listener
        self.order_listener = None
        self.order_listener_callback_called = False

        # boolean used to know id we played the on ready notification at least one time
        self.on_ready_notification_played_once = False

        # rpi setting for led and mute button
        self.init_rpi_utils()

        # Initialize the state machine
        self.machine = Machine(model=self, states=Order.states, initial='init', queued=True)

        # define transitions
        self.machine.add_transition('start_trigger', ['init', 'analysing_order'], 'starting_trigger')
        self.machine.add_transition('play_ready_sound', 'starting_trigger', 'playing_ready_sound')
        self.machine.add_transition('wait_trigger_callback', 'playing_ready_sound', 'waiting_for_trigger_callback')
        self.machine.add_transition('stop_trigger', 'waiting_for_trigger_callback', 'stopping_trigger')
        self.machine.add_transition('play_wake_up_answer', 'stopping_trigger', 'playing_wake_up_answer')
        self.machine.add_transition('wait_for_order', 'playing_wake_up_answer', 'waiting_for_order_listener_callback')
        self.machine.add_transition('analyse_order', 'playing_wake_up_answer', 'analysing_order')

        self.machine.add_ordered_transitions()

        # add method which are called when changing state
        self.machine.on_enter_starting_trigger('start_trigger_process')
        self.machine.on_enter_playing_ready_sound('play_ready_sound_process')
        self.machine.on_enter_waiting_for_trigger_callback('waiting_for_trigger_callback_thread')
        self.machine.on_enter_playing_wake_up_answer('play_wake_up_answer_thread')
        self.machine.on_enter_stopping_trigger('stop_trigger_process')
        self.machine.on_enter_start_order_listener('start_order_listener_thread')
        self.machine.on_enter_waiting_for_order_listener_callback('waiting_for_order_listener_callback_thread')
        self.machine.on_enter_analysing_order('analysing_order_thread')
示例#12
0
 def DeleteList(self, list):
     my_list = None
     for l in self.keep.all():
         if l.title.lower() == list.lower():
             my_list = l
     if my_list:
         my_list.delete()
         self.syncKeep()
         Utils.print_info('[Gnote] List %s deleted' % list)
         return True
     else:
         Utils.print_info('[Gnote] List %s not found' % list)
         return False
示例#13
0
 def waiting_for_trigger_callback_thread(self):
     """
     Method to print in debug that the main process is waiting for a trigger detection
     """
     logger.debug("[MainController] Entering state: %s" % self.state)
     if self.is_trigger_muted:  # the user asked to mute inside the mute neuron
         Utils.print_info("Kalliope is muted")
         self.trigger_instance.pause()
     else:
         Utils.print_info("Waiting for trigger detection")
     # this loop is used to keep the main thread alive
     while not self.trigger_callback_called:
         sleep(0.1)
     self.next_state()
示例#14
0
 def set_mute_status(self, muted=False):
     """
     Define is the trigger is listening or not
     :param muted: Boolean. If true, kalliope is muted
     """
     logger.debug("[MainController] Mute button pressed. Switch trigger process to muted: %s" % muted)
     if muted:
         self.trigger_instance.pause()
         self.is_trigger_muted = True
         Utils.print_info("Kalliope now muted")
     else:
         self.trigger_instance.unpause()
         self.is_trigger_muted = False
         Utils.print_info("Kalliope now listening for trigger detection")
示例#15
0
    def __init__(self):
        super(SignalModule, self).__init__()
        Thread.__init__(self, name=Order)
        Utils.print_info('Starting order signal')
        # load settings and brain from singleton
        sl = SettingLoader()
        self.settings = sl.settings
        self.brain = BrainLoader().brain

        # keep in memory the order to process
        self.order_to_process = None

        # get the player instance
        self.player_instance = PlayerLauncher.get_player(settings=self.settings)

        # save an instance of the trigger
        self.trigger_instance = None
        self.trigger_callback_called = False

        # variable from notifications
        self.skip_trigger = False       # keep the status of the trigger, if true we can skip it in the statue machine
        self.counter_max_retry = 0      # 0 means disabled

        # save the current order listener
        self.order_listener = None
        self.order_listener_callback_called = False

        # Initialize the state machine
        self.machine = Machine(model=self, states=Order.states, initial='init', queued=True)

        # define transitions
        self.machine.add_transition('start_trigger', 'init', 'starting_trigger')
        self.machine.add_transition('unpause_trigger',  'analysing_order', 'unpausing_trigger')
        self.machine.add_transition('wait_trigger_callback', 'unpausing_trigger', 'waiting_for_trigger_callback')
        self.machine.add_transition('pause_trigger', 'waiting_for_trigger_callback', 'pausing_trigger')
        self.machine.add_transition('wait_for_order', 'pausing_trigger', 'waiting_for_order_listener_callback')
        self.machine.add_transition('analyse_order', 'waiting_for_order_listener_callback', 'analysing_order')
        self.machine.add_transition('start_order_listener', 'analysing_order', 'start_order_listener')

        self.machine.add_ordered_transitions()

        # add method which are called when changing state
        self.machine.on_enter_starting_trigger('start_trigger_process')
        self.machine.on_enter_unpausing_trigger('unpausing_trigger_process')
        self.machine.on_enter_waiting_for_trigger_callback('waiting_for_trigger_callback_thread')
        self.machine.on_enter_pausing_trigger('pause_trigger_process')
        self.machine.on_enter_start_order_listener('start_order_listener_thread')
        self.machine.on_enter_waiting_for_order_listener_callback('waiting_for_order_listener_callback_thread')
        self.machine.on_enter_analysing_order('analysing_order_thread')
示例#16
0
    def wa_engine(self, question):
        result = None
        try:
            key = self.engines['wolfram_alpha']['key']
        except KeyError:
            raise MissingParameterException(
                "API key is missing or is incorrect. Please set a valid API key."
            )

        try:
            option = self.engines['wolfram_alpha']['option']
            if option not in ["spoken_answer", "short_answer"]:
                raise MissingParameterException(
                    "%s is not a valid option. Valid options are short_answer or spoken_answer."
                    % option)
        except KeyError:
            option = "spoken_answer"

        try:
            unit = self.engines['wolfram_alpha']['unit']
            if unit.lower() not in ["metric", "imperial"]:
                raise MissingParameterException(
                    "%s is not a valid unit. Valid units are metric or imperial."
                    % unit)
        except KeyError:
            unit = "metric"

        if self.language:
            question = self.translate_question(question, self.language)

        w_a = WolframAlpha(question, key, unit.lower())
        if option == "spoken_answer":
            result = w_a.spoken_answer()
            if result is None:
                result = w_a.short_answer()

        if option == "short_answer":
            result = w_a.short_answer()

        if result:
            if self.language:
                result = self.translate_answer(result, self.language)
            result = self.format_result(result)
            Utils.print_info('Found answer on Wolfram Alpha')
        else:
            Utils.print_info('No answer found on Wolfram Alpha')

        return result
示例#17
0
 def waiting_for_trigger_callback_thread(self):
     """
     Method to print in debug that the main process is waiting for a trigger detection
     """
     logger.debug("[Order] Entering state: %s" % self.state)
     if self.settings.options.deaf:  # the user asked to deaf inside the deaf neuron
         Utils.print_info("Kalliope is deaf")
         self.trigger_instance.pause()
     else:
         Utils.print_info("Waiting for trigger detection")
     # this loop is used to keep the main thread alive
     while not self.trigger_callback_called:
         sleep(0.1)
     # if here, then the trigger has been called
     HookManager.on_triggered()
     self.next_state()
示例#18
0
 def waiting_for_trigger_callback_thread(self):
     """
     Method to print in debug that the main process is waiting for a trigger detection
     """
     logger.debug("[Order] Entering state: %s" % self.state)
     if self.settings.options.deaf:  # the user asked to deaf inside the deaf neuron
         Utils.print_info("Kalliope is deaf")
         self.trigger_instance.pause()
     else:
         Utils.print_info("Waiting for trigger detection")
     # this loop is used to keep the main thread alive
     while not self.trigger_callback_called:
         sleep(0.1)
     # if here, then the trigger has been called
     HookManager.on_triggered()
     self.next_state()
示例#19
0
    def run(self):
        """
        Start the thread that listen the microphone and then give the audio to the callback method
        """
        if self.audio_stream is None:
            Utils.print_info("Say something!")
            try:
                with self.microphone as source:
                    logger.debug("[SpeechRecognition] STT timeout: %s" % self.settings.options.stt_timeout)
                    self.audio_stream = self.recognizer.listen(source, timeout=self.settings.options.stt_timeout)
            except sr.WaitTimeoutError:
                logger.debug("[SpeechRecognition] timeout reached while waiting for audio input")
                self.audio_stream = None
            logger.debug("[SpeechRecognition] end of speech recognition process")

            self.callback(self.recognizer, self.audio_stream)
示例#20
0
    def ddg_engine(self, question):
        result = None
        if self.language:
            question = self.translate_question(question, self.language)

        ddg = DuckDuckGo(question)
        result = ddg.get_answer()
        if result:
            if self.language:
                result = self.translate_answer(result, self.language)
            result = self.format_result(result)
            Utils.print_info('Found answer on DuckDuckGo')
        else:
            Utils.print_info('No answer found on DuckDuckGo')

        return result
示例#21
0
    def __init__(self, audio_file=None):
        """
        Thread used to caught n audio from the microphone and pass it to a callback method
        """
        super(SpeechRecognition, self).__init__()
        self.recognizer = sr.Recognizer()
        self.microphone = sr.Microphone()
        self.callback = None
        self.stop_thread = None
        self.kill_yourself = False
        self.audio_stream = None

        # get global configuration
        sl = SettingLoader()
        self.settings = sl.settings

        if audio_file is None:
            # audio file not set, we need to capture a sample from the microphone
            with self.microphone as source:
                if self.settings.options.adjust_for_ambient_noise_second > 0:
                    # threshold is calculated from capturing ambient sound
                    logger.debug(
                        "[SpeechRecognition] threshold calculated by "
                        "capturing ambient noise during %s seconds" %
                        self.settings.options.adjust_for_ambient_noise_second)
                    Utils.print_info(
                        "[SpeechRecognition] capturing ambient sound during %s seconds"
                        %
                        self.settings.options.adjust_for_ambient_noise_second)
                    self.recognizer.adjust_for_ambient_noise(
                        source,
                        duration=self.settings.options.
                        adjust_for_ambient_noise_second)
                else:
                    # threshold is defined manually
                    logger.debug(
                        "[SpeechRecognition] threshold defined by settings: %s"
                        % self.settings.options.energy_threshold)
                    self.recognizer.energy_threshold = self.settings.options.energy_threshold

                Utils.print_info("[SpeechRecognition] Threshold set to: %s" %
                                 self.recognizer.energy_threshold)
        else:
            # audio file provided
            with sr.AudioFile(audio_file) as source:
                self.audio_stream = self.recognizer.record(
                    source)  # read the entire audio file
示例#22
0
    def run(self):
        logger.debug("detecting...")
        while True:
            if not self.paused_loop:
                data = self.stream.read()
                if len(data) > 0:
                    self.stream.write(data)

                if self.found_keyword:
                    self.pause()                          # We start pausing it here, to avoid double activations
                    message = "[Precise] Keyword detected"
                    Utils.print_info(message)
                    logger.debug(message)
                    self.detected_callback()

            time.sleep(0.01)
        logger.debug("finished")
示例#23
0
    def __init__(self):
        super(SignalModule, self).__init__()
        Thread.__init__(self, name=Order)
        Utils.print_info('Starting order signal')
        # load settings and brain from singleton
        sl = SettingLoader()
        self.settings = sl.settings
        self.brain = BrainLoader().brain

        # keep in memory the order to process
        self.order_to_process = None

        # get the player instance
        self.player_instance = PlayerLauncher.get_player(settings=self.settings)

        # save an instance of the trigger
        self.trigger_instance = None
        self.trigger_callback_called = False
        self.skip_trigger = False  # keep the status of the trigger, if true we can skip it in the statue machine

        # save the current order listener
        self.order_listener = None
        self.order_listener_callback_called = False

        # Initialize the state machine
        self.machine = Machine(model=self, states=Order.states, initial='init', queued=True)

        # define transitions
        self.machine.add_transition('start_trigger', ['init', 'analysing_order'], 'starting_trigger')
        self.machine.add_transition('wait_trigger_callback', 'starting_trigger', 'waiting_for_trigger_callback')
        self.machine.add_transition('stop_trigger', 'waiting_for_trigger_callback', 'stopping_trigger')
        self.machine.add_transition('wait_for_order', 'stopping_trigger', 'waiting_for_order_listener_callback')
        self.machine.add_transition('analyse_order', 'waiting_for_order_listener_callback', 'analysing_order')
        self.machine.add_transition('start_order_listener', 'analysing_order', 'start_order_listener')

        self.machine.add_ordered_transitions()

        # add method which are called when changing state
        self.machine.on_enter_starting_trigger('start_trigger_process')
        self.machine.on_enter_waiting_for_trigger_callback('waiting_for_trigger_callback_thread')
        self.machine.on_enter_stopping_trigger('stop_trigger_process')
        self.machine.on_enter_start_order_listener('start_order_listener_thread')
        self.machine.on_enter_waiting_for_order_listener_callback('waiting_for_order_listener_callback_thread')
        self.machine.on_enter_analysing_order('analysing_order_thread')
示例#24
0
 def run(self):
     """
     Start the thread that listen the microphone and then give the audio to the callback method
     """
     if self.audio_stream is None:
         Utils.print_info("Say something!")
         # Turn on the listening led if we are on a Raspberry
         if self.settings.rpi_settings:
             if self.settings.rpi_settings.pin_led_listening:
                 RpiUtils.switch_pin_to_on(
                     self.settings.rpi_settings.pin_led_listening)
         self.stop_thread = self.recognizer.listen_in_background(
             self.microphone, self.callback)
         while not self.kill_yourself:
             sleep(0.1)
         logger.debug("kill the speech recognition process")
         self.stop_thread()
     else:
         self.callback(self.recognizer, self.audio_stream)
示例#25
0
 def DeleteItem(self, list, items):
     my_list = None
     processed_items = []
     for l in self.keep.all():
         if l.title.lower() == list.lower():
             my_list = l
             break
     if my_list:
         for item in items:
             for l in my_list.items:
                 if l.text.lower() == item.lower():
                     l.delete()
                     Utils.print_info('[Gnote] Item %s from %s deleted' %
                                      (item, list))
                     processed_items.append(item)
         self.syncKeep()
         return processed_items
     else:
         Utils.print_info('[Gnote] List %s not found' % list)
         return False
示例#26
0
    def UnCheckItem(self, list, items):
        my_list = None
        processed_items = []
        for l in self.keep.all():
            if l.title == list:
                my_list = l
                break
        if my_list:
            for item in items:
                for i in my_list.items:
                    if i.text.lower() == item:
                        i.checked = False
                        Utils.print_info('[Gnote] Item %s on %s unchecked' %
                                         (item.replace("☐ ", ""), list))
                        processed_items.append(item)

            self.syncKeep()
            return processed_items
        else:
            Utils.print_info('[Gnote] List %s not found' % list)
            return False
示例#27
0
    def __init__(self, audio_file=None):
        """
        Thread used to caught n audio from the microphone and pass it to a callback method
        """
        super(SpeechRecognition, self).__init__()
        self.recognizer = sr.Recognizer()
        self.microphone = sr.Microphone()
        self.callback = None
        self.stop_thread = None
        self.kill_yourself = False
        self.audio_stream = None

        # get global configuration
        sl = SettingLoader()
        self.settings = sl.settings

        if audio_file is None:
            # audio file not set, we need to capture a sample from the microphone
            with self.microphone as source:
                if self.settings.options.adjust_for_ambient_noise_second > 0:
                    # threshold is calculated from capturing ambient sound
                    logger.debug("[SpeechRecognition] threshold calculated by "
                                 "capturing ambient noise during %s seconds" %
                                 self.settings.options.adjust_for_ambient_noise_second)
                    Utils.print_info("[SpeechRecognition] capturing ambient sound during %s seconds" %
                                     self.settings.options.adjust_for_ambient_noise_second)
                    self.recognizer.adjust_for_ambient_noise(source,
                                                             duration=self.settings.
                                                             options.adjust_for_ambient_noise_second)
                else:
                    # threshold is defined manually
                    logger.debug("[SpeechRecognition] threshold defined by settings: %s" %
                                 self.settings.options.energy_threshold)
                    self.recognizer.energy_threshold = self.settings.options.energy_threshold

                Utils.print_info("[SpeechRecognition] Threshold set to: %s" % self.recognizer.energy_threshold)
        else:
            # audio file provided
            with sr.AudioFile(audio_file) as source:
                self.audio_stream = self.recognizer.record(source)  # read the entire audio file
示例#28
0
    def AddToList(self, list, items):
        my_list = None
        processed_items = []
        for l in self.keep.all():
            if l.title.lower() == list.lower():
                my_list = l
                break
        if not my_list:
            Utils.print_info("List not available, create a new list")
            my_list = self.keep.createList(list)
        if self.pin_list:
            my_list.pinned = True
        else:
            my_list.pinned = False
        for item in items:
            for i in my_list.items:
                if i.text.lower() == item.lower():
                    i.checked = False
                    Utils.print_info("Item %s already in list" %
                                     str(i).replace("☐ ", ""))
                    break
            else:
                my_list.add(item, False)
                Utils.print_info("Add item %s to list" % item)
                processed_items.append(item)

        self.syncKeep()
        return processed_items
示例#29
0
    def __init__(self,
                 keyword=None,
                 sensitivity=None,
                 detected_callback=None
                ):
                 
        super(HotwordDetector, self).__init__()
        sl = SettingLoader()
        self.settings = sl.settings
        self.paused_loop = False
        self.detected_callback = detected_callback
        self.sensitivity = sensitivity
        trigger_level = 3
        self.keyword = keyword
        self.found_keyword = False

        if not os.path.exists(RESOURCE_FILE):
            if self.downloadPreciseEngine():
                Utils.print_info("[Precise] Download complete")
            else:
                raise PreciseEngineNotFound("Error downloading precise engine, check your internet connection or try again later.")

        engine = PreciseEngine(RESOURCE_FILE, self.keyword)

        self.stream = ReadWriteStream()
        self.runner = PreciseRunner(engine,
                                    sensitivity=float(self.sensitivity),
                                    trigger_level=trigger_level,
                                    on_activation=self.activation
                                    )
        
        self.runner.start()
        self.pause()                                    # To avoid that precise starts detecting without beeing ready, we pause it right after start
        if self.settings.machine.startswith("arm"):     # Because importing tensorflow takes up to 10 seconds, we sleep a while
            Utils.print_info("Starting precise trigger")
            time.sleep(10)
示例#30
0
    def __init__(self):
        super(Order, self).__init__()
        Utils.print_info('Starting order signal')
        # load settings and brain from singleton
        sl = SettingLoader()
        self.settings = sl.settings
        self.brain = BrainLoader().brain

        # keep in memory the order to process
        self.order_to_process = None

        # get the player instance
        self.player_instance = PlayerLauncher.get_player(
            settings=self.settings)

        # save an instance of the trigger
        self.trigger_instance = None
        self.trigger_callback_called = False
        self.is_trigger_muted = False

        # If kalliope is asked to start muted
        if self.settings.start_options['muted'] is True:
            self.is_trigger_muted = True

        # save the current order listener
        self.order_listener = None
        self.order_listener_callback_called = False

        # Initialize the state machine
        self.machine = Machine(model=self,
                               states=Order.states,
                               initial='init',
                               queued=True)

        # define transitions
        self.machine.add_transition('start_trigger',
                                    ['init', 'analysing_order'],
                                    'starting_trigger')
        self.machine.add_transition('wait_trigger_callback',
                                    'starting_trigger',
                                    'waiting_for_trigger_callback')
        self.machine.add_transition('stop_trigger',
                                    'waiting_for_trigger_callback',
                                    'stopping_trigger')
        self.machine.add_transition('wait_for_order', 'stopping_trigger',
                                    'waiting_for_order_listener_callback')
        self.machine.add_transition('analyse_order',
                                    'waiting_for_order_listener_callback',
                                    'analysing_order')

        self.machine.add_ordered_transitions()

        # add method which are called when changing state
        self.machine.on_enter_starting_trigger('start_trigger_process')
        self.machine.on_enter_waiting_for_trigger_callback(
            'waiting_for_trigger_callback_thread')
        self.machine.on_enter_stopping_trigger('stop_trigger_process')
        self.machine.on_enter_start_order_listener(
            'start_order_listener_thread')
        self.machine.on_enter_waiting_for_order_listener_callback(
            'waiting_for_order_listener_callback_thread')
        self.machine.on_enter_analysing_order('analysing_order_thread')
示例#31
0
    def run(self):
        """
        Start the voice detector. For every `sleep_time` second it checks the
        audio buffer for triggering keywords. If detected, then call
        corresponding function in `detected_callback`, which can be a single
        function (single model) or a list of callback functions (multiple
        models). Every loop it also calls `interrupt_check` -- if it returns
        True, then breaks from the loop and return.

        :param detected_callback: a function or list of functions. The number of
                                  items must match the number of models in
                                  `decoder_model`.
        :param interrupt_check: a function that returns True if the main loop
                                needs to stop.
        :param float sleep_time: how much time in second every loop waits.
        :param audio_recorder_callback: if specified, this will be called after
                                        a keyword has been spoken and after the
                                        phrase immediately after the keyword has
                                        been recorded. The function will be
                                        passed the name of the file where the
                                        phrase was recorded.
        :param silent_count_threshold: indicates how long silence must be heard
                                       to mark the end of a phrase that is
                                       being recorded.
        :param recording_timeout: limits the maximum length of a recording.
        :return: None
        """
        if self.interrupt_check():
            logger.debug("detect voice return")
            return

        tc = type(self.detected_callback)
        if tc is not list:
            self.detected_callback = [self.detected_callback]
        if len(self.detected_callback) == 1 and self.num_hotwords > 1:
            self.detected_callback *= self.num_hotwords

        assert self.num_hotwords == len(self.detected_callback), \
            "Error: hotwords in your models (%d) do not match the number of " \
            "callbacks (%d)" % (self.num_hotwords, len(self.detected_callback))

        logger.debug("detecting...")

        SR = SpeechRecorder()

        while not self.kill_received:
            #if not self.paused:
            if self.interrupt_check():
                logger.debug("detect voice break")
                break
            data = self.ring_buffer.get()

            if len(data) == 0:
                time.sleep(self.sleep_time)
                continue
            self.saveMessage(
                data
            )  # Save trigger data so it can be append to the record for STT
            status = self.detector.RunDetection(data)

            if status > 0:  #key word found
                SR.start()  # Start the speech recorder
                Utils.print_info("Keyword " + self.keyword_names[status] +
                                 " detected")
                Cortex.save(
                    'kalliope_trigger_called', self.keyword_names[status]
                )  # I save it to the Cortex, to use it by another neuron
                # for changing the tts acording to the trigger name
                HookManager.on_triggered()
                callback = self.detected_callback[status - 1]
                if callback is not None:
                    callback()

            if status == -1:
                logger.warning(
                    "Error initializing streams or reading audio data")

        logger.debug("finished.")
示例#32
0
    def __init__(self, **kwargs):
        super(Enigma, self).__init__(**kwargs)
        # the args from the neuron configuration
        self.ip = kwargs.get('ip', None)
        self.port = kwargs.get('port', 80)
        self.login = kwargs.get('login', None)
        self.password = kwargs.get('passwort', None)
        self.command = kwargs.get('command', None)
        self.channel = kwargs.get('channel', None)
        self.file = kwargs.get('file', None)

        if self._is_parameters_ok():
            path = Utils.get_real_file_path(self.file)
            file = None
            found = False

            if self.login:
                url = "http://" + self.login + ":" + self.password + "@" + str(
                    self.ip) + ":" + str(self.port)
            else:
                url = "http://" + str(self.ip) + ":" + str(self.port)

            if self.channel:
                url = url + "/web/zap?sRef="
                file = open(path, "r")
                channels = yaml.load(file)
                for channel in channels:
                    if channel == self.channel:
                        Utils.print_info("Channel found: " + channel)
                        url = url + channels[channel]
                        found = True
                        break
                if not found:
                    Utils.print_info("Channel : " + self.channel +
                                     " not found")

            else:
                url = url + "/web/remotecontrol?command="
                file = open(path, "r")
                cmds = yaml.load(file)
                for cmd in cmds:
                    if cmd == self.command:
                        Utils.print_info("Command found: " + cmd)
                        url = url + cmds[cmd]
                        found = True
                        break
                if not found:
                    Utils.print_info("Command : " + self.command +
                                     " not found")

            if found:
                Utils.print_info("Sending: " + url)
                try:
                    requests.post(url)
                except requests.exceptions.ConnectionError:
                    Utils.print_info("Failed to establish a new connection")