-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
69 lines (62 loc) · 2.55 KB
/
script.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
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const message = document.getElementById("message");
const output = document.getElementById("result");
const image1 = document.getElementById("image1");
startRecognition = () => {
if (SpeechRecognition !== undefined) { // test if speechrecognitio is supported
let recognition = new SpeechRecognition();
recognition.lang = 'en-US'; // which language is used?
recognition.interimResults = false; // https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition/interimResults
recognition.continuous = false; // https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition/continuous
recognition.onstart = () => {
message.innerHTML = `Starting listening, speak in the microphone please<br>Say "help me" for help`;
output.classList.add("hide"); // hide the output
};
recognition.onspeechend = () => {
message.innerHTML = `I stopped listening `;
recognition.stop();
};
recognition.onresult = (result) => {
let transcript = result.results[0][0].transcript;
let confidenceTranscript= Math.floor(result.results[0][0].confidence * 100); // calc. 'confidence'
output.classList.remove("hide"); // show the output
output.innerHTML = `I'm ${confidenceTranscript}% certain you just said: <b>${transcript}</b>`;
actionSpeech(transcript);
};
recognition.start();
}
else { // speechrecognition is not supported
message.innerHTML = "sorry speech to text is not supported in this browser";
}
};
// process speech results
actionSpeech = (speechText) => {
speechText = speechText.toLowerCase().trim(); // trim spaces + to lower case
console.log(speechText); // debug
switch(speechText){
// switch evaluates using stric comparison, ===
case "black":
document.body.style.background = "#000000";
document.body.style.color="#FFFFFF";
break;
case "reset":
document.body.style.background = "#ffe6ab";
document.body.style.color="#000000";
image1.classList.add("hide"); // hide image (if any)
break;
case "image": // let op, "fall-through"
case "caroline": // let op, "fall-through"
image1.src = "./img/caroline.jpg";
image1.style.width = "400px";
image1.classList.remove("hide") // show image
break;
case "next page":
window.open("https://www.ma-web.nl/", "_self");
break;
case "help me":
alert("Valid speech commands: black, reset, next page");
break;
default:
// do nothing yet
}
}