phpgoogle-text-to-speech

How to submit a V2 Google text-to-speech request via php?


The below code does just show a NULL instead of the mp3 data. It is missing the actual submit of the request I guess. Unfortunately, all documentation is based on V1 of the api.

$synthesizeSpeechRequest = new SynthesizeSpeechRequest();
$synthesizeSpeechRequest->setInput($input_text);
$synthesizeSpeechRequest->setVoice($voice);
$synthesizeSpeechRequest->setAudioConfig($audioConfig);

//$client->execute($synthesizeSpeechRequest); // ? Something like this here ?

$synthesizeSpeechResponse = new SynthesizeSpeechResponse();
$audioContent = $synthesizeSpeechResponse->getAudioContent();
var_dump($audioContent); // does display NULL

complete code:

<?php    
require_once realpath(__DIR__ . '/vendor').'/autoload.php';

use Google\Service\Texttospeech;
use Google\Service\Texttospeech\AudioConfig;
use Google\Service\Texttospeech\SynthesisInput;
use Google\Service\Texttospeech\VoiceSelectionParams;
use Google\Service\Texttospeech\SynthesizeSpeechRequest;
use Google\Service\Texttospeech\SynthesizeSpeechResponse;

putenv('GOOGLE_APPLICATION_CREDENTIALS=neural-reactor-SOMENUMBERHERE.json');
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$client->addScope(Texttospeech::CLOUD_PLATFORM);

$service = new Texttospeech($client);

$text = 'There are over 200 Google API services. The chances are good that you will not want them all. In order to avoid shipping these dependencies with your code';
$input_text = new SynthesisInput();
$input_text->setText($text);

$voice = new VoiceSelectionParams();
$voice->setLanguageCode('en-gb');
$voice->setName('en-GB-Standard-A');
$voice->setSsmlGender('FEMALE');

$audioConfig = new AudioConfig();
$audioConfig->setAudioEncoding('MP3');

$synthesizeSpeechRequest = new SynthesizeSpeechRequest();
$synthesizeSpeechRequest->setInput($input_text);
$synthesizeSpeechRequest->setVoice($voice);
$synthesizeSpeechRequest->setAudioConfig($audioConfig);
echo '<pre>'; var_dump($synthesizeSpeechRequest); echo '</pre>';

//$client->execute($synthesizeSpeechRequest); // ? Something like this here ?

$synthesizeSpeechResponse = new SynthesizeSpeechResponse();
$audioContent = $synthesizeSpeechResponse->getAudioContent();
echo '<pre>'; var_dump($audioContent); echo '</pre>'; // does display NULL


// file_put_contents(__DIR__.'/output.mp3', $audioContent);

composer.json

{
    "config": {
        "platform": {
            "php": "7.4"
        }
    },
    "require": {
        "google/apiclient": "^2.12.1"
    },
    "scripts": {
        "pre-autoload-dump": "Google\\Task\\Composer::cleanup"
    },
    "extra": {
        "google/apiclient-services": [
            "Texttospeech"
        ]
    }
}

Solution

  • To submit a request you should add next code:

    $synthesizeSpeechResponse = $service->text->synthesize($synthesizeSpeechRequest);
    

    It will return an instance of SynthesizeSpeechResponse. Don't forget to decode audio content, that is encoded in Base64.

    So, your final code would be something like this:

    <?php
    require_once realpath(__DIR__ . '/vendor').'/autoload.php';
    
    use Google\Service\Texttospeech;
    use Google\Service\Texttospeech\AudioConfig;
    use Google\Service\Texttospeech\SynthesisInput;
    use Google\Service\Texttospeech\SynthesizeSpeechRequest;
    use Google\Service\Texttospeech\VoiceSelectionParams;
    
    putenv('GOOGLE_APPLICATION_CREDENTIALS=neural-reactor-SOMENUMBERHERE.json');
    $client = new Google\Client();
    $client->useApplicationDefaultCredentials();
    $client->addScope(Texttospeech::CLOUD_PLATFORM);
    
    $service = new Texttospeech($client);
    
    $text = 'There are over 200 Google API services. The chances are good that you will not want them all. In order to avoid shipping these dependencies with your code';
    $input_text = new SynthesisInput();
    $input_text->setText($text);
    
    $voice = new VoiceSelectionParams();
    $voice->setLanguageCode('en-gb');
    $voice->setName('en-GB-Standard-A');
    $voice->setSsmlGender('FEMALE');
    
    $audioConfig = new AudioConfig();
    $audioConfig->setAudioEncoding('MP3');
    
    $synthesizeSpeechRequest = new SynthesizeSpeechRequest();
    $synthesizeSpeechRequest->setInput($input_text);
    $synthesizeSpeechRequest->setVoice($voice);
    $synthesizeSpeechRequest->setAudioConfig($audioConfig);
    
    $synthesizeSpeechResponse = $service->text->synthesize($synthesizeSpeechRequest);
    $audioContent = $synthesizeSpeechResponse->getAudioContent();
    
    file_put_contents(__DIR__ . '/output.mp3', base64_decode($audioContent));
    

    If you want to use an API key instead of Application Default Credentials (ADC), add this code:

    $client->setDeveloperKey('AIza...');
    

    instead of

    $client->useApplicationDefaultCredentials();