OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview The class to Manage both offline / online speech recognition. | 6 * @fileoverview The class to Manage both offline / online speech recognition. |
7 */ | 7 */ |
8 | 8 |
9 <include src="plugin_manager.js"/> | 9 <include src="plugin_manager.js"/> |
10 <include src="audio_manager.js"/> | 10 <include src="audio_manager.js"/> |
11 <include src="speech_recognition_manager.js"/> | 11 <include src="speech_recognition_manager.js"/> |
12 | 12 |
13 cr.define('speech', function() { | 13 cr.define('speech', function() { |
14 'use strict'; | 14 'use strict'; |
15 | 15 |
16 /** | 16 /** |
17 * The state of speech recognition. | 17 * The state of speech recognition. |
18 * | 18 * |
19 * @enum {string} | 19 * @enum {string} |
20 */ | 20 */ |
21 var SpeechState = { | 21 var SpeechState = { |
22 READY: 'READY', | 22 READY: 'READY', |
23 HOTWORD_RECOGNIZING: 'HOTWORD_RECOGNIZING', | 23 HOTWORD_RECOGNIZING: 'HOTWORD_RECOGNIZING', |
24 RECOGNIZING: 'RECOGNIZING', | 24 RECOGNIZING: 'RECOGNIZING', |
25 IN_SPEECH: 'IN_SPEECH', | 25 IN_SPEECH: 'IN_SPEECH', |
26 STOPPING: 'STOPPING' | 26 STOPPING: 'STOPPING' |
27 }; | 27 }; |
28 | 28 |
29 /** | 29 /** |
| 30 * Checks the prefix for the hotword module based on the language. This is |
| 31 * fragile if the file structure has changed. |
| 32 */ |
| 33 function getHotwordPrefix() { |
| 34 var prefix = navigator.language.toLowerCase(); |
| 35 if (prefix == 'en-gb') |
| 36 return prefix; |
| 37 var hyphen = prefix.indexOf('-'); |
| 38 if (hyphen >= 0) |
| 39 prefix = prefix.substr(0, hyphen); |
| 40 if (prefix == 'en') |
| 41 prefix = ''; |
| 42 return prefix; |
| 43 } |
| 44 |
| 45 /** |
30 * @constructor | 46 * @constructor |
31 */ | 47 */ |
32 function SpeechManager() { | 48 function SpeechManager() { |
33 this.audioManager_ = new speech.AudioManager(); | 49 this.audioManager_ = new speech.AudioManager(); |
34 this.audioManager_.addEventListener('audio', this.onAudioLevel_.bind(this)); | 50 this.audioManager_.addEventListener('audio', this.onAudioLevel_.bind(this)); |
35 this.speechRecognitionManager_ = new speech.SpeechRecognitionManager(this); | 51 this.speechRecognitionManager_ = new speech.SpeechRecognitionManager(this); |
36 } | 52 } |
37 | 53 |
38 /** | 54 /** |
39 * Updates the state. | 55 * Updates the state. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 * ready. | 87 * ready. |
72 * @private | 88 * @private |
73 */ | 89 */ |
74 SpeechManager.prototype.onHotwordRecognizerReady_ = function(pluginManager) { | 90 SpeechManager.prototype.onHotwordRecognizerReady_ = function(pluginManager) { |
75 this.pluginManager_ = pluginManager; | 91 this.pluginManager_ = pluginManager; |
76 this.audioManager_.addEventListener( | 92 this.audioManager_.addEventListener( |
77 'audio', pluginManager.sendAudioData.bind(pluginManager)); | 93 'audio', pluginManager.sendAudioData.bind(pluginManager)); |
78 this.pluginManager_.startRecognizer(); | 94 this.pluginManager_.startRecognizer(); |
79 this.audioManager_.start(); | 95 this.audioManager_.start(); |
80 this.setState_(SpeechState.HOTWORD_RECOGNIZING); | 96 this.setState_(SpeechState.HOTWORD_RECOGNIZING); |
81 chrome.send('setHotwordRecognizerState', [true]); | |
82 }; | 97 }; |
83 | 98 |
84 /** | 99 /** |
85 * Called when the hotword is recognized. | 100 * Called when the hotword is recognized. |
86 * | 101 * |
87 * @private | 102 * @private |
88 */ | 103 */ |
89 SpeechManager.prototype.onHotwordRecognized_ = function() { | 104 SpeechManager.prototype.onHotwordRecognized_ = function() { |
90 if (this.state != SpeechState.HOTWORD_RECOGNIZING) | 105 if (this.state != SpeechState.HOTWORD_RECOGNIZING) |
91 return; | 106 return; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 /** | 171 /** |
157 * Changes the availability of the hotword plugin. | 172 * Changes the availability of the hotword plugin. |
158 * | 173 * |
159 * @param {boolean} enabled Whether enabled or not. | 174 * @param {boolean} enabled Whether enabled or not. |
160 */ | 175 */ |
161 SpeechManager.prototype.setHotwordEnabled = function(enabled) { | 176 SpeechManager.prototype.setHotwordEnabled = function(enabled) { |
162 var recognizer = $('recognizer'); | 177 var recognizer = $('recognizer'); |
163 if (enabled) { | 178 if (enabled) { |
164 if (recognizer) | 179 if (recognizer) |
165 return; | 180 return; |
| 181 if (!this.naclArch) |
| 182 return; |
| 183 |
| 184 var prefix = getHotwordPrefix(); |
166 var pluginManager = new speech.PluginManager( | 185 var pluginManager = new speech.PluginManager( |
| 186 prefix, |
167 this.onHotwordRecognizerReady_.bind(this), | 187 this.onHotwordRecognizerReady_.bind(this), |
168 this.onHotwordRecognized_.bind(this)); | 188 this.onHotwordRecognized_.bind(this)); |
169 pluginManager.scheduleInitialize( | 189 var modelUrl = 'chrome://app-list/_platform_specific/' + this.naclArch + |
170 this.audioManager_.sampleRate, 'chrome://app-list/hotword.data'); | 190 '_' + prefix + '/hotword.data'; |
| 191 pluginManager.scheduleInitialize(this.audioManager_.sampleRate, modelUrl); |
171 } else { | 192 } else { |
172 if (!recognizer) | 193 if (!recognizer) |
173 return; | 194 return; |
174 document.body.removeChild(recognizer); | 195 document.body.removeChild(recognizer); |
175 this.pluginManager_ = null; | 196 this.pluginManager_ = null; |
176 if (this.state == SpeechState.HOTWORD_RECOGNIZING) { | 197 if (this.state == SpeechState.HOTWORD_RECOGNIZING) { |
177 this.audioManager_.stop(); | 198 this.audioManager_.stop(); |
178 this.setState_(SpeechState.READY); | 199 this.setState_(SpeechState.READY); |
179 } | 200 } |
180 } | 201 } |
181 }; | 202 }; |
182 | 203 |
183 /** | 204 /** |
| 205 * Sets the NaCl architecture for the hotword module. |
| 206 * |
| 207 * @param {string} arch The architecture. |
| 208 */ |
| 209 SpeechManager.prototype.setNaclArch = function(arch) { |
| 210 this.naclArch = arch; |
| 211 }; |
| 212 |
| 213 /** |
184 * Called when the app-list bubble is shown. | 214 * Called when the app-list bubble is shown. |
185 * | 215 * |
186 * @param {boolean} hotwordEnabled Whether the hotword is enabled or not. | 216 * @param {boolean} hotwordEnabled Whether the hotword is enabled or not. |
187 */ | 217 */ |
188 SpeechManager.prototype.onShown = function(hotwordEnabled) { | 218 SpeechManager.prototype.onShown = function(hotwordEnabled) { |
189 this.setHotwordEnabled(hotwordEnabled); | 219 this.setHotwordEnabled(hotwordEnabled); |
190 | 220 |
191 // No one sets the state if the content is initialized on shown but hotword | 221 // No one sets the state if the content is initialized on shown but hotword |
192 // is not enabled. Sets the state in such case. | 222 // is not enabled. Sets the state in such case. |
193 if (!this.state && !hotwordEnabled) | 223 if (!this.state && !hotwordEnabled) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 if (this.audioManager_.state == speech.AudioState.STOPPED) | 255 if (this.audioManager_.state == speech.AudioState.STOPPED) |
226 this.audioManager_.start(); | 256 this.audioManager_.start(); |
227 this.speechRecognitionManager_.start(); | 257 this.speechRecognitionManager_.start(); |
228 } | 258 } |
229 }; | 259 }; |
230 | 260 |
231 return { | 261 return { |
232 SpeechManager: SpeechManager | 262 SpeechManager: SpeechManager |
233 }; | 263 }; |
234 }); | 264 }); |
OLD | NEW |