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 manager of offline hotword speech recognizer plugin. | 6 * @fileoverview The manager of offline hotword speech recognizer plugin. |
7 */ | 7 */ |
8 | 8 |
9 cr.define('speech', function() { | 9 cr.define('speech', function() { |
10 'use strict'; | 10 'use strict'; |
11 | 11 |
12 /** | 12 /** |
13 * The type of the plugin state. | 13 * The type of the plugin state. |
14 ** @enum {number} | 14 ** @enum {number} |
15 */ | 15 */ |
16 var PluginState = { | 16 var PluginState = { |
17 UNINITIALIZED: 0, | 17 UNINITIALIZED: 0, |
18 LOADED: 1, | 18 LOADED: 1, |
19 SAMPLING_RATE_READY: 2, | 19 READY: 2, |
20 READY: 3, | 20 RECOGNIZING: 3 |
21 RECOGNIZING: 4 | |
22 }; | 21 }; |
23 | 22 |
24 /** | 23 /** |
25 * The command names of the plugin. | 24 * The command names of the plugin. |
26 * @enum {string} | 25 * @enum {string} |
27 */ | 26 */ |
28 var pluginCommands = { | 27 var pluginCommands = { |
29 SET_SAMPLING_RATE: 'h', | 28 SET_SAMPLING_RATE: 'h', |
30 SET_CONFIG: 'm', | 29 SET_CONFIG: 'm', |
31 START_RECOGNIZING: 'r', | 30 START_RECOGNIZING: 'r', |
32 STOP_RECOGNIZING: 's' | 31 STOP_RECOGNIZING: 's' |
33 }; | 32 }; |
34 | 33 |
35 /** | 34 /** |
36 * The regexp pattern of the hotword recognition result. | 35 * The regexp pattern of the hotword recognition result. |
37 */ | 36 */ |
38 var recognitionPattern = /^HotwordFiredEvent:/; | 37 var recognitionPattern = /^(HotwordFiredEvent:|hotword)/; |
39 | 38 |
40 /** | 39 /** |
41 * @constructor | 40 * @constructor |
42 */ | 41 */ |
43 function PluginManager(onReady, onRecognized) { | 42 function PluginManager(onReady, onRecognized) { |
44 this.state = PluginState.UNINITIALIZED; | 43 this.state = PluginState.UNINITIALIZED; |
45 this.onReady_ = onReady; | 44 this.onReady_ = onReady; |
46 this.onRecognized_ = onRecognized; | 45 this.onRecognized_ = onRecognized; |
47 this.samplingRate_ = null; | 46 this.samplingRate_ = null; |
48 this.config_ = null; | 47 this.config_ = null; |
(...skipping 11 matching lines...) Expand all Loading... |
60 recognizer.addEventListener('load', this.onLoad_.bind(this)); | 59 recognizer.addEventListener('load', this.onLoad_.bind(this)); |
61 }; | 60 }; |
62 | 61 |
63 /** | 62 /** |
64 * The event handler of the plugin status. | 63 * The event handler of the plugin status. |
65 * | 64 * |
66 * @param {Event} messageEvent the event object from the plugin. | 65 * @param {Event} messageEvent the event object from the plugin. |
67 * @private | 66 * @private |
68 */ | 67 */ |
69 PluginManager.prototype.onMessage_ = function(messageEvent) { | 68 PluginManager.prototype.onMessage_ = function(messageEvent) { |
70 if (this.state == PluginState.LOADED) { | |
71 if (messageEvent.data == 'stopped') | |
72 this.state = PluginState.SAMPLING_RATE_READY; | |
73 return; | |
74 } | |
75 | |
76 if (messageEvent.data == 'audio') { | 69 if (messageEvent.data == 'audio') { |
77 if (this.state < PluginState.READY) | 70 var wasNotReady = this.state < PluginState.READY; |
| 71 this.state = PluginState.RECOGNIZING; |
| 72 if (wasNotReady) |
78 this.onReady_(this); | 73 this.onReady_(this); |
79 this.state = PluginState.RECOGNIZING; | 74 } else if (messageEvent.data == 'stopped' && |
80 } else if (messageEvent.data == 'stopped') { | 75 this.state == PluginState.RECOGNIZING) { |
81 this.state = PluginState.READY; | 76 this.state = PluginState.READY; |
82 } else if (recognitionPattern.exec(messageEvent.data)) { | 77 } else if (recognitionPattern.exec(messageEvent.data)) { |
83 this.onRecognized_(); | 78 this.onRecognized_(); |
84 } | 79 } |
85 }; | 80 }; |
86 | 81 |
87 /** | 82 /** |
88 * The event handler when the plugin is loaded. | 83 * The event handler when the plugin is loaded. |
89 * | 84 * |
90 * @private | 85 * @private |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 PluginManager.prototype.sendAudioData = function(event) { | 146 PluginManager.prototype.sendAudioData = function(event) { |
152 if (this.state == PluginState.RECOGNIZING) | 147 if (this.state == PluginState.RECOGNIZING) |
153 $('recognizer').postMessage(event.data.buffer); | 148 $('recognizer').postMessage(event.data.buffer); |
154 }; | 149 }; |
155 | 150 |
156 return { | 151 return { |
157 PluginManager: PluginManager, | 152 PluginManager: PluginManager, |
158 PluginState: PluginState, | 153 PluginState: PluginState, |
159 }; | 154 }; |
160 }); | 155 }); |
OLD | NEW |