Skip to content

Commit 7930380

Browse files
committed
added getVoices method, example
1 parent dffe1d3 commit 7930380

File tree

8 files changed

+549
-34
lines changed

8 files changed

+549
-34
lines changed

dist/watson-speech.js

Lines changed: 451 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Custom Voice - Watson Text to Speech</title>
6+
</head>
7+
<body>
8+
9+
<section>
10+
<h2>Text to Speech with Custom Voice Example</h2>
11+
<textarea id="text" rows="6" cols="80">Hello from IBM Watson</textarea>
12+
<p>Voice: <select id="voice"></select></p>
13+
<p><button id="button">Synthesize Text</button></p>
14+
</section>
15+
16+
<script src="watson-speech.js"></script>
17+
<script src="http://code.jquery.com/jquery-2.2.0.min.js"></script>
18+
19+
<h2>Code for this demo:</h2>
20+
<pre><code><script style="display: block;">
21+
$(function() {
22+
$.get('/api/text-to-speech/token').then(function (token) {
23+
WatsonSpeech.TextToSpeech.getVoices({token: token}).then(function (voices) {
24+
$('#voice').html(voices.map(function(v) {
25+
return '<option value="' + v.name + '"' + (v.name == 'en-GB_KateVoice' ? ' selected' : '') + '>' + v.name + '</option>';
26+
}).join('\n'));
27+
}).catch(console.error.bind(console));
28+
});
29+
30+
$('#button').click(function () {
31+
$.get('/api/text-to-speech/token').then(function (token) {
32+
WatsonSpeech.TextToSpeech.synthesize({
33+
text: $('#text').val(),
34+
voice: $('#voice').val(),
35+
token: token
36+
})
37+
});
38+
});
39+
});
40+
</script></code></pre>
41+
</body>
42+
</html>

examples/public/text-to-speech.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
<section>
1010
<h2>Basic Text to Speech Example</h2>
11-
<input id="text" value="Hello from IBM Watson"/> <button id="button">Synthesize Text</button>
11+
<textarea id="text" rows="6" cols="80">Hello from IBM Watson</textarea>
12+
<p><button id="button">Synthesize Text</button></p>
1213
</section>
1314

1415
<script src="watson-speech.js"></script>

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"object.assign": "^4.0.3",
4646
"object.pick": "^1.1.1",
4747
"readable-blob-stream": "^1.1.0",
48-
"websocket": "^1.0.22"
48+
"websocket": "^1.0.22",
49+
"whatwg-fetch": "^0.11.0"
4950
},
5051
"repository": {
5152
"type": "git",

speech-to-text/recognize-stream.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ var QUERY_PARAMS_ALLOWED = ['model', 'watson-token']; //, 'X-Watson-Learning-Opt
4949
* @param {Boolean} [options.timestamps=false] - include timestamps with results. Defaults to true when in objectMode.
5050
* @param {Number} [options.max_alternatives=1] - maximum number of alternative transcriptions to include. Defaults to 3 when in objectMode.
5151
* @param {Number} [options.inactivity_timeout=30] - how many seconds of silence before automatically closing the stream (even if continuous is true). use -1 for infinity
52-
* @param {Boolean} [options.objectMode=false] - emit `result` objects instead of string Buffers for the `data` events. Changes several other defaults. (Effectively readableObjectMode
52+
* @param {Boolean} [options.readableObjectMode=false] - emit `result` objects instead of string Buffers for the `data` events. Changes several other defaults.
53+
* @param {Number} [options.X-WDC-PL-OPT-OUT=0] set to 1 to opt-out of allowing Watson to use this request to improve it's services
5354
*
5455
* //todo: investigate other options at http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/#!/speech-to-text/recognizeSessionless
5556
*

text-to-speech/get-voices.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
require('whatwg-fetch'); // pollyfill - most supported browsers have this built-in
3+
4+
module.exports = function getVoices(options) {
5+
if (!options || !options.token) {
6+
throw new Error("Watson TextToSpeech: missing required parameter: options.token");
7+
}
8+
var reqOpts = {
9+
credentials: 'omit',
10+
headers: {
11+
'accept': 'application/json'
12+
}
13+
};
14+
return fetch('https://stream.watsonplatform.net/text-to-speech/api/v1/voices?watson-token=' + options.token, reqOpts)
15+
.then(function(response){
16+
return response.json();
17+
}).then(function(obj) {
18+
return obj.voices;
19+
});
20+
};

text-to-speech/index.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
"use strict";
2-
var qs = require('../util/querystring.js');
32

4-
module.exports.synthesize = function(options) {
5-
if (!options || !options.token) {
6-
throw new Error("Watson TextToSpeech: missing required parameter: options.token");
7-
}
8-
options['watson-token'] = options.token;
9-
delete options.token;
10-
var audio = new Audio();
11-
audio.crossOrigin = true;
12-
audio.src = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?' + qs.stringify(options);
13-
audio.play();
14-
return audio;
15-
};
3+
exports.synthesize = require('./synthesize');
4+
5+
exports.getVoices = require('./get-voices');

text-to-speech/synthesize.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
var qs = require('../util/querystring.js');
3+
4+
/**
5+
* voice=en-US_AllisonVoice
6+
text=Conscious%20of%20its%20spiritual%20and%20moral%20heritage%2C%20the%20Union%20is%20founded%20on%20the%20indivisible%2C%20universal%20values%20of%20human%20dignity%2C%20freedom%2C%20equality%20and%20solidarity%3B%20it%20is%20based%20on%20the%20principles%20of%20democracy%20and%20the%20rule%20of%20law.%20It%20places%20the%20individual%20at%20the%20heart%20of%20its%20activities%2C%20by%20establishing%20the%20citizenship%20of%20the%20Union%20and%20by%20creating%20an%20area%20of%20freedom%2C%20security%20and%20justice.
7+
=0
8+
* @param options
9+
* @param options.token auth token
10+
* @param options.text text ty speak
11+
* @param [options.voice=en-US_MichaelVoice] what voice to use - call TextToSpeech.getVoices() for a complete list.
12+
* @param [options.X-WDC-PL-OPT-OUT=0] set to 1 to opt-out of allowing Watson to use this request to improve it's services
13+
* @returns {Audio}
14+
*/
15+
module.exports = function synthesize(options) {
16+
if (!options || !options.token) {
17+
throw new Error("Watson TextToSpeech: missing required parameter: options.token");
18+
}
19+
options['watson-token'] = options.token;
20+
delete options.token;
21+
var audio = new Audio();
22+
audio.crossOrigin = true;
23+
audio.src = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?' + qs.stringify(options);
24+
audio.play();
25+
return audio;
26+
};
27+

0 commit comments

Comments
 (0)