Skip to content

Instantly share code, notes, and snippets.

@nickstewart95

nickstewart95/sample.php Secret

Created Jan 3, 2021
Embed
What would you like to do?
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment