I am trying to generate long audio from text with the Google text-to-speech api, using the google/cloud-text-to-speech PHP client.
I have created the Service Account and given it the permissions in the bucket, as explained in all two links
https://angnasution.com/2023/07/long-audio-api-for-google-cloud-text-to-speech/
https://angnasution.com/2023/07/using-google-cloud-text-to-speech-api-with-python/
The PHP code I use is as follows:
require_once 'vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=C:\Users\Name\Projects\google-text-to-speech\project-219819-da3fdd6a76ad.json');
use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\Client\TextToSpeechLongAudioSynthesizeClient;
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\SynthesizeLongAudioRequest;
use Google\Cloud\TextToSpeech\V1\VoiceSelectionParams;
try {
$client = new TextToSpeechLongAudioSynthesizeClient();
$synthesisInputText = (new SynthesisInput())
->setText('Hola, ¿cómo estás? gracias por venir.');
$voice = (new VoiceSelectionParams())
->setLanguageCode('es-ES')
->setName('es-ES-Standard-A')
->setSsmlGender(SsmlVoiceGender::FEMALE);
$effectsProfileId = 'handset-class-device';
$audioConfig = (new AudioConfig())
->setAudioEncoding(AudioEncoding::LINEAR16)
->setSpeakingRate(0.75)
->setEffectsProfileId(array($effectsProfileId));
$request = (new SynthesizeLongAudioRequest())
->setParent('projects/project/locations/global')
->setOutputGcsUri('gs://bucket/audios/ejemplo.mp3')
->setInput($synthesisInputText)
->setVoice($voice)
->setAudioConfig($audioConfig);
$response = $client->synthesizeLongAudio($request);
$audioContent = $response->getAudioContent();
file_put_contents('output-long.mp3', $audioContent);
echo '<audio src="output.mp3" controls />' . PHP_EOL;
$textToSpeechClient->close();
} catch(Exception $e) {
echo $e->getMessage();
}
But when I run it I get the following error
{ "message": "Unable to perform long audio synthesis. Neither the client project number: 863009906802 nor ID: project-219819 matches the resource project: bucket", "code": 3, "status": "INVALID_ARGUMENT", "details": [] }
Any ideas?
As per the error, you need to use the project ID, see the BOLD part below:
$request = (new SynthesizeLongAudioRequest()) -\>setParent('projects/
PROJECT ID
/locations/global') -\>setOutputGcsUri('gs://bucket/audios/ejemplo.mp3') -\>setInput($synthesisInputText) -\>setVoice($voice) -\>setAudioConfig($audioConfig);