1 // Copyright (c) 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.
5 function VoiceInput(keyboard
) {
6 this.finaResult_
= null;
7 this.recognizing_
= false;
8 this.keyboard_
= keyboard
;
9 this.recognition_
= new webkitSpeechRecognition();
10 this.recognition_
.onstart
= this.onStartHandler
.bind(this);
11 this.recognition_
.onresult
= this.onResultHandler
.bind(this);
12 this.recognition_
.onerror
= this.onErrorHandler
.bind(this);
13 this.recognition_
.onend
= this.onEndHandler
.bind(this);
16 VoiceInput
.prototype = {
18 * Event handler for mouse/touch down events.
21 if (this.recognizing_
) {
22 this.recognition_
.stop();
25 this.recognition_
.start();
29 * Speech recognition started. Change microphone key's icon.
31 onStartHandler: function() {
32 this.recognizing_
= true;
33 this.finalResult_
= '';
34 if (!this.keyboard_
.classList
.contains('audio'))
35 this.keyboard_
.classList
.add('audio');
39 * Speech recognizer returns a result.
40 * @param{Event} e The SpeechRecognition event that is raised each time
42 * are any changes to interim or final results.
44 onResultHandler: function(e
) {
45 for (var i
= e
.resultIndex
; i
< e
.results
.length
; i
++) {
46 if (e
.results
[i
].isFinal
)
47 this.finalResult_
= e
.results
[i
][0].transcript
;
49 insertText(this.finalResult_
);
53 * Speech recognizer returns an error.
54 * @param{Event} e The SpeechRecognitionError event that is raised each time
57 onErrorHandler: function(e
) {
58 console
.error('error code = ' + e
.error
);
62 * Speech recognition ended. Reset microphone key's icon.
64 onEndHandler: function() {
65 if (this.keyboard_
.classList
.contains('audio'))
66 this.keyboard_
.classList
.remove('audio');
67 this.recognizing_
= false;