Skip to content

Instantly share code, notes, and snippets.

@anaptfox
Last active May 24, 2020 00:14
Show Gist options
  • Save anaptfox/64951815358ba545f94ff864c02887da to your computer and use it in GitHub Desktop.
Save anaptfox/64951815358ba545f94ff864c02887da to your computer and use it in GitHub Desktop.
Node.js Amazon Polly to speaker example.
// 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)
}
}
})
@mimiqkz
Copy link

mimiqkz commented May 24, 2020

how to include this in an nextjs typescript project ?

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