Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Unified Diff: chrome/browser/resources/network_speech_synthesis/tts_extension.js

Issue 27034009: Implement Google network speech synthesis (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix getApiKey description Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/network_speech_synthesis/tts_extension.js
diff --git a/chrome/browser/resources/network_speech_synthesis/tts_extension.js b/chrome/browser/resources/network_speech_synthesis/tts_extension.js
new file mode 100644
index 0000000000000000000000000000000000000000..d4e74295fbfc8cd63fa6b986cc28f225f61d271a
--- /dev/null
+++ b/chrome/browser/resources/network_speech_synthesis/tts_extension.js
@@ -0,0 +1,101 @@
+// Copyright 2013 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.
+
+/**
+ * @fileoverview
+ * This is a component extension that implements a speech engine
+ * powered by Google's speech synthesis API.
+ *
+ * This is an "event page", so it's not loaded when the API isn't being used,
+ * and doesn't waste resources. When a web page or web app makes a speech
+ * request and the parameters match one of the voices in this extension's
+ * manifest, it makes a request to Google's API using Chrome's private key
+ * and plays the resulting speech using HTML5 audio.
+ */
+
+/**
+ * The main class for this extension. Adds listeners to
+ * chrome.ttsEngine.onSpeak and chrome.ttsEngine.onStop and implements
+ * them using Google's speech synthesis API.
+ */
+function TtsExtension() {
+}
+
+TtsExtension.prototype = {
+ speechServerUrl_:
+ 'https://www.google.com/speech-api/v2/synthesize?enc=mpeg&client=chrome',
gshires1 2013/10/17 00:34:50 ASR uses client=chromium (Unless there's other rea
dmazzoni 2013/10/17 17:14:00 Done.
+
+ currentUtterance_: null,
+
+ audioElement_: null,
+
+ run: function() {
+ this.audioElement_ = document.createElement('audio');
+ document.body.appendChild(this.audioElement_);
+ this.audioElement_.addEventListener('ended', (function() {
+ this.onStop_();
+ }).bind(this), false);
+
+ this.audioElement_.addEventListener('canplaythrough', (function() {
+ if (this.currentUtterance_) {
+ this.audioElement_.play();
+ this.currentUtterance_.callback({
+ 'type': 'start',
+ 'charIndex': 0
+ });
+ }
+ }).bind(this), false);
+
+ chrome.ttsEngine.onSpeak.addListener(this.onSpeak_.bind(this));
+ chrome.ttsEngine.onStop.addListener(this.onStop_.bind(this));
+ },
+
+ onSpeak_: function(utterance, options, callback) {
+ try {
+ this.onStop_();
+ this.currentUtterance_ = {
+ utterance: utterance,
+ options: options,
+ callback: callback
+ };
+
+ var url = this.speechServerUrl_;
+ chrome.systemPrivate.getApiKey((function(key) {
+ url += '&key=' + key;
+ url += '&text=' + escape(utterance);
gshires1 2013/10/17 00:34:50 How long might text be? (Assuming it could be qui
dmazzoni 2013/10/17 17:14:00 The maximum length is a good point, but I still pr
+ url += '&lang=' + options.lang.toLowerCase();
+ if (options.rate) {
+ url += '&speed=' + (options.rate / 2.0);
+ }
+ if (options.pitch) {
+ url += '&pitch=' + (options.pitch / 2.0);
+ }
+ if (options.volume) {
+ url += '&vol=' + options.volume;
+ }
+ this.audioElement_.src = url;
+ }).bind(this));
+ } catch (err) {
+ console.log(String(err));
+ callback({
+ 'type': 'error',
+ 'errorMessage': String(err)
+ });
+ this.currentUtterance_ = null;
+ }
+ },
+
+ onStop_: function() {
+ if (this.currentUtterance_) {
+ this.audioElement_.pause();
+ this.currentUtterance_.callback({
+ 'type': 'end',
+ 'charIndex': this.currentUtterance_.utterance.length
+ });
+ }
+ this.currentUtterance_ = null;
+ }
+};
+
+(new TtsExtension()).run();

Powered by Google App Engine
This is Rietveld 408576698