Skip to content

Commit

Permalink
added getVoices method, example
Browse files Browse the repository at this point in the history
  • Loading branch information
nfriedly committed Feb 16, 2016
1 parent dffe1d3 commit 7930380
Show file tree
Hide file tree
Showing 8 changed files with 549 additions and 34 deletions.
469 changes: 451 additions & 18 deletions dist/watson-speech.js

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions examples/public/text-to-speech-custom-voice.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Custom Voice - Watson Text to Speech</title>
</head>
<body>

<section>
<h2>Text to Speech with Custom Voice Example</h2>
<textarea id="text" rows="6" cols="80">Hello from IBM Watson</textarea>
<p>Voice: <select id="voice"></select></p>
<p><button id="button">Synthesize Text</button></p>
</section>

<script src="watson-speech.js"></script>
<script src="http://code.jquery.com/jquery-2.2.0.min.js"></script>

<h2>Code for this demo:</h2>
<pre><code><script style="display: block;">
$(function() {
$.get('/api/text-to-speech/token').then(function (token) {
WatsonSpeech.TextToSpeech.getVoices({token: token}).then(function (voices) {
$('#voice').html(voices.map(function(v) {
return '<option value="' + v.name + '"' + (v.name == 'en-GB_KateVoice' ? ' selected' : '') + '>' + v.name + '</option>';
}).join('\n'));
}).catch(console.error.bind(console));
});

$('#button').click(function () {
$.get('/api/text-to-speech/token').then(function (token) {
WatsonSpeech.TextToSpeech.synthesize({
text: $('#text').val(),
voice: $('#voice').val(),
token: token
})
});
});
});
</script></code></pre>
</body>
</html>
3 changes: 2 additions & 1 deletion examples/public/text-to-speech.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

<section>
<h2>Basic Text to Speech Example</h2>
<input id="text" value="Hello from IBM Watson"/> <button id="button">Synthesize Text</button>
<textarea id="text" rows="6" cols="80">Hello from IBM Watson</textarea>
<p><button id="button">Synthesize Text</button></p>
</section>

<script src="watson-speech.js"></script>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"object.assign": "^4.0.3",
"object.pick": "^1.1.1",
"readable-blob-stream": "^1.1.0",
"websocket": "^1.0.22"
"websocket": "^1.0.22",
"whatwg-fetch": "^0.11.0"
},
"repository": {
"type": "git",
Expand Down
3 changes: 2 additions & 1 deletion speech-to-text/recognize-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ var QUERY_PARAMS_ALLOWED = ['model', 'watson-token']; //, 'X-Watson-Learning-Opt
* @param {Boolean} [options.timestamps=false] - include timestamps with results. Defaults to true when in objectMode.
* @param {Number} [options.max_alternatives=1] - maximum number of alternative transcriptions to include. Defaults to 3 when in objectMode.
* @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
* @param {Boolean} [options.objectMode=false] - emit `result` objects instead of string Buffers for the `data` events. Changes several other defaults. (Effectively readableObjectMode
* @param {Boolean} [options.readableObjectMode=false] - emit `result` objects instead of string Buffers for the `data` events. Changes several other defaults.
* @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
*
* //todo: investigate other options at http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/#!/speech-to-text/recognizeSessionless
*
Expand Down
20 changes: 20 additions & 0 deletions text-to-speech/get-voices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";
require('whatwg-fetch'); // pollyfill - most supported browsers have this built-in

module.exports = function getVoices(options) {
if (!options || !options.token) {
throw new Error("Watson TextToSpeech: missing required parameter: options.token");
}
var reqOpts = {
credentials: 'omit',
headers: {
'accept': 'application/json'
}
};
return fetch('https://stream.watsonplatform.net/text-to-speech/api/v1/voices?watson-token=' + options.token, reqOpts)
.then(function(response){
return response.json();
}).then(function(obj) {
return obj.voices;
});
};
16 changes: 3 additions & 13 deletions text-to-speech/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
"use strict";
var qs = require('../util/querystring.js');

module.exports.synthesize = function(options) {
if (!options || !options.token) {
throw new Error("Watson TextToSpeech: missing required parameter: options.token");
}
options['watson-token'] = options.token;
delete options.token;
var audio = new Audio();
audio.crossOrigin = true;
audio.src = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?' + qs.stringify(options);
audio.play();
return audio;
};
exports.synthesize = require('./synthesize');

exports.getVoices = require('./get-voices');
27 changes: 27 additions & 0 deletions text-to-speech/synthesize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
var qs = require('../util/querystring.js');

/**
* voice=en-US_AllisonVoice
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.
=0
* @param options
* @param options.token auth token
* @param options.text text ty speak
* @param [options.voice=en-US_MichaelVoice] what voice to use - call TextToSpeech.getVoices() for a complete list.
* @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
* @returns {Audio}
*/
module.exports = function synthesize(options) {
if (!options || !options.token) {
throw new Error("Watson TextToSpeech: missing required parameter: options.token");
}
options['watson-token'] = options.token;
delete options.token;
var audio = new Audio();
audio.crossOrigin = true;
audio.src = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?' + qs.stringify(options);
audio.play();
return audio;
};

0 comments on commit 7930380

Please sign in to comment.