Chromium Code Reviews| Index: chrome/browser/resources/app_list/speech_recognition_manager.js |
| diff --git a/chrome/browser/resources/app_list/speech_recognition_manager.js b/chrome/browser/resources/app_list/speech_recognition_manager.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..243b30003edccccabbb3178295d4c33c01bbba44 |
| --- /dev/null |
| +++ b/chrome/browser/resources/app_list/speech_recognition_manager.js |
| @@ -0,0 +1,73 @@ |
| +// 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 manager of speech recognition. |
| + */ |
| + |
| +cr.define('speech', function() { |
| + 'use strict'; |
| + |
| + /** |
| + * @constructor |
| + */ |
| + function SpeechRecognitionManager(delegate) { |
| + this.isActive = true; |
| + this.delegate_ = delegate; |
| + |
| + this.recognizer_ = new window.webkitSpeechRecognition(); |
| + this.recognizer_.continuous = true; |
| + this.recognizer_.interimResults = true; |
| + // TODO(mukai): should switch to the user's UI language. |
| + this.recognizer_.lang = 'en_US'; |
| + |
| + this.recognizer_.onresult = this.onRecognizerResult_.bind(this); |
| + } |
| + |
| + /** |
| + * Called when new speech recognition results arrive. |
| + * |
| + * @param {Event} speech_event The event to contain the recognition results. |
| + * @private |
| + */ |
| + SpeechRecognitionManager.prototype.onRecognizerResult_ = function( |
| + speech_event) { |
| + // Do not collect interim result for now. |
| + var result = ''; |
| + var isFinal = false; |
| + for (var i = 0; i < speech_event.results.length; i++) { |
| + if (speech_event.results[i].isFinal) |
| + isFinal = true; |
| + result += speech_event.results[i][0].transcript; |
| + } |
| + if (this.delegate_) |
| + this.delegate_.onSpeechRecognized(result, isFinal); |
| + }; |
| + |
| + /** |
| + * Starts the speech recognition through webkitSpeechRecognition. |
| + */ |
| + SpeechRecognitionManager.prototype.start = function() { |
| + if (this.delegate_) { |
| + this.recognizer_.onstart = |
| + this.delegate_.onSpeechRecognitionStarted.bind(this.delegate_); |
| + this.recognizer_.onend = |
| + this.delegate_.onSpeechRecognitionEnded.bind(this.delegate_); |
| + this.recognizer_.onerror = |
| + this.delegate_.onSpeechRecognitionError.bind(this.delegate_); |
|
xiyuan
2013/10/29 18:10:07
nit: move these into constructor so that we have o
Jun Mukai
2013/10/30 01:01:32
Done.
|
| + } |
| + this.recognizer_.start(); |
| + }; |
| + |
| + /** |
| + * Stops the ongoing speech recognition. |
| + */ |
| + SpeechRecognitionManager.prototype.stop = function() { |
| + this.recognizer_.abort(); |
| + }; |
| + |
| + return { |
| + SpeechRecognitionManager: SpeechRecognitionManager |
| + }; |
| +}); |