Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: ui/keyboard/resources/voice_input.js

Issue 15176004: Web Component Virtual Keyboard (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
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);
14 };
15
16 VoiceInput.prototype = {
17 /**
18 * Event handler for mouse/touch down events.
19 */
20 onDown: function() {
21 if (this.recognizing_) {
22 this.recognition_.stop();
23 return;
24 }
25 this.recognition_.start();
26 },
27
28 /**
29 * Speech recognition started. Change mic key's icon.
30 */
31 onStartHandler: function() {
32 this.recognizing_ = true;
33 this.finalResult_ = '';
34 if (!this.keyboard_.classList.contains('audio'))
35 this.keyboard_.classList.add('audio');
36 },
37
38 /**
39 * Speech recognizer returns a result.
40 * @param{Event} e The SpeechRecognition event that is raised each time
41 * there
42 * are any changes to interim or final results.
43 */
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;
48 }
49 for (var i = 0; i < this.finalResult_.length; i++) {
50 sendKey(this.finalResult_.charAt(i));
51 }
52 },
53
54 /**
55 * Speech recognizer returns an error.
56 * @param{Event} e The SpeechRecognitionError event that is raised each time
57 * there is an error.
58 */
59 onErrorHandler: function(e) {
60 console.error('error code = ' + e.error);
61 },
62
63 /**
64 * Speech recognition ended. Reset mic key's icon.
65 */
66 onEndHandler: function() {
67 if (this.keyboard_.classList.contains('audio'))
68 this.keyboard_.classList.remove('audio');
69 this.recognizing_ = false;
70 }
71 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698