[Tutor] sending a wav file to a website?

eryksun eryksun at gmail.com
Wed Feb 13 08:33:01 CET 2013

On Tue, Feb 12, 2013 at 3:48 PM, richard kappler <richkappler at gmail.com> wrote:
> So the question is, how do I get a wav file to a website like the one listed
> above and retrieve the text result using python (or don't I)? I've looked at
> and played around with urllib, I get how it works, I'm pretty comfy with it,
> but it seems to be for sending text only? Or is it?

Are you looking to use Google's speech recognition service? It's meant
for use with the Chrome browser. I suppose you can play around with it
for non-commercial, personal use. Bear in mind Google can pull the
plug on public access to the API at any time. Here's the unofficial


And a rough draft:

    import urllib
    import urllib2
    import json

    SPEECH_URL = 'https://www.google.com/speech-api/v1/recognize'
    CLIENT = 'chromium'
    USER_AGENT = (
      'Mozilla/5.0 (X11; Linux x86_64) '
      'AppleWebKit/537.4 (KHTML like Gecko) '
      'Chrome/22.0.1229.56 Safari/537.4')

    def gspeech(data, rate=16000, lang='en-US', maxresults=10):
        params = urllib.urlencode({
          'lang': lang,
          'maxresults': maxresults,
          'client': CLIENT})
        url = '{0}?{1}'.format(SPEECH_URL, params)
        header = {
          'Content-Type': 'audio/x-flac; rate={0}'.format(rate),
          'User-Agent': USER_AGENT}
        request = urllib2.Request(url, data, header)
        data = urllib2.urlopen(request).read()
        return json.loads(data)

Sample data:

    >>> url = ('http://archive.org/download/'
    ...        'MichaelGraySampleSamplewav/Sample.flac')
    >>> data = urllib2.urlopen(url).read()
    >>> result = gspeech(data, 44100, 'en-UK')
    >>> result['hypotheses'][0]['utterance']
    u"I'm speaking normally"
    >>> result['hypotheses'][0]['confidence']

More information about the Tutor mailing list