<s> import pyaudio <EOL> import wave <EOL> import audioop <EOL> from collections import deque <EOL> import os <EOL> import urllib2 <EOL> import urllib <EOL> import time <EOL> import math <EOL> LANG_CODE = '<STR_LIT>' <EOL> GOOGLE_SPEECH_URL = '<STR_LIT>' % ( LANG_CODE ) <EOL> FLAC_CONV = '<STR_LIT>' <EOL> CHUNK = <NUM_LIT> <EOL> FORMAT = pyaudio . paInt16 <EOL> CHANNELS = <NUM_LIT:1> <EOL> RATE = <NUM_LIT> <EOL> THRESHOLD = <NUM_LIT> <EOL> SILENCE_LIMIT = <NUM_LIT:1> <EOL> PREV_AUDIO = <NUM_LIT:0.5> <EOL> def audio_int ( num_samples = <NUM_LIT:50> ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> p = pyaudio . PyAudio ( ) <EOL> stream = p . open ( format = FORMAT , <EOL> channels = CHANNELS , <EOL> rate = RATE , <EOL> input = True , <EOL> frames_per_buffer = CHUNK ) <EOL> values = [ math . sqrt ( abs ( audioop . avg ( stream . read ( CHUNK ) , <NUM_LIT:4> ) ) ) <EOL> for x in range ( num_samples ) ] <EOL> values = sorted ( values , reverse = True ) <EOL> r = sum ( values [ : int ( num_samples * <NUM_LIT> ) ] ) / int ( num_samples * <NUM_LIT> ) <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" , r <EOL> stream . close ( ) <EOL> p . terminate ( ) <EOL> return r <EOL> def listen_for_speech ( threshold = THRESHOLD , num_phrases = - <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> p = pyaudio . PyAudio ( ) <EOL> stream = p . open ( format = FORMAT , <EOL> channels = CHANNELS , <EOL> rate = RATE , <EOL> input = True , <EOL> frames_per_buffer = CHUNK ) <EOL> print "<STR_LIT>" <EOL> audio2send = [ ] <EOL> cur_data = '<STR_LIT>' <EOL> rel = RATE / CHUNK <EOL> slid_win = deque ( maxlen = SILENCE_LIMIT * rel ) <EOL> prev_audio = deque ( maxlen = PREV_AUDIO * rel ) <EOL> started = False <EOL> n = num_phrases <EOL> response = [ ] <EOL> while ( num_phrases == - <NUM_LIT:1> or n > <NUM_LIT:0> ) : <EOL> cur_data = stream . read ( CHUNK ) <EOL> slid_win . append ( math . sqrt ( abs ( audioop . avg ( cur_data , <NUM_LIT:4> ) ) ) ) <EOL> if ( sum ( [ x > THRESHOLD for x in slid_win ] ) > <NUM_LIT:0> ) : <EOL> if ( not started ) : <EOL> print "<STR_LIT>" <EOL> started = True <EOL> audio2send . append ( cur_data ) <EOL> elif ( started is True ) : <EOL> print "<STR_LIT>" <EOL> filename = save_speech ( list ( prev_audio ) + audio2send , p ) <EOL> r = stt_google_wav ( filename ) <EOL> if num_phrases == - <NUM_LIT:1> : <EOL> print "<STR_LIT>" , r <EOL> else : <EOL> response . append ( r ) <EOL> os . remove ( filename ) <EOL> started = False <EOL> slid_win = deque ( maxlen = SILENCE_LIMIT * rel ) <EOL> prev_audio = deque ( maxlen = <NUM_LIT:0.5> * rel ) <EOL> audio2send = [ ] <EOL> n -= <NUM_LIT:1> <EOL> print "<STR_LIT>" <EOL> else : <EOL> prev_audio . append ( cur_data ) <EOL> print "<STR_LIT>" <EOL> stream . close ( ) <EOL> p . terminate ( ) <EOL> return response <EOL> def save_speech ( data , p ) : <EOL> """<STR_LIT>""" <EOL> filename = '<STR_LIT>' + str ( int ( time . time ( ) ) ) <EOL> data = '<STR_LIT>' . join ( data ) <EOL> wf = wave . open ( filename + '<STR_LIT>' , '<STR_LIT:wb>' ) <EOL> wf . setnchannels ( <NUM_LIT:1> ) <EOL> wf . setsampwidth ( p . get_sample_size ( pyaudio . paInt16 ) ) <EOL> wf . setframerate ( <NUM_LIT> ) <EOL> wf . writeframes ( data ) <EOL> wf . close ( ) <EOL> return filename + '<STR_LIT>' <EOL> def stt_google_wav ( audio_fname ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" , audio_fname <EOL> filename = audio_fname <EOL> del_flac = False <EOL> if '<STR_LIT>' not in filename : <EOL> del_flac = True <EOL> print "<STR_LIT>" <EOL> print FLAC_CONV + filename <EOL> os . system ( FLAC_CONV + '<STR_LIT:U+0020>' + filename ) <EOL> filename = filename . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> f = open ( filename , '<STR_LIT:rb>' ) <EOL> flac_cont = f . read ( ) <EOL> f . close ( ) <EOL> hrs = { "<STR_LIT>" : "<STR_LIT>" , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> req = urllib2 . Request ( GOOGLE_SPEECH_URL , data = flac_cont , headers = hrs ) <EOL> print "<STR_LIT>" <EOL> try : <EOL> p = urllib2 . urlopen ( req ) <EOL> response = p . read ( ) <EOL> res = eval ( response ) [ '<STR_LIT>' ] <EOL> except : <EOL> print "<STR_LIT>" <EOL> res = None <EOL> if del_flac : <EOL> os . remove ( filename ) <EOL> return res <EOL> if ( __name__ == '<STR_LIT:__main__>' ) : <EOL> listen_for_speech ( ) </s>
