def main(): # start to listen background with another thread. start_listen_background() while not os.path.exists(BG_WAV_PATH): print('ready for bg wav ...') time.sleep(1) # initialize recognizer recognizer = Recognizer() recognizer.speaking_duration = 0.1 recognizer.phrase_threshold = 0.1 with Microphone(sample_rate=SAMPLE_RATE) as source: # listen for 1 second to calibrate the energy threshold for ambient noise levels recognizer.adjust_for_ambient_noise(source) print("Calibrated. Say something!") source = Microphone(sample_rate=SAMPLE_RATE) with Pool(THREAD_NUM) as pool: callback_with_model = partial(callback, model_map=ModelMap(), pool=pool) recognizer.listen_in_background(source, callback_with_model, PHRASE_TIME_LIMIT) while True: time.sleep(10)
def escuchar(): print("Escuchando...") recognizer = Recognizer() microfono = Microphone() with microfono: recognizer.adjust_for_ambient_noise(microfono) recognizer.listen_in_background(microfono, callback)
def listen(): print("Say something!") r = Recognizer() m = Microphone() with m as src: # Deal with ambient noise since microphone output is rather unpredictable r.adjust_for_ambient_noise(src) # Start listening r.listen_in_background(m, callback) # Keep listening even though main thread is blocked while True: sleep(1)
class Assistant(): def __init__(self, name, mic): self.name = name self.mic = mic self.recognizer = Recognizer() self.awake = False def listen(self): with self.mic: self.recognizer.adjust_for_ambient_noise(self.mic, 2) print("Starting listener...") self.stop = self.recognizer.listen_in_background(self.mic, self._proc_audio) def stop(self): raise RuntimeError("Attempted to stop assistant before starting it") def wake_up(self): self.awake = True def _timeout(): self.awake = False timer = Timer(30, _timeout) timer.start() def _proc_audio(self, recog, audio): try: # decoded = recog.recognize_sphinx(audio, show_all=True) # txt = decoded.hyp().hypstr txt = recog.recognize_google(audio, show_all=True) print(f"recognized utterance: {txt}") except UnknownValueError as e: # txt = recog.recognize_google(audio) print("Did not recognize utterance") # print(txt) if self.name in txt: self.wake_up() if self.awake and not txt.endswith(self.name): print(f"Sending commands: {txt}") pixel_ring.think() # send command to HA here pixel_ring.off() self.awake = False else: print(f"Not awake or name was give without command") return
class TriggerListener(): def __init__(self): self.recognizer = Recognizer() self.microphone = Microphone(device_index=0, sample_rate=16000, chunk_size=256) self.is_trigger_listener_active = None self.start_listener() def stop_listen_for_trigger(self): print("Stopping listener for trigger") self.stop_listening(wait_for_stop=False) self.is_trigger_listener_active = False def process_trigger(self, data): if "computer" in data.lower(): print("computer found") self.stop_listen_for_trigger() def listen_for_trigger(self, recognizer, audio): try: print("Sound detected...") trigger = recognizer.recognize_sphinx(audio, keyword_entries=[('computer', 0.5)]) if trigger: print(f"Trigger detected! - {trigger}") self.process_trigger(trigger) except sr.UnknownValueError: print("Sphinx could not understand audio") except sr.RequestError as e: print("Sphinx error; {0}".format(e)) def start_listener(self): print("Starting trigger listener") self.is_trigger_listener_active = True self.recognizer.pause_threshold = 0.5 with self.microphone as source: self.recognizer.adjust_for_ambient_noise(source, duration=2.0) self.stop_listening = self.recognizer.listen_in_background(self.microphone, self.listen_for_trigger, phrase_time_limit=1.5) while self.is_trigger_listener_active: print("Listening...") time.sleep(1.0) print("TriggerListener finished!")
class CommandsRecognition(ApiState): def __init__(self, callback, language="pl-PL", api_option=ApiOption.GOOGLE): super().__init__() self.callback_command_detected = callback self.api_option = api_option self.language = language self.listen_thread = None self.phrase_time_limit = 3 try: self.recognizer = Recognizer() self.microphone = Microphone() """ adjust the recognizer sensitivity to ambient noise and record audio from the microphone """ with self.microphone as source: self.recognizer.adjust_for_ambient_noise(source) except OSError as ose_err: Logger.critical("OSError: {0}".format(ose_err)) self.api_runs = False self.api_info = GLO_MSG['MICROPHONE_FAILURE'] return except Exception as err: Logger.critical("Exception: {0}".format(err)) self.api_runs = False self.api_info = GLO_MSG['MICROPHONE_FAILURE'] return Logger.debug("Initialization of CommandsRecognition class") self.api_info = GLO_MSG['MICROPHONE_INITIALIZED'] def validate_command(self, command): if command.lower() in GLO_CMD.values(): detected_command_id = GET_COMMAND(command.lower()) Logger.info("GLO_CMD command available -> {0}".format(command.lower())) self.callback_command_detected(detected_command_id) else: Logger.info("Detected command: {0}".format(command.lower())) def callback_recognition(self, recognizer, audio): try: command = self._api_recognition(audio) self.validate_command(command) except UnboundLocalError as err: Logger.warning("UnboundLocalError : {0} ".format(err)) except RequestError as err: Logger.warning("RequestError : {0} ".format(err)) except UnknownValueError as err: Logger.debug("UnknownValueError : {0} ".format(err)) def background_listen(self): self.listen_thread = self.recognizer.listen_in_background( source=self.microphone, callback=self.callback_recognition, phrase_time_limit=self.phrase_time_limit) def _api_recognition(self, audio): if self.api_option is ApiOption.GOOGLE: return self.recognizer.recognize_google(audio, language=self.language) elif self.api_option is ApiOption.GOOGLE_CLOUD: # Support languages: https://cloud.google.com/speech-to-text/docs/languages return self.recognizer.recognize_google_cloud(audio, credentials_json='', language=self.language) elif self.api_option is ApiOption.SPHINX: # Support languages : https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/ return self.recognizer.recognize_sphinx(audio, language=self.language) elif self.api_option is ApiOption.WIT: # Support languages : https://wit.ai/faq, login required return self.recognizer.recognize_wit(audio, key='',) elif self.api_option is ApiOption.AZURE_BING: # Support languages : https://docs.microsoft.com/en-us/azure/cognitive-services/bing-web-search/language-support, login required self.recognizer.recognize_bing(audio, key='', language=self.language) elif self.api_option is ApiOption.LEX: # Support languages: ONLY ENG -> https://docs.aws.amazon.com/lex/latest/dg/gl-limits.html return self.recognizer.recognize_lex(audio) elif self.api_option is ApiOption.HOUNDIFY: # Support languages: ONLY ENG, login required return self.recognizer.recognize_houndify(audio, client_id='', client_key='') elif self.api_option is ApiOption.IBM: # Support languages : https://www.ibm.com/watson/services/language-translator/, login required return self.recognizer.recognize_ibm(audio, username='', password='', language=self.language) else: Logger.error("Api recognition option is not defined")
except sr.UnknownValueError: print("Google Speech Recognition could not understand audio") except sr.RequestError as e: print( "Could not request results from Google Speech Recognition service; {0}" .format(e)) # Listen for voice audio in a loop and wait for the word 'Dance party' if __name__ == '__main__': try: dance_party_audio.load_audio() recognizer = Recognizer() recognizer.energy_threshold = 200 microphone = Microphone() with microphone as source: recognizer.adjust_for_ambient_noise(source) # start listening on the mic stop_listening = recognizer.listen_in_background( microphone, onAudioReceived) while True: # "Block" the main thread ... will run additional logic to handle # Hardware here - TODO time.sleep(0.1) # Kill the background listener stop listening stop_listening() except DancePartyAudioLoadError as error: print(error)
class MyWindow(arcade.Window): """ Single window ball game with levels. """ def __init__(self, width, height): super().__init__(width, height) arcade.set_background_color(arcade.color.AMAZON) self.microphone = Microphone() self.recognizer = Recognizer() # with self.microphone as source: # self.recognizer.adjust_for_ambient_noise(source, duration=2.0) self.recognizer.energy_threshold = 5000 self.stop_listening = None def cleanup(self): if self.stop_listening is not None: self.stop_listening(False) def setup(self): def process_sound(recognizer, audio_data): try: result = self.recognizer.recognize_google(audio_data) except Exception: return force = 0. if result.lower().startswith('l'): force = -10000 elif result.lower().startswith('r'): force = 10000 self.ball.push((force, 0), result) self.stop_listening = self.recognizer.listen_in_background( self.microphone, process_sound, phrase_time_limit=1., ) self.space = pymunk.Space() self.space.gravity = 0.0, -900.0 self.bottom_line = Line(100, 100, 400, 100, self.space) self.left_line = Line(100, 100, 100, 200, self.space) self.right_line = Line(400, 100, 400, 200, self.space) self.ball = Ball(200, 200, 10, self.space) def on_draw(self): """ Render the screen. """ # This command should happen before we start drawing. It will clear # the screen to the background color, and erase what we drew last frame. arcade.start_render() self.bottom_line.draw() self.left_line.draw() self.right_line.draw() self.ball.draw() def on_update(self, delta_time): # First update all physics. self.space.step(delta_time) self.bottom_line.update() self.left_line.update() self.right_line.update() self.ball.update()
class SpeechRecognition(Thread): def __init__(self): Thread.__init__(self, daemon=True); self.rec = Recognizer(); self.rec.energy_threshold=110; self.rec.pause_threshold=0.5; self.rec.operation_timeout=5; self.mic = Microphone(device_index = 2); self.answer = AnswerBot('de.txt'); def command(self, command, said): global render_mode; command = command.split(" "); if command[0]=='time': render_mode = 'time'; yield str(current_time.hour) + "Uhr und " + str(current_time.minute) + " minuten."; elif command[0]=='shutdown': exit(True); elif command[0]=='restart': exit(False); elif command[0]=='wiki': search = said.lower().split(command[1].replace('_', ' '))[1].lower(); setAlert('Suche: "' + search + '"'); say("Suche nach " + search); result = wikipedia.summary(search); split = result.split('.'); if len(split)>4: result = ''; for i in range(0,4): result += split[i] + '.'; yield result; elif command[0]=='weather': global weather; if command[1] == 'today': yield "Hier ist die aktuelle Wetterlage."; weather = weathersystem.getCurrentWeather(); elif command[1] == 'tomorrow': yield "Hier ist die vorrausichtliche Wetterlage."; weather = weathersystem.getWeatherTomorrow(); render_mode = 'weather'; yield 'Temperatur: ' + str(weather['temperature']) + '°C'; yield 'Luftfeuchtigkeit: ' + str(weather['humidity']) + '%'; yield 'Wind: ' + str(weather['wind']) + 'km/h'; yield 'Bewölkung: ' + str(weather['clouds']) + '%'; def out(self, pMessage): setAlert('"' + pMessage + '"'); print("Heard: " + pMessage); response = self.answer.find_answer(pMessage); print("Response: ",response); if response[0]!=None: if response[0][0]=='>': for entry in self.command(response[0][1:], pMessage): say(entry); else: say(response[0]); def listen(self, audio): self.out(self.rec.recognize_google(audio, language='de-DE')); def initialize(self): with self.mic as source: self.rec.listen_in_background(source, self.listen); def run(self): while True: try: with self.mic as source: audio = self.rec.listen(source, timeout=None); self.out(self.rec.recognize_google(audio, language='de-DE')); except Exception as e: print(e);
# this is called from the background thread def callback(recognizer, audio): # received audio data, now we'll recognize it using Google Speech Recognition try: output = recognizer.recognize_google(audio) print("WE THINKS: " + output) with open("backgroundAudioListen.txt", "a") as f: f.write(f"{output}\r\n") f.close() VLC_COMMANDS(output) except: pass r = Recognizer() mic = Microphone() vlclib.connect() with mic as source: r.adjust_for_ambient_noise(source) stop_listening = r.listen_in_background(mic, callback) # `stop_listening` is now a function that, when called, stops background listening while True: sleep(0.1) # calling this function requests that the background listener stop listening #stop_listening(wait_for_stop=False)