How to use AWS polly in boto3 for pause

485 views Asked by At

My code is the following:

import boto3
polly_client = boto3.Session().client('polly')    

response = polly_client.synthesize_speech(
    VoiceId='Joanna',
    OutputFormat='mp3', 
    Text = sentence
)
audio = response['AudioStream']

I've tried using the following sentence:

sentence = '''<speak><s>Mary had a little lamb</s> <s>Whose fleece was white as snow</s>And everywhere that Mary went, the lamb was sure to go.</speak>'''

but the generated audio doesn't have the pause, it just reads out the text.

1

There are 1 answers

0
mcernak On BEST ANSWER

This generates an audio file for the phrase "Hello world" with a 2 second pause between the words:

import boto3
polly_client = boto3.Session().client('polly')

sentence = '''<speak>Hello <break time='2000ms'/> World</speak>'''

response = polly_client.synthesize_speech(
    VoiceId='Joanna',
    OutputFormat='mp3',
    TextType='ssml',
    Text=sentence
)

file = open('speech.mp3', 'wb')
file.write(response['AudioStream'].read())
file.close()

See the aws documentation subsections under Using SSML for more details.