Skip to content

Commit 9b68353

Browse files
committed
adding autoPlay option to .synthesize(), filtering querystring params
1 parent 849b34d commit 9b68353

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ where the max length is around 1000 characters after the token is accounted for.
4040
Options:
4141
* text - the text to transcribe // todo: list supported languages
4242
* voice - the desired playback voice's name - see .getVoices(). Note that the voices are language-specific.
43-
* todo: autoPlay option that can be set to false to preload audio
43+
* autoPlay - set to false to prevent the audio from automatically playing
4444

4545
### `.getVoices()` -> Promise
4646

@@ -155,6 +155,10 @@ Accepts input from `RecognizeStream()` and friends, writes text to supplied `out
155155

156156
## Changelog
157157

158+
### v0.12
159+
* Added `autoPlay` option to `synthesize()`
160+
* Added proper parameter filtering to `synthesize()`
161+
158162
### v0.11
159163
* renamed `recognizeBlob` to `recognizeFile` to make the primary usage more apparent
160164
* Added support for `<input>` and `<textarea>` elements when using the `targetElement` option (or a `WritableElementStream`)
@@ -201,3 +205,4 @@ Accepts input from `RecognizeStream()` and friends, writes text to supplied `out
201205
* consider moving STT core to standalone module
202206
* look for bug where single-word final results may omit word confidence (possibly due to FormatStream?)
203207
* fix bug where TimingStream shows words slightly before they're spoken
208+
* automatically turn on objectMode when required by other options (timing, confidence, etc.

text-to-speech/synthesize.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* limitations under the License.
1515
*/
1616
"use strict";
17+
var pick = require('object.pick');
1718
var qs = require('../util/querystring.js');
1819

20+
var QUERY_PARAMS_ALLOWED = ['voice', 'X-WDC-PL-OPT-OUT', 'text', 'watson-token'];
21+
1922
/**
2023
* @module watson-speech/text-to-speech/synthesize
2124
*/
@@ -30,6 +33,7 @@ var qs = require('../util/querystring.js');
3033
* @param {String} options.text text to speak
3134
* @param {String} [options.voice=en-US_MichaelVoice] what voice to use - call getVoices() for a complete list.
3235
* @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
36+
* @param {Boolean} [options.autoPlay=true] automatically play the audio
3337
* @returns {Audio}
3438
* @see module:watson-speech/text-to-speech/get-voices
3539
*/
@@ -41,8 +45,10 @@ module.exports = function synthesize(options) {
4145
delete options.token;
4246
var audio = new Audio();
4347
audio.crossOrigin = true;
44-
audio.src = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?' + qs.stringify(options);
45-
audio.play();
48+
audio.src = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?' + qs.stringify(pick(options, QUERY_PARAMS_ALLOWED));
49+
if (options.autoPlay !== false) {
50+
audio.play();
51+
}
4652
return audio;
4753
};
4854

0 commit comments

Comments
 (0)