Last active
February 26, 2020 14:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 | |
}); | |
}); | |
}); |
This reads only the first line with node js 8.10.0 on Windows 10. The program exits before Brian gets to speak.
Ok. I figured out that this works if you switch back to npm install [email protected]
. The newest version 0.4.1 breaks this code.
Thank you for creating this Pete!
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
See the comment i just posted below the gist:
https://gist.github.com/pete-rai/dc894c13f11de6e9634eb3379fb39c3b
Let me know how you get on
On 2020-02-26 14:06, ij0209 wrote:
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
--
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub [1], or unsubscribe [2].
Links:
------
[1]
https://gist.github.com/241bb8409f8218394590ab2800dc0e8e?email_source=notifications&email_token=ADX5IS2LAKYLF7AAJRGY7FTREZZNHA5CNFSM4K4GHA5KYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAGCWZW#gistcomment-3190171
[2]
https://github.com/notifications/unsubscribe-auth/ADX5IS64XUVBOQU5CJM6N33REZZNHANCNFSM4K4GHA5A
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want a lighter version, which does not require the full AWS-SDK, check out my leaner gist.