Skip to content

Instantly share code, notes, and snippets.

@abhishekmurthy
Created December 11, 2013 07:17
Show Gist options
  • Save abhishekmurthy/7906243 to your computer and use it in GitHub Desktop.
Save abhishekmurthy/7906243 to your computer and use it in GitHub Desktop.
Google speech API in python
# based on http://mikepultz.com/2011/03/accessing-google-speech-api-chrome-11/
# needs pyaudio package <apt-get install python-pyaudio>
# sox commands to convert from way to flac format <apt-get install sox>
import pyaudio
import wave
import subprocess, shlex
import os
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "record.wav"
p = pyaudio.PyAudio()
inputKey = ''
while inputKey != 'q':
frames = []
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
print("* recording")
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
# p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()
# sox command
commandLine = 'sox record.wav record.flac'
args = shlex.split(commandLine)
proc = subprocess.call(args)
# google wget...
commandLine = 'wget --post-file record.flac --header="Content-Type: audio/x-flac; rate=44100" -O - "http://www.google.com/speech-api/v1/recognize?lang=en-us&client=chromium"'
args = shlex.split(commandLine)
proc = subprocess.call(args)
print 'want to record next sentence? press q to exit any other key to continue...',
inputKey = raw_input()
os.remove("record.wav")
os.remove("record.flac")
p.terminate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment