meSpeak.jsJavaScript 的 TTS 库
meSpeak.js (speak.js 增强版) 是一个 100% 纯客户端 JavaScript 实现的 TTS 库,基于 speak.js 开发,在其基础上增加对 Webkit 和 Safari 浏览器的支持,引入了可加载的语音模块。
使用方法:
meSpeak.loadConfig("mespeak_config.json");
meSpeak.loadVoice('en-us.json');
meSpeak.speak('hello world');
meSpeak.speak('hello world', { option1: value1, option2: value2 .. });
meSpeak.speak('hello world', { option1: value1, option2: value2 .. }, myCallback);
var id = meSpeak.speak('hello world');
meSpeak.stop(id);
meSpeak.speak( text [, { option1: value1, option2: value2 .. } [, callback ]] );
text: The string of text to be spoken.
The text may contain line-breaks ("\n") and special characters.
Default text-encoding is UTF-8 (see the option "utf16" for other).
options (eSpeak command-options):
* amplitude: How loud the voice will be (default: 100)
* pitch: The voice pitch (default: 50)
* speed: The speed at which to talk (words per minute) (default: 175)
* voice: Which voice to use (default: last voice loaded or defaultVoice, see below)
* wordgap: Additional gap between words in 10 ms units (default: 0)
* variant: One of the variants to be found in the eSpeak-directory "~/espeak-data/voices/!v"
Variants add some effects to the normally plain voice, e.g. notably a female tone.
Valid values are:
"f1", "f2", "f3", "f4", "f5" for female voices
"m1", "m2", "m3", "m4", "m5", "m6, "m7" for male voices
"croak", "klatt", "klatt2", "klatt3", "whisper", "whisperf" for other effects.
(Using eSpeak, these would be appended to the "-v" option by "+" and the value.)
Note: Try "f2" or "f5" for a female voice.
* linebreak: (Number) Line-break length, default value: 0.
* capitals: (Number) Indicate words which begin with capital letters.
1: Use a click sound to indicate when a word starts with a capital letter,
or double click if word is all capitals.
2: Speak the word "capital" before a word which begins with a capital letter.
Other values: Increases the pitch for words which begin with a capital letter.
The greater the value, the greater the increase in pitch. (eg.: 20)
* punct: (Boolean or String) Speaks the names of punctuation characters when they are encountered
in the text. If a string of characters is supplied, then only those listed punctuation
characters are spoken, eg. { "punct": ".,;?" }.
* nostop: (Boolean) Removes the end-of-sentence pause which normally occurs at the end of the text.
* utf16: (Boolean) Indicates that the input is UTF-16, default: UTF-8.
* ssml: (Boolean) Indicates that the text contains SSML (Speech Synthesis Markup Language)
tags or other XML tags. (A small set of HTML is supported too.)
further options (meSpeak.js specific):
* volume: Volume relative to the global volume (number, 0..1, default: 1)
Note: the relative volume has no effect on the export using option 'rawdata'.
* rawdata: Do not play, return data only.
The type of the returned data is derived from the value (case-insensitive) of 'rawdata':
- 'base64': returns a base64-encoded string.
- 'mime': returns a base64-encoded data-url (including the MIME-header).
(synonyms: 'data-url', 'data-uri', 'dataurl', 'datauri')
- 'array': returns a plain Array object with uint 8 bit data.
- default (any other value): returns the generated wav-file as an ArrayBuffer (8-bit unsigned).
Note: The value of 'rawdata' must evaluate to boolean 'true' in order to be recognized.
* log: (Boolean) Logs the compiled eSpeak-command to the JS-console.
callback: An optional callback function to be called after the sound output ended.
The callback will be called with a single boolean argument indicating success.
If the resulting sound is stopped by meSpeak.stop(), the success-flag will be set to false.
Returns:
* if called with option rawdata: a stream in the requested format
(or null, if the required resources have not loaded yet).
* default: a 32bit integer ID greater than 0 (or 0 on failure).
The ID may be used to stop this sound by calling meSpeak.stop(<id>).
if (meSpeak.isVoiceLoaded('de')) meSpeak.setDefaultVoice('de');
// note: the default voice is always the the last voice loaded
meSpeak.loadVoice('fr.json', userCallback);
// userCallback is an optional callback-handler. The callback will receive two arguments:
// * a boolean flag for success
// * either the id of the voice, or a reason for errors ('network error', 'data error', 'file error')
alert(meSpeak.getDefaultVoice()); // 'fr'
if (meSpeak.isConfigLoaded()) meSpeak.speak('Configuration data has been loaded.');
// note: any calls to speak() will be deferred, if no valid config-data has been loaded yet.
meSpeak.setVolume(0.5);
meSpeak.setVolume( volume [, id-list] );
Sets a volume level (0 <= v <= 1)
* if called with a single argument, the method sets the global playback-volume, any sounds currently
playing will be updated immediately with respect to their relative volume (if specified).
* if called with more than a single argument, the method will set and adjust the relative volume of
the sound(s) with corresponding ID(s).
Returns: the volume provided.
alert(meSpeak.getVolume()); // 0.5
meSpeak.getVolume( [id] );
Returns a volume level (0 <= v <= 1)
* if called without an argument, the method returns the global playback-volume.
* if called with an argument, the method will return the relative volume of the sound with the ID
corresponding to the first argument.
if no sound with a corresponding ID is found, the method will return 'undefined'.
var browserCanPlayWavFiles = meSpeak.canPlay(); // test for compatibility
// export speech-data as a stream (no playback):
var myUint8Array = meSpeak.speak('hello world', { 'rawdata': true }); // typed array
var base64String = meSpeak.speak('hello world', { 'rawdata': 'base64' });
var myDataUrl = meSpeak.speak('hello world', { 'rawdata': 'data-url' });
var myArray = meSpeak.speak('hello world', { 'rawdata': 'array' }); // simple array
// playing cached streams (any of the export formats):
meSpeak.play( stream [, relativeVolume [, callback]] );
var stream1 = meSpeak.speak('hello world', { 'rawdata': true });
var stream2 = meSpeak.speak('hello again', { 'rawdata': true });
var stream3 = meSpeak.speak('hello yet again', { 'rawdata': 'data-url' });
meSpeak.play(stream1); // using global volume
meSpeak.play(stream2, 0.75); // 75% of global volume
meSpeak.play(stream3); // v.1.4.2: play data-urls or base64-encoded
var id = meSpeak.play(stream1);
meSpeak.stop(id);
Arguments:
stream: A stream in any of the formats returned by meSpeak.play() with the "rawdata"-option.
volume: (optional) Volume relative to the global volume (number, 0..1, default: 1)
callback: (optional) A callback function to be called after the sound output ended.
The callback will be called with a single boolean argument indicating success.
If the sound is stopped by meSpeak.stop(), the success-flag will be set to false.
(See also: meSpeak.speak().)
Returns: A 32bit integer ID greater than 0 (or 0 on failure).
The ID may be used to stop this sound by calling meSpeak.stop(<id>).
meSpeak.stop( [<id-list>] );
Stops the sound(s) specified by the id-list.
If called without an argument, all sounds currently playing, processed, or queued are stopped.
Any callback(s) associated to the sound(s) will return false as the success-flag.
Arguments:
id-list: Any number of IDs returned by a call to meSpeak.speak() or meSpeak.play().
Returns:
The number (integer) of sounds actually stopped.评论
