Skip to content

@alotaiba /google_speech2text.md
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Google Speech To Text API

Google Speech To Text API

Base URL: https://www.google.com/speech-api/v1/recognize
It accepts POST requests with voice file encoded in FLAC format, and query parameters for control.

Query Parameters

client
The client's name you're connecting from. For spoofing purposes, let's use chromium

lang
Speech language, for example, ar-QA for Qatari Arabic, or en-US for U.S. English

maxresults
Maximum results to return for utterance

POST

body
Should contain FLAC formatted voice binary

HTTP Header

Content-Type
Should be audio/x-flac; rate=16000;, where MIME and sample rate of the FLAC file is included

User-Agent
Can be the client's user agent string, for spoofing purposes, we'll use Chrome's

Examples

These examples assume you have a voice file encoded in FLAC called alsalam-alikum.flac.

wget

This will save JSON response in a file called recognized.json

wget --post-file='alsalam-alikum.flac' \
--user-agent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7' \
--header='Content-Type: audio/x-flac; rate=16000;' \
-O 'recognized.json' \
'https://www.google.com/speech-api/v1/recognize?client=chromium&lang=ar-QA&maxresults=10'

curl

curl -X POST \
--data-binary @alsalam-alikum.flac \
--user-agent 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7' \
--header 'Content-Type: audio/x-flac; rate=16000;' \
'https://www.google.com/speech-api/v1/recognize?client=chromium&lang=ar-QA&maxresults=10'
@taf2

API might have changed

<HTML>
<HEAD>
<TITLE>Recognition failed: NO_MATCH</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Recognition failed: NO_MATCH</H1>
<H2>Error 400</H2>
</BODY>
</HTML>
@firekinger
<HEAD>
<TITLE>HTTP method GET is not supported by this URL</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>HTTP method GET is not supported by this URL</H1>
<H2>Error 405</H2>
</BODY>
</HTML>
@rgtk
wget -q -U "Mozilla/5.0" --post-file /message.flac --header="Content-Type: audio/x-flac; rate=16000" -O - "http://www.google.com/speech-api/v1/recognize?lang=pl-pl&client=chromium"

This should work.

@ewoudenberg

In Python:

$ cat speech.py
import urllib2
url = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"
audio = open('rainspain.flac','rb').read()
headers={'Content-Type': 'audio/x-flac; rate=16000', 'User-Agent':'Mozilla/5.0'}
request = urllib2.Request(url, data=audio, headers=headers)
response = urllib2.urlopen(request)
print response.read()

$ python speech.py
{"status":0,"id":"57d2d1a7e7f1fa12d200026dde946c34-1","hypotheses":[{"utterance":"the rain in Spain falls mainly on the plains","confidence":0.8385102}]}

@kindziora

google does not return anything... no result, has somebody an idea why?

@selvakumar1989

Any one can tell how to implement this using javascript(Ajax post)

@flux3000

Note that the API only accepts audio files of 15 seconds or less.

@AvatarDa

use java or jsp

<%@ page import="java.io.BufferedInputStream"%>
<%@ page import="java.io.BufferedOutputStream"%>
<%@ page import="java.io.ByteArrayOutputStream"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.*"%>
<%@ page import="java.io.FileOutputStream"%>
<%@ page import="java.net.HttpURLConnection"%>
<%@ page import="java.net.URL"%>
<%@ page import="java.net.URLEncoder"%>
<%@ page import="java.net.Proxy"%>
<%@ page import="java.net.InetSocketAddress"%>

<%@ page import="javaFlacEncoder.FLACEncoder"%>
<%@ page import="javaFlacEncoder.FLACFileOutputStream"%>
<%@ page import="javaFlacEncoder.FLAC_FileEncoder"%>
<%@ page import="javaFlacEncoder.StreamConfiguration"%>

<%@ page import="org.apache.http.client.HttpClient"%>
<%@ page import="org.apache.http.client.methods.HttpPost"%>
<%@ page import="org.apache.http.entity.mime.MultipartEntity"%>
<%@ page import="org.apache.http.entity.mime.content.ContentBody"%>
<%@ page import="org.apache.http.entity.mime.content.FileBody"%>
<%@ page import="org.apache.commons.httpclient.params.HttpClientParams"%>
<%@ page import="org.apache.http.impl.client.DefaultHttpClient"%>
<%@ page import="org.apache.http.params.CoreProtocolPNames"%>
<%@ page import="org.apache.http.HttpVersion"%>
<%@ page import="org.apache.http.HttpResponse"%>
<%@ page import="org.apache.commons.httpclient.methods.PostMethod"%>
<%@ page import="org.apache.http.conn.params.ConnRoutePNames"%>
<%@ page import="org.apache.http.HttpHost"%>

<%@ page import="javax.sound.sampled.AudioFormat"%>
<%@ page import="javax.sound.sampled.AudioInputStream"%>
<%@ page import="javax.sound.sampled.AudioSystem"%>
<%@ page import="java.nio.ByteBuffer"%>
<%@ page import="java.nio.ByteOrder"%>

<%@ page import="java.net.URLConnection"%>

<%@ page import="java.util.Date"%>

<% String SPEECH_TEXT_TO_SERVICE = "https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/RE8a3e55ab37113780ca65f1e02fa1ad11";
//"https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/RE23b0826200ff4f2adcaa9e1d9ac4180a";

      String strUrl = (request.getParameter("urls")+"" != null)?request.getParameter("urls")+"":SPEECH_TEXT_TO_SERVICE;
      //SPEECH_TEXT_TO_SERVICE;
      //
      String USER_AGENT =  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) " + "Gecko/20100101 Firefox/11.0";

      URL url = new URL(strUrl);          
      Proxy proxy =new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.53.29", 3128));
      HttpURLConnection connection=(HttpURLConnection) url.openConnection(proxy);
      connection.setRequestMethod("GET");
      connection.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.70 Safari/537.36");

      out.println(new Date().toString().substring(11,20).replaceAll("IST 2013 ","")+" =====> Starting downloading <br>");
      connection.connect();
      for (int i=0;connection.getHeaderField(i) != null;i++)
      { 
         //out.println(connection.getContentType());
      }
    Long time = connection.getDate();
    InputStream inputStream = connection.getInputStream();
      File input = new File(time+"_input.wav");

      FileOutputStream f = new FileOutputStream(input);
    OutputStream outputStream = f;
      int read = 0;
    byte[] bytes = new byte[1024];
    while ((read = inputStream.read(bytes)) != -1) 
    {
        outputStream.write(bytes, 0, read);
    }
    outputStream.close();
    AudioInputStream audioInputStream1 = AudioSystem.getAudioInputStream(input);
    AudioFormat audioFormat1 = audioInputStream1.getFormat();
    int sampleRate =(int) audioFormat1.getSampleRate();

    out.println(new Date().toString().substring(11,20)+" =====> Create file <br>");
    File output = new File(time+"output.flac");          
      out.println(new Date().toString().substring(11,20)+" =====> FLAC Convert Start <br>");

        StreamConfiguration streamConfiguration = new StreamConfiguration();
    streamConfiguration.setSampleRate(sampleRate);
    streamConfiguration.setBitsPerSample(16);
    streamConfiguration.setChannelCount(1);

    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(input);
    AudioFormat format = audioInputStream.getFormat();

    int frameSize = format.getFrameSize();

    FLACEncoder flacEncoder = new FLACEncoder();
    FLACFileOutputStream flacOutputStream = new FLACFileOutputStream(output);

    flacEncoder.setStreamConfiguration(streamConfiguration);
    flacEncoder.setOutputStream(flacOutputStream);

    flacEncoder.openFLACStream();

    int[] sampleData = new int[(int) audioInputStream.getFrameLength()];
    byte[] samplesIn = new byte[frameSize];

      int x = 0;

        while (audioInputStream.read(samplesIn, 0, frameSize) != -1) {
            if (frameSize != 1) 
            {
                ByteBuffer bb = ByteBuffer.wrap(samplesIn);
                bb.order(ByteOrder.LITTLE_ENDIAN);
                short shortVal = bb.getShort();
                sampleData[x] = shortVal;
            } else {
                sampleData[x] = samplesIn[0];
            }

            x++;
        }

    flacEncoder.addSamples(sampleData, x);
    flacEncoder.encodeSamples(x, false);
    flacEncoder.encodeSamples(flacEncoder.samplesAvailableToEncode(), true);

    audioInputStream.close();
    flacOutputStream.close();
    out.println(new Date().toString().substring(11,20)+" =====> Convet End <br>");
    out.println(new Date().toString().substring(11,20)+" =====> Send google api Start <br>");

    StringBuilder sb = new StringBuilder("https://www.google.com/speech-api/v1/recognize?client=chromium&lang=en-US&maxresults=10");

// sb.append("&lang=en");
sb.append("&pfilter=0");
// sb.append("&maxresults=2");
URL url1 = new URL(sb.toString());
URLConnection urlConn = url1.openConnection(proxy);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", "audio/x-flac; rate=8000");
OutputStream outputStream1 = urlConn.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(output);
byte[] buffer = new byte[1024];
while ((fileInputStream.read(buffer, 0, 256)) != -1)
{
outputStream1.write(buffer, 0, 256);
outputStream1.flush();
}
fileInputStream.close();
outputStream1.close();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String response2 = br.readLine();
br.close();

    input.delete();
    output.delete();
      out.println(new Date().toString().substring(11,20)+" =====> receive google api result <br>");
      String resp[] = response2.split("utterance");
      for(int k=0;k<resp.length;k++)
      { resp[k]=resp[k].replaceAll("\":\"","").replace("\"},{\"","").replace("\"}]}","");
        if (k==1)
        {
            String temp[] =resp[k].split("\",\"confidence\":");
            out.println("<br> Confidence :<b> "+temp[1].replace("},{\"","")+"</b>");
            out.println("<br>"+k+" . "+temp[0]+"<br>");
        }
        else if(k==resp.length-1)
        {
            out.println(k+" . "+resp[k]+"<br>");
        }
        else if(k!=0)
        {
            out.println(k+" . "+resp[k]+"<br>");
        }

    }
    //out.println(response2+"<br>");
      out.println(sampleRate+"<br>");

%>

@AvatarDa

use java or jsp

<%@ page import="java.io.BufferedInputStream"%>
<%@ page import="java.io.BufferedOutputStream"%>
<%@ page import="java.io.ByteArrayOutputStream"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.*"%>
<%@ page import="java.io.FileOutputStream"%>
<%@ page import="java.net.HttpURLConnection"%>
<%@ page import="java.net.URL"%>
<%@ page import="java.net.URLEncoder"%>
<%@ page import="java.net.Proxy"%>
<%@ page import="java.net.InetSocketAddress"%>

<%@ page import="javaFlacEncoder.FLACEncoder"%>
<%@ page import="javaFlacEncoder.FLACFileOutputStream"%>
<%@ page import="javaFlacEncoder.FLAC_FileEncoder"%>
<%@ page import="javaFlacEncoder.StreamConfiguration"%>

<%@ page import="org.apache.http.client.HttpClient"%>
<%@ page import="org.apache.http.client.methods.HttpPost"%>
<%@ page import="org.apache.http.entity.mime.MultipartEntity"%>
<%@ page import="org.apache.http.entity.mime.content.ContentBody"%>
<%@ page import="org.apache.http.entity.mime.content.FileBody"%>
<%@ page import="org.apache.commons.httpclient.params.HttpClientParams"%>
<%@ page import="org.apache.http.impl.client.DefaultHttpClient"%>
<%@ page import="org.apache.http.params.CoreProtocolPNames"%>
<%@ page import="org.apache.http.HttpVersion"%>
<%@ page import="org.apache.http.HttpResponse"%>
<%@ page import="org.apache.commons.httpclient.methods.PostMethod"%>
<%@ page import="org.apache.http.conn.params.ConnRoutePNames"%>
<%@ page import="org.apache.http.HttpHost"%>

<%@ page import="javax.sound.sampled.AudioFormat"%>
<%@ page import="javax.sound.sampled.AudioInputStream"%>
<%@ page import="javax.sound.sampled.AudioSystem"%>
<%@ page import="java.nio.ByteBuffer"%>
<%@ page import="java.nio.ByteOrder"%>

<%@ page import="java.net.URLConnection"%>

<%@ page import="java.util.Date"%>

<% String SPEECH_TEXT_TO_SERVICE = "https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/RE8a3e55ab37113780ca65f1e02fa1ad11";
//"https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/RE23b0826200ff4f2adcaa9e1d9ac4180a";

      String strUrl = (request.getParameter("urls")+"" != null)?request.getParameter("urls")+"":SPEECH_TEXT_TO_SERVICE;
      //SPEECH_TEXT_TO_SERVICE;
      //
      String USER_AGENT =  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) " + "Gecko/20100101 Firefox/11.0";

      URL url = new URL(strUrl);          
      Proxy proxy =new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.53.29", 3128));
      HttpURLConnection connection=(HttpURLConnection) url.openConnection(proxy);
      connection.setRequestMethod("GET");
      connection.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.70 Safari/537.36");

      out.println(new Date().toString().substring(11,20).replaceAll("IST 2013 ","")+" =====> Starting downloading <br>");
      connection.connect();
      for (int i=0;connection.getHeaderField(i) != null;i++)
      { 
         //out.println(connection.getContentType());
      }
    Long time = connection.getDate();
    InputStream inputStream = connection.getInputStream();
      File input = new File(time+"_input.wav");

      FileOutputStream f = new FileOutputStream(input);
    OutputStream outputStream = f;
      int read = 0;
    byte[] bytes = new byte[1024];
    while ((read = inputStream.read(bytes)) != -1) 
    {
        outputStream.write(bytes, 0, read);
    }
    outputStream.close();
    AudioInputStream audioInputStream1 = AudioSystem.getAudioInputStream(input);
    AudioFormat audioFormat1 = audioInputStream1.getFormat();
    int sampleRate =(int) audioFormat1.getSampleRate();

    out.println(new Date().toString().substring(11,20)+" =====> Create file <br>");
    File output = new File(time+"output.flac");          
      out.println(new Date().toString().substring(11,20)+" =====> FLAC Convert Start <br>");

        StreamConfiguration streamConfiguration = new StreamConfiguration();
    streamConfiguration.setSampleRate(sampleRate);
    streamConfiguration.setBitsPerSample(16);
    streamConfiguration.setChannelCount(1);

    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(input);
    AudioFormat format = audioInputStream.getFormat();

    int frameSize = format.getFrameSize();

    FLACEncoder flacEncoder = new FLACEncoder();
    FLACFileOutputStream flacOutputStream = new FLACFileOutputStream(output);

    flacEncoder.setStreamConfiguration(streamConfiguration);
    flacEncoder.setOutputStream(flacOutputStream);

    flacEncoder.openFLACStream();

    int[] sampleData = new int[(int) audioInputStream.getFrameLength()];
    byte[] samplesIn = new byte[frameSize];

      int x = 0;

        while (audioInputStream.read(samplesIn, 0, frameSize) != -1) {
            if (frameSize != 1) 
            {
                ByteBuffer bb = ByteBuffer.wrap(samplesIn);
                bb.order(ByteOrder.LITTLE_ENDIAN);
                short shortVal = bb.getShort();
                sampleData[x] = shortVal;
            } else {
                sampleData[x] = samplesIn[0];
            }

            x++;
        }

    flacEncoder.addSamples(sampleData, x);
    flacEncoder.encodeSamples(x, false);
    flacEncoder.encodeSamples(flacEncoder.samplesAvailableToEncode(), true);

    audioInputStream.close();
    flacOutputStream.close();
    out.println(new Date().toString().substring(11,20)+" =====> Convet End <br>");
    out.println(new Date().toString().substring(11,20)+" =====> Send google api Start <br>");

    StringBuilder sb = new StringBuilder("https://www.google.com/speech-api/v1/recognize?client=chromium&lang=en-US&maxresults=10");

// sb.append("&lang=en");
sb.append("&pfilter=0");
// sb.append("&maxresults=2");
URL url1 = new URL(sb.toString());
URLConnection urlConn = url1.openConnection(proxy);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", "audio/x-flac; rate=8000");
OutputStream outputStream1 = urlConn.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(output);
byte[] buffer = new byte[1024];
while ((fileInputStream.read(buffer, 0, 256)) != -1)
{
outputStream1.write(buffer, 0, 256);
outputStream1.flush();
}
fileInputStream.close();
outputStream1.close();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String response2 = br.readLine();
br.close();

    input.delete();
    output.delete();
      out.println(new Date().toString().substring(11,20)+" =====> receive google api result <br>");
      String resp[] = response2.split("utterance");
      for(int k=0;k<resp.length;k++)
      { resp[k]=resp[k].replaceAll("\":\"","").replace("\"},{\"","").replace("\"}]}","");
        if (k==1)
        {
            String temp[] =resp[k].split("\",\"confidence\":");
            out.println("<br> Confidence :<b> "+temp[1].replace("},{\"","")+"</b>");
            out.println("<br>"+k+" . "+temp[0]+"<br>");
        }
        else if(k==resp.length-1)
        {
            out.println(k+" . "+resp[k]+"<br>");
        }
        else if(k!=0)
        {
            out.println(k+" . "+resp[k]+"<br>");
        }

    }
    //out.println(response2+"<br>");
      out.println(sampleRate+"<br>");

%>

@AvatarDa

<%@ page import="java.io.File"%>
<%@ page import="java.io.InputStream"%>
<%@ page import="java.io.OutputStream"%>
<%@ page import="java.io.BufferedReader"%>
<%@ page import="java.io.InputStreamReader"%>
<%@ page import="java.io.FileInputStream"%>
<%@ page import="java.io.FileOutputStream"%>
<%@ page import="java.net.HttpURLConnection"%>

<%@ page import="java.net.URL"%>
<%@ page import="java.net.URLConnection"%>
<%@ page import="java.net.URLEncoder"%>
<%@ page import="java.net.Proxy"%>
<%@ page import="java.net.InetSocketAddress"%>

<%@ page import="javaFlacEncoder.FLACEncoder"%>
<%@ page import="javaFlacEncoder.FLACFileOutputStream"%>
<%@ page import="javaFlacEncoder.FLAC_FileEncoder"%>
<%@ page import="javaFlacEncoder.StreamConfiguration"%>

<%@ page import="javax.sound.sampled.AudioFormat"%>
<%@ page import="javax.sound.sampled.AudioInputStream"%>
<%@ page import="javax.sound.sampled.AudioSystem"%>

<%@ page import="java.nio.ByteBuffer"%>
<%@ page import="java.nio.ByteOrder"%>

<%@ page import="java.util.Date"%>

<%

      String SPEECH_TEXT_TO_SERVICE = "https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/RE8a3e55ab37113780ca65f1e02fa1ad11";
      //"https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/RE23b0826200ff4f2adcaa9e1d9ac4180a";
      //"https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/REc17df0c2d73720fbc55cf6c28f584fca";
      //"https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/RE8a3e55ab37113780ca65f1e02fa1ad11";
      //"http://rajesh-zu273:8080/audiotest/speach.wav";
      //"https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/RE45b081bfe0f00a3cddac4c27512d0159";
      //
      //"https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/RE5c752448b7498b68125e33cdf9c971dd";
      //"https://api.twilio.com/2010-04-01/Accounts/AC9d34d1192faffb0989f0227e5768d24d/Recordings/RE26ecb20f15953f6177fda582f9a32aa4";
      //"https://api.twilio.com/2010-04-01/Accounts/ACda006002ebc844b68606b05be0df5e4c/Recordings/REfda7271ce5acc220258dcc1f48d210c2";
      //"http://www5.online-convert.com/download-file/c700fea6cbfc16e7b2172c7f89602a62/converted-b2773e34.wav";
      //"http://www5.online-convert.com/download-file/353123ab67c2ec6659f3722230df2713/converted-fa81c0f8.wav";
      //"http://www19.online-convert.com/download-file/7466a3c51f7c5875e01335a3d5212b7f/converted-6b28c038.wav";
      String strUrl = (request.getParameter("url")+"" != null)?request.getParameter("url")+"":SPEECH_TEXT_TO_SERVICE;

      String USER_AGENT =  "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) " + "Gecko/20100101 Firefox/11.0";

      URL url = new URL(strUrl);          
      Proxy proxy =new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.53.29", 3128));
      HttpURLConnection connection=(HttpURLConnection) url.openConnection(proxy);
      connection.setRequestMethod("GET");
      connection.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.70 Safari/537.36");

      out.println(new Date().toString().substring(11,20).replaceAll("IST 2013 ","")+" =====> Starting downloading <br>");
      connection.connect();

    Long time = connection.getDate();
    InputStream inputStream = connection.getInputStream();
      File input = new File(time+"_input.wav");

      FileOutputStream f = new FileOutputStream(input);
    OutputStream outputStream = f;
      int read = 0;
    byte[] bytes = new byte[1024];
    while ((read = inputStream.read(bytes)) != -1) 
    {
        outputStream.write(bytes, 0, read);
    }
    outputStream.close();



    AudioInputStream audioInputStream1 = AudioSystem.getAudioInputStream(input);
    AudioFormat audioFormat1 = audioInputStream1.getFormat();
    int sampleRate =(int) audioFormat1.getSampleRate();

    out.println(new Date().toString().substring(11,20)+" =====> Create file <br>");
    File output = new File(time+"output.flac");  


    //FLAC Convert Start

      out.println(new Date().toString().substring(11,20)+" =====> FLAC Convert Start <br>");

    StreamConfiguration streamConfiguration = new StreamConfiguration();
    streamConfiguration.setSampleRate(sampleRate);
    streamConfiguration.setBitsPerSample(16);
    streamConfiguration.setChannelCount(1);

    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(input);
    AudioFormat format = audioInputStream.getFormat();

    int frameSize = format.getFrameSize();

    FLACEncoder flacEncoder = new FLACEncoder();
    FLACFileOutputStream flacOutputStream = new FLACFileOutputStream(output);

    flacEncoder.setStreamConfiguration(streamConfiguration);
    flacEncoder.setOutputStream(flacOutputStream);

    flacEncoder.openFLACStream();

    int[] sampleData = new int[(int) audioInputStream.getFrameLength()];
    byte[] samplesIn = new byte[frameSize];

      int x = 0;
    while (audioInputStream.read(samplesIn, 0, frameSize) != -1) 
    {
            if (frameSize != 1) 
            {
                ByteBuffer bb = ByteBuffer.wrap(samplesIn);
                bb.order(ByteOrder.LITTLE_ENDIAN);
                short shortVal = bb.getShort();
                sampleData[x] = shortVal;
            }
            else
            {
                sampleData[x] = samplesIn[0];
            }
            x++;
        }

    flacEncoder.addSamples(sampleData, x);
    flacEncoder.encodeSamples(x, false);
    flacEncoder.encodeSamples(flacEncoder.samplesAvailableToEncode(), true);

    audioInputStream.close();
    flacOutputStream.close();

    //FLAC Convert Start
    out.println(new Date().toString().substring(11,20)+" =====> Convet End <br>");

    out.println(new Date().toString().substring(11,20)+" =====> Send google api Start <br>");


    StringBuilder sb = new StringBuilder("https://www.google.com/speech-api/v1/recognize?client=chromium&lang=en-US&maxresults=10");

// sb.append("&lang=en");
sb.append("&pfilter=0");
// sb.append("&maxresults=2");
URL url1 = new URL(sb.toString());
URLConnection urlConn = url1.openConnection(proxy);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type", "audio/x-flac; rate=8000");
OutputStream outputStream1 = urlConn.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(output);
byte[] buffer = new byte[1024];
while ((fileInputStream.read(buffer, 0, 256)) != -1)
{
outputStream1.write(buffer, 0, 256);
outputStream1.flush();
}
fileInputStream.close();
outputStream1.close();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String response2 = br.readLine();
br.close();

    input.delete();
    output.delete();
      out.println(new Date().toString().substring(11,20)+" =====> receive google api result <br>");
      String resp[] = response2.split("utterance");
      for(int k=0;k<resp.length;k++)
      { resp[k]=resp[k].replaceAll("\":\"","").replace("\"},{\"","").replace("\"}]}","");
        if (k==1)
        {
            String temp[] =resp[k].split("\",\"confidence\":");
            out.println("<br> Confidence :<b> "+temp[1].replace("},{\"","")+"</b>");
            out.println("<br>"+k+" . "+temp[0]+"<br>");
        }
        else if(k==resp.length-1)
        {
            out.println(k+" . "+resp[k]+"<br>");
        }
        else if(k!=0)
        {
            out.println(k+" . "+resp[k]+"<br>");
        }

    }
    //out.println(response2+"<br>");
      out.println(sampleRate+"<br>");

%>

@kumachan2013

Flux3000 mentioned above that "Note that the API only accepts audio files of 15 seconds or less." Is there any way to make it longer? I would like to convert longer audio files to text like 2 or 3 minutes. Has anyone tried it yet?

@titanioverde

Just tested from Python, with @ewoudenberg wonderfully simple script.
Still working. Perfectly when audio is clear enough.

I guess @kumachan2013 already found a solution. The first workaround I can think about is to split the audio on every pause (commas and periods). If there's an easy way to detect a pause in your audio, that is. ^_^u

@zjh1943

i have tried the command below:

curl -v -X POST \
--data-binary @speech.flac \
--user-agent 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7' \
--header 'Content-Type: audio/x-flac; rate=8000;' \
'https://www.google.com/speech-api/v1/recognize?client=chromium&lang=zh-CN&maxresults=10' 

It never works but return as below:

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 500 (Server Error)!!1</title>
  <style>
   ...
  </style>
  <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
  <p><b>500.</b> <ins>That’s an error.</ins>
  <p>The server encountered an error and could not complete your request.<p>If the problem persists, please <A HREF="http://www.google.com/support/">report</A> your problem and mention this error message and the query that caused it.  <ins>That’s all we know.</ins>

does anyone have the same problem?

@imskull

@zjh1943, I had encountered the same error when I set incorrect sample rate, I found this notice:
** Your sample rate must match your file- if it doesn’t, you’ll either get nothing returned, or you’ll get a really bad transcription. **
at http://mikepultz.com/2013/07/google-speech-api-full-duplex-php-version/, so check your sample rate.

@gillesdemey

Google has a second version of their API available, I've posted some research and examples in a seperate repository. https://github.com/gillesdemey/google-speech-v2

@shobhitsingh

I have tried both first and second versions of the API. They are too slow. Don't know how the speech demo is so fast (https://www.google.com/intl/en/chrome/demos/speech.html).
Even spoofing doesn't seems to work.
Has anyone got it work close to the Chrome demo.

@rowntreerob

im showing a rate limit of 50 per day @ https://console.developers.google.com/ for the speech-api/v2 mention by @gillesdemey . There does not appear to be any way to up the increase the limit or to pay for an increased limit.

any advice? I have an app ready for beta making extensive use of V2 speech-api.

@achilez

another award winning piece from Google

@fritz-fritz

any way to have google detect the language instead of specifying?

@zakimak9

The version 1 does not seem to work and returns HTTP 500 error code. The version 2 executes but sends back an empty response. Like this:
{ "result": [ ] }

@ORlON

It used to work for a longer audio file but now it only works for a smaller then 100kb file or 15 seconds.
Does someone has a way to sent longer files like 30 seconds ?
For now the only way I'm thinking of is to split the audio file but there must be another way because in the chrome browser it works for a longer audio.

@loureiromahia

Alotaiba:
I don't know what is happening, but since yesterday API is not working for me: I sent this request:

wget -q0 --post-file "$1" --header 'Content-type: audio/x-flac; rate=8000' "$URL" > result.json

where URL="https://www.google.com/speech-api/v1/recognize?lang=es_ES.utf8&client=chromium" (for spanish)
or URL="https://www.google.com/speech-api/v1/recognize?lang=en_US.utf8&client=chromium" (for english)

" $1" is a speech file in .flac format.

SINCE YESTERDAY I ALWAYS GET "400 BAD REQUEST".

WHAT HAS BEEN CHANGED???
PLEASE ANSWER ME; IT IS URGENT, as i am using this API for a project in my University and within this month i have to deliver it...

THANKS IN ADVANCE
M.Loureiro

@code5381

loureiromahia, You have to change the URL.

  1. host : https://www.google.com/speech-api/v2/recognize

  2. encoding
    FLAC
    Flac file; 44100Hz 32bit float, exported with Audacity. Check the audio folder in this repository for some hilarious examples.
    Channels : 2
    Sample Rate : 44100
    Precision : 32-bit
    Sample Encoding: 32-bit Float

16-bit PCM
The following audio options are confirmed working for 16-bit PCM sample encoding:
Channels : 1
Sample Rate : 16000
Precision : 16-bit
Sample Encoding: 16-bit Signed Integer PCM

  1. key AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4

For example(AS3)

.
.
.

var PATH:String = "https://www.google.com/speech-api/v2/recognize?output=json&lang=en_US&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4";
var urlRequest:URLRequest = new URLRequest(PATH);
var urlLoader:URLLoader = new URLLoader();
urlRequest.contentType = "audio/x-flac; rate=44100";
//urlRequest.contentType = "audio/l16; rate=16000";
urlRequest.data = flacData;
urlRequest.method = URLRequestMethod.POST;
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.addEventListener(ErrorEvent.ERROR, urlLoader_error);
urlLoader.load(urlRequest);
.
.
.

http://storyjava.tistory.com/ (Korean)
https://github.com/gillesdemey/google-speech-v2

Good luck!

@loureiromahia

Thanks a lot for your quick answer. I will try with the v2 API, following your recommendations. I will let you know.

Regards:
M.Loureiro

@loureiromahia

Anyhow, can you confirm that this is going to work??:

wget -q0 --post-file "$1" --header 'Content-type: audio/x-flac; rate=16000' "$URL" > result.json

where URL="https://www.google.com/speech-api/v1/recognize?lang=es_ES.utf8&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4" (for spanish)
or URL="https://www.google.com/speech-api/v1/recognize?lang=en_US.utf8&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4" (for english)

" $1" is a speech file in .flac format.
Or do i have to do something different?...

Thanks:
M.LOUREIRO

@code5381
  1. Check the URL again.

    not v1 but v2.

    my code in JSP is...

StringBuilder sb = new StringBuilder("https://www.google.com/speech-api/v2/recognize?");
sb.append("output=json");
sb.append("&lang=en-US");
sb.append("&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4");

  1. Check the rate.

    You have to set the rate to be equal to the rate of the FLAC file's rate.

    "Content-Type", "audio/x-flac; rate={ *** FLAC file's rate *** }"

It should be work if you keep the rules. :)

@loureiromahia

Hello, code5381:

IT IS WORKING:

{"result":[]}
{"result":[{"alternative":[{"transcript":"Hola OpenDomo","confidence":0.95670336
},{"transcript":"holaaa OpenDomo"},{"transcript":"Olga OpenDomo"},{"transcript":
"Hola a OpenDomo"},{"transcript":"hola a OpenDomo"}],"final":true}],"result_inde
x":0}

But it creates to me a file like this:
recognize?output=json&lang=es_ES&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4

For me, it is more or less OK, but there are some points that may help me:

1.Now it returns to me several alternatives...In my case,the first one is the valid one... Is it posible to change the bahavior to return only 1st alternative as before??

  1. The answer time seems to be much longer than before ...may be that this is due to this deeper analisys..isn't it? ...Is it posible to improve the speed? Take into account that i am working in areal time product which it is always listening, and when detects voice , makes simething.... I am working in OpenDomo_VR project (you can get some info in github looking for OpenDomo_VR)....

  2. Is it posible to specify an output file instead of that starnge file name: recognize?output=json&lang=es_ES&key=AIza****???

Thanks in advance:
M.Loureiro

@davejavu1969

Hi, I am having same issue as previous poster. My code was running perfectly, until last night.

I have now updated the URL to v2 as per previous suggestion but am still getting same response. The connection attempts, is apparently connected but then retrys and I get no response back....

size=321kB time=5.02 bitrate= 524.6kbits/s

video:0kB audio:313kB global headers:0kB muxing overhead 2.573212%
Processing...
--2014-05-07 10:12:13-- https://www.google.com/speech-api/v2/recognize?lang=en-us&client=chromium
Resolving www.google.com (www.google.com)... 173.194.41.178, 173.194.41.180, 173.194.41.179, ...
Connecting to www.google.com (www.google.com)|173.194.41.178|:443... connected.
Retrying.

--2014-05-07 10:12:20-- (try: 2) https://www.google.com/speech-api/v2/recognize?lang=en-us&client=chromium

..then I get nothing further. Have tried http / https for both v1 and v2 versions of URL. Have also tried on 2 diffrent machines from 2 different locations so do not believe I have hit the daily request limit.

All input greatfully received!

D.

@loureiromahia

Hello:

I am "the previous poster", maybe i can help you, as i see that you send the request with the "old fashion"...You use client=chromium...You should use the key...this was the solution in my case....

So, i think you should replace:
https://www.google.com/speech-api/v2/recognize?lang=en-us&client=chromium
by:
https://www.google.com/speech-api/v2/recognize?lang=en-us&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4

In my case, it is working in this way....

Hope it helps...
M:Loureiro

@quyetnv8893

Hi loureiromahia,
I already do like your comment
"Content-Type", "audio/x-flac; rate=44100"
and the url also is
https://www.google.com/speech-api/v2/recognize?lang=en-us&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4
I just use audio flac file good-morning-google.flac form github https://github.com/gillesdemey/google-speech-v2
But it return me :
{"result":[]}
Can you tell me why?
Thanks a lot!

@davejavu1969

Hi there,

Thanks LOADS for your reply! - I am seriously struggling here atm. That has certainly made some progress, but I think maybe the code I am using cannot understand the response from v2 of the api.

My original POST was....

wget -q -U "Mozilla/5.0" --post-file file.flac --header "Content-Type: audio/x-flac; rate=44100" -O - "http://www.google.com/speech-api/v1/recognize?lang=en-us&client=chromium" | cut -d\" -f12 >stt.txt

Which worked every time with a word perfect response saved to stt.txt - until last night. Grrr.

Have updated this to....

wget -q -U "Mozilla/5.0" --post-file file.flac --header "Content-Type: audio/x-flac; rate=44100" -O - "http://www.google.com/speech-api/v1/recognize?lang=en-us&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4" | cut -d\" -f12 >stt.txt

I can now connect to server, and get a reponse - but very very slowly as you also said.

I am writing in Python & the original v1 api call returned a perfect response everytime and was saved in stt.txt which I am then passing to another part of the app.

I now get a 200 OK response from google and I am receiving an approx 100k file back for a 5 second flac file sent to the v2 api and this is written to SDTOUT - but nothing happens next, and nothing is written to stt.txt.

Was the original response a from v1 in json format?

I had one response where I said 'testing' repeatedly for five seconds, and stt.txt contained just the word 'final' ?! all other tests have produced nothing written to the text file.

All help gratefully received! - I has a fully working app ready for a project demo next week, until last night, so this is extremely frustrating to say the least.

Many thanks,

D.

@davejavu1969

One other thing to note - does the key we are now all using belong to poster code5381 ? - If so how do you get your own key & is there still the 50 per day request limit. If so, we are going to hit this rather quickly if we are all trying v2 with this key.

@davejavu1969

Ignore last question - just found the answer.

@quyetnv8893

Hi all,
Could you please tell me, param "key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4"
It is a key for us use it forever or not. If not, i want by it from google, what sould i do?

Thanks all!

@davejavu1969

Full info including details of key here....

https://github.com/gillesdemey/google-speech-v2

@quyetnv8893

@davejavu1969
Key was extracted from the Google Hotword chrome extension and is not optional
How I can extract new key?

@loureiromahia

About the key i dn't know....they gave me in some of the previous posts....

quyetnv8893: I am not an expert on this API...i just started today asking because i was using API V1, and stopped working since yesterday... After several tests it is working for me, taking into account that i have to do several adaptations and at the end, let's say it is working (i said that because i still have to test it in detail....)...
So, unfortunately i cannot help you more: i don't know why it answers you with a blank result...
I just follwed the recomendations provided by code5381...you can see them 5 or 6 posts before....

dejavu1969....You say that you know the answer about thekey policy....Can you tell us...may be we are all using the same key and we shouldn't....

@quyetnv8893

loureiromhia: I already have a not blank result :D
But, is that key which we are using just only 50 request/day?

@davejavu1969

I do not know about the key - I only read what was on the link I sent sorry.

"key: AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4

(Key was extracted from the Google Hotword chrome extension and is not optional)."

If someone can shed further light on this - it would obviously be appreciated by a lot of people!

@sambalec

I have the same problem ...

@dicilorenzo

Using key and the v2 URL voice recognition will work again, there is some changes like the returned json and some feature like bad words filter.

I read on the Chromium developer's community that V1 API could be dismissed server side due a security bug.
https://code.google.com/p/chromium/issues/detail?id=360448

I hope that google is going to release a public version of api and maybe a commercial one for intensive use.

@davejavu1969

Yup - 'there is some changes like the returned json' - and that is a fairly critical change IMHO!

Am currently trying to restructure my code to accept new json layout.

Wish they'd left it alone tbh.

@neo3

I also used the API v1 and this afternoon I noticed the problem, I made the changes to the API v2 and be functioning normally, but for how long this key will work.

I tried to see the price to increase the quota API v2, but do not have permission to view the form of the request.

@gillesdemey

Author of the v2 API info here.

As far as I know, the Google Hotword extension uses the same key for all the users that installed it.

A lot of users have been using the key for a while now, and none have reported that the key has hit it's limit yet.

It's not my personal key, it's one that was generated for the Hotword extension specifically.

I'll be posting updates as I discover them on my repository, and don't be afraid to submit additional info or issues.

https://github.com/gillesdemey/google-speech-v2

@neo3

One thing I do not understand, if the Google provides free Hotword extension, what is the problem that makes it not want to use the API v1 free? In the program I did for example, I incentive the users to use Google Calendar and create documents in Google Drive.

And if the Google wants that we pay for use of the API, then it that provides a system of purchasing of the key available to everyone.

@Haodan

I have a question, I was previously using speex codec with linear PCM on iOS using V1 google speech, now since it changes, my previous content-type: x-speex-with-header-byte(following this one: https://github.com/todoroo/iPhone-Speech-To-Text ) does not work any more, I changed the construction of the data not to be compressed, and change the content-type to l16, still it does not work, can anyone point out the audio file you sent is compressed or just flac or pcm without compression ?

@sqlogist

I am a newbie to the Google speech to text APIs.

In these several days, I had been using the V1 version of these APIs.
As same as some people mentioned above, about 16 hours before, I also met the situation that the V1 became unavailable to use.
But at 8 hours before I tried the V2 then I could get the response.
Strangely enough, I also tried the V1 again, then I found it came back to available to use.
I cannot understand what had been happening to these APIs and what had changed around my situation.
Is it a tentative troubled inside the Google or there is the understandable reason?

Can I continue to use V1?

@dicilorenzo

Sqlogist V1 Api are not working any more, probably was switched off for security reason.

I'm using V2 Api and I didn't notice any usage limitation.

Some of you experienced any kind of limitation? In this case which error is returned?

@sqlogist

Hi dicilorenzo.

I just tried to use V1 API and it actually works.
I used the code below on 2014-05-08 07:00:00AM(EST).

wget -q -U "Mozilla/5.0" --post-file myFlacFile.flac --header "Content-Type: audio/x-flac; rate=8000" -O - "http://www.google.com/speech-api/v1/recognize?lang=ja&client=chromium" | cut -d\" -f12 > stt.txt

This code returns fine result.
So, I think the Google speech to text API V1 is still available.

Certainly, the Google Translate API V1(this is not a speech recognize API) seems be no longer available few years ago.

https://developers.google.com/translate/v2/faq

I wonder that it makes to confuse the understanding about the STT API V1.

If anyone knows the details, please let me know.

@neo3

True, API v1 is working again.

@dicilorenzo

Hi sqlogist,

I tried V1 api the result it:
"Your client has issued a malformed or illegal request. Missing parameter: key That’s all we know."

V2 is working without limitations

@neo3

Today the API v1 was working, but blocked again.

@sqlogist

Hi dicilorenzo, neo3 and all.

It seems that now the API V1 become unavailable again.
I found other developers also suffered to this same situation about the API v1 in these few days.

http://stackoverflow.com/questions/23511015/google-speech-api-v1-not-working/23538656#23538656

I guess the availability of the API V1 is often changing hour by hour.
To avoid this unstable behavior we might have to use API V2.

Even though the usage is permitted for only development or hobby, the courtesy limit of the API V2, 50 request per day, is too severe.

We need a work around solution to this problem.

@amd5200

I'm so sad, my robot used api v1, now it can't chat with me by Chinese :(

@sqlogist

Hi dicilorenzo, neo3, amd5200 and everyone,

I confirmed an interesting test.
I have called the API V2 again and again....

Meanwhile, this testing, I had been checking the value of QUOTA on Google Developer Console.
When I called the API V2 50 times, the value became "100%".

But the 51th test was succeeded with the correct result!
And I continue the testing until it reaches to the 60th test, but every test was succeeded.

Before this experience, I expect it to become unavailable to use API V2 when the value of QUATA reached to 100%.
But It is not true.

I guess we can use the API V2 over 50 times a day.
(It reminds me that dicilorenzo said "V2 is working without limitations" two days ago.)

I hope, it's become an important knowledge to everyone who want to use API V2.

@lux89

Hi guys, in need of serious help here.

//code provided by korylprince
import sys
import urllib2
import json

f = open("test.flac")
data = f.read()
f.close()

req = urllib2.Request('https://www.google.com/speech-api/v2/recognize?output=json&lang=en&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4', data=data, headers={'Content-type': 'audio/x-flac; rate=16000'})

try:
ret = urllib2.urlopen(req)
except urllib2.URLError:
print "Error Transcribing Voicemail"

resp = ret.read()
text = json.loads(resp)['hypotheses'][0]['utterance']
print text

This is the code i use in python for speech to text. Unfortunately, i am not able to retrieve the transcription. Perhaps, some one is able to help. thanks alot

@Eybon

Hey guy
Last week, i used google speech api v1 in a script bash and everything worked ....
Now, tons of problems !!
I saw all your post from 4 days, So I go to v2, create an account for use google key like that (http://www.chromium.org/developers/how-tos/api-keys) but i have a probleme, the result is always :

{"result":[]}

I dont understand why .... i just changed url in my script.
here is the code :

arecord -q -f cd -t wav -d 2 -r 44100 | flac --totally-silent - -f --best --sample-rate 16000 -o out.flac;
wget -U "Mozilla/5.0" --post-file out.flac --header="Content-Type: audio/x-flac; rate=16000" -O - "http://www.google.com/speech-api/v2/recognize?lang=fr&key=mygooglekey" > speech.txt

I saw the same problem for your post but dont saw answer.
I hope you can help me because .... Dont understand god ! ;-)

PS : im french, sorry for my bad english, but if no one understand difference between "lang=fr","lang=fr-FR","lang=fr-fr" because i saw 3 on net ?

Thx

@kaarskop

Was having the same issue untill I realised V2 gives back 2 lines instead of 1:
{"result":[]}
{"result":[{"alternative":[{"transcript":"van","confidence":0.88741958},{"transcript":"van jou"},{"transcript":"van je"},{"transcript":"vaughan"},{"transcript":"van dam"}],"final":true}],"result_index":0}

@2128

yes! 2 lines result. what's google doing recently?

@satana

I'am trying to make it work with c#. And i don't know how to read the second line? Can someone help me with this?

HttpWebRequest request =(HttpWebRequest) HttpWebRequest.Create(PATH);
request.Method = "POST";
byte[] byteArray = File.ReadAllBytes(flacName);
sampleRate = 44100;
request.ContentType = "audio/x-flac; rate=" + sampleRate.ToString();
request.ContentLength = byteArray.Length;
Stream sendStream = request.GetRequestStream();
sendStream.Write(byteArray,0,byteArray.Length);
sendStream.Close();
string responseFromServer;
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
responseFromServer = reader.ReadToEnd();
reader.Close();
response.Close();

@bryanrtboy

If you are only getting {"result":[]} you need to properly use ReadToEnd - http://stackoverflow.com/questions/7085785/readtoend-works-readline-doesnt

Now, how do I get the second array named "result" from the json?

@satana

I use ReadToEnd, but it doesn't work. I'm getting only first line.

HttpWebResponse response = (HttpWebResponse) request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
responseFromServer = reader.ReadToEnd();

bryanrtboy, i can send you my jSon deserializer, if you need. It's on c#

@niluwin

u can use a very basic method like below:
lang_code='en-US'
googl_speech_url = 'https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4'
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'}
req = urllib2.Request(googl_speech_url, data=flac_cont, headers=hrs)
p = urllib2.urlopen(req)
print p.read
data= (p.read()).split('transcript\":\"')[1]
print data.split('"')[0]

@CHERTS

Today there is a problem when trying to recognize issued

Your client does not have permission to get URL /speech-api/v2/recognize?xjerr=1&client=chromium&output=json&lang=ru-RU&key=AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4 from this server. ThatvЂ ™ s all we know.

Only yesterday everything worked.

@davejavu1969

Hi there,

I have been following this thread for the last week or so since the initial change to V2 api. I too had a talking robot that worked perfectly until the upgrade! I made the changes to v2 and added a small python script to parse the new format results.

BUT - this AM (UK time) I am now getting 403 Forbidden when using v2 of the api - is anyone else experiencing this?

In regards to earlier posts - -

@brianrtboy and others, I have a small (very crappy!) script I wrote in Python that takes the mulitple line response from google and extracts the single answer, and writes that over the original .txt file - I can post it here if anyone needs it.

re the key - this is a 'Global' key that GDM extracted - it does not 'belong' to anyone - I have a horrible feeling that this key may have now been blocked, hence the 'forbidden' - anyone else got any thoughts?

I have a demo tomorrow and this is rather critical to it!

@CHERTS

Key AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4 is locked. :(

@davejavu1969

Are you getting '403 Forbidden' ?

@CHERTS

Yes

@davejavu1969

Crap :-( - Me too.

I suspect google have blocked that key then. I have found one of the Google dev guys on Groups and have pinged him a msg, but they are all west coast US and it's 3am there.

@davejavu1969

I have also just used my google account to generate a generic google API server side key for all Google APIs - although Speech API does not appear in Google API list, or developer console anywhere.

Therefore, not surprised to report that this new key also generates the same 403 Forbidden response.

Any one at Google reading this??!

@ecmnet

You have to sign up with the API projects group. However, you then have only about 50+ shots. Afterwards your key will be blocked too :(

@CHERTS

The new version of Google Voice Search Hotword (Beta) 0.1.1.5018_0 using the old API key

audio-input.js
var b = new T("https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&app=web-hotword");
chrome && chrome.app && chrome.app.getDetails && chrome.app.getDetails() && X(b, "app", "web-hotword-" + chrome.app.getDetails().version);
X(b, "client", "chrome-hotword");
X(b, "key", "AIzaSyCnl6MRydhw_5fLXIdASxkLJzcJh5iX0M4");
var c = {};
c["Content-Type"] = "audio/l16; rate=" + a.Ga;

So put an additional check, which I still can not understand.

@kaarskop

For now Google doesn't enforce the 50/day limit yet. I am using it for a few days now with well over 50/day requests. I get the feeling they are getting ready to ask money though...

@ecmnet

mine was blocked after some 100 request...

@kaarskop

I reached 318 yesterday and not blocked yet. Haven't used it today yet though.

@davejavu1969

I have signed up for the Google API projects group - do I have to request a key?

@ORlON

Use this Key : &key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw
It works for now.

@davejavu1969

Hi Guys - thanks Orion, I will try that shortly!

kaarskop - I think I am in wrong place, can you post a link plz.

Many thanks.

@SanyaGross

thanks Orion, how can I get my own key? (Without copy-pasting from this site)

@kaarskop

https://console.developers.google.com -> your project -> APIs & auth -> Credentials

Apparently here it's possible as well.

@SanyaGross

In PHP code I wrote:

<?..................
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=AI...............");
curl_setopt($ch, CURLOPT_POST,1);
......
$result=curl_exec($ch);
echo "result=".$result
?>

I get answer only one string:
{"result":[]}

Why?

@SanyaGross

kaarskop
I make key:

Key for server applications
API key AIzaSyC53OK59kFcMRMXtxxNujBUHU130A6XvZo
IPs 188...*1
Activation date May 7, 2014 11:52 PM

but my key doesn't work

@davejavu1969

@ Kaarskop...

Yup - that's what I did. Generated a key but still get 403 forbidden error.

@ecmnet

You have to join the API group. Then Speech API will appear on the list of APIs. Switch it on and use your key. But still, it will shut down after more than 50+ requests.

@davejavu1969

Could you post a link to join API Group please? - this is what I can't find.

@SanyaGross

@ kaarskop

Yes, I did it.
You have to join the API group. Then Speech API will appear on the list of APIs. Switch it on and use your key.

@ecmnet

i figured out, that the limit is at 500 request/day.

@ORlON

The only way to get your own key is by using this guide here: http://www.chromium.org/developers/how-tos/api-keys
read it from 7-11.
But in order to make it available you must subscribe the chromium-dev group here : https://groups.google.com/a/chromium.org/forum/?fromgroups#!forum/chromium-dev .
Right now I'm looking for another way hope I'll find something because it's really annoying.

@davejavu1969

Many thanks - much appreciated folks. :-)

@ORlON

Well it's not really a solution because after 500 request you will need to activate another key.
I hope they will release a dev sdk for that or something.

@bryanrtboy

@satana - you need to use 'using' or it will only read the first line. My C# snippet is this:

            using (HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse ())
                            if (HWR_Response.StatusCode == HttpStatusCode.OK) {
                                    string line;
                                    using (SR_Response = new StreamReader (HWR_Response.GetResponseStream ())) {
                                            line = SR_Response.ReadToEnd ();
                                    }

                                    SR_Response.Close ();
@neo3

The key "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw" was taken from where? It has usage limit?

@SanyaGross

@ ORION :
here: http://www.chromium.org/developers/how-tos/api-keys
read it from 7-11.
But I can not understand difference between 8.1 and 8.2. Is this importantly?

@ORlON

SanyaGross - it depends what your application is based on.
Mine is a desktop application so I chose Installed Application > Other.
Then you should make a Browser key because the speech is a web api.
Hope I helped.

@immran

H all,

I have changed my api v1 to v2 now and I'm using the new key AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw but I got the response as empty like {"result":[]}

Any suggestions?

Thanks

@Homez386

ORlON, I can't manage this way, always get 403 with a key generated like this. Maybe I miss something?

@ORlON

You must subscribe to the chromium-dev group and ask them to activate your key.

@SanyaGross

immran

I got answer {"result":[]} from 3G-modem. But from optic-channel - all right.

@satana

@bryanrtboy it's strange, but i still getting only this {"result":[]}

@channelz

What kind of response times are you guys seeing for V2? I used to be under 2 seconds all the time on V1. Now the response times range from 2-9 seconds past couple days (using the same test file). (Mean 3.7) https overhead slowing it down some? Thoughts/suggestions to speed it up?

{"result":[]}
{"result":[{"alternative":[{"transcript":"the wheels on the bus go round and round"}],"final":true}],"result_index":0}

real 0m3.548s
user 0m0.160s
sys 0m0.030s

@sambalec

@Orion Hi, many thanks for the new key AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw
How did you extract this key ? Can you explain me :) ? Thanks.

@littlekill

@channelz Hi, how did you get two lines? I only get one line like {"result":[]}. This is my command in linux.

wget -q -U "Mozilla/5.0" --post-file daveconroy.flac --header "Content-Type: audio/x-flac; rate=44100" -O - "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=mykey" > result.txt

Can you help me? Thank you.

@ORlON

Hey @sambalec in order to make your own you should open a new project here: https://console.developers.google.com.
Then use this guide from 7-11 : http://www.chromium.org/developers/how-tos/api-keys.
But you must activate your key by emailing the chromium-dev group or something but I'm not sure they will because the speech api is made for chrome and google doesn't want people to use it.
So I think it doesn't matter because very soon we wont be able to use the speech api unless google will release a SDK for it.
As you can see the securty from V1 to V2 has changed a lot and in V3 I believe we wont be able to use it anymore.
right now I'm trying to find something, hope I will.

@davejavu1969

Hi, all - been here for a while working through this. So I have joined the chromium group, got my key. The speech API has been added to my list within the console, it was not there before so all appears ok. I have generated a new key and added IP addresses but I am still getting 403 Forbidden. Anyone else getting the same?

@mcella

@davejavu1969 same here :-(

@fatdh

@davejavu1969, @mcella
I got the same error, until changed the key for this one AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw
from @orion

@davejavu1969

Yup - we know that key works, but for how long?! Hence we are trying to establish our own keys.

@danielmahon

I was able to create my own key and use it, but im still getting a {"result":[]} response...

@Homez386

ORION, thanks for your reply:

You must subscribe to the chromium-dev group and ask them to activate your key.

I've already subscribed, but do you mean that I must ask for activation thru a personal message in this group (not automatically)?

@ORlON

Hey everyone!
Here is my guide for you to make keys for yourselves:
Go to this link : https://cloud.google.com/console and create your own project.
Join this group here : https://groups.google.com/a/chromium.org/forum/?fromgroups#!forum/chromium-dev.
In your project go to APIs & auth > APIs , and activate Speech API (only 50 requests for each key).
Go to Credentials and make your client.
Generate a Browser key.
And your done.
Hope I helped ;)

@Homez386

ORION, thanks! Strange, but yesterday this didn't work, today works with the key generated 15th May. Maybe the browser key doesn't get activated immediately?

@greenlakegithub

Hey guys, I'm using this v2 solution, but I feel it can be blocked any time. So is there any other speech to text solution we can try? I know the nuance has a mobile sdk, but the price is way to high. Welcome discussion about other solutions.

@NSThread

Can anyone tell me what's the reason of getting "{"result":[]}" as a response? I checked, there is no other line in response.

@akifnaeem21

can any1 please share solution for v2 and json format...coz many of us have very urgent situations.
i am using google speech to text api in my final year project of BS.
and 31may is last date of project submission. every thing was working very fine till 7may.
but after dat google block v1.

code till 7may.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
echo "Recording your Speech (Ctrl+C to Transcribe)"

arecord -D plughw:0,0 -q -f cd -t wav -d 0 -r 16000 | flac - -f --best --sample-rate 16000 -s -o SpeechtoText.flac; 

echo "Converting Speech to Text..."

wget -q -U "Mozilla/5.0" --post-file SpeechtoText.flac --header "Content-Type: audio/x-flac; rate=16000" -O - "http://www.google.com/speech-api/v1/recognize?lang=en-us&client=chromium" | cut -d\" -f12 > SpeechtoText.txt

echo "You Said:"

value=cat SpeechtoText.txt

echo "$value"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

kindly share solutions how us v2 and how to get out from json to text.

waiting ur response eagerly ... :(

@NSThread

@akifnaeem21,
Check https://github.com/todoroo/iPhone-Speech-To-Text, use "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw" as key.
I'm having problem with empty json ({"result":[]}) output. let me know if you succeed.

@akifnaeem21

@NSThread

having same empty response from google.

@howerson

I have same problem, I have change to following code, but I failed;

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"good-morning-google" ofType:@"flac"];

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

    request.defaultResponseEncoding = NSUTF8StringEncoding;

    [request setRequestMethod:@"POST"];

    [request addFile:filePath forKey:@"file"];

    [request addRequestHeader:@"Content-Type" value:@"audio/x-flac; rate=44100"];

    [request setTimeOutSeconds:6];

    [request startSynchronous];

    NSData *responseData = [request responseData];
@Kaspler

@akifnaeem21

I'm in same situation as you - need to finish my BS project till the end of May.
I also was working fine with V1 till now... Anyway, now, thanks to this page and all the comments here I managed to make it work with V2.

Here is my C# code (it's just a prototype where I tested, so it's simple):


using System;
using System.IO;
using System.IO.Compression;
using System.Net;


class Program
{
    static void Main(string[] args)
    {
        using (var fileStream = new FileStream(@"C:\LetsArrangeMeeting.flac", FileMode.Open))
        {
            const string requestUrl = "https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=KEY_GOES_HERE&client=chromium&maxresults=6&pfilter=2";
            var request = (HttpWebRequest)WebRequest.Create(requestUrl);
            ConfigureRequest(request);
            var requestStream = request.GetRequestStream();

            CopyStream(fileStream, requestStream);

            using (var response = request.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    using (var zippedStream = new GZipStream(responseStream, CompressionMode.Decompress))
                    {
                        using (var sr = new StreamReader(zippedStream))
                        {
                            var res = sr.ReadToEnd();
                            Console.WriteLine(res);
                        }
                    }
                }
            }
        }

        Console.ReadLine();
    }

    private static void CopyStream(FileStream fileStream, Stream requestStream)
    {
        var buffer = new byte[32768];
        int read;
        while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)
        {
            requestStream.Write(buffer, 0, read);
        }
    }

    private static void ConfigureRequest(HttpWebRequest request)
    {
        request.KeepAlive = true;
        request.SendChunked = true;
        request.ContentType = "audio/x-flac; rate=44100";
        request.UserAgent =
            "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
        request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
        request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-GB,en-US;q=0.8,en;q=0.6");
        request.Headers.Set(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
        request.Method = "POST";
    }
}

And this is the result:


{"result":[]}
{"result":[{"alternative":[{"transcript":"hello let's arrange a meeting in Tel Aviv at 5 o'clock"},{"transcript":"hello lets arrange a meeting in Tel Aviv at 5 o'clock"},{"transcript":"hello let's arrange a meeting in Tel Aviv at 5 o'clock Buy"},{"transcript":"hello lips arrange a meeting in Tel Aviv at 5 o'clock"},{"transcript":"hello let's arrange a meeting in Tel Aviv at 5 o'clock by"},{"transcript":"hello let's arrange a meeting in Tel Aviv at 5 o'clock buy"}],"final":true}],"result_index":0}

Hope this helps!

@ecmnet

V2 seems to be down. None of my API keys work. Same experience?

@kaarskop

Everything seems normal for me.

@markusschweitzer

I also only got an empty response. Does anyone know how to fix this?

@mudler

empty response too here, i'm digging on the chromium code, but no luck for now.
Edit: seems pfilter is doing the trick, gillesdemey/google-speech-v2#6 (comment)

@Inoho

@NSThread how did you get the key? Is there any constraints of limited number of use of the key? I have used the following link to get a key "https://console.developers.google.com" but it is not working.

@hauhhvn

I want to use more than 50 requests / day. How to do it?

@Homez386

Same as hauhhvn! Why doesn't Google have commercial offerings for this API (at least, I haven't seen them)? When I try to extend quota in my developer's cabinet, some private Google doc is about to be shown, two times I requested permission to read it, but no further reaction!

@pannous

Error 403 (Forbidden)!!1
original google response "!!1" lol

@fatdh

Working java code:

//libs to import
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * Send post to google
 */
private void sendPost() throws Exception {
    String USER_AGENT = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2",
    url = "https://www.google.com/speech-api/v2/recognize?output=json&lang=ru-RU&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&client=chromium&maxresults=6&pfilter=2";

    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    // add reuqest header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Content-Type", "audio/l16; rate=16000");
    con.setRequestProperty("AcceptEncoding", "gzip,deflate,sdch");

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.write(Files.readAllBytes(Paths
            .get("C:\\tmp\\test_sounds\\1_16000.wav")));
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(
            con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    // print result
    System.out.println(response.toString());

}
@yochze

Here's my working Ruby code.

        url = 'https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw&results=6&pfilter=2'
        uri = URI.parse(url)
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE    
        request = Net::HTTP::Post.new(uri.request_uri)
        request.body = File.read("your_flac_file.flac}")
        request.content_type = 'audio/x-flac; rate=16000;'
        res = http.request(request)

BTW - Does anybody know what's the time limit for audio file?

@bezerko

I thought I would share this for those who just get back:
{"result":[]}

I was setting the "rate" in the Content-Type HTTP header to 16000 that had always worked in V1. Changing this to 44100 fixed the issue:

"Content-type": "audio/x-flac; rate=44100"

@LRNAB

Does anyone know how to get the key?

@voicer

@LRNAB
The key for google api you can get in the google APIs console for free.
But the question is how to increase the max limit of 50 requests 'a day?
I found no way to get more, and the link to order quote is dead.

The Google corp. forces me and other developers to find any other way to solve this problem.
I see the solution in java script for Chrome browser. But if anybody need this API i.e. for C# then he must to switch the source of sound from microphone to the file stream in flac format.

My question is now - how to change the source of sound from microphone into binary file on disk?

@sakhnevych

@voicer
Could you tell if there is a way to use java script for Chrome browser from terminal? I realize it is a little bit strange question, but I can't find a way to use unlimited access to google speech API from linux using annyang.

@smithas

I went through the docs for creating key but it gives me forbidden error 403. I had tried a key few weeks ago shared by someone here which had worked but now that also fails. What step am I missing that I get 403 error? Is there any other way. Please help.

@ijazsarwar

Anyone got it working in Java with V2?? I am getting 403 error.
Also looking at the comments looks like it only converts 10 to 15 secs of audio ... Anyway we can extend that time limit to say one minute or even longer??

@amd5200

I changed to use pocketsphinx, it can be works offline and supports Chinese language.

@marfnk

I also have issues with the authentication for Google Speech2Text API on Android.
It works with my API key in WiFi if I tell Google my current IP. But the Android Key isn't working. I provided the correct debug-keystore SHA1 but am receiving: The client does not have permission to get url ... from this server. I use the Apache HttpClient within my Android application. Any ideas?

@ijazsarwar This Google API is not intended to continuously recognize speech. Its for short commands. Thats why there is this few seconds limit. Take a look at my master thesis project where I try to send 15sec chunks to Google to get something continuous-like.

@amd5200 I'd love to talk to you about PocketSphinx on Andoird for generic vocabulary searches since I already tried unsuccessfully to get it to work as described here - you'll find my email in my github profile.

@amd5200

@marfnk I just used it on my single board, like raspberry pi, and for Chinese. ( https://www.youtube.com/watch?v=EucxVToC58E&list=UUgBhkLQyk0LwxpKAmIZQNgA&hd=1 )
I never tried PocketSphinx on Andoird, so i got no idea for this.

@shinichi0802

IS this possible to use ajax to post in to Google Speech API ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.