Google CloudTextからPHPでの音声合成
ライブCpanelサーバーでホストされるPHPWebサイトでGoogleのテキスト読み上げを使用しようとしています
テキスト読み上げAPI、[認証情報]セクションで[API KEYの作成]を有効にし、[サービスアカウントキーの作成]ページから認証情報のjsonファイルもダウンロードしました。
次に、Githubからサンプルファイルをダウンロードし、composerを使用してライブラリを構築しました
今、私は自分の鍵をどこに置くべきか理解していません。あらゆる場所で、シェルでキーをエクスポートするようにデマンシングしますが、これは1つのオープンコマンドプロンプトセッションで機能し、毎回エクスポートする必要があります。
このコードをライブのcpanelベースのホスティングで実行したいので、エクスポートすることはできないと思います。
コード内にキーを渡すことができる場所はありますか?
で、このURLのstackoverflowの記事:最初の回答の輸出にCURLの応答のsynthesize-TEXT.TXTが、我々はmp3出力を必要とします
別の回答では、jqを使用する必要があると述べていますが、共有サーバーであるため、jqを配置できるかどうかはわかりません。
この問題を解決する方法はありますか?
更新
@ V.Turによる回答を参照した後、次のコードを試しました
$params = [ "audioConfig"=>[ "audioEncoding"=>"MP3", "pitch"=> "1", "speakingRate"=> "1", "effectsProfileId"=> [ "medium-bluetooth-speaker-class-device" ] ], "input"=>[ "ssml"=>'<speak>The <say-as interpret-as=\"characters\">SSML</say-as> standard <break time=\"1s\"/>is defined by the <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>' ], "voice"=>[ "languageCode"=> "hi-IN", "name" =>"hi-IN-Wavenet-B", 'ssmlGender'=>'MALE' ] ]; $data_string = json_encode($params); $speech_api_key = "My_Key_Here";
$url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)
]
);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);
curl_close($handle); if($responseDecoded['audioContent']){
return $responseDecoded['audioContent'];
}
オーディオをダウンロードしましたが、ssmlで説明した一時停止/中断が機能しませんでした。以下のように$ paramsにデータを渡してみました
$params = "{
'input':{
'ssml':'<speak>The <say-as interpret-as=\"characters\">SSML</say-as>
standard <break time=\"1s\"/>is defined by the
<sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>'
},
'voice':{
'languageCode':'en-us',
'name':'en-US-Standard-B',
'ssmlGender':'MALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}";
しかし、次のエラーが発生します。
配列([エラー] =>配列([コード] => 400 [メッセージ] =>無効なJSONペイロードを受信しました。不明な名前 "":ルート要素はメッセージである必要があります。[ステータス] => INVALID_ARGUMENT [詳細] =>配列( [0] =>配列([@type] => type.googleapis.com/google.rpc.BadRequest [fieldViolations] =>配列([0] =>配列([説明] =>無効なJSONペイロードを受信しました。不明な名前"":ルート要素はメッセージである必要があります。))))))))
これを解決する方法は?
回答
私の実際の例のテキスト読み上げの下で、あなたはあなたの必要性のためにやり直すことができます:
public static function getSound($text) { $text = trim($text); if($text == '') return false;
$params = [ "audioConfig"=>[ "audioEncoding"=>"LINEAR16", "pitch"=> "1", "speakingRate"=> "1", "effectsProfileId"=> [ "medium-bluetooth-speaker-class-device" ] ], "input"=>[ "text"=>$text
],
"voice"=>[
"languageCode"=> "en-US",
"name" =>"en-US-Wavenet-F"
]
];
$data_string = json_encode($params);
$url = 'https://texttospeech.googleapis.com/v1/text:synthesize?fields=audioContent&key=' . $speech_api_key;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($handle, CURLOPT_POSTFIELDS, $data_string); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string)
]
);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);
curl_close($handle); if($responseDecoded['audioContent']){
return $responseDecoded['audioContent']; } return false; } using: public static function saveSound($text)
{
$speech_data = SpeechAPI::getSound($text);//see method upper
if($speech_data) { $file_name = strtolower(md5(uniqid($text)) . '.mp3'); $path = FileUpload::getFolder();//just return directory path
if(file_put_contents($path.$file_name, base64_decode($speech_data))){ return $file_name;
}
}
return null;
}
SSML標準の場合、入力パラメーターを変更する必要があります。
$text = "<speak>The <say-as interpret-as=\"characters\">SSML</say-as> standard <break time=\"1s\"/>is defined by the <sub alias=\"World Wide Web Consortium\">W3C</sub>.</speak>"; $params = [
"audioConfig"=>[
"audioEncoding"=>"LINEAR16",
"pitch"=> "1",
"speakingRate"=> "1",
"effectsProfileId"=> [
"medium-bluetooth-speaker-class-device"
]
],
"input"=>[
//"text"=>$text "ssml" => $text
],
"voice"=>[
"languageCode"=> "en-US",
"name" =>"en-US-Wavenet-F"
]
];
audioEncodingの選択について- https://cloud.google.com/speech-to-text/docs/encoding