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"/> |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 * | 58 * |
59 * @param {cr.event.Event} event The event object for the audio data. | 59 * @param {cr.event.Event} event The event object for the audio data. |
60 * @private | 60 * @private |
61 */ | 61 */ |
62 SpeechManager.prototype.onAudioLevel_ = function(event) { | 62 SpeechManager.prototype.onAudioLevel_ = function(event) { |
63 var data = event.data; | 63 var data = event.data; |
64 var level = 0; | 64 var level = 0; |
65 for (var i = 0; i < data.length; ++i) | 65 for (var i = 0; i < data.length; ++i) |
66 level += Math.abs(data[i]); | 66 level += Math.abs(data[i]); |
67 level /= data.length; | 67 level /= data.length; |
68 // TODO(mukai): use the result to make the audio feedback during the speech | 68 chrome.send('speechSoundLevel', [level]); |
69 // recognition. | |
70 }; | 69 }; |
71 | 70 |
72 /** | 71 /** |
73 * Called when the hotword recognizer is ready. | 72 * Called when the hotword recognizer is ready. |
74 * | 73 * |
75 * @param {PluginManager} pluginManager The hotword plugin manager which gets | 74 * @param {PluginManager} pluginManager The hotword plugin manager which gets |
76 * ready. | 75 * ready. |
77 * @private | 76 * @private |
78 */ | 77 */ |
79 SpeechManager.prototype.onHotwordRecognizerReady_ = function(pluginManager) { | 78 SpeechManager.prototype.onHotwordRecognizerReady_ = function(pluginManager) { |
(...skipping 19 matching lines...) Expand all Loading... | |
99 | 98 |
100 /** | 99 /** |
101 * Called when the speech recognition has happened. | 100 * Called when the speech recognition has happened. |
102 * | 101 * |
103 * @param {string} result The speech recognition result. | 102 * @param {string} result The speech recognition result. |
104 * @param {boolean} isFinal Whether the result is final or not. | 103 * @param {boolean} isFinal Whether the result is final or not. |
105 */ | 104 */ |
106 SpeechManager.prototype.onSpeechRecognized = function(result, isFinal) { | 105 SpeechManager.prototype.onSpeechRecognized = function(result, isFinal) { |
107 console.log('speech result: ' + result + ' ' + | 106 console.log('speech result: ' + result + ' ' + |
108 (isFinal ? 'final' : 'interim')); | 107 (isFinal ? 'final' : 'interim')); |
109 if (isFinal) { | 108 chrome.send('speechResult', [result, isFinal]); |
110 chrome.send('search', [result]); | 109 if (isFinal) |
111 this.speechRecognitionManager_.stop(); | 110 this.speechRecognitionManager_.stop(); |
112 } | |
113 }; | 111 }; |
114 | 112 |
115 /** | 113 /** |
116 * Called when the speech recognition has started. | 114 * Called when the speech recognition has started. |
117 */ | 115 */ |
118 SpeechManager.prototype.onSpeechRecognitionStarted = function() { | 116 SpeechManager.prototype.onSpeechRecognitionStarted = function() { |
119 this.setState_(SpeechState.RECOGNIZING); | 117 this.setState_(SpeechState.RECOGNIZING); |
120 chrome.send('setSpeechRecognitionState', [true]); | 118 chrome.send('setSpeechRecognitionState', ['on']); |
121 }; | 119 }; |
122 | 120 |
123 /** | 121 /** |
124 * Called when the speech recognition has ended. | 122 * Called when the speech recognition has ended. |
125 */ | 123 */ |
126 SpeechManager.prototype.onSpeechRecognitionEnded = function() { | 124 SpeechManager.prototype.onSpeechRecognitionEnded = function() { |
127 // Restarts the hotword recognition. | 125 // Restarts the hotword recognition. |
128 if (this.pluginManager_) { | 126 if (this.pluginManager_) { |
129 this.pluginManager_.startRecognizer(); | 127 this.pluginManager_.startRecognizer(); |
130 this.audioManager_.start(); | 128 this.audioManager_.start(); |
131 this.setState_(SpeechState.HOTWORD_RECOGNIZING); | 129 this.setState_(SpeechState.HOTWORD_RECOGNIZING); |
132 } else { | 130 } else { |
133 this.audioManager_.stop(); | 131 this.audioManager_.stop(); |
134 } | 132 } |
135 chrome.send('setSpeechRecognitionState', [false]); | 133 chrome.send('setSpeechRecognitionState', ['off']); |
136 }; | 134 }; |
137 | 135 |
138 /** | 136 /** |
137 * Called when a speech has started. | |
138 */ | |
139 SpeechManager.prototype.onSpeechStarted = function() { | |
140 console.log('speechStarted: ' + this.state); | |
xiyuan
2013/12/05 22:06:30
nit: remove debugging log?
Jun Mukai
2013/12/06 00:31:10
Done.
| |
141 if (this.state == SpeechState.RECOGNIZING) | |
142 chrome.send('setSpeechRecognitionState', ['in-speech']); | |
143 }; | |
144 | |
145 /** | |
146 * Called when a speech has ended. | |
147 */ | |
148 SpeechManager.prototype.onSpeechEnded = function() { | |
149 console.log('speechEnded: ' + this.state); | |
xiyuan
2013/12/05 22:06:30
nit: remove debugging log?
Jun Mukai
2013/12/06 00:31:10
Done.
| |
150 if (this.state == SpeechState.RECOGNIZING) | |
151 chrome.send('setSpeechRecognitionState', ['on']); | |
152 }; | |
153 | |
154 /** | |
139 * Called when an error happened during the speech recognition. | 155 * Called when an error happened during the speech recognition. |
140 * | 156 * |
141 * @param {SpeechRecognitionError} e The error object. | 157 * @param {SpeechRecognitionError} e The error object. |
142 */ | 158 */ |
143 SpeechManager.prototype.onSpeechRecognitionError = function(e) { | 159 SpeechManager.prototype.onSpeechRecognitionError = function(e) { |
144 this.setState_(SpeechState.READY); | 160 this.setState_(SpeechState.READY); |
145 }; | 161 }; |
146 | 162 |
147 /** | 163 /** |
148 * Starts the speech recognition session. | 164 * Starts the speech recognition session. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 if (this.audioManager_.state == speech.AudioState.STOPPED) | 201 if (this.audioManager_.state == speech.AudioState.STOPPED) |
186 this.audioManager_.start(); | 202 this.audioManager_.start(); |
187 this.speechRecognitionManager_.start(); | 203 this.speechRecognitionManager_.start(); |
188 } | 204 } |
189 }; | 205 }; |
190 | 206 |
191 return { | 207 return { |
192 SpeechManager: SpeechManager | 208 SpeechManager: SpeechManager |
193 }; | 209 }; |
194 }); | 210 }); |
OLD | NEW |