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 } | |
27 | |
28 /** | |
29 * Called when new speech recognition results arrive. | |
30 * | |
31 * @param {Event} speech_event The event to contain the recognition results. | |
32 * @private | |
33 */ | |
34 SpeechRecognitionManager.prototype.onRecognizerResult_ = function( | |
35 speech_event) { | |
36 // Do not collect interim result for now. | |
37 var result = ''; | |
38 var isFinal = false; | |
39 for (var i = 0; i < speech_event.results.length; i++) { | |
40 if (speech_event.results[i].isFinal) | |
41 isFinal = true; | |
42 result += speech_event.results[i][0].transcript; | |
43 } | |
44 if (this.delegate_) | |
45 this.delegate_.onSpeechRecognized(result, isFinal); | |
46 }; | |
47 | |
48 /** | |
49 * Starts the speech recognition through webkitSpeechRecognition. | |
50 */ | |
51 SpeechRecognitionManager.prototype.start = function() { | |
52 if (this.delegate_) { | |
53 this.recognizer_.onstart = | |
54 this.delegate_.onSpeechRecognitionStarted.bind(this.delegate_); | |
55 this.recognizer_.onend = | |
56 this.delegate_.onSpeechRecognitionEnded.bind(this.delegate_); | |
57 this.recognizer_.onerror = | |
58 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.
| |
59 } | |
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 |