2

I've successfully sent an RAW audio file to Google's Cloud Speech API using my API key. Now, I would like to record my voice using Chrome's browser and send it, instead of "audio.raw" (just like on Google.com). I'm guessing that it should be an HTML5 script.

Here's what I've done so far:

<?php
$data = json_encode(array(
    'config' => array(
        'encoding' => 'LINEAR16',
        'sample_rate' => 16000,
        'language_code' => 'en-US'
    ),
    'audio' => array(
        'content' => base64_encode(file_get_contents(dirname(__FILE__) . '/audio.raw'))
    )
));

$ch = curl_init('https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=XXX');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));

$result = json_decode(curl_exec($ch));

$text = (isset($result->results[0]->alternatives[0]->transcript) ? $result->results[0]->alternatives[0]->transcript : '');

echo $text;
?>
  • bit of a duplicate here http://stackoverflow.com/questions/12149451/how-can-i-record-a-users-voice-using-javascript-php – Richard Housham Oct 17 '16 at 18:46
  • example here https://webaudiodemos.appspot.com/AudioRecorder/index.html# – Richard Housham Oct 17 '16 at 18:48
  • use javascript and any webRtc type dictation demo as a JS Client to the recognizer api. In JS the 'userMedia ' API can get mic input and create output audio.blob object on chrome... add some http lib so you create proper http protocol with headers and api key. The response will be speech to text from the api. – Robert Rowntree Oct 18 '16 at 11:32

0 Answers0