Skip to content

Instantly share code, notes, and snippets.

@pete-rai
Last active February 26, 2020 14:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pete-rai/241bb8409f8218394590ab2800dc0e8e to your computer and use it in GitHub Desktop.
Save pete-rai/241bb8409f8218394590ab2800dc0e8e to your computer and use it in GitHub Desktop.
A wrapper around Amazon Polly to make text-to-speech in nodejs super simple.
'use strict'
/*
first install the following node modules:
npm install aws-sdk
npm install stream
npm install speaker
if after running the sample, you get an error 'Illegal instruction: 4'
then also do the following step, AFTER those above:
npm install speaker --mpg123-backend=openal
also sure that you have a file in the following location:
~/.aws/credentials (c:\users\USER_NAME\.aws\credentials on Windows)
which contains your aws credentials in this form:
[default]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
when all set-up, run the sample like this:
node polly.js
*/
// --- dependencies
const aws = require ('aws-sdk');
const stream = require ('stream');
const speaker = require ('speaker');
// --- constants
const DEFAULT_VOICE = /* The lovely */ 'Amy';
const SPEAKER_CONFIG = { channels: 1, bitDepth: 16, sampleRate: 16000 };
// --- the polly class
function Polly (region)
{
this._polly = new aws.Polly ({region: region});
this._voice = DEFAULT_VOICE;
this.voice = function (voice) // see https://docs.aws.amazon.com/polly/latest/dg/voicelist.html
{
this._voice = voice;
}
this.say = function (speech, callback)
{
var utterance = { OutputFormat: 'pcm', VoiceId: this._voice, Text: speech };
this._polly.synthesizeSpeech (utterance, (error, data) =>
{
if (!error && data && data.AudioStream instanceof Buffer)
{
var buffer = new stream.PassThrough ();
var player = new speaker (SPEAKER_CONFIG);
buffer.end (data.AudioStream);
buffer.pipe (player);
player.on ('finish', function ()
{
buffer.unpipe (player);
buffer.end ();
player.close ();
if (callback)
{
callback ();
}
});
}
});
}
}
// --- example usage
var polly = new Polly ('us-west-2');
console.log ();
console.log ('Ulysses by Sir Alfred Lord Tennyson');
console.log ();
polly.say ('We are not now that strength, which in old days moved Earth and heaven', function ()
{
polly.voice ('Brian');
polly.say ('That which we are we are. One equal temper of heroic hearts', function ()
{
polly.voice ('Emma');
polly.say ('Made weak by time and fate, but strong in will', function ()
{
polly.voice ('Raveena');
polly.say ('To strive, to seek, to find, and not to yield.'); // no callback here, as its optional
});
});
});
@pete-rai
Copy link
Author

pete-rai commented Feb 18, 2018

If you want a lighter version, which does not require the full AWS-SDK, check out my leaner gist.

@rsshilli
Copy link

rsshilli commented Jun 8, 2018

This reads only the first line with node js 8.10.0 on Windows 10. The program exits before Brian gets to speak.

@rsshilli
Copy link

rsshilli commented Jun 8, 2018

Ok. I figured out that this works if you switch back to npm install speaker@0.4.0. The newest version 0.4.1 breaks this code.

Thank you for creating this Pete!

@ij0209
Copy link

ij0209 commented Feb 26, 2020

it doesn't work. i cannot do 'npm install speaker'
i got error. how can i install speaker. if possible, please let us know. thanks

@pete-rai
Copy link
Author

pete-rai commented Feb 26, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment