def listen(): r = sr.Recognizer() with sr.Microphone(sample_rate = 48000) as source: say('Calibrating. Silence, please.') log.update('calibrating noise level') r.adjust_for_ambient_noise(source) log.update('energy threshold at ' + str(r.energy_threshold)) say('Ready.') while True: # obtain audio from the microphone log.update('listening for speech') audio = r.listen(source) log.update('processing speech') with open('/tmp/teleradio.wav', 'wb') as f: f.write(audio.get_wav_data()) audio_bytes = BytesIO(audio.get_wav_data()) nlp.handle_audio(audio = audio_bytes)
def light_set(text, entities): state = nlp.top_confidence(entities=entities, entity_name='gpio_state', min_confidence=.75) if state == 'on': say('Turning light on') return if state == 'off': say('Turning light off') return if state == 'toggle': say('Toggling light') return log.warning('light_set state unclear') say('Clarify what to do with the light.') return
def shutdown(text, entities): say('Shutting down') os.system('shutdown -h now')
def reboot(text, entities): say('Rebooting') os.system('reboot')
def ip_get(text, entities): say('I P functionality not ready yet.')
def update(text, entities): say('Update functionality not ready yet.')