Skip to content

Instantly share code, notes, and snippets.

@inobo55
Created October 27, 2014 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save inobo55/415a227fcb4c16965599 to your computer and use it in GitHub Desktop.
Save inobo55/415a227fcb4c16965599 to your computer and use it in GitHub Desktop.
PHPでGoogleSpeechAPIを使ってみた
<?php
/*
Google Speech APIを利用して
音声ファイルー>テキストを返すAPIを作成
参考サイト:
Twilio x Google Speech API ブログ
http://katsumi-takahashi.blogspot.jp/2013/12/twilio-twiliotranscribe-api-twilio.html
Google Speech API – Full Duplex PHP Version
http://mikepultz.com/2013/07/google-speech-api-full-duplex-php-version/
Google Speech API v2:
https://github.com/gillesdemey/google-speech-v2/
ffmpegコマンド
http://stackoverflow.com/questions/16119516/how-to-convert-flac-to-mp3-using-php
*/
header("Content-Type: application/json; charset=utf-8");
require_once 'wave_editor.php';
function voiceToText($voicePath){
if(!isset($voicePath)) return "[ERROR] Error Voice!!";
exec("sudo rm wave.flac",$out,$res);
//wave保存
$w1 = new Wavedata();
$w1->LoadFile($voicePath);
exec('ls', $ls);
$wave_name = count($ls)."_wave.wav";
$w1->SaveFile($wave_name);
//wav-flac
$flac = "wave.flac";
$command = "sudo /usr/local/bin/ffmpeg -i $wave_name $flac";
exec($command,$out,$res);
if($res) return "Error Command";
//GoogleSpeechAPI
$output = GoogleSpeechAPI($flac);
return $output;
}
function GoogleSpeechAPI($flacFile){
$base = "https://www.google.com/speech-api/v2/recognize";
$lang = "ja-jp";
$output="json";
$key = "YOUR_GOOGLE_API_BROUSE_PUBLIC_KEY"; //Google Consoleからブラウザキーを取得.
$contentType = "Content-Type: audio/x-flac; rate=8000;"; //Twilio rate is 8000.
$requestUrl = $base."?output=json&lang=ja-jp&key=$key";
$curl=curl_init($requestUrl);
curl_setopt($curl,CURLOPT_POST, TRUE);
curl_setopt($curl,CURLOPT_HTTPHEADER,array($contentType));
curl_setopt($curl,CURLOPT_POSTFIELDS,file_get_contents('wave.flac'));
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION, TRUE);
$output= curl_exec($curl);
return $output;
}
/* レスポンス例
$output =
{"result":[{"alternative":[{"transcript":"パパパパッパパパパパパ"},
{"transcript":"ぱぱぱぱっぱーぱぱぱぱ"},{"transcript":"パパパパパパパパ"},
{"transcript":"あっぱらぱー ぱぱぱぱぱぱ"},{"transcript":"パパパパパパパパパパ"}],
"final":true}],"result_index":0}
*/
/* テスト例
$url = "http://api.twilio.com/2010-04-01/Accounts/AC1a5cb11fc93ebd4c8df75d7b056e62bb/Recordings/RE3525f04a13b7630533acc1ce73660074";
$text = voiceToText($url);
echo $text;
*/
$text = "Error";
if(isset($_GET["voice"]) && isset($_GET["account_id"]) ){
$voice = $_GET["voice"];
$account = $_GET["account_id"];
$voice = "http://api.twilio.com/2010-04-01/Accounts/$account/Recordings/$voice";
$text = voiceToText($voice);
// transcript第一候補のみ格納
$text = split('"transcript":',$text);
$text = split('"',$text[1]);
$text = $text[1];
}
echo $text;
?>
<?php
/*
参考サイト:
*/
class WaveData {
var $_d = ''; // データ
var $datasize = 0; // データサイズ
var $fmtid = 0; // フォーマットID
var $chsize = 0; // チャンネル数
var $freq = 0; // サンプリング周波数
function LoadFile($fn) {
$this->_d = file_get_contents($fn);
// 識別コード RIFF が存在するかどうか調査
if (substr($this->_d, 0, 4) != 'RIFF') return false;
// chunk 識別コード WAVE が存在するかどうか調査
if (substr($this->_d, 8, 4) != 'WAVE') return false;
// chunk 識別コード fmt が存在するかどうか調査
if (substr($this->_d, 12, 4) != 'fmt ') return false;
// chunk 識別コード data が存在するかどうか調査
if (substr($this->_d, 36, 4) != 'data') return false;
// フォーマットID を取得します
// リニアPCMだけを対象にするので、それ以外はエラー
$d = unpack('v', substr($this->_d, 20, 2));
$this->fmtid = $d[1];
if ($this->fmtid != 1) return false;
// チャンネル数を取得
// モノラルチャンネルだけを対象にします
$d = unpack('v', substr($this->_d, 22, 2));
$this->chsize = $d[1];
if ($this->fmtid != 1) return false;
// サンプリング周波数を取得
// 44100hz のみを対象とします
$d = unpack('V', substr($this->_d, 24, 4));
$this->freq = $d[1];
// データサイズを取得
$d = unpack('V', substr($this->_d, 40, 4));
$this->datasize = $d[1];
}
function WaveConnect(&$p1) {
// WAVE ファイルのデータ部分だけ結合します
$this->_d = $this->_d . substr($p1->_d, 44, $p1->datasize);
// データサイズを更新します
$this->datasize = strlen($this->_d) - 44;
// 実際のデータのサイズも更新します
$d = pack('V', strlen($this->_d) - 8);
$this->_d[4] = $d[0];
$this->_d[5] = $d[1];
$this->_d[6] = $d[2];
$this->_d[7] = $d[3];
$d = pack('V', $this->datasize);
$this->_d[40] = $d[0];
$this->_d[41] = $d[1];
$this->_d[42] = $d[2];
$this->_d[43] = $d[3];
}
function SaveFile($p1) {
file_put_contents($p1, $this->_d);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment