rendered paste bodyroot@debian:/home/darwin# cat gtts.py
import pycurl
import StringIO
import simplejson as json
import re
import os
import subprocess
DEV = "default:CARD=U0x93a0x2625"
def recognize():
url = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"
res = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.POST, 1)
c.setopt(c.URL, url)
c.setopt(pycurl.HTTPHEADER, ["Content-Type: audio/x-flac; rate=16000"])
c.setopt(c.HTTPPOST, [("speech.flac", (c.FORM_FILE, "speech.flac"))])
c.setopt(pycurl.WRITEFUNCTION, res.write)
c.perform()
c.close()
rcvdjson = res.getvalue()
return json.loads(rcvdjson)['hypotheses'][0]
def record(device):
os.system('arecord -D %s -t wav -r 16000 > speech.wav' % device)
def toflac():
os.system('rm -f speech.flac')
#os.system('ffmpeg -i speech.wav speech.flac &> %s' % os.devnull)
s = subprocess.Popen(['ffmpeg', '-i', 'speech.wav', 'speech.flac'], \
stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
print "Recording your voice from "+DEV
print "Hit ^C when you're done"
record(DEV)
print "Converting your speech to FLAC"
toflac()
print "Sending your speech to the Google."
print "You said: "+recognize()['utterance']