Столкнулся с тем, что сервис гугла по распознаванию речи загнулся, а точнее переделался в облачный и, похоже, стлал платным.
Поэтому решил воспользоваться сервисом от Яндекса.
Распознование и синтез речи (Speech Api)
30.10.2016
Еще по теме: raspberry pi распознавание речи
Сначала нужно получить ключ для API.
Теперь можно использовать один ключ для целого ряда сервизов
Первый пример: Генерация/синтез речи
// https://tech.yandex.ru/speechkit/cloud/doc/dg/concepts/speechkit-dg-tts-docpage/
$text = 'Какой-то текст';
$qs = http_build_query(array(
'format' => 'mp3',
'lang' => 'ru-RU',
'speaker' => 'jane',
'key' => 'cefd81f7-b266-XX-b38b-XXX',
'emotion' => 'good',
'text' => $text
));
$ctx = stream_context_create(array(
'http'=>array('method'=>'GET','header'=>'Referer: \r\n')
));
$soundfile = file_get_contents('https://tts.voicetech.yandex.net/generate?'.$qs, false, $ctx); // запрос на генерацию mp3 файла
file_put_contents('out.mp3', $soundfile);
echo('Done');
Второй пример: Распознавание речи
<?php
// https://tech.yandex.ru/speechkit/cloud/doc/dg/concepts/speechkit-dg-recogn-quick-start-docpage/
/*
if (!function_exists('curl_file_create')) {
function curl_file_create($filename, $mimetype = '', $postname = '') {
return "@$filename;filename="
. ($postname ?: basename($filename))
. ($mimetype ? ";type=$mimetype" : '');
}
}
*/
function generateRandomSelection($min, $max, $count)
{
$result=array();
if($min>$max) return $result;
$count=min(max($count,0),$max-$min+1);
while(count($result)<$count) {
$value=rand($min,$max-count($result));
foreach($result as $used) if($used<=$value) $value++; else break;
$result[]=dechex($value);
sort($result);
}
shuffle($result);
return $result;
}
function recognize($file, $key) {
$uuid=generateRandomSelection(0,30,64);
$uuid=implode($uuid); $uuid=substr($uuid,1,32);
$curl = curl_init();
$url = 'https://asr.yandex.net/asr_xml?'.http_build_query(array(
'key'=>$key,
'uuid' => $uuid,
'topic' => 'queries',
'lang'=>'ru-RU'
));
curl_setopt($curl, CURLOPT_URL, $url);
$data=file_get_contents(realpath($file));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// https://tech.yandex.ru/speechkit/cloud/doc/dg/concepts/speechkit-dg-recogn-params-docpage/
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: audio/x-mpeg-3'));
curl_setopt($curl, CURLOPT_VERBOSE, true);
$response = curl_exec($curl);
$err = curl_errno($curl);
curl_close($curl);
if ($err)
throw new exception("curl err $err");
echo '$response: '.$response.';';
}
/*
print_r($argv);
$filename = $argv[1];
$key = $argv[2];
*/
$filename = 'out.mp3';
$key = ' 'key' => 'cefd81f7-b266-XX-b38b-XXX',
recognize($filename, $key);
echo('<br />Done');