Commits

Ivan Vučica committed 05f3fda Draft

Google's Console TTS Engine example extension.

  • Participants

Comments (0)

Files changed (3)

console_tts_engine.html

+<html>
+<head>
+  <title>Console TTS Engine</title>
+  <style>
+    body {
+      font-family: arial, helvetica, sans-serif;
+    }
+    table {
+      text-align: center;
+      padding: 10px;
+    }
+    #text {
+      text-align: left;
+      padding: 4px;
+      border: 1px solid #aaa;
+      width: 99%;
+      min-height: 100px;
+      overflow: auto;
+    }
+  </style>
+  <script type="text/javascript">
+    function clearText() {
+      document.getElementById("text").innerHTML = "";
+    }
+  </script>
+</head>
+<body>
+  <table>
+    <tr>
+      <th>Voice Name</th>
+      <th>Language</th>
+      <th>Gender</th>
+      <th>Rate</th>
+      <th>Pitch</th>
+      <th>Volume</th>
+    </tr>
+    <tr>
+      <td id="voiceName"></td>
+      <td id="lang"></td>
+      <td id="gender"></td>
+      <td id="rate"></td>
+      <td id="pitch"></td>
+      <td id="volume"></td>
+    </tr>
+  </table>
+  <button onclick="clearText()">Clear</button>
+  <p id="text"></p>
+</body>
+</html>

console_tts_engine.js

+// 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);
+{
+  "name": "Console TTS Engine",
+  "manifest_version": 2,
+  "version": "2.1",
+  "description": "A \"silent\" TTS engine that prints text to a small window rather than synthesizing speech.",
+  "permissions": ["ttsEngine", "tabs"],
+  "background": {
+    "persistent": false,
+    "scripts": ["console_tts_engine.js"]
+  },
+  "tts_engine": {
+    "voices": [
+      {
+        "voice_name": "Console",
+        "event_types": ["start", "word", "sentence", "end"]
+      }
+    ]
+  }
+}