net - UK (2020-04)

(Antfer) #1

PROJECTS
Text-to-Speech API


STEP 5:6(77+((19,5210(17
9$5,$%/(
The private key file needs to be used to authenticate
you when calls are made to the API. To make this
easy, set the GOOGLE_APPLICATION_CREDENTIALS
environmental variable to point to your JSON file you
just downloaded.
Open a new terminal window on your computer
and use the export command like this:

export GOOGLE_APPLICATION_CREDENTIALS=“/[path to
file]/[file name].json”

Mine looked like this, for example:

export GOOGLE_APPLICATION_CREDENTIALS=”/Library/
WebServer/Documents/net-text-to-speech/Text To
Speech-bc489c0a7499.json”

Be sure the path is correct to the place you stored
the private key file. Replace the [file name] with your
private key file and use the path to your file.
On Windows, you can do the same thing via the
command line, like this:

$env:GOOGLE_APPLICATION_CREDENTIALS=“C:\[path to
file]\[FILE_NAME].json”

If you close your terminal or console window, you
may need to run that command again. We’ll add this
into our server-side code shortly as well, so you don’t
have to worry about it again.

STEP 6:0$.($1$3,5(48(67
86,1*&85/
Curl requests can be made in many languages
including PHP, Python and Node. This way you can
make the calls direct in command line or assign the
result to a variable in the language of your choice. If
you need some help using curl, visit this link here:
https://curl.haxx.se/.
Here is an example of a curl call to return MP3 data
of synthesised speech. You’ll notice the data we send
is comprised of three main nodes; input, voice and
audioConfig. We use the input to indicate the ‘text’ to
synthesise into speech, the voice to define how the
voice sounds and the audioConfig node to define the
data type we want to have returned.

curl -H “Authorization: Bearer “$(gcloud auth application-
default print-access-token) -H “Content-Type: application/
json; charset=utf-8” --data “{
‘input’:{
‘text’:’I’ve added the event to your calendar.’
},

‘voice’:{
‘languageCode’:’en-gb’,
‘name’:’en-GB-Standard-A’,
‘ssmlGender’:’FEMALE’
},
‘audioConfig’:{
‘audioEncoding’:’MP3’
}
}” “https://texttospeech.googleapis.com/v1/
text:synthesize”

This will return a JSON object with a property called
audioContent, which is in Base64 format. You’ll need
to decode this into an audio file to play it or process
this data as you prefer. For example, you could write
the data to a text file then run the decode command
like this:

base64 source_base64_text_file -d > dest_audio_file

This method is a couple of steps but gives you a lot
of flexibility in terms of the devices and platforms
you may wish to use.
Continue using this method to test out the calls
if you wish. Make curl calls from your app if your
language supports them. You can also set up a local
SDK in a language you prefer and integrate into your
app. That’s what we’ll look at next.

STEP 7:,167$//&/,(17/,%5$5<
Next we’re going to create a web-based app to
demonstrate how to integrate the API into your
projects. This is a lot easier to use than making
manual curl calls via command line.
There are a number of SDKs available in a variety
of languages. We’re going to use the PHP SDK. If
you want to adjust the code slightly and use another
language such as Node.js, there is a great resource
of SDKs here: https://cloud.google.com/text-to-speech/
docs/reference/libraries.
Start the process by making sure that you have a
project folder set up on your local or remote server.
If you don’t already have Composer, make sure
to set that up to install the SDK. If you don’t have

Above When creating a
service account, give a
name to your new service
and make sure to leave the
role unselected
Free download pdf