Google Speech API – Full Duplex PHP Version

So this is a follow up to my post a while ago, talking about how to use the Google Speech Recognition API built in to Google Chrome.

Since my last post, Chrome has had some significant upgrades to this feature- specifically around the length of audio you can pass to the API. The old version would only let you pass very short clips (only a few seconds), but the new API is a full-duplex streaming API. What this means, is that it actually uses two HTTP connections- one POST request to upload the content as a “live” chunked stream, and a second GET request to access the results, which makes much more sense for longer audio samples, or for streaming audio.

I created a simple PHP class to access this API; while this likely won’t make sense for anybody that wants to do a real-time stream, it should satisfy most cases where people just want to send “longer” audio clips.

Before you can use this PHP class, you must get a developer API key from Google. The class does not include one, and I cannot give you one- they’re free, and easy to get just go to the Google APIs site, and sign up for one.

google_apis

Then download the class below, and start with a simple example:

<? 
require 'google_speech.php';

$s = new cgoogle_speech('put your API key here'); 

$output = $s->process('@test.flac', 'en-US', 8000);      

print_r($output);
?>

Audio can be passed as a filename (by prefixing the ‘@’ sign in front of the file name), or by passing in raw FLAC content. The second argument is an IETF language tag. I’ve only been able to test with both English and French, but I assume others work. It defaults to ‘en-US’. The third argument is sample rate, it defaults to 8000.

** 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. **

The output will return as an array, and should look something like this:

Array
(
    [0] => Array
        (
            [alternative] => Array
                (
                    [0] => Array
                        (
                            [transcript] => my CPU is a neural net processor a learning computer
                            [confidence] => 0.74177068
                        )
                    [1] => Array
                        (
                            [transcript] => my CPU is the neuron that process of learning
                        )
                    [2] => Array
                        (
                            [transcript] => my CPU is the neural net processor a learning
                        )
                    [3] => Array
                        (
                            [transcript] => my CPU is the neuron that process a balloon
                        )
                    [4] => Array
                        (
                            [transcript] => my CPU is the neural net processor a living
                        )
                )
            [final] => 1
        )
)

Get the PHP class here: http://mikepultz.com/uploads/google_speech.php.zip

66 thoughts on “Google Speech API – Full Duplex PHP Version

  1. Jeremy

    Something does seem to have changed. I enabled the API, created a key and ran a few queries with a file that is under 5 minutes in length. I never did get a positive response from the API, but my project is now marked abusive in the google console with over 18,000 hits. The appeal screen has no submit on it, only a cancel.

  2. vris

    Hi Jeremy,

    where did you can found the speech api to enable? I can’t found the speech api service to enabled in my account. please help

  3. asmi shah

    Hi Mike,

    I was able to use your solution but could only get the output out of the first 10 seconds only !! wherein, my audio flac file is of 31 seconds. Any idea, why this wont be working ??
    Any help is appreciated!

    best,
    asmi

  4. Keith Spiteri

    Hi,

    I am trying to use google api to translate speech to text but with a wav/mp3 file but it does not seem to work. Can you please tell me if google support this types of audio instead of flac files

    Regards,

  5. shammis

    Hi, i am unable to find speech-to-text api on google developer console, is there still a Google streaming speech recognition webservice there?

  6. shammis

    hi, so i got through the api key for speech api. now i want to upload a wav file

    can i simply change your code to .
    curl_setopt($this->m_up_handle, CURLOPT_HTTPHEADER, array(‘Transfer-Encoding: chunked’,
    ‘Content-Type: audio/l16; rate=16000’ ));

    and wav file work? does google accept a wav file. because i am getting empty array

  7. zhangsidao

    hi shammis

    i am also unable to find speech-to-text api on google developer console,could you tell me ?

  8. Ashok

    I tried to run the script as it is. But I was not getting the output at all. Probably there is something wrong with my flac file. Can you share the flac file you have tested with. It would be very much helpful. Thanks.

  9. TuanAnh

    It’s working good on my hosting. But It’s not work on my Localhost. I used Xampp. it just return “Array ( )”. I don’t what is the problem. please help me.
    Thanks

  10. sandeep

    Hi,
    I have followed all your above instructions but, still i am not getting output.

    1. Enable Speech API.
    2. Properly setup API key in application.

    How i can check that google is providing proper response?

  11. Pingback: Continuous Speech Recognition Android - Without Gaps - android

  12. Pangea

    The google API url is outdated. This demo and other 100 along with it are obsolete. Google screw all of them..

Leave a Reply

Your email address will not be published. Required fields are marked *