-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (111 loc) · 4.87 KB
/
script.js
File metadata and controls
133 lines (111 loc) · 4.87 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
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
127
128
129
130
131
132
133
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
var phrases = [
'apple',
'pear',
'pineapple',
'banana',
'tomato',
'orange',
'potato',
'mango'
];
var phrasePara = document.querySelector('.phrase');
var resultPara = document.querySelector('.result');
var diagnosticPara = document.querySelector('.output');
var testBtn = document.querySelector('button');
function randomPhrase() {
var number = Math.floor(Math.random() * phrases.length);
return number;
}
function testSpeech() {
testBtn.disabled = true;
testBtn.textContent = 'Test in progress';
var phrase = phrases[randomPhrase()];
// To ensure case consistency while checking with the returned output text
phrase = phrase.toLowerCase();
phrasePara.textContent = phrase;
resultPara.textContent = 'Good or bad?';
resultPara.style.background = 'rgba(0,1,0,0.2)';
diagnosticPara.textContent = '...diagnostic messages';
var grammar = '#JSGF V1.0; grammar phrase; public <phrase> = ' + phrase +';';
var recognition = new SpeechRecognition();
var speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.lang = 'en-US';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
recognition.start();
recognition.onresult = function(event) {
// The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object
// The SpeechRecognitionResultList object contains SpeechRecognitionResult objects.
// It has a getter so it can be accessed like an array
// The first [0] returns the SpeechRecognitionResult at position 0.
// Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects that contain individual results.
// These also have getters so they can be accessed like arrays.
// The second [0] returns the SpeechRecognitionAlternative at position 0.
// We then return the transcript property of the SpeechRecognitionAlternative object
var speechResult = event.results[0][0].transcript.toLowerCase();
diagnosticPara.textContent = 'Speech received: ' + speechResult + '.';
console.log('Confidence: ' + event.results[0][0].confidence);
console.log(speechResult);
if(speechResult === phrase) {
if (event.results[0][0].confidence >= 0.95) {
resultPara.textContent = 'Good!';
resultPara.style.background = 'lime';
}else {
resultPara.textContent = 'Fair!';
resultPara.style.background = 'yellow';
}
} else {
resultPara.textContent = 'Bad!';
resultPara.style.background = 'red';
}
console.log('Confidence: ' + event.results[0][0].confidence);
}
recognition.onspeechend = function() {
recognition.stop();
testBtn.disabled = false;
testBtn.textContent = 'Start new test';
}
recognition.onerror = function(event) {
testBtn.disabled = false;
testBtn.textContent = 'Start new test';
diagnosticPara.textContent = 'Error occurred in recognition: ' + event.error;
}
recognition.onaudiostart = function(event) {
//Fired when the user agent has started to capture audio.
console.log('SpeechRecognition.onaudiostart');
}
recognition.onaudioend = function(event) {
//Fired when the user agent has finished capturing audio.
console.log('SpeechRecognition.onaudioend');
}
recognition.onend = function(event) {
//Fired when the speech recognition service has disconnected.
console.log('SpeechRecognition.onend');
}
recognition.onnomatch = function(event) {
//Fired when the speech recognition service returns a final result with no significant recognition. This may involve some degree of recognition, which doesn't meet or exceed the confidence threshold.
console.log('SpeechRecognition.onnomatch');
}
recognition.onsoundstart = function(event) {
//Fired when any sound — recognisable speech or not — has been detected.
console.log('SpeechRecognition.onsoundstart');
}
recognition.onsoundend = function(event) {
//Fired when any sound — recognisable speech or not — has stopped being detected.
console.log('SpeechRecognition.onsoundend');
}
recognition.onspeechstart = function (event) {
//Fired when sound that is recognised by the speech recognition service as speech has been detected.
console.log('SpeechRecognition.onspeechstart');
}
recognition.onstart = function(event) {
//Fired when the speech recognition service has begun listening to incoming audio with intent to recognize grammars associated with the current SpeechRecognition.
console.log('SpeechRecognition.onstart');
}
}
testBtn.addEventListener('click', testSpeech);