From b51f6ab136aa37f932c3d268693238f8f6b9e52f Mon Sep 17 00:00:00 2001 From: "mitchellwrosen@chromium.org" Date: Sun, 20 May 2012 00:55:04 +0000 Subject: Sample extensions page does not feature chrome.ttsEngine. Wrote a small sample TTS Engine that prints text to a small window on the bottom of the screen rather than synthesizing speech. BUG=117480 TEST=Manual Review URL: https://chromiumcodereview.appspot.com/9688004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@138047 0039d316-1c4b-4281-b951-d872f2087c98 --- .../examples/api/ttsEngine/console_tts_engine.zip | 0 .../console_tts_engine/console_tts_engine.html | 49 +++++++++ .../console_tts_engine/console_tts_engine.js | 115 +++++++++++++++++++++ .../api/ttsEngine/console_tts_engine/manifest.json | 17 +++ 4 files changed, 181 insertions(+) create mode 100644 chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine.zip create mode 100644 chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/console_tts_engine.html create mode 100644 chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/console_tts_engine.js create mode 100644 chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/manifest.json (limited to 'chrome/common/extensions/docs/examples') diff --git a/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine.zip b/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine.zip new file mode 100644 index 0000000..e69de29 diff --git a/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/console_tts_engine.html b/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/console_tts_engine.html new file mode 100644 index 0000000..62fd68d --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/console_tts_engine.html @@ -0,0 +1,49 @@ + + + Console TTS Engine + + + + + + + + + + + + + + + + + + + + + +
Voice NameLanguageGenderRatePitchVolume
+ +

+ + diff --git a/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/console_tts_engine.js b/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/console_tts_engine.js new file mode 100644 index 0000000..3f69094f --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/console_tts_engine.js @@ -0,0 +1,115 @@ +// 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', 'gender', '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("gender").innerHTML = curOptions.gender; + 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); diff --git a/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/manifest.json b/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/manifest.json new file mode 100644 index 0000000..43e7edc --- /dev/null +++ b/chrome/common/extensions/docs/examples/api/ttsEngine/console_tts_engine/manifest.json @@ -0,0 +1,17 @@ +{ + "name": "Console TTS Engine", + "version": "2.0", + "description": "A \"silent\" TTS engine that prints text to a small window rather than synthesizing speech.", + "permissions": ["ttsEngine", "tabs"], + "background": { + "scripts": ["console_tts_engine.js"] + }, + "tts_engine": { + "voices": [ + { + "voice_name": "Console", + "event_types": ["start", "word", "sentence", "end"] + } + ] + } +} -- cgit v1.1