-
Notifications
You must be signed in to change notification settings - Fork 2
/
speech.js
115 lines (99 loc) · 3.34 KB
/
speech.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* webkitSpeechRecognition Wrapper class.
* Created by: Daniel Hodvogner, <[email protected]>
* Feel free to use =).
**/
function SpeechRecognition() {
var self = this;
self.recognition = null;
self.options = {
language : "en-US", //Default language. see https://github.com/GoogleChrome/webplatform-samples/blob/master/webspeechdemo/webspeechdemo.html#L138 for other languages
continuous : true, //if false, speech recognition will end when the user stops talking, otherwise it keeps on recording until it is stopped manually
interimResults: false, //if false, the only results returned by the recognizer are final and will not change
}
/** Private Methods **/
/**
* Constructor
**/
self._create = function() {
if(typeof window.webkitSpeechRecognition != "undefined") {
if(self.recognition != null) return;
self.recognition = new webkitSpeechRecognition();
self._set();
//Events
self.recognition.onresult = self._handleOnResult;
self.recognition.onstart = function() { self.fire("start"); self.fire("started"); }
self.recognition.onend = function() { self.fire("stop"); self.fire("stopped"); }
self.recognition.onerror = function(evt) {
//evt.error == 'no-speech' //Can't recognize anything.
//evt.error == 'audio-capture' //No microphone.
//evt.error == 'not-allowed' //User denied the access of their microphone.
self.fire("error", evt.error);
}
} else {
var msg = "Cannot access the speech recognition API. Are you using Google Chrome 25 or higher?"
console.error(msg); throw new Error(msg);
}
}
/**
* Apply options to SpeechRecognition instance if it isn't null.
**/
self._set = function() {
if(self.recognition == null) return;
self.recognition.continuous = self.options.continuous;
self.recognition.interimResults = self.options.interimResults;
self.recognition.lang = self.options.language;
}
/**
* SpeechRecognition.onresult event handler.
**/
self._handleOnResult = function(evt) {
//@DEBUG console.log("_handleOnResult:", evt, this, self);
self.fire("result", evt);
}
/** Public Methods **/
/**
* Set options, and apply if it possible.
**/
self.set = function(params, value) {
if(typeof params != "object") {
self.options[params] = value;
} else {
for( name in params) {
self.options[name] = params[name];
}
}
self.fire("optionschanged");
}
/**
* Start Speech Recognition.
**/
self.start = function() {
if(self.recognition == null) self._create();
self.recognition.start();
self.fire("starting");
}
/**
* Stop Speech Recognition.
**/
self.stop = function() {
if(self.recognition == null) return;
self.recognition.stop();
self.fire("stopping");
}
/** +--------------------------------+
* | Callbacks |
* +--------------------------------+
**/
self._events = {};
self.on = function(eventName, callback) {
self._events[eventName] = self._events[eventName] || [];
self._events[eventName].push(callback);
};
self.fire = function(eventName, _) {
var events = self._events[eventName];
var args = Array.prototype.slice.call(arguments, 1);
if (!events) return;
for (var i = 0, len = events.length; i < len; i++) events[i].apply(null, args);
};
}