Skip to content

Commit 36c7049

Browse files
committed
non-working tts
1 parent 548fbce commit 36c7049

File tree

9 files changed

+132
-18
lines changed

9 files changed

+132
-18
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ node_modules/
33
npm-debug.log.*
44
.DS_store
55
Thumbs.db
6-
test/resources/auth.json
6+
test/resources/stt-auth.json
7+
test/resources/tts-auth.json
78
doc/
89
*-auth.json

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ For use with `.recognizeBlob({play: true})` - slows the results down to match th
138138
## todo
139139

140140
* Solidify API
141-
* add text-to-speech support - may require service improvements
141+
* support objectMode instead of having random events
142142
* add an example that includes alternatives and word confidence scores
143143
* enable eslint - https://github.ibm.com/fed/javascript-style-guides
144144
* break components into standalone npm modules where it makes sense

dist/watson-speech.js

Lines changed: 40 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"express": "^4.13.3",
1111
"vcap_services": "^0.1.7",
12-
"watson-developer-cloud": "^1.2.0"
12+
"watson-developer-cloud": "^1.2.0",
13+
"watson-speech": "*"
1314
}
1415
}

examples/public/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<title>Watson Speech to Text client example</title>
66
</head>
77
<body>
8+
<h1>IBM Watson Speech JavaScript SDK Examples</h1>
9+
<h2>Speech to Text</h2>
810
<ul>
911
<li><a href="microphone-streaming.html">Transcribe from Microphone, Streaming</a></li>
1012
<li><a href="microphone-streaming-auto-stop.html">Transcribe from Microphone, Streaming, automatically stop at first pause</a></li>
@@ -15,5 +17,9 @@
1517
<li><a href="audio-element-programmatic.html">Transcribe from <code>new Audio()</code>, Streaming</a></li>
1618
</ul>
1719

20+
<h2>Text to Speech</h2>
21+
<ul>
22+
<li><a href="text-to-speech.html">Basic Example</a></li>
23+
</ul>
1824
</body>
1925
</html>

examples/public/text-to-speech.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Basic Example - Watson Text to Speech</title>
6+
</head>
7+
<body>
8+
9+
<section>
10+
<h2>Basic Text to Speech Example</h2>
11+
<input id="text" value="Hello from IBM Watson"/> <button id="button">Synthesize Text</button>
12+
</section>
13+
14+
<script src="watson-speech.js"></script>
15+
<script src="http://code.jquery.com/jquery-2.2.0.min.js"></script>
16+
17+
<h2>Code for this demo:</h2>
18+
<pre><code><script style="display: block;">
19+
$(function() {
20+
$('#button').click(function () {
21+
$.get('/api/text-to-speech/token').then(function (token) {
22+
WatsonSpeech.TextToSpeech.synthesize({
23+
text: $('#text').val(),
24+
token: token
25+
})
26+
});
27+
});
28+
});
29+
</script></code></pre>
30+
</body>
31+
</html>

examples/token-server.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,25 @@ app.use(express.static(__dirname + '/public'));
2626
app.use(express.static(__dirname + '/../dist')); // normally these files would also go into public/ but this way the example always has the latest code
2727

2828

29+
// set up an endpoint to serve speech-to-text auth tokens
30+
2931
// For local development, replace username and password
30-
var config = extend({
32+
var sttConfig = extend({
3133
version: 'v1',
3234
url: 'https://stream.watsonplatform.net/speech-to-text/api',
3335
username: '<username>',
3436
password: '<password>'
3537
}, vcapServices.getCredentials('speech_to_text'));
3638

3739
// quick hack to make development easier
38-
try { extend(config, require('../test/resources/stt-auth.json')) } catch (ex) {}
39-
40-
var authService = watson.authorization(config);
40+
try { extend(sttConfig, require('../test/resources/stt-auth.json')) } catch (ex) {}
4141

42+
var sttAuthService = watson.authorization(sttConfig);
4243

4344
// Get token using your credentials
44-
// **Warning**: this endpoint should be guarded with additional authentication & authorization for production use
45-
app.get('/token', function(req, res, next) {
46-
authService.getToken({url: config.url}, function(err, token) {
45+
// **Warning**: these endpoints should be guarded with additional authentication & authorization for production use
46+
app.get('/api/speech-to-text/token', function(req, res) {
47+
sttAuthService.getToken({url: sttConfig.url}, function(err, token) {
4748
if (err) {
4849
console.log('Error retrieving token: ', err);
4950
return res.status(500).send('Error retrieving token')
@@ -52,12 +53,35 @@ app.get('/token', function(req, res, next) {
5253
});
5354
});
5455

56+
// and, do it all again for the text to speech service
57+
var ttsConfig = extend({
58+
version: 'v1',
59+
url: 'https://stream.watsonplatform.net/text-to-speech/api',
60+
username: '<username>',
61+
password: '<password>'
62+
}, vcapServices.getCredentials('text_to_speech'));
63+
64+
// quick hack to make development easier
65+
try { extend(ttsConfig, require('../test/resources/tts-auth.json')) } catch (ex) {}
66+
67+
var ttsAuthService = watson.authorization(ttsConfig);
68+
69+
app.get('/api/text-to-speech/token', function(req, res) {
70+
ttsAuthService.getToken({url: ttsConfig.url}, function(err, token) {
71+
if (err) {
72+
console.log('Error retrieving token: ', err);
73+
return res.status(500).send('Error retrieving token')
74+
}
75+
res.send(token);
76+
});
77+
});
78+
5579
var port = process.env.VCAP_APP_PORT || 3000;
5680
app.listen(port, function() {
57-
console.log('Example IBM Watson STT client app & server live at http://localhost:%s/', port);
81+
console.log('Example IBM Watson Speech JS SDK client app & token server live at http://localhost:%s/', port);
5882
});
5983

60-
// chrome requires https to access the user's mic
84+
// chrome requires https to access the user's mic unless it's a localhost url
6185
if (!process.env.VCAP_APP_PORT) {
6286
var fs = require("fs"),
6387
https = require("https"),
@@ -68,6 +92,6 @@ if (!process.env.VCAP_APP_PORT) {
6892
cert: fs.readFileSync(__dirname + '/keys/localhost.cert')
6993
};
7094
https.createServer(options, app).listen(HTTPS_PORT, function () {
71-
console.log('Secure example IBM Watson STT client app & server live at https://localhost:%s/', port)
95+
console.log('Secure server live at https://localhost:%s/', port)
7296
});
7397
}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ exports.version = process.env.TRAVIS_BRANCH;
1818
*/
1919
exports.SpeechToText = require('./speech-to-text');
2020

21-
// todo: add text-to-speech
21+
exports.TextToSpeech = require('./text-to-speech');

text-to-speech/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
var qs = require('querystring');
3+
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/synthesize?' + qs.stringify(options);
13+
audio.play();
14+
return audio;
15+
};

0 commit comments

Comments
 (0)