1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Base class for Text-to-Speech engines that actually transform
* text to speech.
*
*/
goog.provide('cvox.AbstractTts');
goog.require('cvox.TtsInterface');
goog.require('goog.i18n.MessageFormat');
/**
* Creates a new instance.
* @constructor
* @implements {cvox.TtsInterface}
*/
cvox.AbstractTts = function() {
this.ttsProperties = new Object();
/**
* Default value for TTS properties.
* Note that these as well as the subsequent properties might be different
* on different host platforms (like Chrome, Android, etc.).
* @type {{pitch : number,
* rate: number,
* volume: number}}
* @protected
*/
this.propertyDefault = {
'rate': 0.5,
'pitch': 0.5,
'volume': 0.5
};
/**
* Min value for TTS properties.
* @type {{pitch : number,
* rate: number,
* volume: number}}
* @protected
*/
this.propertyMin = {
'rate': 0.0,
'pitch': 0.0,
'volume': 0.0
};
/**
* Max value for TTS properties.
* @type {{pitch : number,
* rate: number,
* volume: number}}
* @protected
*/
this.propertyMax = {
'rate': 1.0,
'pitch': 1.0,
'volume': 1.0
};
/**
* Step value for TTS properties.
* @type {{pitch : number,
* rate: number,
* volume: number}}
* @protected
*/
this.propertyStep = {
'rate': 0.1,
'pitch': 0.1,
'volume': 0.1
};
/** @private */
if (cvox.AbstractTts.pronunciationDictionaryRegexp_ == undefined) {
// Create an expression that matches all words in the pronunciation
// dictionary on word boundaries, ignoring case.
var words = [];
for (var word in cvox.AbstractTts.PRONUNCIATION_DICTIONARY) {
words.push(word);
}
var expr = '\\b(' + words.join('|') + ')\\b';
cvox.AbstractTts.pronunciationDictionaryRegexp_ = new RegExp(expr, 'ig');
}
if (cvox.AbstractTts.substitutionDictionaryRegexp_ == undefined) {
// Create an expression that matches all words in the substitution
// dictionary.
var symbols = [];
for (var symbol in cvox.AbstractTts.SUBSTITUTION_DICTIONARY) {
symbols.push(symbol);
}
var expr = '(' + symbols.join('|') + ')';
cvox.AbstractTts.substitutionDictionaryRegexp_ = new RegExp(expr, 'ig');
}
};
/**
* Default TTS properties for this TTS engine.
* @type {Object}
* @protected
*/
cvox.AbstractTts.prototype.ttsProperties;
/** @override */
cvox.AbstractTts.prototype.speak = function(textString, queueMode, properties) {
return this;
};
/** @override */
cvox.AbstractTts.prototype.isSpeaking = function() {
return false;
};
/** @override */
cvox.AbstractTts.prototype.stop = function() {
};
/** @override */
cvox.AbstractTts.prototype.addCapturingEventListener = function(listener) { };
/** @override */
cvox.AbstractTts.prototype.increaseOrDecreaseProperty =
function(propertyName, increase) {
var min = this.propertyMin[propertyName];
var max = this.propertyMax[propertyName];
var step = this.propertyStep[propertyName];
var current = this.ttsProperties[propertyName];
current = increase ? current + step : current - step;
this.ttsProperties[propertyName] = Math.max(Math.min(current, max), min);
};
/**
* Merges the given properties with the default ones. Always returns a
* new object, so that you can safely modify the result of mergeProperties
* without worrying that you're modifying an object used elsewhere.
* @param {Object=} properties The properties to merge with the current ones.
* @return {Object} The merged properties.
* @protected
*/
cvox.AbstractTts.prototype.mergeProperties = function(properties) {
var mergedProperties = new Object();
var p;
if (this.ttsProperties) {
for (p in this.ttsProperties) {
mergedProperties[p] = this.ttsProperties[p];
}
}
if (properties) {
var tts = cvox.AbstractTts;
if (typeof(properties[tts.VOLUME]) == 'number') {
mergedProperties[tts.VOLUME] = properties[tts.VOLUME];
}
if (typeof(properties[tts.PITCH]) == 'number') {
mergedProperties[tts.PITCH] = properties[tts.PITCH];
}
if (typeof(properties[tts.RATE]) == 'number') {
mergedProperties[tts.RATE] = properties[tts.RATE];
}
if (typeof(properties[tts.LANG]) == 'string') {
mergedProperties[tts.LANG] = properties[tts.LANG];
}
var context = this;
var mergeRelativeProperty = function(abs, rel) {
if (typeof(properties[rel]) == 'number' &&
typeof(mergedProperties[abs]) == 'number') {
mergedProperties[abs] += properties[rel];
var min = context.propertyMin[abs];
var max = context.propertyMax[abs];
if (mergedProperties[abs] > max) {
mergedProperties[abs] = max;
} else if (mergedProperties[abs] < min) {
mergedProperties[abs] = min;
}
}
};
mergeRelativeProperty(tts.VOLUME, tts.RELATIVE_VOLUME);
mergeRelativeProperty(tts.PITCH, tts.RELATIVE_PITCH);
mergeRelativeProperty(tts.RATE, tts.RELATIVE_RATE);
}
for (p in properties) {
if (!mergedProperties.hasOwnProperty(p)) {
mergedProperties[p] = properties[p];
}
}
return mergedProperties;
};
/**
* Method to preprocess text to be spoken properly by a speech
* engine.
*
* 1. Replace any single character with a description of that character.
*
* 2. Convert all-caps words to lowercase if they don't look like an
* acronym / abbreviation.
*
* @param {string} text A text string to be spoken.
* @param {Object= } properties Out parameter populated with how to speak the
* string.
* @return {string} The text formatted in a way that will sound better by
* most speech engines.
* @protected
*/
cvox.AbstractTts.prototype.preprocess = function(text, properties) {
if (text.length == 1 && text >= 'A' && text <= 'Z') {
for (var prop in cvox.AbstractTts.PERSONALITY_CAPITAL)
properties[prop] = cvox.AbstractTts.PERSONALITY_CAPITAL[prop];
}
// Substitute all symbols in the substitution dictionary. This is pretty
// efficient because we use a single regexp that matches all symbols
// simultaneously.
text = text.replace(
cvox.AbstractTts.substitutionDictionaryRegexp_,
function(symbol) {
return ' ' + cvox.AbstractTts.SUBSTITUTION_DICTIONARY[symbol] + ' ';
});
// Handle single characters that we want to make sure we pronounce.
if (text.length == 1) {
return cvox.AbstractTts.CHARACTER_DICTIONARY[text] ?
(new goog.i18n.MessageFormat(cvox.ChromeVox.msgs.getMsg(
cvox.AbstractTts.CHARACTER_DICTIONARY[text])))
.format({'COUNT': 1}) :
text.toUpperCase();
}
// Substitute all words in the pronunciation dictionary. This is pretty
// efficient because we use a single regexp that matches all words
// simultaneously, and it calls a function with each match, which we can
// use to look up the replacement in our dictionary.
text = text.replace(
cvox.AbstractTts.pronunciationDictionaryRegexp_,
function(word) {
return cvox.AbstractTts.PRONUNCIATION_DICTIONARY[word.toLowerCase()];
});
// Special case for google+, where the punctuation must be pronounced.
text = text.replace(/google\+/ig, 'google plus');
// Expand all repeated characters.
text = text.replace(
cvox.AbstractTts.repetitionRegexp_, cvox.AbstractTts.repetitionReplace_);
// If there's no lower case letters, and at least two spaces, skip spacing
// text.
var skipSpacing = false;
if (!text.match(/[a-z]+/) && text.indexOf(' ') != text.lastIndexOf(' ')) {
skipSpacing = true;
}
// Convert all-caps words to lowercase if they don't look like acronyms,
// otherwise add a space before all-caps words so that all-caps words in
// the middle of camelCase will be separated.
text = text.replace(/[A-Z]+/g, function(word) {
// If a word contains vowels and is more than 3 letters long, it is
// probably a real word and not just an abbreviation. Convert it to lower
// case and speak it normally.
if ((word.length > 3) && word.match(/([AEIOUY])/g)) {
return word.toLowerCase();
} else if (!skipSpacing) {
// Builds spaced-out camelCased/all CAPS words so they sound better when
// spoken by TTS engines.
return ' ' + word.split('').join(' ');
} else {
return word;
}
});
return text;
};
/** TTS rate property. @type {string} */
cvox.AbstractTts.RATE = 'rate';
/** TTS pitch property. @type {string} */
cvox.AbstractTts.PITCH = 'pitch';
/** TTS volume property. @type {string} */
cvox.AbstractTts.VOLUME = 'volume';
/** TTS language property. @type {string} */
cvox.AbstractTts.LANG = 'lang';
/** TTS relative rate property. @type {string} */
cvox.AbstractTts.RELATIVE_RATE = 'relativeRate';
/** TTS relative pitch property. @type {string} */
cvox.AbstractTts.RELATIVE_PITCH = 'relativePitch';
/** TTS relative volume property. @type {string} */
cvox.AbstractTts.RELATIVE_VOLUME = 'relativeVolume';
/** TTS color property (for the lens display). @type {string} */
cvox.AbstractTts.COLOR = 'color';
/** TTS CSS font-weight property (for the lens display). @type {string} */
cvox.AbstractTts.FONT_WEIGHT = 'fontWeight';
/** TTS punctuation-echo property. @type {string} */
cvox.AbstractTts.PUNCTUATION_ECHO = 'punctuationEcho';
/** TTS pause property. @type {string} */
cvox.AbstractTts.PAUSE = 'pause';
/**
* TTS personality for annotations - text spoken by ChromeVox that
* elaborates on a user interface element but isn't displayed on-screen.
* @type {Object}
*/
cvox.AbstractTts.PERSONALITY_ANNOTATION = {
'relativePitch': -0.25,
// TODO:(rshearer) Added this color change for I/O presentation.
'color': 'yellow',
'punctuationEcho': 'none'
};
/**
* TTS personality for announcements - text spoken by ChromeVox that
* isn't tied to any user interface elements.
* @type {Object}
*/
cvox.AbstractTts.PERSONALITY_ANNOUNCEMENT = {
'punctuationEcho': 'none'
};
/**
* TTS personality for alerts from the system, such as battery level
* warnings.
* @type {Object}
*/
cvox.AbstractTts.PERSONALITY_SYSTEM_ALERT = {
'punctuationEcho': 'none',
'doNotInterrupt': true
};
/**
* TTS personality for an aside - text in parentheses.
* @type {Object}
*/
cvox.AbstractTts.PERSONALITY_ASIDE = {
'relativePitch': -0.1,
'color': '#669'
};
/**
* TTS personality for capital letters.
* @type {Object}
*/
cvox.AbstractTts.PERSONALITY_CAPITAL = {
'relativePitch': 0.6
};
/**
* TTS personality for deleted text.
* @type {Object}
*/
cvox.AbstractTts.PERSONALITY_DELETED = {
'punctuationEcho': 'none',
'relativePitch': -0.6
};
/**
* TTS personality for quoted text.
* @type {Object}
*/
cvox.AbstractTts.PERSONALITY_QUOTE = {
'relativePitch': 0.1,
'color': '#b6b',
'fontWeight': 'bold'
};
/**
* TTS personality for strong or bold text.
* @type {Object}
*/
cvox.AbstractTts.PERSONALITY_STRONG = {
'relativePitch': 0.1,
'color': '#b66',
'fontWeight': 'bold'
};
/**
* TTS personality for emphasis or italicized text.
* @type {Object}
*/
cvox.AbstractTts.PERSONALITY_EMPHASIS = {
'relativeVolume': 0.1,
'relativeRate': -0.1,
'color': '#6bb',
'fontWeight': 'bold'
};
/**
* Flag indicating if the TTS is being debugged.
* @type {boolean}
*/
cvox.AbstractTts.DEBUG = true;
/**
* Character dictionary. These symbols are replaced with their human readable
* equivalents. This replacement only occurs for single character utterances.
* @type {Object.<string, string>}
*/
cvox.AbstractTts.CHARACTER_DICTIONARY = {
' ': 'space',
'`': 'backtick',
'~': 'tilde',
'!': 'exclamation',
'@': 'at',
'#': 'pound',
'$': 'dollar',
'%': 'percent',
'^': 'caret',
'&': 'ampersand',
'*': 'asterisk',
'(': 'open_paren',
')': 'close_paren',
'-': 'dash',
'_': 'underscore',
'=': 'equals',
'+': 'plus',
'[': 'left_bracket',
']': 'right_bracket',
'{': 'left_brace',
'}': 'right_brace',
'|': 'pipe',
';': 'semicolon',
':': 'colon',
',': 'comma',
'.': 'dot',
'<': 'less_than',
'>': 'greater_than',
'/': 'slash',
'?': 'question_mark',
'"': 'quote',
'\'': 'apostrophe',
'\t': 'tab',
'\r': 'return',
'\n': 'new_line',
'\\': 'backslash'
};
/**
* Pronunciation dictionary. Each key must be lowercase, its replacement
* should be spelled out the way most TTS engines will pronounce it
* correctly. This particular dictionary only handles letters and numbers,
* no symbols.
* @type {Object.<string, string>}
*/
cvox.AbstractTts.PRONUNCIATION_DICTIONARY = {
'admob': 'ad-mob',
'adsense': 'ad-sense',
'adwords': 'ad-words',
'angularjs': 'angular j s',
'bcc': 'B C C',
'cc': 'C C',
'chromevox': 'chrome vox',
'cr48': 'C R 48',
'ctrl': 'control',
'doubleclick': 'double-click',
'gmail': 'gee mail',
'gtalk': 'gee talk',
'http': 'H T T P',
'https' : 'H T T P S',
'igoogle': 'eye google',
'pagerank': 'page-rank',
'username': 'user-name',
'www': 'W W W',
'youtube': 'you tube'
};
/**
* Pronunciation dictionary regexp.
* @type {RegExp};
* @private
*/
cvox.AbstractTts.pronunciationDictionaryRegexp_;
/**
* Substitution dictionary. These symbols or patterns are ALWAYS substituted
* whenever they occur, so this should be reserved only for unicode characters
* and characters that never have any different meaning in context.
*
* For example, do not include '$' here because $2 should be read as
* "two dollars".
* @type {Object.<string, string>}
*/
cvox.AbstractTts.SUBSTITUTION_DICTIONARY = {
'://': 'colon slash slash',
'\u00bc': 'one fourth',
'\u00bd': 'one half',
'\u2190': 'left arrow',
'\u2191': 'up arrow',
'\u2192': 'right arrow',
'\u2193': 'down arrow',
'\u21d0': 'left double arrow',
'\u21d1': 'up double arrow',
'\u21d2': 'right double arrow',
'\u21d3': 'down double arrow',
'\u21e6': 'left arrow',
'\u21e7': 'up arrow',
'\u21e8': 'right arrow',
'\u21e9': 'down arrow',
'\u2303': 'control',
'\u2318': 'command',
'\u2325': 'option',
'\u25b2': 'up triangle',
'\u25b3': 'up triangle',
'\u25b4': 'up triangle',
'\u25b5': 'up triangle',
'\u25b6': 'right triangle',
'\u25b7': 'right triangle',
'\u25b8': 'right triangle',
'\u25b9': 'right triangle',
'\u25ba': 'right pointer',
'\u25bb': 'right pointer',
'\u25bc': 'down triangle',
'\u25bd': 'down triangle',
'\u25be': 'down triangle',
'\u25bf': 'down triangle',
'\u25c0': 'left triangle',
'\u25c1': 'left triangle',
'\u25c2': 'left triangle',
'\u25c3': 'left triangle',
'\u25c4': 'left pointer',
'\u25c5': 'left pointer',
'\uf8ff': 'apple'
};
/**
* Substitution dictionary regexp.
* @type {RegExp};
* @private
*/
cvox.AbstractTts.substitutionDictionaryRegexp_;
/**
* repetition filter regexp.
* @type {RegExp}
* @private
*/
cvox.AbstractTts.repetitionRegexp_ =
/([-\/\\|!@#$%^&*\(\)=_+\[\]\{\}.?;'":<>])\1{2,}/g;
/**
* Constructs a description of a repeated character. Use as a param to
* string.replace.
* @param {string} match The matching string.
* @return {string} The description.
* @private
*/
cvox.AbstractTts.repetitionReplace_ = function(match) {
var count = match.length;
return ' ' + (new goog.i18n.MessageFormat(cvox.ChromeVox.msgs.getMsg(
cvox.AbstractTts.CHARACTER_DICTIONARY[match[0]])))
.format({'COUNT': count}) + ' ';
};
/**
* @override
*/
cvox.AbstractTts.prototype.getDefaultProperty = function(property) {
return this.propertyDefault[property];
};
|