def loop_test(self): if self.cmd: print_debug('act | performing action \'%s\'...\n' % ACT2STR[int(self.cmd)]) # the 'result' variable is currently not being used # but it can be used later to return the action performing status self.result = True self.cmd = '' return
def setup(self): self.audio = pyaudio.PyAudio() self.stream = self.audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, frames_per_buffer=CHUNK, input_device_index=DEVICE, input=True) print_debug('asr | listening...\n') return
def loop(self): if self.cmd: print_debug('act | performing action ...\n') # shell_cmd = ACTION_SCRIPT+' '+self.cmd # os.system(shell_cmd) call([ACTION_SCRIPT, self.cmd]) # the 'result' variable is currently not being used # but it can be used later to return the action performing status self.result = True self.cmd = '' return
def dm_process(pipe): # initialize dialog manager dm = DialogManager() print '=> dialog manager started' # main loop while True: if pipe.poll(): cmd = pipe.recv() print_debug('controller | %s\n' % cmd) ret = dm.handle(cmd) if not ret: break dm.loop() pipe.close() print '=> dialog manager closed' return
def start_module(self, mod_id): if mod_id == MODULE.ALL: self.start_all() return if not self.processes[mod_id]: parent, child = Pipe() self.pipes[mod_id] = parent # self.processes[mid] = Queue() try: self.processes[mod_id] = Process(target=PROCESS_FN[mod_id], args=(child,)) self.processes[mod_id].start() except Exception as e: print_debug('manager | unable to start {0} ({1})\n'.format(self.id2mod[mod_id],e)) else: print_debug('manager | %s already started\n' % self.id2mod[mod_id]) return
def terminate_module(self, mod_id): if mod_id==MODULE.ALL: self.terminate_all() return if self.processes[mod_id]: if self.processes[mod_id].is_alive(): self.pipes[mod_id].send('exit') self.processes[mod_id].join() else: print_debug('manager | %s terminated\n' % self.id2mod[mod_id]) self.processes[mod_id] = None if self.pipes[mod_id]: self.pipes[mod_id].close() self.pipes[mod_id] = None else: print_debug('manager | %s already terminated\n' % self.id2mod[mod_id]) return
def loop(self): if self.text: # self.http_para['text'] = self.text text = '+'.join(self.text.split()) with open(TTS_OUTPUT_WAVE, 'wb') as outfile: # response = requests.get(TTS_URL, headers=self.http_header, data=self.http_para, stream=True) url = TTS_URL+'&text='+text response = requests.get(url, headers=self.http_header, stream=True) if not response.ok: # Something went wrong print_debug('tts | fail to download wave file ({0})\n'.format(response.status_code)) else: for block in response.iter_content(1024): outfile.write(block) self.result = True # print_debug('tts | wave file downloaded\n') self.text = '' return
def process(self): # suspend mic self.stream.stop_stream() if self.utter<=UTTERANCE_THRESHOLD: print_debug('asr | (temporal noise)\n') else: print_debug('asr | processing...\n') # (deprecated) for processing ring buffer that could be not full # if self.rb_full: # _data = self.silence[self.p_rb:] + self.silence[:self.p_rb] + self.utterance # else: # _data = self.silence[:self.p_rb] + self.utterance # ring better is guaranteed to be full data = self.silence[self.p_rb:] + self.silence[:self.p_rb] + self.utterance # save as wave file self.save_speech(data) # submit to GSR self.transcribe() print_debug('asr | listening...\n') # resume mic self.reset() self.stream.start_stream() return
def handle(self, command): # high priority command if command == 'exit': self.terminate_all() return False # extract module, command, message tmp = command.split(':') for _ in xrange(len(tmp),3): tmp.append(None) mod,cmd,msg = tmp[0],tmp[1],tmp[2] # check module validity if mod not in self.mod2id: print_debug('manager | unable to recognize module: %s\n' % command) return True else: mod = self.mod2id[mod] # map command into function if cmd=='start': self.start_module(mod) elif cmd=='exit': self.terminate_module(mod) elif cmd=='msg': if not msg: print_debug('manager | empty message: %s\n' % command) else: self.send_msg(mod,msg) else: print_debug('manager | unrecognized command: %s\n' % command) return True
def process(self): # suspend mic self.stream.stop_stream() if self.utter <= UTTERANCE_THRESHOLD: print_debug('asr | (temporal noise)\n') else: print_debug('asr | processing...\n') # (deprecated) for processing ring buffer that could be not full # if self.rb_full: # _data = self.silence[self.p_rb:] + self.silence[:self.p_rb] + self.utterance # else: # _data = self.silence[:self.p_rb] + self.utterance # ring better is guaranteed to be full data = self.silence[ self.p_rb:] + self.silence[:self.p_rb] + self.utterance # save as wave file self.save_speech(data) # submit to GSR self.transcribe() print_debug('asr | listening...\n') # resume mic self.reset() self.stream.start_stream() return
def loop(self): # record new data chunk from mic try: data = self.stream.read(CHUNK) except IOError: return # calculate amplitude based on root mean square amp = audioop.rms(data, DEPTH / 8) # display amplitude bar bar_len = min(MAX_LINE, amp / SLOPE) print_debug('[' + '|' * bar_len + ' ' * (MAX_LINE - bar_len) + '] rms:' + str(amp)) # determine silent or not based on threshold silent = amp < SILENT_THRESHOLD if silent and not self.recording: # append data to ring buffer self.silence[self.p_rb] = data self.p_rb = (self.p_rb + 1) % SIL_BEG if not self.p_rb: self.rb_full = True else: # ensure 1 sec of beginning silence for the first utterance if not self.rb_full: return # append data to utterance self.utterance.append(data) if not self.recording: # implies that it's not silent self.utter += 1 self.recording = True elif silent: # implies that it's recording self.p_sil += 1 if self.p_sil == SIL_END: # utterance collected, process! self.process() else: # recording and not silent self.utter += 1 self.p_sil = 0 return
def loop(self): if self.text: # self.http_para['text'] = self.text text = '+'.join(self.text.split()) with open(TTS_OUTPUT_WAVE, 'wb') as outfile: # response = requests.get(TTS_URL, headers=self.http_header, data=self.http_para, stream=True) url = TTS_URL + '&text=' + text response = requests.get(url, headers=self.http_header, stream=True) if not response.ok: # Something went wrong print_debug( 'tts | fail to download wave file ({0})\n'.format( response.status_code)) else: for block in response.iter_content(1024): outfile.write(block) self.result = True # print_debug('tts | wave file downloaded\n') self.text = '' return
def loop(self): # record new data chunk from mic try: data = self.stream.read(CHUNK) except IOError: return # calculate amplitude based on root mean square amp = audioop.rms(data, DEPTH/8) # display amplitude bar bar_len = min(MAX_LINE, amp/SLOPE) print_debug('['+'|'*bar_len+' '*(MAX_LINE-bar_len)+'] rms:'+str(amp)) # determine silent or not based on threshold silent = amp < SILENT_THRESHOLD if silent and not self.recording: # append data to ring buffer self.silence[self.p_rb] = data self.p_rb = (self.p_rb + 1) % SIL_BEG if not self.p_rb: self.rb_full = True else: # ensure 1 sec of beginning silence for the first utterance if not self.rb_full: return # append data to utterance self.utterance.append(data) if not self.recording: # implies that it's not silent self.utter += 1 self.recording = True elif silent: # implies that it's recording self.p_sil += 1 if self.p_sil == SIL_END: # utterance collected, process! self.process() else: # recording and not silent self.utter += 1 self.p_sil = 0 return
def asr_process(pipe): print 'asr | process started' gsr = GSR() # setup gsr gsr.setup() while True: # process input command if any if pipe.poll(): cmd = pipe.recv() print_debug('asr | received: %s\n' % cmd) if cmd=='exit': break # loop gsr # gsr.loop_test() gsr.loop() # send recognized text if any if gsr.text: pipe.send(gsr.text) if gsr.text=='exit': break gsr.text = '' # clean up gsr gsr.clean_up() pipe.close() print 'asr | process terminated' return
def asr_process(pipe): print 'asr | process started' gsr = GSR() # setup gsr gsr.setup() while True: # process input command if any if pipe.poll(): cmd = pipe.recv() print_debug('asr | received: %s\n' % cmd) if cmd == 'exit': break # loop gsr # gsr.loop_test() gsr.loop() # send recognized text if any if gsr.text: pipe.send(gsr.text) if gsr.text == 'exit': break gsr.text = '' # clean up gsr gsr.clean_up() pipe.close() print 'asr | process terminated' return
def slu_process(pipe): slu = SLU() print_debug('slu | process started\n') while True: # process input command if any if pipe.poll(): text = pipe.recv() if text=='exit': break slu.text = text slu.loop() # send result back to dialog manager if slu.result: pipe.send(slu.result) print_debug('slu | %s | %s\n' % (slu.result['mod'],slu.result['data'])) slu.result = None pipe.close() print_debug('slu | process terminated\n') return
def slu_process(pipe): slu = SLU() print_debug('slu | process started\n') while True: # process input command if any if pipe.poll(): text = pipe.recv() if text == 'exit': break slu.text = text slu.loop() # send result back to dialog manager if slu.result: pipe.send(slu.result) print_debug('slu | %s | %s\n' % (slu.result['mod'], slu.result['data'])) slu.result = None pipe.close() print_debug('slu | process terminated\n') return
def act_process(pipe): print_debug('act | process started\n') act = ACT() while True: # process input command if any if pipe.poll(): text = pipe.recv() if text == 'exit': break act.cmd = text act.loop() # feed back if necessary if act.result: print_debug('act | action done') # pipe.send('some kind of indicator') act.result = False # less CPU occupancy time.sleep(0.01) pipe.close() print_debug('act | process terminated\n') return
def transcribe(self): # wave_file = os.path.join(path.dirname(path.realpath(__file__)), WAVE_OUTPUT_FILENAME) wave_file = os.path.join(ASR_OUTPUT_WAVE) recognizer = sr.Recognizer() with sr.WavFile(wave_file) as source: _data = recognizer.record(source) # read the entire WAV file # recognize speech using Google Speech Recognition try: self.text = recognizer.recognize_google(_data) # print recognizer.recognize_google(_data, show_all=True) except sr.UnknownValueError: # print(" | error: could not understand audio (empty input?)") print_debug('asr | (empty)\n') self.text = '' return except sr.RequestError as e: print_debug('asr | error: could not request results from service; {0}\n'.format(e)) self.clean_up() exit() print_debug('asr | speech: %s\n' % self.text) return
def tts_process(pipe): print_debug('tts | process started\n') tts = TTS() tts.setup() while True: # process input command if any if pipe.poll(): text = pipe.recv() if text=='exit': break tts.text = text tts.loop() # play the returned wave only once if tts.result: # pipe.send('something') print_debug('tts | playing back...\n') tts.play() tts.result = False # less CPU occupancy time.sleep(0.01) tts.clean_up() pipe.close() print_debug('tts | process terminated\n') return
def tts_process(pipe): print_debug('tts | process started\n') tts = TTS() tts.setup() while True: # process input command if any if pipe.poll(): text = pipe.recv() if text == 'exit': break tts.text = text tts.loop() # play the returned wave only once if tts.result: # pipe.send('something') print_debug('tts | playing back...\n') tts.play() tts.result = False # less CPU occupancy time.sleep(0.01) tts.clean_up() pipe.close() print_debug('tts | process terminated\n') return
def transcribe(self): # wave_file = os.path.join(path.dirname(path.realpath(__file__)), WAVE_OUTPUT_FILENAME) wave_file = os.path.join(ASR_OUTPUT_WAVE) recognizer = sr.Recognizer() with sr.WavFile(wave_file) as source: _data = recognizer.record(source) # read the entire WAV file # recognize speech using Google Speech Recognition try: self.text = recognizer.recognize_google(_data) # print recognizer.recognize_google(_data, show_all=True) except sr.UnknownValueError: # print(" | error: could not understand audio (empty input?)") print_debug('asr | (empty)\n') self.text = '' return except sr.RequestError as e: print_debug( 'asr | error: could not request results from service; {0}\n'. format(e)) self.clean_up() exit() print_debug('asr | speech: %s\n' % self.text) return
def act_process(pipe): print_debug('act | process started\n') act = ACT() while True: # process input command if any if pipe.poll(): text = pipe.recv() if text == 'exit': break act.cmd = text act.loop() # feed back if necessary if act.result: print_debug('act | action done') # pipe.send('some kind of indicator') act.result = False # less CPU occupancy time.sleep(0.01) pipe.close() print_debug('act | process terminated\n') return if __name__ == '__main__': _act = ACT() _act.cmd = '1' _act.loop_test() if _act.result: print_debug('act | performing action \'%s\'...\n' % _act.result) _act.result = ''
text = pipe.recv() if text=='exit': break tts.text = text tts.loop() # play the returned wave only once if tts.result: # pipe.send('something') print_debug('tts | playing back...\n') tts.play() tts.result = False # less CPU occupancy time.sleep(0.01) tts.clean_up() pipe.close() print_debug('tts | process terminated\n') return if __name__ == '__main__': TTS_OUTPUT_WAVE = '../tmp/tts.wav' API_AI_CREDENTIAL = '../configs/credential.json' _tts = TTS() _tts.setup() _tts.text = 'it\'s 26 degree, with shower rain.' _tts.loop() if _tts.result: print_debug('tts | playing back...\n') _tts.play() _tts.clean_up()
def load_hmm(self): print_debug('slu | initializing recognizer...\n') # set True to train new model and False to load from pre-trained self.hmm_seq_recognizer = predefined_hmm(MODEL_PATH, False) return
if __name__ == '__main__': # make sure the controller is running if not os.path.exists(NAMED_PIPE): print '=> missing named pipe, please run the controller first' exit() # create dialog manager process parent, child = Pipe() dmp = Process(target=dm_process, args=(child, )) dmp.start() # main loop, receive commands from named pipeto and relay to dialog manager while True: # print('waiting...') fifo = open(NAMED_PIPE, 'r') # receive command from controller through named pipe cmd = fifo.readline() print_debug('controller | ' + cmd) fifo.close() # send command to dialog manager parent.send(cmd) if cmd == 'exit': # wait for dialog manager to terminate dmp.join() # close parent pipe, child pipe will be close in child process parent.close() break # leave time for controller to open the named pipe first time.sleep(0.1)
if pipe.poll(): text = pipe.recv() if text == 'exit': break tts.text = text tts.loop() # play the returned wave only once if tts.result: # pipe.send('something') print_debug('tts | playing back...\n') tts.play() tts.result = False # less CPU occupancy time.sleep(0.01) tts.clean_up() pipe.close() print_debug('tts | process terminated\n') return if __name__ == '__main__': TTS_OUTPUT_WAVE = '../tmp/tts.wav' API_AI_CREDENTIAL = '../configs/credential.json' _tts = TTS() _tts.setup() _tts.text = 'it\'s 26 degree, with shower rain.' _tts.loop() if _tts.result: print_debug('tts | playing back...\n') _tts.play() _tts.clean_up()
if __name__ == '__main__': # make sure the controller is running if not os.path.exists(NAMED_PIPE): print '=> missing named pipe, please run the controller first' exit() # create dialog manager process parent,child = Pipe() dmp = Process(target=dm_process, args=(child,)) dmp.start() # main loop, receive commands from named pipeto and relay to dialog manager while True: # print('waiting...') fifo = open(NAMED_PIPE, 'r') # receive command from controller through named pipe cmd = fifo.readline() print_debug('controller | '+cmd) fifo.close() # send command to dialog manager parent.send(cmd) if cmd=='exit': # wait for dialog manager to terminate dmp.join() # close parent pipe, child pipe will be close in child process parent.close() break # leave time for controller to open the named pipe first time.sleep(0.1)