1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview The class to Manage both offline / online speech recognition.
*/
<include src="plugin_manager.js"/>
<include src="audio_manager.js"/>
<include src="speech_recognition_manager.js"/>
cr.define('speech', function() {
'use strict';
/**
* The state of speech recognition.
*
* @enum {string}
*/
var SpeechState = {
READY: 'READY',
HOTWORD_RECOGNIZING: 'HOTWORD_RECOGNIZING',
RECOGNIZING: 'RECOGNIZING',
IN_SPEECH: 'IN_SPEECH',
STOPPING: 'STOPPING'
};
/**
* @constructor
*/
function SpeechManager() {
this.audioManager_ = new speech.AudioManager();
this.audioManager_.addEventListener('audio', this.onAudioLevel_.bind(this));
if (speech.isPluginAvailable()) {
var pluginManager = new speech.PluginManager(
this.onHotwordRecognizerReady_.bind(this),
this.onHotwordRecognized_.bind(this));
pluginManager.scheduleInitialize(
this.audioManager_.getSampleRate(),
'chrome://app-list/okgoogle_hotword.config');
}
this.shown_ = false;
this.speechRecognitionManager_ = new speech.SpeechRecognitionManager(this);
this.setState_(SpeechState.READY);
}
/**
* Updates the state.
*
* @param {SpeechState} newState The new state.
* @private
*/
SpeechManager.prototype.setState_ = function(newState) {
this.state = newState;
chrome.send('setSpeechRecognitionState', [this.state]);
};
/**
* Called with the mean audio level when audio data arrives.
*
* @param {cr.event.Event} event The event object for the audio data.
* @private
*/
SpeechManager.prototype.onAudioLevel_ = function(event) {
var data = event.data;
var level = 0;
for (var i = 0; i < data.length; ++i)
level += Math.abs(data[i]);
level /= data.length;
chrome.send('speechSoundLevel', [level]);
};
/**
* Called when the hotword recognizer is ready.
*
* @param {PluginManager} pluginManager The hotword plugin manager which gets
* ready.
* @private
*/
SpeechManager.prototype.onHotwordRecognizerReady_ = function(pluginManager) {
this.pluginManager_ = pluginManager;
this.audioManager_.addEventListener(
'audio', pluginManager.sendAudioData.bind(pluginManager));
if (this.shown_) {
this.pluginManager_.startRecognizer();
this.audioManager_.start();
this.setState_(SpeechState.HOTWORD_RECOGNIZING);
} else {
this.setState_(SpeechState.READY);
}
};
/**
* Called when the hotword is recognized.
*
* @param {number} confidence The confidence store of the recognition.
* @private
*/
SpeechManager.prototype.onHotwordRecognized_ = function(confidence) {
if (this.state != SpeechState.HOTWORD_RECOGNIZING)
return;
this.setState_(SpeechState.READY);
this.pluginManager_.stopRecognizer();
this.speechRecognitionManager_.start();
};
/**
* Called when the speech recognition has happened.
*
* @param {string} result The speech recognition result.
* @param {boolean} isFinal Whether the result is final or not.
*/
SpeechManager.prototype.onSpeechRecognized = function(result, isFinal) {
chrome.send('speechResult', [result, isFinal]);
if (isFinal)
this.speechRecognitionManager_.stop();
};
/**
* Called when the speech recognition has started.
*/
SpeechManager.prototype.onSpeechRecognitionStarted = function() {
this.setState_(SpeechState.RECOGNIZING);
};
/**
* Called when the speech recognition has ended.
*/
SpeechManager.prototype.onSpeechRecognitionEnded = function() {
// Restarts the hotword recognition.
if (this.state != SpeechState.STOPPING && this.pluginManager_) {
this.pluginManager_.startRecognizer();
this.audioManager_.start();
this.setState_(SpeechState.HOTWORD_RECOGNIZING);
} else {
this.audioManager_.stop();
}
chrome.send('setSpeechRecognitionState', ['off']);
};
/**
* Called when a speech has started.
*/
SpeechManager.prototype.onSpeechStarted = function() {
if (this.state == SpeechState.RECOGNIZING)
this.setState_(SpeechState.IN_SPEECH);
};
/**
* Called when a speech has ended.
*/
SpeechManager.prototype.onSpeechEnded = function() {
if (this.state == SpeechState.IN_SPEECH)
this.setState_(SpeechState.RECOGNIZING);
};
/**
* Called when an error happened during the speech recognition.
*
* @param {SpeechRecognitionError} e The error object.
*/
SpeechManager.prototype.onSpeechRecognitionError = function(e) {
if (this.state != SpeechState.STOPPING)
this.setState_(SpeechState.READY);
};
/**
* Called when the app-list bubble is shown.
*/
SpeechManager.prototype.onShown = function() {
this.shown_ = true;
if (!this.pluginManager_)
return;
if (this.state == SpeechState.HOTWORD_RECOGNIZING) {
console.warn('Already in recognition state...');
return;
}
this.pluginManager_.startRecognizer();
this.audioManager_.start();
this.setState_(SpeechState.HOTWORD_RECOGNIZING);
};
/**
* Called when the app-list bubble is hidden.
*/
SpeechManager.prototype.onHidden = function() {
this.shown_ = false;
if (this.pluginManager_)
this.pluginManager_.stopRecognizer();
// SpeechRecognition is asynchronous.
this.audioManager_.stop();
if (this.state == SpeechState.RECOGNIZING ||
this.state == SpeechState.IN_SPEECH) {
this.setState_(SpeechState.STOPPING);
this.speechRecognitionManager_.stop();
} else {
this.setState_(SpeechState.READY);
}
};
/**
* Toggles the current state of speech recognition.
*/
SpeechManager.prototype.toggleSpeechRecognition = function() {
if (this.state == SpeechState.RECOGNIZING ||
this.state == SpeechState.IN_SPEECH) {
this.audioManager_.stop();
this.speechRecognitionManager_.stop();
} else {
if (this.pluginManager_)
this.pluginManager_.stopRecognizer();
if (this.audioManager_.state == speech.AudioState.STOPPED)
this.audioManager_.start();
this.speechRecognitionManager_.start();
}
};
return {
SpeechManager: SpeechManager
};
});
|