Last active
May 24, 2020 00:14
-
-
Save anaptfox/64951815358ba545f94ff864c02887da to your computer and use it in GitHub Desktop.
Node.js Amazon Polly to speaker example.
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
// Load the SDK | |
const AWS = require('aws-sdk') | |
const Stream = require('stream') | |
const Speaker = require('speaker') | |
// Create an Polly client | |
const Polly = new AWS.Polly({ | |
signatureVersion: 'v4', | |
region: 'us-east-1' | |
}) | |
// Create the Speaker instance | |
const Player = new Speaker({ | |
channels: 1, | |
bitDepth: 16, | |
sampleRate: 16000 | |
}) | |
let params = { | |
'Text': 'Hi, my name is @anaptfox.', | |
'OutputFormat': 'pcm', | |
'VoiceId': 'Kimberly' | |
} | |
Polly.synthesizeSpeech(params, (err, data) => { | |
if (err) { | |
console.log(err.code) | |
} else if (data) { | |
if (data.AudioStream instanceof Buffer) { | |
// Initiate the source | |
var bufferStream = new Stream.PassThrough() | |
// convert AudioStream into a readable stream | |
bufferStream.end(data.AudioStream) | |
// Pipe into Player | |
bufferStream.pipe(Player) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to include this in an nextjs typescript project ?