| 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',
|
| +
|
| + 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);
|
| + 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();
|
|
|