1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 cr
.define('hotword', function() {
9 * Class used to manage speaker training. Starts a hotwording session
10 * if training is on, and automatically restarts the detector when a
11 * a hotword is triggered.
12 * @param {!hotword.StateManager} stateManager
14 * @extends {hotword.BaseSessionManager}
16 function TrainingManager(stateManager
) {
18 * Chrome event listeners. Saved so that they can be de-registered when
19 * hotwording is disabled.
22 this.finalizedSpeakerModelListener_
=
23 this.handleFinalizeSpeakerModel_
.bind(this);
25 hotword
.BaseSessionManager
.call(this,
27 hotword
.constants
.SessionSource
.TRAINING
);
31 * Handles a success event on mounting the file system event.
32 * @param {FileSystem} fs The FileSystem object.
35 TrainingManager
.onRequestFileSystemSuccess_ = function(fs
) {
36 fs
.root
.getFile(hotword
.constants
.SPEAKER_MODEL_FILE_NAME
, {create
: false},
37 TrainingManager
.deleteFile_
, TrainingManager
.fileErrorHandler_
);
39 for (var i
= 0; i
< hotword
.constants
.NUM_TRAINING_UTTERANCES
; ++i
) {
40 fs
.root
.getFile(hotword
.constants
.UTTERANCE_FILE_PREFIX
+ i
+
41 hotword
.constants
.UTTERANCE_FILE_EXTENSION
,
43 TrainingManager
.deleteFile_
, TrainingManager
.fileErrorHandler_
);
49 * @param {FileEntry} fileEntry The FileEntry object.
52 TrainingManager
.deleteFile_ = function(fileEntry
) {
53 if (fileEntry
.isFile
) {
54 hotword
.debug('File found: ' + fileEntry
.fullPath
);
55 if (hotword
.DEBUG
|| window
.localStorage
['hotword.DEBUG']) {
56 fileEntry
.getMetadata(function(md
) {
57 hotword
.debug('File size: ' + md
.size
);
60 fileEntry
.remove(function() {
61 hotword
.debug('File removed: ' + fileEntry
.fullPath
);
62 }, TrainingManager
.fileErrorHandler_
);
67 * Handles a failure event on mounting the file system event.
68 * @param {FileError} e The FileError object.
71 TrainingManager
.fileErrorHandler_ = function(e
) {
72 hotword
.debug('File error: ' + e
.code
);
76 * Handles a request to delete the speaker model.
78 TrainingManager
.handleDeleteSpeakerModel = function() {
79 window
.webkitRequestFileSystem(PERSISTENT
,
80 hotword
.constants
.FILE_SYSTEM_SIZE_BYTES
,
81 TrainingManager
.onRequestFileSystemSuccess_
,
82 TrainingManager
.fileErrorHandler_
);
85 TrainingManager
.prototype = {
86 __proto__
: hotword
.BaseSessionManager
.prototype,
90 return this.stateManager
.isTrainingEnabled();
94 updateListeners: function() {
95 hotword
.BaseSessionManager
.prototype.updateListeners
.call(this);
98 // Detect when the speaker model needs to be finalized.
99 if (!chrome
.hotwordPrivate
.onFinalizeSpeakerModel
.hasListener(
100 this.finalizedSpeakerModelListener_
)) {
101 chrome
.hotwordPrivate
.onFinalizeSpeakerModel
.addListener(
102 this.finalizedSpeakerModelListener_
);
104 this.startSession(hotword
.constants
.RecognizerStartMode
.NEW_MODEL
);
106 chrome
.hotwordPrivate
.onFinalizeSpeakerModel
.removeListener(
107 this.finalizedSpeakerModelListener_
);
112 handleHotwordTrigger: function(log
) {
113 if (this.enabled()) {
114 hotword
.BaseSessionManager
.prototype.handleHotwordTrigger
.call(
116 this.startSession(hotword
.constants
.RecognizerStartMode
.ADAPT_MODEL
);
121 startSession: function(opt_mode
) {
122 this.stateManager
.startSession(
125 chrome
.hotwordPrivate
.setHotwordSessionState(true, function() {});
127 this.handleHotwordTrigger
.bind(this),
128 this.handleSpeakerModelSaved_
.bind(this),
133 * Handles a hotwordPrivate.onFinalizeSpeakerModel event.
136 handleFinalizeSpeakerModel_: function() {
138 this.stateManager
.finalizeSpeakerModel();
142 * Handles a hotwordPrivate.onFinalizeSpeakerModel event.
145 handleSpeakerModelSaved_: function() {
147 chrome
.hotwordPrivate
.notifySpeakerModelSaved();
152 TrainingManager
: TrainingManager