- [Instructor] Next let's take a look at the
text to speech function which we call twice in this app.
Once to convert Spanish text into spoken Spanish
and once to convert English text into spoken English.
In both cases we're going to take advantage
of the Watson sdk's TextToSpeechV1 class
and when you create an object of this class
like the other objects we showed you up above
you will need your API key as an argument.
Now we're calling this variable tts
which is shorthand for text to speech
and we're going to use that object to create the audio
and the stream of bytes that it gives us back
we will simply write into a file on the local system
which we will then play back in the app.
We use a with statement here
to open up the specified file name argument
for writing in binary format
because audio is a binary format.
We call that object audio file
and you can see here the call to audio file.write
is going to be outputting, excuse me,
outputting this content.
We use the text to speech objects synthesize method
to actually create the audio.
We give it the text that we want it to speak
and we also give it the format
in which we want to receive back the audio.
So in this case the media type is audio/wav
which means we're going to get a wav file back
from the Watson service.
And the voice that we want to use
is going to be used to synthesize that speech.
And as you may recall
we had a couple of different voices
that we specified up above.
One was the U.S. English voice called Allison Voice
and one was the Spanish voice for the U.S.
called Sophia Voice.
Now this method call will generate the actual audio
and what we will get back once again
is a detailed response object from Watson
and inside that detailed response object
we can get the result that was returned to us.
The result objects content property
is going to contain the actually bytes of the audio
that we want to write out to disk.
So we get that content and then we simply blast it out
into a file that we can then play back
in a subsequent function call.