-
-
Save nickstewart95/7dab9dd6ffa6938d3ee41e64476d2e01 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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