-
Notifications
You must be signed in to change notification settings - Fork 459
/
console_tts_engine.js
114 lines (92 loc) · 3.02 KB
/
console_tts_engine.js
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
// Copyright (c) 2012 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.
var timeoutId;
var ttsId = -1;
var ttsWindow;
var milliseconds;
var curOptions;
function areNewOptions(options) {
var properties = ['voiceName', 'lang', 'rate', 'pitch', 'volume'];
for (var i = 0; i < properties.length; ++i) {
if (options[properties[i]] != curOptions[properties[i]]) {
return true;
}
}
return false;
}
function getTtsElement(element) {
return ttsWindow.document.getElementById(element);
}
function appendText(text) {
getTtsElement("text").innerHTML += text;
}
function logOptions() {
getTtsElement("voiceName").innerHTML = curOptions.voiceName;
getTtsElement("lang").innerHTML = curOptions.lang;
getTtsElement("rate").innerHTML = curOptions.rate;
getTtsElement("pitch").innerHTML = curOptions.pitch;
getTtsElement("volume").innerHTML = curOptions.volume;
}
function logUtterance(utterance, index, sendTtsEvent) {
if (index == utterance.length) {
sendTtsEvent({'type': 'end', 'charIndex': utterance.length});
return;
}
appendText(utterance[index]);
if (utterance[index] == ' ') {
sendTtsEvent({'type': 'word', 'charIndex': index});
}
else if (utterance[index] == '.' ||
utterance[index] == '?' ||
utterance[index] == '!') {
sendTtsEvent({'type': 'sentence', 'charIndex': index});
}
timeoutId = setTimeout(function() {
logUtterance(utterance, ++index, sendTtsEvent)
}, milliseconds);
}
var speakListener = function(utterance, options, sendTtsEvent) {
clearTimeout(timeoutId);
sendTtsEvent({'type': 'start', 'charIndex': 0});
if (ttsId == -1) {
// Create a new window that overlaps the bottom 40% of the current window
chrome.windows.getCurrent(function(curWindow) {
chrome.windows.create(
{"url": "console_tts_engine.html",
"focused": false,
"top": Math.round(curWindow.top + 6/10 * curWindow.height),
"left": curWindow.left,
"width": curWindow.width,
"height": Math.round(4/10 * curWindow.height)},
function(newWindow) {
ttsId = newWindow.id;
ttsWindow = chrome.extension.getViews({"windowId": ttsId})[0];
curOptions = options;
logOptions();
// Fastest timeout == 1 ms (@ options.rate = 10.0)
milliseconds = 10 / curOptions.rate;
logUtterance(utterance, 0, sendTtsEvent);
}
);
});
} else {
if (areNewOptions(options)) {
curOptions = options;
logOptions();
milliseconds = 10 / curOptions.rate;
}
logUtterance(utterance, 0, sendTtsEvent);
}
};
var stopListener = function() {
clearTimeout(timeoutId);
};
var removedListener = function(windowId, removeInfo) {
if (ttsId == windowId) {
ttsId = -1;
}
}
chrome.ttsEngine.onSpeak.addListener(speakListener);
chrome.ttsEngine.onStop.addListener(stopListener);
chrome.windows.onRemoved.addListener(removedListener);