Skip to content

Instantly share code, notes, and snippets.

@MasterOdin
Last active May 27, 2020 17:50
Show Gist options
  • Save MasterOdin/c436d823ac2a8a12a43f79b84bed1c04 to your computer and use it in GitHub Desktop.
Save MasterOdin/c436d823ac2a8a12a43f79b84bed1c04 to your computer and use it in GitHub Desktop.
watson text to speech stream
// npm install speaker wav
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1');
const Speaker = require('speaker');
const wav = require('wav');
const textToSpeech = new TextToSpeechV1({});
const sentences = [
'On a cool night lit only by the orange glow of fire, we rushed to my grandfather’s home as his decades-old barn burned to the ground. The firemen let us stand nearby as they pumped water from the creek a quarter mile away. We watched the barn go up in flames, which stirred memories of jumping off foot-wide wooden beams into the hay below. The real sadness came as my elderly grandfather, who did not get out of bed, quietly asked if his cows were safe. He hadn’t had dairy cows in a dozen years.',
'I have always worn my children’s birthstones around my neck. One morning, when I was late for work, my infant son Larry’s topaz birthstone fell from my gold chain. I frantically searched for it, whispering to myself, I lost my Larry, but I will get him back. That day, Larry’s cardiologist called with test results from one of his first checkups. He would need emergency heart surgery. Happily, the operation was a success, and I whispered in Larry’s ear, I thought I lost you, but I knew I’d get you back.',
'Toto was a white dog with a small red tongue, and his stuffing was red as well. When his seams began to come apart beneath his knitted collar, it looked to my six-year-old eyes as though he were bleeding. That night, my father left for his shift in the emergency room with Toto wrapped in a blanket. The next day, Dad showed me the X-rays and Polaroid photographs of the surgery. Beneath the bandage on Toto’s neck was a clean row of stitches. I still have the injury report! I love you, Dad.',
'While walking across an open, grassy field, I became excited as my hand swooped toward the ground like an eagle attacking its prey. I picked up half of a $5 bill. I continued to walk around looking for the other half but thought to myself it would be impossible to find it on such a windy day. As I lifted my head, I spotted the other half of the bill tangled in crabgrass. Somehow, finding two halves of a ripped $5 bill felt better than working for a twenty.',
'A long flight of weathered steps led to a hollow wooden door with rusty numbers beckoning us into room 1108. Inside, we barely noticed the lumpy bed, faded wood paneling, and thin, tacky carpet.',
'We could see the seashore from our perch and easily wander down to feel the sand between our toes. We returned again and again until the burgeoning resort tore down our orange-shingled eyesore. Forty years later, my husband periodically sends me short e-mails that declare the time: 11:08. “I love you, too,” I write back.'
];
// specify the text to synthesize
const params = {
text: sentences.join(' '),
accept: 'audio/wav',
xWatsonLearningOptOut: true,
};
let hrstart;
function printTiming() {
const hrend = process.hrtime(hrstart);
console.info('Execution time (hr): %ds %dms', hrend[0], hrend[1] / 1000000);
}
const reader = new wav.Reader();
reader.on('format', function (format) {
console.log('piping to speaker');
printTiming();
reader.pipe(new Speaker(format));
});
hrstart = process.hrtime();
const synthesizeStream = textToSpeech.synthesizeUsingWebSocket(params);
synthesizeStream.pipe(reader);
// the 'error' event is emitted if there is an error during the connection
// 'err' is the Error object describing the error
synthesizeStream.on('error', err => {
console.log(err);
});
// the 'close' event is emitted once, when the connection is terminated by the service
// the 'code' parameter is the status code. 1000 is the code for a normal termination
// the 'reason' parameter provides a string description of how the connection closed
synthesizeStream.on('close', (code, reason) => {
console.log('finished receiving Watson buffer');
printTiming();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment