-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpeechRecognizer.js
126 lines (118 loc) · 4.03 KB
/
SpeechRecognizer.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
116
117
118
119
120
121
122
123
124
125
126
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
class SpeechRecognizer {
constructor(eventListener = () => {}, lang = 'de-De') {
this.eventListener = eventListener;
this.oldResults = [];
this.reset(lang);
}
reset(lang = 'de-De') {
this.working = false;
this.listening = false;
this.error = 'none';
try { this.recognition.stop(); } catch (e) {}
this.currentFinal = '';
this.currentInterim = '';
this.currentResult = '';
try {
this.recognition = new SpeechRecognition();
this.recognition.continuous = true;
this.recognition.interimResults = true;
this.recognition.lang = lang;
this.recognition.addEventListener('start', e => this.startListener(e))
this.recognition.addEventListener('end', e => this.endListener(e))
this.recognition.addEventListener('result', e => this.resultListener(e))
this.recognition.addEventListener('error', e => this.errorListener(e))
this.working = true;
} catch (e) {
this.error = 'API not available';
this.eventListener('error', this.error, 'The WebSpeech API is not available in your browser. Use Chrome.');
}
}
start() {
if (this.working) {
try {
this.recognition.start();
} catch (e) {
this.working = false;
this.listening = false;
this.error = 'API not working';
this.eventListener('error', this.error, 'The WebSpeech API in your browser is not working. Use Chrome.');
}
}
}
stop() {
if (this.working) {
try {
this.recognition.stop();
} catch (e) {
this.working = false;
this.listening = false;
this.error = 'API not working';
this.eventListener('error', this.error, 'The WebSpeech API in your browser is not working. Use Chrome.');
}
}
}
startListener(evt) {
if (this.working) {
this.listening = true;
this.currentFinal = '';
this.currentInterim = '';
this.currentResult = '';
this.eventListener('start', 'recognition start');
}
}
endListener(evt) {
if (this.working) {
this.listening = false;
if (this.currentFinal)
this.oldResults.push(this.currentFinal);
this.eventListener('end', 'recognition end');
}
}
resultListener(evt) {
if (this.working) {
if (typeof evt.results === 'undefined') {
this.working = false;
this.listening = false;
try { this.recognition.stop(); } catch (e) {}
this.error = 'API not working';
this.eventListener('error', this.error, 'The WebSpeech API in your browser is not working. Use Chrome.');
return;
}
this.currentInterim = '';
for (let i = evt.resultIndex; i < evt.results.length; ++i) {
if (evt.results[i].isFinal)
this.currentFinal += evt.results[i][0].transcript;
else
this.currentInterim += evt.results[i][0].transcript;
}
this.currentResult = this.currentFinal + this.currentInterim;
this.eventListener('result', this.currentFinal, this.currentInterim);
}
}
errorListener(evt) {
if (this.working) {
if (evt.error == 'no-speech') {
this.error = 'no speech detected';
this.working = false;
this.listening = false;
try { this.recognition.stop(); } catch (e) {}
this.eventListener('error', this.error, 'No speech detected. Check your microphone or try again.');
}
if (evt.error == 'audio-capture') {
this.error = 'no microphone found';
this.working = false;
this.listening = false;
try { this.recognition.stop(); } catch (e) {}
this.eventListener('error', this.error, 'No microphone found. Check your settings.');
}
if (evt.error == 'not-allowed') {
this.error = 'no microphone permission';
this.working = false;
this.listening = false;
try { this.recognition.stop(); } catch (e) {}
this.eventListener('error', this.error, 'You denied the permission for microphone access.');
}
}
}
}