Last active
August 29, 2015 14:07
-
-
Save inobo55/cba0a084ef86024f0f1f to your computer and use it in GitHub Desktop.
AITalkさんの音声合成APIを利用してみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// WebAPI 仕様 | |
// https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_docs_id=71#tag01 | |
// コード参考 | |
// http://www.d-labo.net/laboratory/php/7001.php | |
// http://stackoverflow.com/questions/9412650/how-to-fix-411-length-required-error-with-file-get-contents-and-the-expedia-xml | |
// http://okwave.jp/qa/q8782761.html | |
//話者名とセリフを入力すると,音声データ(バイナリ)が返ってくるクラス | |
class voiceAPI{ | |
/* | |
* @param:$xml: webapiの仕様に沿ったxmlを入れる | |
* 参考:https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_docs_id=71#tag01 | |
* @return:(binary)音声データ: どう処理したら良いかはまだ不明. | |
* 拡張子がmp3のファイルとして保存すればいいのかな. | |
****/ | |
private function xml_to_voice($xml) { | |
if (!ini_get('allow_url_fopen')) throw new Exception("Not Allowed URL Open!"); | |
if (!isset($xml)) throw new Exception("Empty XML!"); | |
$url = 'https://api.apigw.smt.docomo.ne.jp/aiTalk/v1/textToSpeech?APIKEY='. | |
'HERE_YOUR_APP_KEY_XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; | |
$headers = array( | |
'Content-Type:application/ssml+xml', | |
'Accept:audio/L16', | |
'Content-Length: '.sprintf("%d",strlen($xml)) | |
); | |
$stream = stream_context_create(array('http' => array( | |
'method' => 'POST', | |
'header' => implode("\r\n",$headers), | |
'content' => $xml | |
))); | |
return file_get_contents($url, false, $stream); | |
} | |
/* | |
* @param: | |
* $speaker:話者名.この中から選べる(http://www.ai-j.jp/)例:nozomi | |
* $text:せりふ | |
* @return: $xml | |
***/ | |
public function text_to_xml($speaker,$text){ | |
$xml = '<?xml version="1.0" encoding="utf-8" ?>'; | |
$voice = '<voice name="'.$speaker.'">'; | |
$xml .= '<speak version="1.1">'.$voice.$text.'</voice></speak>'; | |
return $xml; | |
} | |
public function getVoice($xml){ | |
try { | |
$hoge = $this->xml_to_voice($xml); | |
var_dump($hoge); | |
return $hoge; | |
} catch (Exception $e) { | |
var_dump($e); | |
} | |
} | |
} | |
/* | |
////// usage sample | |
$test = new voiceAPI(); | |
$xml = $test->text_to_xml("nozomi","お腹すいたわ.なんかちょーだい"); | |
$voice_bi = $test->getVoice($xml); | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment