I'm creating a smart home assistant, I tried using basic Chrome TTS, other apis, and then fell on the Google Cloud Platform Text To Speech WaveNet thingie. I used a PHP code example to make it put the audio into a file in a folder (named clips.)
When I try to run the PHP file in a browser, it does not work, but running it using the php command in Mac OS terminal does work and creates the file successfully with no errors.
I've tried using Node.js, but it didn't work because I needed to run the file from HTML page and I didn't want to expose my Google Cloud Platform API credentials.
<?php
header("Content-Type: application/json");
if(!isset($_GET['text'])) {
json_encode(array(
"success" => "false",
"error" => "missingPhrase"
));
}
require 'vendor/autoload.php';
use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
$textToSpeechClient = new TextToSpeechClient();
$input = new SynthesisInput();
$input->setText($_GET['text']);
$voice = new VoiceSelectionParams();
$voice->setLanguageCode('en-US-Wavenet-D');
$audioConfig = new AudioConfig();
$audioConfig->setAudioEncoding(AudioEncoding::LINEAR16);
$number = 0;
$fi = new FilesystemIterator("clips", FilesystemIterator::SKIP_DOTS);
foreach ($fi as $f) {
$number = $number + 1;
}
$number = $number + 1;
$resp = $textToSpeechClient->synthesizeSpeech($input, $voice, $audioConfig);
file_put_contents("clips/" . $number . '.mp3', $resp->getAudioContent());
echo json_encode(array(
"file_name" => 'clips/' . $number . ".mp3"
));
?>
The result of the code above results in the default chrome "This page isn’t working" non-sence.
PS, I looked at some other answers on Stack Overflow related to the problem I'm having and they did NOT solve my problem and didn't cover nearly the same issue.
Thanks, Nathan
With the help of @jdp, I was able to solve it. I needed to link to the json file containing authorization credentials. It works now. :)
$textToSpeechClient = new TextToSpeechClient(['credentials' => 'credentials.json']);