-
Notifications
You must be signed in to change notification settings - Fork 1
/
speechAPI.js
72 lines (57 loc) · 1.79 KB
/
speechAPI.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
//based heavily on the google demo
//https://www.google.com/intl/en/chrome/demos/speech.html
var recognition;
function initSpeechRecognition(options, resultsElement){
//options is an object with properties continuous (boolean), interimResults (boolean), lang (language encoding, eg en-AU), keep alive (boolean)
//resultsElement is an element we'll return results to
var final_transcript = "";
var interim_transcript = "";
recognition = new webkitSpeechRecognition();
recognition.continuous = options.continuous;
recognition.interimResults = options.interimResults;
recognition.lang = options.lang
recognition.start();
recognition.onstart = function() {
recognizing = true;
//you could use an icon or message to indicate recording is on
};
recognition.onerror = function(event) {
if (event.error == 'no-speech') {
// respond to the no-speech error
}
if (event.error == 'audio-capture') {
// respond to the audio-capture error
}
if (event.error == 'not-allowed') {
// respond to the not-allowed error
}
};
recognition.onend = function(event) {
//if you want to respond to the end event, do that here
};
recognition.onaudioend = function(event) {
recognition.stop();
if (options.keepAlive) {
//restart recognition
recognition.start();
}
}
recognition.onresult = function(event) {
var interim_transcript = '';
if (typeof(event.results) == 'undefined') {
recognition.onend = null;
recognition.stop();
return;
}
for (var i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
final_transcript += event.results[i][0].transcript;
} else {
interim_transcript += event.results[i][0].transcript;
}
}
if (interim_transcript !== "") {
resultsElement.innerHTML = interim_transcript;
}
};
}