def task(quit_event): mic = Microphone(quit_event=quit_event) player = Player(mic.pyaudio_instance) pixel_ring.set_color(rgb=0x505000) time.sleep(3) speech = Speech() myBot = Bot() while not quit_event.is_set(): if mic.wakeup(keyword='olaf'): pixel_ring.listen() data = mic.listen() pixel_ring.wait() text = speech.recognize(data) if text: logger.debug('Recognized : %s', text) result = myBot.request(text) pixel_ring.speak(4, 0) audio = speech.synthetize(result) if (audio != None): player.play_raw(audio) pixel_ring.off() mic.close() pixel_ring.off()
def handle_int(sig, frame): global mission_completed print "terminating..." pixel_ring.off() mission_completed = True mic.close() player.close() myworker.stop()
def task(quit_event): mic = Microphone(quit_event=quit_event) bing = BingSpeechAPI(BING_KEY) while not quit_event.is_set(): if mic.wakeup('respeaker'): print('Wake up') pixel_ring.listen() robot("WakeUp") time.sleep(.1) data = mic.listen() try: pixel_ring.wait() text = bing.recognize(data, language='en-US') # spi.write('answer\n') print('\nBing:' + text.encode('utf-8')) if re.search(r'shake', text) and re.search(r'left hand', text): robot("LeftHand") print("Shake Left hand") elif re.search(r'shake', text) and re.search( r'right hand', text): robot("RightHand") print("Shake right hand") elif re.search(r'shake.*(head).*', text): robot("ShakeHead") print("Shake head") elif re.search(r'head', text) or re.search(r'had', text): robot("NodHead") print("Nod head") elif re.search(r'hand', text): robot("ShakeHand") print("Shake hand") elif re.search(r'hello', text): robot("RightHand") print("Hello") else: print("Other") except Exception as e: print( "\nCould not request results from Microsoft Bing Voice Recognition service; {0}" .format(e)) # if text: # print('Recognized %s' % text) pixel_ring.off()
def run(device_index=None): device = audio.Device() recognizer = None pixel_ring.off() try: # hotword_settings = config.pocket_sphinx hotword_settings = config.http_activation recognizer = Listener(hotword_settings, config.snowboy, recognizer_settings) hotword_recognizer = recognizer.get_hotword_recognizer() phrase_recognizer = recognizer.get_phrase_recognizer() settings = recognizer.get_stream_settings(device, device_index=device_index) print("settings: {}".format(settings)) mic = device.create_microphone_stream(settings) while True: print("start wait hotword...") if not recognizer.wait_hotword(mic): print("error") return pixel_ring.set_volume(12) print("start record...") if not recognizer.read_phrase(mic): print("error read phrase") return print("start send...") pixel_ring.wait() result = recognizer.recognize() audio.AudioData(phrase_recognizer.get_all_data(), settings).save_as_wav('record.wav') print(result) pixel_ring.off() hotword_recognizer.set_answer(result) except KeyboardInterrupt: pass finally: print('stop') pixel_ring.off() recognizer.close() device.terminate()
pixel_ring.off() mission_completed = True mic.close() player.close() myworker.stop() signal.signal(signal.SIGINT, handle_int) myworker.start() while not mission_completed: print print "*********** wake me up with \"respeaker\" ***************" if mic.wakeup('respeaker'): data = mic.listen() time.sleep(0.5) if data: try: pixel_ring.wait() text = bing.recognize(data, language='en-US') print print('BING recognize:', text.encode('utf-8')) myworker.push_cmd(text) myworker.wait_done() except Exception as e: print(e.message) pixel_ring.off() time.sleep(2)