Google Cloud Text to speech in php
Sto cercando di utilizzare la sintesi vocale di Google nel mio sito Web php per essere ospitato su un server Cpanel live
Ho abilitato l'API di sintesi vocale, la CHIAVE API creata nella sezione Credenziali, inoltre ho scaricato il file json delle credenziali dalla pagina Crea chiave dell'account di servizio.
Quindi ho scaricato i file di esempio da Github e ho anche usato il compositore per creare la libreria
Adesso non capisco dove mettere le chiavi. In ogni luogo, dimostra di ESPORTARE la chiave in Shell, ma funzionerebbe per 1 sessione del prompt dei comandi aperto e dovrà essere esportato ogni volta.
Dato che voglio eseguire questo codice su un hosting basato su cpanel live, penso che non sarà possibile esportarlo.
C'è un posto all'interno dei codici dove posso passare la chiave?
In questo articolo URL su stackoverflow: la prima risposta esporta la risposta di CURL a synthesize-text.txt ma abbiamo bisogno dell'output mp3
Un'altra risposta afferma che dovremmo usare jq ma poiché è un server hsoting condiviso, non sono sicuro che possiamo organizzare jq .
C'è una via d'uscita a questo problema?
Aggiornare
Ho provato il seguente codice dopo aver fatto riferimento alla risposta di @ 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'];
}
Ho scaricato l'audio ma le pause / interruzioni che ho menzionato in ssml non hanno funzionato. Ho provato a passare i dati a $ params come di seguito
$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'
}
}";
Ma ottengo il seguente errore:
Array ([error] => Array ([code] => 400 [message] => Payload JSON non valido ricevuto. Nome sconosciuto "": l'elemento root deve essere un messaggio. [Status] => INVALID_ARGUMENT [details] => Array ( [0] => Array ([@type] => type.googleapis.com/google.rpc.BadRequest [fieldViolations] => Array ([0] => Array ([description] => Payload JSON non valido ricevuto. Nome sconosciuto "": L'elemento radice deve essere un messaggio.))))))
Come risolverlo?
Risposte
Sotto il mio esempio di sintesi vocale funzionante, potresti ripetere per le tue esigenze:
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;
}
Per lo standard SSML è necessario modificare i parametri di input:
$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"
]
];
informazioni su scegli audioEncoding - https://cloud.google.com/speech-to-text/docs/encoding