-
Notifications
You must be signed in to change notification settings - Fork 1
/
ron2.js
71 lines (60 loc) · 2.96 KB
/
ron2.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
//talking ron code!!
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition(); //speech recognition api
var isListening = false; //tracks whether or not Ron should listen
function listen() {
if (!isListening) {
//if listening is disabled
isListening = true;
speak(); //start speech loop
document.getElementById('talkBtn').value = "Stop Listening"; //change button text
} else {
//if listening already
isListening = false;
document.getElementById("ronImg").src = "ron.png"; //default state
recognition.abort() //no more listening
document.getElementById('talkBtn').value = "Stopping..."; //change button text
}
}
function speak() {
if (isListening) { //only run if listening
try { //if browser supports the speech recognition feature
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.start(); //listen
recognition.onstart = function() { //when voice recognition activated
document.getElementById("ronImg").src = "listen.gif";
};
recognition.onresult = function(event) {
const current = event.resultIndex;
const transcript = event.results[current][0].transcript; //transcript of what Ron heard
const transcriptTxt = document.getElementById("transcriptTxt"); //where transcript is shown to user
//speak input
document.getElementById("ronImg").src = "speak.gif";
const speech = new SpeechSynthesisUtterance();
speech.text = transcript;
speech.volume = 1;
speech.rate = 1;
speech.pitch = 0.1; //toggle for max voicage!!
transcriptTxt.innerText = '"' + transcript + '"'; //show user what Ron heard
window.speechSynthesis.speak(speech); //talk
//handle speech synthesis end event
speech.addEventListener("end", (event) => {
speak(); //keep listening when speech ends
});
};
} catch {
alert("Whoops! Looks like your browser isn't supported."); //only if browser badbad
};
} else {
document.getElementById("transcriptTxt").innerText = "your talking skeleton pal"
document.getElementById("ronImg").src = "ron.png"; //default state
document.getElementById('talkBtn').value = "Talk to Ron!"; //change button text
}
}
function help() {
alert("Didn't hear Ron reply? Your browser may not be fully supported, mic permission might not have been granted, or your device's volume may be muted.");
}
function about() {
alert("My Talking Ron v0.1.1\n\nProgrammed and designed by John Spahr\ngithub.com/johnspahr\n\nMade with <3 and vanilla JS!");
}