net - UK (2020-04)

(Antfer) #1

Text-to-Speech API


require DIR. ‘/vendor/autoload.php’;
use Google\Cloud\TextToSpeech\V1\AudioConfig;
use Google\Cloud\TextToSpeech\V1\AudioEncoding;
use Google\Cloud\TextToSpeech\V1\SsmlVoiceGender;
use Google\Cloud\TextToSpeech\V1\SynthesisInput;
use Google\Cloud\TextToSpeech\V1\TextToSpeechClient;
use Google\Cloud\TextToSpeech\V1\
VoiceSelectionParams;


STEP 13:,1,7,$/,6(THE &/,(17$1'
6(77(;7
You now need to initialise a TextToSpeechClient and set
the text you’d like to create speech from. We’ll use
the text we loaded from our form.


// instantiate a client
$client = new TextToSpeechClient();
// set the text to synthesise
$synthesisInputText = (new SynthesisInput())-



setText($text);



STEP 14: '(),1(92,&(3$5$0(7(56
Next, you can define the type of voice that you’d like
to synthesise. You can pick from a wide range of
voices, as per the list here: https://cloud.google.com/
text-to-speech/docs/voices.
We included a few options as an example in the
select element on our form. We also define the
gender of the voice as well. You can expand these
options as much as you need.


$voice = (new VoiceSelectionParams())
->setLanguageCode($type)
->setSsmlGender(SsmlVoiceGender::MALE);
if($gender==”female”){
$voice->setSsmlGender(SsmlVoiceGender::FEMALE);
}


STEP 15: 6(77,1*7+('(9,&(
+$5':$5(,'
You can specify device profiles to align with the
hardware your website or app will be run on. For
example, if you are primarily targeting phones or
watches you could use the wearable-class-device or the
handset-class-device. There is a full list of supported
device profiles here: https://cloud.google.com/text-to-
speech/docs/audio-profiles.


// Effects profile
$effectsProfileId = “large-home-entertainment-class-
device”;


STEP 16:&21),*85(7+($8',2
You have a lot of control over the audio you get back
from the API. You can specify WAV or MP3 formats,


as well as speed, pitch and even pre-configured
device profiles. Add the following code to configure
the audio:

$audioConfig = (new AudioConfig())
>setAudioEncoding(AudioEncoding::MP3)
->setEffectsProfileId(array($effectsProfileId));

STEP 17:6(1'7+(7(;77263((&+
5(48(67
It is now time to put all of this together to request
the text to be synthesised. We send the text, the
voice options and the audio configuration. We use
the getAudioContent method to get the audio file from
the results we get back from the API call.
Then you can store it locally or use the result how
you wish, since it is returned as a binary format.
In this example we’ll store it and give it a unique
filename. Add the following code to do that:

$response = $client->synthesizeSpeech($synthesisInputT
ext, $voice, $audioConfig);
$audioContent = $response->getAudioContent();
$filename = ‘output’. uniqid() .’.mp3’;
file_put_contents( $filename, $audioContent);

STEP 18:3/$<7+(5(68/7
Finally, set the MP3 file as a source to a standard
HTML audio element. You can auto play it, so it plays
automatically when the page loads.

<audio src=’<?php echo $filename; ?>’ controls autoplay></
audio>

Now you can test out the form. You can enter any
text you like, play about with the voice options and
hear the results!

P$57,1*7+28*+76
As AI continues to advance in functionality, it
will not only impact problem solving and complex
computation but unlock new ways of interacting
with technology. Typing will give way more
and more to voice commands and even thought
commands. Hearing a confirmation in real time,
in a human voice, can be far more efficient and
flexible than having to read text. Our interactions
will become more and more natural, dissolving any
friction in communications.
Your projects can leverage AI to make them easier
to use and have more valuable, deeper results. As you
explore AI technologies, try to find new ways to
make experiences better, to make life better for your
end users. AI creates great opportunities but also
comes with a need for responsible, ethical use.
Free download pdf