forked from watson-developer-cloud/node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_to_speech_to_speakers.js
28 lines (22 loc) · 1.19 KB
/
text_to_speech_to_speakers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'use strict';
// This example takes uncompressed wav audio from the Text to Speech service and plays it through the computer's speakers
// Should work on windows/mac/linux, but linux may require some extra setup first: https://www.npmjs.com/package/speaker
const TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
const wav = require('wav');
const Speaker = require('speaker');
require('dotenv').load({ silent: true }); // imports environment properties from a .env file if present
const textToSpeech = new TextToSpeechV1(
{
// if left unspecified here, the SDK will fall back to the TEXT_TO_SPEECH_USERNAME and TEXT_TO_SPEECH_PASSWORD
// environment properties, and then Bluemix's VCAP_SERVICES environment property
// username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
// password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE'
}
);
const reader = new wav.Reader();
// the "format" event gets emitted at the end of the WAVE header
reader.on('format', function(format) {
// the WAVE header is stripped from the output of the reader
reader.pipe(new Speaker(format));
});
textToSpeech.synthesize({ text: 'hello from IBM Watson', accept: 'audio/wav' }).pipe(reader);