Using Amazon Polly sound files

Sample project to demonstrate usage of the AWS SDK for Node.js:

cd /opt/
git clone https://github.com/awslabs/aws-nodejs-sample
cd aws-nodejs-sample
npm install
npm install optimist
npm install child_process

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’: ‘Vitoria’
}

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’);

    })
}

}
})

Script:
node script.js --mp3=/var/lib/asterisk/sounds/mp3file --text=‘Your text’ --wav=/var/lib/asterisk/sounds/wavfile

Example of Dialplan:

exten => 0800,1,Answer()
same => n,System(node /opt/aws-nodejs-sample/script.js --mp3=/var/lib/asterisk/sounds/go.mp3 --text=‘Your text’ --wav=/var/lib/asterisk/sounds/go)
exten => 0800,n,Wait(1)
exten => 0800,n,Playback(test)

CLI result:

-- Executing [0800@from-internal:1] Answer("PJSIP/1000-00000065", "") in new stack
-- Executing [0800@from-internal:2] Wait("PJSIP/1000-00000065", "2") in new stack
   > 0x7f82980238e0 -- Probation passed - setting RTP source address to 100.100.100.100:31460
-- Executing [0800@from-internal:3] System("PJSIP/1000-00000065", "sudo node /opt/aws-nodejs-sample/script.js --mp3=/var/lib/asterisk/sounds/go.mp3 --text='Your text here, test one two tree' --wav=/var/lib/asterisk/sounds/test") in new stack
-- Executing [0800@from-internal:4] Playback("PJSIP/1000-00000065", "test") in new stack