Index: chrome/common/extensions/docs/examples/api/silent_tts_engine/silent_tts_engine.js |
diff --git a/chrome/common/extensions/docs/examples/api/silent_tts_engine/silent_tts_engine.js b/chrome/common/extensions/docs/examples/api/silent_tts_engine/silent_tts_engine.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..30bc906a97a1a400661a5c3b85c83c423927fc89 |
--- /dev/null |
+++ b/chrome/common/extensions/docs/examples/api/silent_tts_engine/silent_tts_engine.js |
@@ -0,0 +1,111 @@ |
+var t; |
dmazzoni
2012/05/16 05:49:58
To be more clear, call this timeoutId or something
mitchellwrosen
2012/05/16 20:40:08
Done.
|
+ |
+var tts_id = -1; |
dmazzoni
2012/05/16 05:49:58
Would prefer consistent variable naming - the Chro
mitchellwrosen
2012/05/16 20:40:08
Done.
|
+var tts_window; |
+ |
+var milliseconds; |
+ |
+var curOptions; |
+ |
+function are_new_options(options) { |
+ var properties = ['voiceName', 'lang', 'gender', 'rate', 'pitch', 'volume']; |
+ |
+ for (var i = 0; i < properties.length; ++i) { |
+ if (options[properties[i]] != curOptions[properties[i]]) { |
+ return true; |
+ } |
+ } |
+ |
+ return false; |
+} |
+ |
+function appendText(text) { |
+ tts_window.document.getElementById("text").innerHTML = |
dmazzoni
2012/05/16 05:49:58
Put tts_window.document in a global like tts_doc.
mitchellwrosen
2012/05/16 20:40:08
Done.
|
+ tts_window.document.getElementById("text").innerHTML + text; |
dmazzoni
2012/05/16 05:49:58
Use the += operator
mitchellwrosen
2012/05/16 20:40:08
Done.
|
+} |
+ |
+function log_options() { |
+ tts_window.document.getElementById("voiceName").innerHTML = |
+ curOptions.voiceName; |
+ tts_window.document.getElementById("lang").innerHTML = curOptions.lang; |
+ tts_window.document.getElementById("gender").innerHTML = curOptions.gender; |
+ tts_window.document.getElementById("rate").innerHTML = curOptions.rate; |
+ tts_window.document.getElementById("pitch").innerHTML = curOptions.pitch; |
+ tts_window.document.getElementById("volume").innerHTML = curOptions.volume; |
+} |
+ |
+function log_utterance(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}); |
+ } |
+ |
+ t = setTimeout(function(){log_utterance(utterance, ++index, sendTtsEvent)}, |
dmazzoni
2012/05/16 05:49:58
Indent it just like any other function, like this:
mitchellwrosen
2012/05/16 20:40:08
Done.
|
+ milliseconds); |
+} |
+ |
+var speakListener = function(utterance, options, sendTtsEvent) { |
+ clearTimeout(t); |
+ |
+ sendTtsEvent({'type': 'start', 'charIndex': 0}); |
+ |
+ if (tts_id == -1) { |
+ // Create a new window that overlaps the bottom 40% of the current window |
+ chrome.windows.getCurrent(function(cur_window) { |
+ chrome.windows.create( |
+ {"url": "silent_tts_engine.html", |
+ "focused": false, |
+ "top": Math.round(cur_window.top + 6/10 * cur_window.height), |
+ "left": cur_window.left, |
+ "width": cur_window.width, |
+ "height": Math.round(4/10 * cur_window.height)}, |
+ function(new_window) { |
+ tts_id = new_window.id; |
+ tts_window = chrome.extension.getViews({"windowId": tts_id})[0]; |
+ |
+ curOptions = options; |
+ log_options(); |
+ |
+ // Fastest timeout == 1 ms (@ options.rate = 10.0) |
+ milliseconds = 10 / curOptions.rate; |
+ log_utterance(utterance, 0, sendTtsEvent); |
+ } |
+ ); |
+ }); |
+ } else { |
+ if (are_new_options(options)) { |
+ curOptions = options; |
+ log_options(); |
+ |
+ milliseconds = 10 / curOptions.rate; |
+ } |
+ |
+ log_utterance(utterance, 0, sendTtsEvent); |
+ } |
+ |
+}; |
+ |
+var stopListener = function() { |
+ clearTimeout(t); |
+}; |
+ |
+var removedListener = function(windowId, removeInfo) { |
+ if (tts_id == windowId) { |
+ tts_id = -1; |
+ } |
+} |
+ |
+chrome.ttsEngine.onSpeak.addListener(speakListener); |
+chrome.ttsEngine.onStop.addListener(stopListener); |
+chrome.windows.onRemoved.addListener(removedListener); |