Skip to content

Instantly share code, notes, and snippets.

@nickstewart95
Created January 3, 2021 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickstewart95/7dab9dd6ffa6938d3ee41e64476d2e01 to your computer and use it in GitHub Desktop.
Save nickstewart95/7dab9dd6ffa6938d3ee41e64476d2e01 to your computer and use it in GitHub Desktop.
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