Using Text To Speech with PHP

Google offer’s a really nifty Text To Speech API, which allows you to pass text that it will convert into natural-sounding speech. The pricing on it relatively cheap and seems to be a really solid offering by Google. As with the rest of the Google APIs, there is a PHP library.

Sample Use Case

Due to COVID, we are doing more at home workouts at Team Jeff Wrestling – a list of different exercises on an Instagram/Facebook/Text that is sent out to all the kids. While this works, it’s not interactive and the likelihood of it being done correctly isn’t the best. We put our heads together and came up with a solution – a countdown timer with the exercise on the screen. As part of this, we wanted the exercise name to be read aloud that way the person doing the workout would know when it was a new exercise. To do this, I landed on using the Google Text to Speech API.

Using it is pretty simple, with the setup being creating a service account and enabling it for the Text to Speech API. From there, you just set up your credentials in a TextToSpeechClient and then pass your configuration to it. In this sample case, the output is an MP3 file that gets saved to the server and then used in the workout. Below is my function that handles this.

public function textToSpeech($text)
{
$file = md5($text) . '.mp3';
$file_path = __DIR__ . '/audio/' . $file;
if (!file_exists($file_path)) {
$textToSpeechClient = new TextToSpeechClient([
'credentials' => ''
]);
$input = (new SynthesisInput())
>setSsml($text);
$voice = (new VoiceSelectionParams())
>setLanguageCode('en-US')
>setName('en-US-Wavenet-B')
>setSsmlGender(SsmlVoiceGender::MALE);
$audioConfig = (new AudioConfig())
>setAudioEncoding(AudioEncoding::MP3);
$resp = $textToSpeechClient->synthesizeSpeech($input, $voice, $audioConfig);
file_put_contents($file_path, $resp->getAudioContent());
}
return $file;
}

view raw
sample.php
hosted with ❤ by GitHub