Skip to content

Commit 4c6db73

Browse files
committed
basic text-to-speech support
1 parent 36c7049 commit 4c6db73

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
IBM Watson Speech To Text Browser Client Library
2-
================================================
1+
IBM Watson Speech Services for Web Browsers
2+
===========================================
33

44
[![Build Status](https://travis-ci.org/watson-developer-cloud/speech-javascript-sdk.svg?branch=master)](https://travis-ci.org/watson-developer-cloud/speech-javascript-sdk)
55

6-
Allows you to easily add voice recognition to any web app with minimal code.
6+
Allows you to easily add voice recognition and synthesis to any web app with minimal code.
77

88
**Warning** This library is still early-stage and may see significant breaking changes.
99

@@ -21,13 +21,27 @@ See several examples at https://github.com/watson-developer-cloud/speech-javascr
2121
This library is built with [browserify](http://browserify.org/) and easy to use in browserify-based projects (`npm install --save watson-speech`), but you can also grab the compiled bundle from the
2222
`dist/` folder and use it as a standalone library.
2323

24-
## `WatsonSpeech.SpeechToText` Basic API
24+
Basic API
25+
---------
2526

2627
Complete API docs should be published at http://watson-developer-cloud.github.io/speech-javascript-sdk/
2728

2829
All API methods require an auth token that must be [generated server-side](https://github.com/watson-developer-cloud/node-sdk#authorization).
2930
(Snp teee examples/token-server.js for a basic example.)
3031

32+
## `Watson.TextToSpeech`
33+
34+
### '.synthesize({text, token})`
35+
36+
Speaks the supplied text through an automatically-created `<audio>` element.
37+
Currently limited to text that can fit within a GET URL (this is particularly an issue on [Internet Explorer before Windows 10](http://stackoverflow.com/questions/32267442/url-length-limitation-of-microsoft-edge)
38+
where the max length is around 1000 characters after the token is accounted for.)
39+
40+
Options:
41+
* voice
42+
43+
## `WatsonSpeech.SpeechToText`
44+
3145

3246
### `.recognizeMicrophone({token})` -> `RecognizeStream`
3347

dist/watson-speech.js

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "watson-speech",
33
"version": "0.8.3",
4-
"description": "IBM Watson Speech to Text SDK for web browsers.",
4+
"description": "IBM Watson Speech to Text and Text to Speech SDK for web browsers.",
55
"main": "index.js",
66
"scripts": {
77
"watch-test": "karma start test/resources/karma.conf.js",

speech-to-text/recognize-stream.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var pick = require('object.pick');
2323
var W3CWebSocket = require('websocket').w3cwebsocket;
2424
var contentType = require('./content-type');
2525
var defaults = require('defaults');
26+
var qs = require('../util/querystring.js');
2627

2728

2829
var OPENING_MESSAGE_PARAMS_ALLOWED = ['continuous', 'max_alternatives', 'timestamps', 'word_confidence', 'inactivity_timeout',
@@ -97,10 +98,7 @@ RecognizeStream.prototype.initialize = function () {
9798
}
9899

99100
var queryParams = util._extend({model: 'en-US_BroadbandModel'}, pick(options, QUERY_PARAMS_ALLOWED));
100-
var queryString = Object.keys(queryParams).map(function (key) {
101-
return key + '=' + (key == 'watson-token' ? queryParams[key] : encodeURIComponent(queryParams[key])); // the server chokes if the token is correctly url-encoded
102-
}).join('&');
103-
101+
var queryString = qs.stringify(queryParams);
104102
var url = (options.url || "wss://stream.watsonplatform.net/speech-to-text/api").replace(/^http/, 'ws') + '/v1/recognize?' + queryString;
105103

106104
// turn off all the extras if we're just outputting text

text-to-speech/index.js

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

44
module.exports.synthesize = function(options) {
55
if (!options || !options.token) {
@@ -9,7 +9,7 @@ module.exports.synthesize = function(options) {
99
delete options.token;
1010
var audio = new Audio();
1111
audio.crossOrigin = true;
12-
audio.src = 'https://stream.watsonplatform.net/text-to-speech/api/synthesize?' + qs.stringify(options);
12+
audio.src = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?' + qs.stringify(options);
1313
audio.play();
1414
return audio;
1515
};

util/querystring.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
3+
/**
4+
* Stringify query params, Watson-style
5+
*
6+
* Why? The server that processes auth tokens currently only accepts the *exact* string, even if it's invalid for a URL.
7+
* Properly url-encoding percent characters causes it to reject the token.
8+
* So, this is a custom qs.stringify function that properly encodes everything except watson-token, passing it along verbatim
9+
*
10+
* @param {Object} queryParams
11+
* @return {String}
12+
*/
13+
exports.stringify = function stringify(queryParams) {
14+
return Object.keys(queryParams).map(function (key) {
15+
return key + '=' + (key == 'watson-token' ? queryParams[key] : encodeURIComponent(queryParams[key])); // the server chokes if the token is correctly url-encoded
16+
}).join('&');
17+
};

0 commit comments

Comments
 (0)