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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 } | 44 } |
45 | 45 |
46 /** | 46 /** |
47 * Updates the state. | 47 * Updates the state. |
48 * | 48 * |
49 * @param {SpeechState} newState The new state. | 49 * @param {SpeechState} newState The new state. |
50 * @private | 50 * @private |
51 */ | 51 */ |
52 SpeechManager.prototype.setState_ = function(newState) { | 52 SpeechManager.prototype.setState_ = function(newState) { |
53 this.state = newState; | 53 this.state = newState; |
54 console.log('speech state: ' + newState); | |
55 }; | 54 }; |
56 | 55 |
57 /** | 56 /** |
58 * Called with the mean audio level when audio data arrives. | 57 * Called with the mean audio level when audio data arrives. |
59 * | 58 * |
60 * @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. |
61 * @private | 60 * @private |
62 */ | 61 */ |
63 SpeechManager.prototype.onAudioLevel_ = function(event) { | 62 SpeechManager.prototype.onAudioLevel_ = function(event) { |
64 var data = event.data; | 63 var data = event.data; |
65 var level = 0; | 64 var level = 0; |
66 for (var i = 0; i < data.length; ++i) | 65 for (var i = 0; i < data.length; ++i) |
67 level += Math.abs(data[i]); | 66 level += Math.abs(data[i]); |
68 level /= data.length; | 67 level /= data.length; |
69 // TODO(mukai): use the result to make the audio feedback during the speech | 68 chrome.send('speechSoundLevel', [level]); |
70 // recognition. | |
71 }; | 69 }; |
72 | 70 |
73 /** | 71 /** |
74 * Called when the hotword recognizer is ready. | 72 * Called when the hotword recognizer is ready. |
75 * | 73 * |
76 * @param {PluginManager} pluginManager The hotword plugin manager which gets | 74 * @param {PluginManager} pluginManager The hotword plugin manager which gets |
77 * ready. | 75 * ready. |
78 * @private | 76 * @private |
79 */ | 77 */ |
80 SpeechManager.prototype.onHotwordRecognizerReady_ = function(pluginManager) { | 78 SpeechManager.prototype.onHotwordRecognizerReady_ = function(pluginManager) { |
(...skipping 17 matching lines...) Expand all Loading... |
98 this.speechRecognitionManager_.start(); | 96 this.speechRecognitionManager_.start(); |
99 }; | 97 }; |
100 | 98 |
101 /** | 99 /** |
102 * Called when the speech recognition has happened. | 100 * Called when the speech recognition has happened. |
103 * | 101 * |
104 * @param {string} result The speech recognition result. | 102 * @param {string} result The speech recognition result. |
105 * @param {boolean} isFinal Whether the result is final or not. | 103 * @param {boolean} isFinal Whether the result is final or not. |
106 */ | 104 */ |
107 SpeechManager.prototype.onSpeechRecognized = function(result, isFinal) { | 105 SpeechManager.prototype.onSpeechRecognized = function(result, isFinal) { |
108 console.log('speech result: ' + result + ' ' + | 106 chrome.send('speechResult', [result, isFinal]); |
109 (isFinal ? 'final' : 'interim')); | 107 if (isFinal) |
110 if (isFinal) { | |
111 chrome.send('search', [result]); | |
112 this.speechRecognitionManager_.stop(); | 108 this.speechRecognitionManager_.stop(); |
113 } | |
114 }; | 109 }; |
115 | 110 |
116 /** | 111 /** |
117 * Called when the speech recognition has started. | 112 * Called when the speech recognition has started. |
118 */ | 113 */ |
119 SpeechManager.prototype.onSpeechRecognitionStarted = function() { | 114 SpeechManager.prototype.onSpeechRecognitionStarted = function() { |
120 this.setState_(SpeechState.RECOGNIZING); | 115 this.setState_(SpeechState.RECOGNIZING); |
121 chrome.send('setSpeechRecognitionState', [true]); | 116 chrome.send('setSpeechRecognitionState', ['on']); |
122 }; | 117 }; |
123 | 118 |
124 /** | 119 /** |
125 * Called when the speech recognition has ended. | 120 * Called when the speech recognition has ended. |
126 */ | 121 */ |
127 SpeechManager.prototype.onSpeechRecognitionEnded = function() { | 122 SpeechManager.prototype.onSpeechRecognitionEnded = function() { |
128 // Restarts the hotword recognition. | 123 // Restarts the hotword recognition. |
129 if (this.state != SpeechState.STOPPING && this.pluginManager_) { | 124 if (this.state != SpeechState.STOPPING && this.pluginManager_) { |
130 this.pluginManager_.startRecognizer(); | 125 this.pluginManager_.startRecognizer(); |
131 this.audioManager_.start(); | 126 this.audioManager_.start(); |
132 this.setState_(SpeechState.HOTWORD_RECOGNIZING); | 127 this.setState_(SpeechState.HOTWORD_RECOGNIZING); |
133 } else { | 128 } else { |
134 this.audioManager_.stop(); | 129 this.audioManager_.stop(); |
135 } | 130 } |
136 chrome.send('setSpeechRecognitionState', [false]); | 131 chrome.send('setSpeechRecognitionState', ['off']); |
137 }; | 132 }; |
138 | 133 |
139 /** | 134 /** |
| 135 * Called when a speech has started. |
| 136 */ |
| 137 SpeechManager.prototype.onSpeechStarted = function() { |
| 138 if (this.state == SpeechState.RECOGNIZING) |
| 139 chrome.send('setSpeechRecognitionState', ['in-speech']); |
| 140 }; |
| 141 |
| 142 /** |
| 143 * Called when a speech has ended. |
| 144 */ |
| 145 SpeechManager.prototype.onSpeechEnded = function() { |
| 146 if (this.state == SpeechState.RECOGNIZING) |
| 147 chrome.send('setSpeechRecognitionState', ['on']); |
| 148 }; |
| 149 |
| 150 /** |
140 * Called when an error happened during the speech recognition. | 151 * Called when an error happened during the speech recognition. |
141 * | 152 * |
142 * @param {SpeechRecognitionError} e The error object. | 153 * @param {SpeechRecognitionError} e The error object. |
143 */ | 154 */ |
144 SpeechManager.prototype.onSpeechRecognitionError = function(e) { | 155 SpeechManager.prototype.onSpeechRecognitionError = function(e) { |
145 if (this.state != SpeechState.STOPPING) | 156 if (this.state != SpeechState.STOPPING) |
146 this.setState_(SpeechState.READY); | 157 this.setState_(SpeechState.READY); |
147 }; | 158 }; |
148 | 159 |
149 /** | 160 /** |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 if (this.audioManager_.state == speech.AudioState.STOPPED) | 204 if (this.audioManager_.state == speech.AudioState.STOPPED) |
194 this.audioManager_.start(); | 205 this.audioManager_.start(); |
195 this.speechRecognitionManager_.start(); | 206 this.speechRecognitionManager_.start(); |
196 } | 207 } |
197 }; | 208 }; |
198 | 209 |
199 return { | 210 return { |
200 SpeechManager: SpeechManager | 211 SpeechManager: SpeechManager |
201 }; | 212 }; |
202 }); | 213 }); |
OLD | NEW |