<s> from zorg . adaptor import Adaptor <EOL> from zorg . driver import Driver <EOL> from multiprocessing import Queue <EOL> import speech_recognition <EOL> import subprocess <EOL> class SpeechRecognition ( Adaptor ) : <EOL> def __init__ ( self , options ) : <EOL> super ( SpeechRecognition , self ) . __init__ ( options ) <EOL> self . recognizer = speech_recognition . Recognizer ( ) <EOL> self . microphone = speech_recognition . Microphone ( ) <EOL> self . recognizer_function = options . get ( <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> self . queue = Queue ( maxsize = <NUM_LIT:7> ) <EOL> self . stop_listening = None <EOL> def start_listening ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . recognizer . listen_in_background ( <EOL> self . microphone , <EOL> self . callback <EOL> ) <EOL> def connect ( self ) : <EOL> subprocess . call ( [ "<STR_LIT>" , "<STR_LIT:start>" ] ) <EOL> with self . microphone as source : <EOL> self . recognizer . adjust_for_ambient_noise ( source ) <EOL> self . stop_listening = self . start_listening ( ) <EOL> def disconnect ( self ) : <EOL> if self . stop_listening : <EOL> self . stop_listening ( ) <EOL> subprocess . call ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def callback ( self , recognizer , audio ) : <EOL> recognizer_function = getattr ( recognizer , self . recognizer_function ) <EOL> try : <EOL> result = recognizer_function ( audio ) <EOL> self . queue . put ( result ) <EOL> except speech_recognition . UnknownValueError : <EOL> self . queue . put ( "<STR_LIT>" ) <EOL> except speech_recognition . RequestError as e : <EOL> m = "<STR_LIT>" <EOL> self . queue . put ( m . format ( e ) ) <EOL> class ApiDriver ( Driver ) : <EOL> def __init__ ( self , options , connection ) : <EOL> super ( ApiDriver , self ) . __init__ ( options , connection ) <EOL> self . commands += [ "<STR_LIT>" ] <EOL> def start ( self ) : <EOL> self . connection . connect ( ) <EOL> def stop ( self ) : <EOL> self . connection . disconnect ( ) <EOL> def get_words ( self ) : <EOL> if self . connection . queue . empty ( ) : <EOL> return None <EOL> return self . connection . queue . get ( ) <EOL> class SpeechSynthesis ( Driver ) : <EOL> def __init__ ( self , options , connection ) : <EOL> super ( SpeechSynthesis , self ) . __init__ ( options , connection ) <EOL> self . commands += [ "<STR_LIT>" , "<STR_LIT:start>" , "<STR_LIT>" ] <EOL> def reply ( self , text ) : <EOL> import time <EOL> from espeak import espeak <EOL> from espeak import core as espeak_core <EOL> response = self . connection . respond ( text ) <EOL> done_synth = [ False ] <EOL> def synth_callback ( event , pos , length ) : <EOL> if event == espeak_core . event_MSG_TERMINATED : <EOL> done_synth [ <NUM_LIT:0> ] = True <EOL> espeak . set_SynthCallback ( synth_callback ) <EOL> call_result = espeak . synth ( response ) <EOL> while call_result and not done_synth [ <NUM_LIT:0> ] : <EOL> time . sleep ( <NUM_LIT> ) <EOL> return call_result <EOL> def start ( self ) : <EOL> self . connection . start_listening ( ) <EOL> def stop ( self ) : <EOL> self . connection . stop_listening ( ) </s>
