Skip to content

Commit 2c77a32

Browse files
committed
docs: add an example and a test for tts over websocket
1 parent 11aa525 commit 2c77a32

File tree

4 files changed

+90
-9
lines changed

4 files changed

+90
-9
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ fs.createReadStream('./resources/speech.wav')
602602
603603
### Text to Speech
604604
605-
Use the [Text to Speech][text_to_speech] service to synthesize text into a .wav file.
605+
Use the [Text to Speech][text_to_speech] service to synthesize text into an audio file.
606606
607607
```js
608608
var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
@@ -632,8 +632,16 @@ textToSpeech
632632
fs.writeFileSync('audio.wav', audio);
633633
console.log('audio.wav written with a corrected wav header');
634634
});
635+
636+
637+
// or, using WebSockets
638+
textToSpeech.synthesizeUsingWebSocket(params);
639+
synthStream.pipe(fs.createWriteStream('./audio.ogg'));
640+
// see more information in examples/text_to_speech_websocket.js
635641
```
636642
643+
644+
637645
### Tone Analyzer
638646
639647
Use the [Tone Analyzer][tone_analyzer] service to analyze the

examples/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
"parserOptions": { "ecmaVersion": 5 },
2+
"parserOptions": { "ecmaVersion": 6 },
33
"rules": {
44
"no-console": "off",
55
"node/no-missing-require": "off",
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
5+
6+
const textToSpeech = new TextToSpeechV1({
7+
// if left unspecified here, the SDK will fall back to the TEXT_TO_SPEECH_USERNAME and TEXT_TO_SPEECH_PASSWORD
8+
// environment properties, and then IBM Cloud's VCAP_SERVICES environment property
9+
// username: 'INSERT YOUR USERNAME FOR THE SERVICE HERE',
10+
// password: 'INSERT YOUR PASSWORD FOR THE SERVICE HERE'
11+
});
12+
13+
// specify the text to synthesize
14+
const params = {
15+
text: 'Hello, world.',
16+
accept: 'audio/ogg;codecs=opus',
17+
};
18+
19+
// synthesizeUsingWebSocket returns a Readable Stream that can be piped or listened to
20+
const synthesizeStream = textToSpeech.synthesizeUsingWebSocket(params);
21+
22+
// the output of the stream can be piped to any writable stream, like an audio file
23+
synthesizeStream.pipe(fs.createWriteStream('./speech.ogg'));
24+
25+
// if the stream is not being piped anywhere and is only being listened to, the stream needs
26+
// to be explicitly set to flowing mode:
27+
28+
// synthesizeStream.resume();
29+
30+
// the 'message' event is emitted when data is processed and returned from the service
31+
// the 'message' parameter is the entire response frame of information returned from the
32+
// service. it is mainly useful for debugging
33+
// the 'data' parameter is the data payload contained within the message. it is typically
34+
// binary audio data, but if the text includes SSML marks or the request includes the
35+
// 'timings' parameter, 'data' could be a string containing marks or timing information
36+
synthesizeStream.on('message', (message, data) => {
37+
console.log(data);
38+
});
39+
40+
// the 'error' event is emitted if there is an error during the connection
41+
// 'err' is the Error object describing the error
42+
synthesizeStream.on('error', err => {
43+
console.log(err);
44+
});
45+
46+
// the 'close' event is emitted once, when the connection is terminated by the service
47+
// the 'code' parameter is the status code. 1000 is the code for a normal termination
48+
// the 'reason' parameter provides a string description of how the connection closed
49+
synthesizeStream.on('close', (code, reason) => {
50+
console.log(code);
51+
});

test/integration/text_to_speech.test.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,39 @@ describe('text_to_speech_integration', function() {
1616
text_to_speech.voices(null, done);
1717
});
1818

19-
it('synthesize()', function(done) {
19+
describe('synthesize', function() {
2020
const params = {
2121
text: 'test',
2222
accept: 'audio/wav',
2323
};
24-
// wav.Reader parses the wav header and will throw if it isn't valid
25-
const reader = new wav.Reader();
26-
text_to_speech
27-
.synthesize(params)
28-
.pipe(reader)
29-
.on('format', done.bind(null, null));
24+
25+
it('synthesize using http', function(done) {
26+
// wav.Reader parses the wav header and will throw if it isn't valid
27+
const reader = new wav.Reader();
28+
text_to_speech
29+
.synthesize(params)
30+
.pipe(reader)
31+
.on('format', done.bind(null, null));
32+
});
33+
34+
it('synthesize using websocket', function(done) {
35+
const synthStream = text_to_speech.synthesizeUsingWebSocket(params);
36+
synthStream.resume();
37+
38+
synthStream.on('message', function(message, data) {
39+
expect(data).not.toBeNull();
40+
});
41+
42+
synthStream.on('error', function(err) {
43+
// fail assertation
44+
throw err;
45+
});
46+
47+
synthStream.on('close', function(code, reason) {
48+
expect(code).toBe(1000);
49+
done();
50+
});
51+
});
3052
});
3153

3254
it('pronunciation()', function(done) {

0 commit comments

Comments
 (0)