1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
11 function areNewOptions(options) {
12 var properties = ['voiceName', 'lang', 'gender', 'rate', 'pitch', 'volume'];
14 for (var i = 0; i < properties.length; ++i) {
15 if (options[properties[i]] != curOptions[properties[i]]) {
23 function getTtsElement(element) {
24 return ttsWindow.document.getElementById(element);
27 function appendText(text) {
28 getTtsElement("text").innerHTML += text;
31 function logOptions() {
32 getTtsElement("voiceName").innerHTML = curOptions.voiceName;
33 getTtsElement("lang").innerHTML = curOptions.lang;
34 getTtsElement("gender").innerHTML = curOptions.gender;
35 getTtsElement("rate").innerHTML = curOptions.rate;
36 getTtsElement("pitch").innerHTML = curOptions.pitch;
37 getTtsElement("volume").innerHTML = curOptions.volume;
40 function logUtterance(utterance, index, sendTtsEvent) {
41 if (index == utterance.length) {
42 sendTtsEvent({'type': 'end', 'charIndex': utterance.length});
46 appendText(utterance[index]);
48 if (utterance[index] == ' ') {
49 sendTtsEvent({'type': 'word', 'charIndex': index});
51 else if (utterance[index] == '.' ||
52 utterance[index] == '?' ||
53 utterance[index] == '!') {
54 sendTtsEvent({'type': 'sentence', 'charIndex': index});
57 timeoutId = setTimeout(function() {
58 logUtterance(utterance, ++index, sendTtsEvent)
62 var speakListener = function(utterance, options, sendTtsEvent) {
63 clearTimeout(timeoutId);
65 sendTtsEvent({'type': 'start', 'charIndex': 0});
68 // Create a new window that overlaps the bottom 40% of the current window
69 chrome.windows.getCurrent(function(curWindow) {
70 chrome.windows.create(
71 {"url": "console_tts_engine.html",
73 "top": Math.round(curWindow.top + 6/10 * curWindow.height),
74 "left": curWindow.left,
75 "width": curWindow.width,
76 "height": Math.round(4/10 * curWindow.height)},
79 ttsWindow = chrome.extension.getViews({"windowId": ttsId})[0];
84 // Fastest timeout == 1 ms (@ options.rate = 10.0)
85 milliseconds = 10 / curOptions.rate;
86 logUtterance(utterance, 0, sendTtsEvent);
91 if (areNewOptions(options)) {
95 milliseconds = 10 / curOptions.rate;
98 logUtterance(utterance, 0, sendTtsEvent);
103 var stopListener = function() {
104 clearTimeout(timeoutId);
107 var removedListener = function(windowId, removeInfo) {
108 if (ttsId == windowId) {
113 chrome.ttsEngine.onSpeak.addListener(speakListener);
114 chrome.ttsEngine.onStop.addListener(stopListener);
115 chrome.windows.onRemoved.addListener(removedListener);