TTS Engine Custom - Amazon Polly - 24 languages

WORKING SUMMARY:

  1. Run these commands in shell:
cd /opt/
git clone https://github.com/awslabs/aws-nodejs-sample
cd aws-nodejs-sample
npm install
npm install optimist
npm install child_process

2a) Create file in directory /opt/aws-nodejs-sample/

vim script.js

// Load the SDK
var argv = require('optimist').argv;
const AWS = require('aws-sdk')
const Fs = require('fs')
var child_process = require('child_process');
// Create an Polly client
const Polly = new AWS.Polly({
accessKeyId: "accessKeyId here",
secretAccessKey: "secretAccesKey here",
signatureVersion: 'v4',
region: 'us-east-1'

})

let params = {
'Text': argv.text,
// 'Text': 'Tatiana',
'OutputFormat': 'mp3',
'SampleRate': '8000',
'VoiceId': 'Amy'
}

Polly.synthesizeSpeech(params, (err, data) => {
if (err) {
console.log(err.code)
} else if (data) {
if (data.AudioStream instanceof Buffer) {
Fs.writeFile(argv.mp3, data.AudioStream, function(err) {
if (err) {
return console.log(err)
}
console.log("The file was saved!")
var output = child_process.execSync('lame --decode ' + argv.mp3 + ' ' + '-b 8000' + ' ' + argv.wav + '.wav');

        })
    }
}
})

2b) You can change the voice, by going to google and typing “Amazon Polly Available Voices”. Sorry but new users cant post links here.

Then copy the Name/ID you want and replace ‘Amy’ in the ‘VoiceId’ line above with that ID.
Also, DO NOT FORGET to obtain and enter your AWS ‘accessKeyId’ and ‘secretAccessKey’ in the lines marked bold above!

3a) Modify /var/lib/asterisk/agi-bin/propolys-tts.agi to change the following lines:

vim /var/lib/asterisk/agi-bin/propolys-tts.agi

FROM

    default;
            $format = array(
                    **"ext" => "sln",**
                    "rate" => "8000"
            );
    break;

TO

    default;
            $format = array(
                    **"ext" => "wav",**
                    "rate" => "8000"
            );
    break;

3b) Continue modifying the same file to add the following lines to switch ($engine) { before the default case.

            case 'aws-polly':
                    exec($enginebin." /opt/aws-nodejs-sample/script.js --mp3=/var/lib/asterisk/sounds/tts/$engine-tts-$hash.mp3 --text='$text' --wav=/var/lib/asterisk/sounds/tts/$engine-tts-$hash");
                    break;

3c) IMPORTANT NOTE: Any time you apply an updated configuration in the GUI it will replace all of these steps in #3 and you will have to re-enter these changes to restore the TTS engine.

  1. Go to Settings >> Text to Speech Engines, then click + Add TTS Engine

Engine Name: Custom, then type ‘aws-polly’ without quotes.
Engine Path: /usr/bin/node

  1. Create TTS from Applications >> Text to Speech using the aws-polly engine.
    DO NOT Apply Config when you create your TTS entries, it will remove all the work in Step #3 above and you will have to re-enter it again.

You now have a fully customizable AWS/Polly TTS

And a HUGE THANK YOU to jersonjunior for all his help in getting this up and running. I came across this post and was a little hesitant, but this is a GREAT solution for TTS in FreePBX.

To make things simpler, when everything is setup, make a copy of /var/lib/asterisk/agi-bin/propolys-tts.agi as /var/lib/asterisk/agi-bin/propolys-tts.bak and then you can simply replace the file when you need to Apply Config.

3 Likes