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;
?>