Amazon Polly Service is used to convert text into lifelike speech. It helps in creating speech-enabled applications and products easily. Polly supports broad set of languages. In addition to Standard Text-to-Speech (TTS) voices, Amazon Polly also offers Neural Text-to-Speech (NTTS) voices supporting two speaking styles - a Newscaster reading style and a Conversational speaking style. In this task, you will write code to convert a text into voice.
-
Goto the AWS Cloud9 console and click on the New File option under the File menu.
-
A new Untitled1 file gets created. Write the following code in the Untitled1 file.
import boto3 polly_client = boto3.client('polly') response = polly_client.synthesize_speech(VoiceId='Salli', OutputFormat='mp3', Text = 'People love AWS Dojo workshops and exercises.') file = open('myspeech.mp3', 'wb') file.write(response['AudioStream'].read()) file.close()
`
-
In the code above, you first create client to polly. Then you call synthesize_speech method to convert the text into voice. The text is provided in the Text parameter. The output format is set to mp3. The lifelike voice is Salli. You have choice to use one from the voices shown below. Finally - you write - the converted voice AudioStream to the local audio file myspeech.mp3.
-
Click on the Save option under the File menu. On the Save As popup, type in dojopolly.py as the file name and click on the Save button. The code is saved in the file.
-
In the console window, execute python dojopolly.py command to run the dojopolly.py code. The code execution finishes in no time. It creates myspeech.mp3 file which is the voice conversion of the text People love AWS Dojo workshops and exercises.
-
You can download myspeech.mp3 on your local machine to listen and test it. Right-click on the myspeech.mp3 file and click on the Download menu option to download the file. Kindly download the file and test it. Hope you don’t find any surprises.
-
This was a quick work with Amazon Polly. You can make little changes to the code to work with other languages and other speech styles. You can also use lexicons to customize the pronunciations. We leave it to your experimentation. If you want AWS Dojo to cover any of these aspects; please let us know and we would happy to create an exercise / workshop for it.
-
Let’s focus on Amazon Translate in the next task.