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!") 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)
def __init__(self, **kwargs): super(Snowboy, self).__init__() self._ignore_stderr() # pause listening boolean self.interrupted = False self.kill_received = False # get the sensitivity if set by the user self.sensitivity = kwargs.get('sensitivity', 0.5) # callback function to call when hotword caught self.callback = kwargs.get('callback', None) if self.callback is None: raise MissingParameterException( "callback function is required with snowboy") # get the pmdl file to load self.pmdl = kwargs.get('pmdl_file', None) if self.pmdl is None: raise MissingParameterException( "Pmdl file is required with snowboy") self.pmdl_path = Utils.get_real_file_path(self.pmdl) if not os.path.isfile(self.pmdl_path): raise SnowboyModelNotFounfd( "The snowboy model file %s does not exist" % self.pmdl_path) self.detector = snowboydecoder.HotwordDetector( self.pmdl_path, sensitivity=self.sensitivity, detected_callback=self.callback, interrupt_check=self.interrupt_callback, sleep_time=0.03)
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("Intelora 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) # if here, then the trigger has been called HookManager.on_triggered() self.next_state()
def set_mute_status(self, muted=False): """ Define is the trigger is listening or not :param muted: Boolean. If true, intelora 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("Intelora now muted") HookManager.on_mute() else: self.trigger_instance.unpause() self.is_trigger_muted = False Utils.print_info("Intelora now listening for trigger detection") HookManager.on_unmute()
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.recognition_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.recognition_options. adjust_for_ambient_noise_second) Utils.print_info( "[SpeechRecognition] capturing ambient sound during %s seconds" % self.settings.recognition_options. adjust_for_ambient_noise_second) self.recognizer.adjust_for_ambient_noise( source, duration=self.settings.recognition_options. adjust_for_ambient_noise_second) else: # threshold is defined manually logger.debug( "[SpeechRecognition] threshold defined by settings: %s" % self.settings.recognition_options.energy_threshold) self.recognizer.energy_threshold = self.settings.recognition_options.energy_threshold Utils.print_info("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
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 intelora 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')
def launch_signal_class_by_name(cls, signal_name, settings=None): """ load the signal class from the given name, pass the brain and settings to the signal :param signal_name: name of the signal class to load :param settings: Settings Object """ signal_folder = None if settings.resources: signal_folder = settings.resources.signal_folder launched_signal = Utils.get_dynamic_class_instantiation( package_name="signals", module_name=signal_name, resources_dir=signal_folder) cls.add_launched_signals_to_list(launched_signal) return launched_signal