-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecognizer.js
More file actions
48 lines (42 loc) · 1.21 KB
/
Recognizer.js
File metadata and controls
48 lines (42 loc) · 1.21 KB
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
define('Recognizer', function() {
function convertLocale(locale) {
return (locale + '').split('-')[0];
}
function Recognizer(locale) {
this.recognition = new webkitSpeechRecognition();
if (locale) {
this.recognition.lang = locale;
}
this.recognition.interimResults = false;
this.recognition.maxAlternatives = 1;
this.isStarted = false;
this.id = 0;
this.onResult = null;
}
Recognizer.prototype.start = function() {
var self = this;
this.isStarted = true;
this.recognition.start();
this.recognition.onend = function() {
if (self.isStarted) {
self.recognition.start();
}
};
this.recognition.onresult = function(event) {
if (self.onResult) {
self.onResult({
text: event.results[0][0].transcript,
locale: convertLocale(self.recognition.lang),
id: self.id++
})
}
}
};
Recognizer.prototype.stop = function() {
if (this.isStarted) {
this.isStarted = false;
this.recognition.stop();
}
}
return Recognizer;
});