def listen(language): CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 16000 p = pyaudio.PyAudio() while GPIO.input(23)==False: continue stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) #buffer print("* recording") frames = [] while GPIO.input(23): data = stream.read(CHUNK) frames.append(data) print("* done recording") filename='output_'+str(int(time.time()))+'.wav' stream.stop_stream() stream.close() p.terminate() wf = wave.open(filename, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close() return stt.stt_google_wav(filename, language)
def listen(language): CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 16000 p = pyaudio.PyAudio() while GPIO.input(23) == False: continue stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) #buffer print("* recording") frames = [] while GPIO.input(23): data = stream.read(CHUNK) frames.append(data) print("* done recording") filename = 'output_' + str(int(time.time())) + '.wav' stream.stop_stream() stream.close() p.terminate() wf = wave.open(filename, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close() return stt.stt_google_wav(filename, language)
def passive_listen(language, threshold=THRESHOLD, num_phrases=-1): """ Listens to Microphone, extracts phrases from it and sends it to s TTS service and returns response. a "phrase" is sound surrounded by silence (according to threshold). num_phrases controls how many phrases to process before finishing the listening process (-1 for infinite). """ #Open stream p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print "* Listening mic. " audio2send = [] cur_data = '' # current chunk of audio data rel = RATE / CHUNK slid_win = deque(maxlen=SILENCE_LIMIT * rel) #Prepend audio from 0.5 seconds before noise was detected prev_audio = deque(maxlen=PREV_AUDIO * rel) started = False n = num_phrases response = [] for i in range(0, RATE / CHUNK * LISTEN_TIME): #Listen for the name just the time we decide while (num_phrases == -1 or n > 0): cur_data = stream.read(CHUNK) slid_win.append(math.sqrt(abs(audioop.avg(cur_data, 4)))) #print slid_win[-1] if (sum([x > THRESHOLD for x in slid_win]) > 0): if (not started): print "Starting record of phrase" started = True audio2send.append(cur_data) elif (started is True): print "Finished" # The limit was reached, finish capture and deliver. filename = save_speech(list(prev_audio) + audio2send, p) # Send file to Google and get response r = stt.stt_google_wav(filename, language) if num_phrases == -1: return r else: response.append(r) # Remove temp file. Comment line to review. os.remove(filename) # Reset all started = False slid_win = deque(maxlen=SILENCE_LIMIT * rel) prev_audio = deque(maxlen=0.5 * rel) audio2send = [] n -= 1 print "Listening ..." else: prev_audio.append(cur_data) print "* Done recording" stream.close() p.terminate() return response
def passive_listen(language, threshold=THRESHOLD, num_phrases=-1): """ Listens to Microphone, extracts phrases from it and sends it to s TTS service and returns response. a "phrase" is sound surrounded by silence (according to threshold). num_phrases controls how many phrases to process before finishing the listening process (-1 for infinite). """ #Open stream p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print "* Listening mic. " audio2send = [] cur_data = '' # current chunk of audio data rel = RATE/CHUNK slid_win = deque(maxlen=SILENCE_LIMIT * rel) #Prepend audio from 0.5 seconds before noise was detected prev_audio = deque(maxlen=PREV_AUDIO * rel) started = False n = num_phrases response = [] for i in range(0, RATE / CHUNK * LISTEN_TIME): #Listen for the name just the time we decide while (num_phrases == -1 or n > 0): cur_data = stream.read(CHUNK) slid_win.append(math.sqrt(abs(audioop.avg(cur_data, 4)))) #print slid_win[-1] if(sum([x > THRESHOLD for x in slid_win]) > 0): if(not started): print "Starting record of phrase" started = True audio2send.append(cur_data) elif (started is True): print "Finished" # The limit was reached, finish capture and deliver. filename = save_speech(list(prev_audio) + audio2send, p) # Send file to Google and get response r = stt.stt_google_wav(filename, language) if num_phrases == -1: return r else: response.append(r) # Remove temp file. Comment line to review. os.remove(filename) # Reset all started = False slid_win = deque(maxlen=SILENCE_LIMIT * rel) prev_audio = deque(maxlen=0.5 * rel) audio2send = [] n -= 1 print "Listening ..." else: prev_audio.append(cur_data) print "* Done recording" stream.close() p.terminate() return response