I have build the text to speech api in php using google cloud. I am able to pass other parameters such as pitch, speaking rate, text from URL parameter but I just want to know how I can do the same for gender voice?
I have tried passing it as string or passing the whole argument but none of these work.
$synthesisInputText = (new SynthesisInput())->setText($_GET['text']);
$voice = (new VoiceSelectionParams())
->setLanguageCode($_GET['lang']))
// ->setName('en-IN-Wavenet-A')
->setSsmlGender(SsmlVoiceGender::FEMALE);
$audioConfig = (new AudioConfig())
->setAudioEncoding(AudioEncoding::MP3)
->setPitch($_GET['pitch'])
->setSpeakingRate($_GET['speking_rate']);
$response = $this->client->synthesizeSpeech($synthesisInputText, $voice, $audioConfig);
$audioContent = $response->getAudioContent();
file_put_contents($this->file,$audioContent);
I am able to get all other parameters correctly but I want get setSsmlGender(SsmlVoiceGender::$_GET['voice'])
from URL parameter as well. Any help would be great?
Use the SsmlVoiceGender::value()
method to convert your input to the proper enum value:
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
$genderInput = $_GET['voice'];
try {
$gender = SsmlVoiceGender::value($genderInput);
} catch (\UnexpectedValueException $e) {
// Invalid input. Hardcode a value?
$gender = SsmlVoiceGender::MALE;
}
$voice->setSsmlGender($gender);
Make sure you are using google/cloud-voice-text-to-speech
v0.4.0 or higher. If you're using google/cloud
, be sure to use v0.98.0
or higher.