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

Side by Side 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: Change client name, truncate utterance length 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6 * @fileoverview
7 * This is a component extension that implements a speech engine
8 * powered by Google's speech synthesis API.
9 *
10 * This is an "event page", so it's not loaded when the API isn't being used,
11 * and doesn't waste resources. When a web page or web app makes a speech
12 * request and the parameters match one of the voices in this extension's
13 * manifest, it makes a request to Google's API using Chrome's private key
14 * and plays the resulting speech using HTML5 audio.
15 */
16
17 /**
18 * The main class for this extension. Adds listeners to
19 * chrome.ttsEngine.onSpeak and chrome.ttsEngine.onStop and implements
20 * them using Google's speech synthesis API.
21 */
22 function TtsExtension() {
David Tseng 2013/10/17 20:43:49 Do you want to declare this function globally? Per
dmazzoni 2013/10/18 16:23:19 I think this is the Chrome idiomatic style for dec
23 }
24
25 TtsExtension.prototype = {
David Tseng 2013/10/17 20:56:30 I personally don't mind this style of js, but let'
dmazzoni 2013/10/18 16:23:19 I don't have a strong preference, I was just tryin
26 speechServerUrl_:
David Tseng 2013/10/17 20:43:49 @private, @const; SPEECH_SERVER_URL_
dmazzoni 2013/10/18 16:23:19 Done.
27 'https://www.google.com/speech-api/v2/synthesize?enc=mpeg&client=chromium' ,
28
29 currentUtterance_: null,
30
31 audioElement_: null,
David Tseng 2013/10/17 20:43:49 JS doc for all of these please.
dmazzoni 2013/10/18 16:23:19 Done.
32
33 run: function() {
34 this.audioElement_ = document.createElement('audio');
35 document.body.appendChild(this.audioElement_);
36 this.audioElement_.addEventListener('ended', (function() {
37 this.onStop_();
38 }).bind(this), false);
39
40 this.audioElement_.addEventListener('canplaythrough', (function() {
41 if (this.currentUtterance_) {
42 this.audioElement_.play();
43 this.currentUtterance_.callback({
44 'type': 'start',
45 'charIndex': 0
46 });
47 }
48 }).bind(this), false);
49
50 chrome.ttsEngine.onSpeak.addListener(this.onSpeak_.bind(this));
51 chrome.ttsEngine.onStop.addListener(this.onStop_.bind(this));
52 },
53
54 onSpeak_: function(utterance, options, callback) {
55 // Truncate the utterance if it's too long.
David Tseng 2013/10/17 20:43:49 Why not just chunk the utterance? (and queue up ad
dmazzoni 2013/10/18 16:23:19 Both Chrome's tts extension api and the web speech
56 utterance = escape(utterance);
57 if (utterance.length > 32768)
58 utterance = utterance.substr(0, 32768);
59
60 try {
61 this.onStop_();
David Tseng 2013/10/17 20:43:49 Can we get this engine to respect queue mode? (per
dmazzoni 2013/10/18 16:23:19 The queue is managed by Chrome. A ttsEngine just n
62 this.currentUtterance_ = {
63 utterance: utterance,
64 options: options,
65 callback: callback
66 };
67
68 var url = this.speechServerUrl_;
69 chrome.systemPrivate.getApiKey((function(key) {
70 url += '&key=' + key;
71 url += '&text=' + utterance;
72 url += '&lang=' + options.lang.toLowerCase();
73 if (options.rate) {
74 url += '&speed=' + (options.rate / 2.0);
David Tseng 2013/10/17 20:43:49 Can you comment on the range expected?
dmazzoni 2013/10/18 16:23:19 Done.
75 }
76 if (options.pitch) {
77 url += '&pitch=' + (options.pitch / 2.0);
David Tseng 2013/10/17 20:43:49 Ditto.
dmazzoni 2013/10/18 16:23:19 Done.
78 }
79 if (options.volume) {
80 url += '&vol=' + options.volume;
David Tseng 2013/10/17 20:43:49 Couldn't we just handle volume directly in the pla
dmazzoni 2013/10/18 16:23:19 Sure, done. Actually I see one potential advantage
81 }
82 this.audioElement_.src = url;
83 }).bind(this));
84 } catch (err) {
85 console.log(String(err));
David Tseng 2013/10/17 20:43:49 console.error
dmazzoni 2013/10/18 16:23:19 Done.
86 callback({
87 'type': 'error',
88 'errorMessage': String(err)
89 });
90 this.currentUtterance_ = null;
91 }
92 },
93
94 onStop_: function() {
95 if (this.currentUtterance_) {
96 this.audioElement_.pause();
97 this.currentUtterance_.callback({
98 'type': 'end',
99 'charIndex': this.currentUtterance_.utterance.length
100 });
101 }
102 this.currentUtterance_ = null;
103 }
104 };
105
106 (new TtsExtension()).run();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698