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 manager of speech recognition. |
| 7 */ |
| 8 |
| 9 cr.define('speech', function() { |
| 10 'use strict'; |
| 11 |
| 12 /** |
| 13 * @constructor |
| 14 */ |
| 15 function SpeechRecognitionManager(delegate) { |
| 16 this.isActive = true; |
| 17 this.delegate_ = delegate; |
| 18 |
| 19 this.recognizer_ = new window.webkitSpeechRecognition(); |
| 20 this.recognizer_.continuous = true; |
| 21 this.recognizer_.interimResults = true; |
| 22 // TODO(mukai): should switch to the user's UI language. |
| 23 this.recognizer_.lang = 'en_US'; |
| 24 |
| 25 this.recognizer_.onresult = this.onRecognizerResult_.bind(this); |
| 26 if (this.delegate_) { |
| 27 this.recognizer_.onstart = |
| 28 this.delegate_.onSpeechRecognitionStarted.bind(this.delegate_); |
| 29 this.recognizer_.onend = |
| 30 this.delegate_.onSpeechRecognitionEnded.bind(this.delegate_); |
| 31 this.recognizer_.onerror = |
| 32 this.delegate_.onSpeechRecognitionError.bind(this.delegate_); |
| 33 } |
| 34 } |
| 35 |
| 36 /** |
| 37 * Called when new speech recognition results arrive. |
| 38 * |
| 39 * @param {Event} speechEvent The event to contain the recognition results. |
| 40 * @private |
| 41 */ |
| 42 SpeechRecognitionManager.prototype.onRecognizerResult_ = function( |
| 43 speechEvent) { |
| 44 // Do not collect interim result for now. |
| 45 var result = ''; |
| 46 var isFinal = false; |
| 47 for (var i = 0; i < speechEvent.results.length; i++) { |
| 48 if (speechEvent.results[i].isFinal) |
| 49 isFinal = true; |
| 50 result += speechEvent.results[i][0].transcript; |
| 51 } |
| 52 if (this.delegate_) |
| 53 this.delegate_.onSpeechRecognized(result, isFinal); |
| 54 }; |
| 55 |
| 56 /** |
| 57 * Starts the speech recognition through webkitSpeechRecognition. |
| 58 */ |
| 59 SpeechRecognitionManager.prototype.start = function() { |
| 60 this.recognizer_.start(); |
| 61 }; |
| 62 |
| 63 /** |
| 64 * Stops the ongoing speech recognition. |
| 65 */ |
| 66 SpeechRecognitionManager.prototype.stop = function() { |
| 67 this.recognizer_.abort(); |
| 68 }; |
| 69 |
| 70 return { |
| 71 SpeechRecognitionManager: SpeechRecognitionManager |
| 72 }; |
| 73 }); |
OLD | NEW |