OLD | NEW |
---|---|
(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() { | |
23 } | |
24 | |
25 TtsExtension.prototype = { | |
26 speechServerUrl_: | |
27 '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.
| |
28 | |
29 currentUtterance_: null, | |
30 | |
31 audioElement_: null, | |
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 try { | |
56 this.onStop_(); | |
57 this.currentUtterance_ = { | |
58 utterance: utterance, | |
59 options: options, | |
60 callback: callback | |
61 }; | |
62 | |
63 var url = this.speechServerUrl_; | |
64 chrome.systemPrivate.getApiKey((function(key) { | |
65 url += '&key=' + key; | |
66 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
| |
67 url += '&lang=' + options.lang.toLowerCase(); | |
68 if (options.rate) { | |
69 url += '&speed=' + (options.rate / 2.0); | |
70 } | |
71 if (options.pitch) { | |
72 url += '&pitch=' + (options.pitch / 2.0); | |
73 } | |
74 if (options.volume) { | |
75 url += '&vol=' + options.volume; | |
76 } | |
77 this.audioElement_.src = url; | |
78 }).bind(this)); | |
79 } catch (err) { | |
80 console.log(String(err)); | |
81 callback({ | |
82 'type': 'error', | |
83 'errorMessage': String(err) | |
84 }); | |
85 this.currentUtterance_ = null; | |
86 } | |
87 }, | |
88 | |
89 onStop_: function() { | |
90 if (this.currentUtterance_) { | |
91 this.audioElement_.pause(); | |
92 this.currentUtterance_.callback({ | |
93 'type': 'end', | |
94 'charIndex': this.currentUtterance_.utterance.length | |
95 }); | |
96 } | |
97 this.currentUtterance_ = null; | |
98 } | |
99 }; | |
100 | |
101 (new TtsExtension()).run(); | |
OLD | NEW |