OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview The class to Manage both offline / online speech recognition. |
| 7 */ |
| 8 |
| 9 <include src="plugin_manager.js"/> |
| 10 <include src="audio_manager.js"/> |
| 11 <include src="speech_recognition_manager.js"/> |
| 12 |
| 13 cr.define('speech', function() { |
| 14 'use strict'; |
| 15 |
| 16 /** |
| 17 * The state of speech recognition. |
| 18 * |
| 19 * @enum {string} |
| 20 */ |
| 21 var SpeechState = { |
| 22 UNINITIALIZED: 'UNINITIALIZED', |
| 23 READY: 'READY', |
| 24 HOTWORD_RECOGNIZING: 'HOTWORD_RECOGNIZING', |
| 25 RECOGNIZING: 'RECOGNIZING' |
| 26 }; |
| 27 |
| 28 /** |
| 29 * @constructor |
| 30 */ |
| 31 function SpeechManager() { |
| 32 this.audioManager_ = new speech.AudioManager( |
| 33 this.onHotwordRecognizerReady_.bind(this), |
| 34 this.onHotwordRecognizing_.bind(this), |
| 35 this.onHotwordRecognized_.bind(this)); |
| 36 this.speechRecognitionManager_ = new speech.SpeechRecognitionManager(this); |
| 37 this.setState_(SpeechState.UNINITIALIZED); |
| 38 } |
| 39 |
| 40 /** |
| 41 * Updates the state. |
| 42 * |
| 43 * @param {SpeechState} newState The new state. |
| 44 * @private |
| 45 */ |
| 46 SpeechManager.prototype.setState_ = function(newState) { |
| 47 this.state = newState; |
| 48 console.log('speech state: ' + newState); |
| 49 }; |
| 50 |
| 51 /** |
| 52 * Called when the hotword recognizer is ready. |
| 53 * |
| 54 * @private |
| 55 */ |
| 56 SpeechManager.prototype.onHotwordRecognizerReady_ = function() { |
| 57 this.setState_(SpeechState.READY); |
| 58 }; |
| 59 |
| 60 /** |
| 61 * Called when the hotword is recognized. |
| 62 * |
| 63 * @param {number} confidence The confidence store of the recognition. |
| 64 * @private |
| 65 */ |
| 66 SpeechManager.prototype.onHotwordRecognized_ = function(confidence) { |
| 67 if (this.state != SpeechState.HOTWORD_RECOGNIZING) |
| 68 return; |
| 69 this.audioManager_.stop(); |
| 70 this.setState_(SpeechState.READY); |
| 71 this.speechRecognitionManager_.start(); |
| 72 }; |
| 73 |
| 74 /** |
| 75 * Called when the hotword recognition has started. |
| 76 * |
| 77 * @private |
| 78 */ |
| 79 SpeechManager.prototype.onHotwordRecognizing_ = function() { |
| 80 this.setState_(SpeechState.HOTWORD_RECOGNIZING); |
| 81 }; |
| 82 |
| 83 /** |
| 84 * Called when the speech recognition has happened. |
| 85 * |
| 86 * @param {string} result The speech recognition result. |
| 87 * @param {boolean} isFinal Whether the result is final or not. |
| 88 */ |
| 89 SpeechManager.prototype.onSpeechRecognized = function(result, isFinal) { |
| 90 // TODO(mukai): updates the UI, make requests, and/or sends the result |
| 91 // to the web_ui handler. |
| 92 console.log('speech result: ' + result + ' ' + |
| 93 (isFinal ? 'final' : 'interim')); |
| 94 }; |
| 95 |
| 96 /** |
| 97 * Called when the speech recognition has started. |
| 98 */ |
| 99 SpeechManager.prototype.onSpeechRecognitionStarted = function() { |
| 100 this.setState_(SpeechState.RECOGNIZING); |
| 101 }; |
| 102 |
| 103 /** |
| 104 * Called when the speech recognition has ended. |
| 105 */ |
| 106 SpeechManager.prototype.onSpeechRecognitionEnded = function() { |
| 107 // Restarts the hotword recognition. |
| 108 this.audioManager_.start(); |
| 109 }; |
| 110 |
| 111 /** |
| 112 * Called when an error happened during the speech recognition. |
| 113 * |
| 114 * @param {Error} e The error object. |
| 115 */ |
| 116 SpeechManager.prototype.onSpeechRecognitionError = function(e) { |
| 117 this.setState_(SpeechState.UNINITIALIZED); |
| 118 }; |
| 119 |
| 120 /** |
| 121 * Starts the speech recognition session. |
| 122 */ |
| 123 SpeechManager.prototype.start = function() { |
| 124 if (this.state == SpeechState.UNINITIALIZED) { |
| 125 console.warn('hotword recognizer is not yet initialized'); |
| 126 return; |
| 127 } |
| 128 |
| 129 if (this.state != SpeechState.READY) { |
| 130 console.warn('Already in recognition state...'); |
| 131 return; |
| 132 } |
| 133 |
| 134 this.audioManager_.start(); |
| 135 }; |
| 136 |
| 137 /** |
| 138 * Stops the speech recognition session. |
| 139 */ |
| 140 SpeechManager.prototype.stop = function() { |
| 141 if (this.state == SpeechState.UNINITIALIZED) |
| 142 return; |
| 143 |
| 144 this.audioManager_.stop(); |
| 145 this.speechRecognitionManager_.stop(); |
| 146 this.setState_(SpeechState.READY); |
| 147 }; |
| 148 |
| 149 return { |
| 150 SpeechManager: SpeechManager |
| 151 }; |
| 152 }); |
OLD | NEW |