Advertisement
Marty1881

Untitled

May 13th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. import pyaudio
  2. import wave
  3. import audioop
  4. from collections import deque
  5. import os
  6. import urllib2
  7. import urllib
  8. import time
  9.  
  10.  
  11. def handleText(text):
  12. print text;
  13.  
  14.  
  15. def listen_for_speech():
  16.  
  17. chunk = 1024
  18. FORMAT = pyaudio.paInt16
  19. CHANNELS = 1
  20. RATE = 16000
  21. THRESHOLD = 180 #The threshold intensity that defines silence signal (lower than).
  22. SILENCE_LIMIT = 2 #Silence limit in seconds. The max ammount of seconds where only silence is recorded. When this time passes the recording finishes and the file is delivered.
  23.  
  24. #open stream
  25. p = pyaudio.PyAudio()
  26.  
  27. stream = p.open(format = FORMAT,
  28. channels = CHANNELS,
  29. rate = RATE,
  30. input = True,
  31. frames_per_buffer = chunk)
  32.  
  33. print "* listening. CTRL+C to finish."
  34. all_m = []
  35. data = ''
  36. SILENCE_LIMIT = 2
  37. rel = RATE/chunk
  38. slid_win = deque(maxlen=SILENCE_LIMIT*rel)
  39. started = False
  40.  
  41. while (True):
  42. data = stream.read(chunk)
  43. slid_win.append (abs(audioop.avg(data, 2)))
  44.  
  45. if(True in [ x>THRESHOLD for x in slid_win]):
  46. if(not started):
  47. print "starting record"
  48. started = True
  49. all_m.append(data)
  50. elif (started==True):
  51. print "finished"
  52. #the limit was reached, finish capture and deliver
  53. filename = save_speech(all_m,p)
  54. handleText(stt_google_wav(filename))
  55. #reset all
  56. started = False
  57. slid_win = deque(maxlen=SILENCE_LIMIT*rel)
  58. all_m= []
  59. print "listening ..."
  60.  
  61. print "* done recording"
  62. stream.close()
  63. p.terminate()
  64.  
  65.  
  66. def save_speech(data, p):
  67. filename = 'output_'+str(int(time.time()))
  68. # write data to WAVE file
  69. data = ''.join(data)
  70. wf = wave.open(filename+'.wav', 'wb')
  71. wf.setnchannels(1)
  72. wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
  73. wf.setframerate(16000)
  74. wf.writeframes(data)
  75. wf.close()
  76. return filename
  77.  
  78.  
  79. def stt_google_wav(filename):
  80. #Convert to flac
  81. os.system('ffmpeg -loglevel panic -i ' + filename + '.wav -ab 192k -y ' + filename + '.flac')
  82. f = open(filename+'.flac','rb')
  83. flac_cont = f.read()
  84. f.close()
  85. #post it
  86. lang_code='en-US'
  87. googl_speech_url = 'https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&pfilter=2&lang=%s&maxresults=6'%(lang_code)
  88. hrs = {"User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7",'Content-type': 'audio/x-flac; rate=16000'}
  89. req = urllib2.Request(googl_speech_url, data=flac_cont, headers=hrs)
  90. p = urllib2.urlopen(req)
  91.  
  92. res = eval(p.read())['hypotheses']
  93. map(os.remove, (filename+'.flac', filename+'.wav'))
  94. return res
  95.  
  96. if(__name__ == '__main__'):
  97. listen_for_speech()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement