Search code examples
node.jsamazon-web-servicesamazon-polly

Add 1s delay to end of Polly MP3 Conversion


I'm using Polly to generate text-to-speech MP3 files. I want to add a 1-second pause to the end of each file. How would I do that?

Here's what I'm working with:

// Load the SDK
const AWS = require('aws-sdk')
const Fs = require('fs')
AWS.config.loadFromPath('config.json');

// Create an Polly client
const Polly = new AWS.Polly({
    signatureVersion: 'v4',
    region: 'us-east-1'
})

let params = {
    'Text': "This is the string I'm converting to MP3" ,
    'OutputFormat': 'mp3',
    'VoiceId': 'Kimberly'
}

Polly.synthesizeSpeech(params, (err, data) => {
    if (err) {
        console.log(err.code)
    } else if (data) {
        if (data.AudioStream instanceof Buffer) {
            Fs.writeFile("./myverse.mp3", data.AudioStream, function(err) {
                if (err) {
                    return console.log(err)
                }
                console.log("The file was saved!")
            })
        }
    }
})

Solution

  • Looks like I just needed to add the 'TextType': 'ssml' parameter. Also, the entire string needs to be enclosed in the <speak></speak> tags, with the pause coming between those.