-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
115 lines (85 loc) · 3.23 KB
/
app.js
File metadata and controls
115 lines (85 loc) · 3.23 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
const btn = document.querySelector('.talk');
const content = document.querySelector('#contentArea');
const reply = document.querySelector('#replyArea');
var volume = 1;
var rate = 1;
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
const greetings = ["I'm good, you little shit", "Fuck you very very very much", "You're a bit thick"];
const weather = ["Brah", "Get your life together", "It's always sunny in hell"];
const unspecified = ["Yeah. Couldn't care less, my dumbass developer didn't program me to reply to that, so I don't give a goddamn, okay? Leave me the fuck alone.", "I CAN NOT HEAR YOU", "What the hell are you saying?", "I'm a simple little program. Almost as simple as you are. That's beyond me, I'm afraid."]
recognition.onstart = function() {
console.log('Voice is activated. Talk to the microphone.');
};
recognition.onresult = function(event) {
const current = event.resultIndex;
const transcript = event.results[current][0].transcript;
content.textContent = transcript;
readOutLoud(transcript);
console.log("Read out loud function initiated.");
};
//add listener to button
btn.addEventListener('click', () => {
recognition.start();
reply.textContent = "";
content.textContent = "";
});
function readOutLoud(message) {
const speech = new SpeechSynthesisUtterance();
const finalText = unspecified[Math.floor(Math.random() * unspecified.length)];
speech.text = finalText;
speech.volume = volume;
speech.pitch = 1;
speech.volume = volume;
if(message.includes('how')){
const finalText = greetings[Math.floor(Math.random() * greetings.length)];
speech.text = finalText;
reply.textContent = finalText;
console.log(finalText);
}
else if(message.includes('weather')){
const finalText = weather[Math.floor(Math.random() * weather.length)];
reply.textContent = finalText;
speech.text = finalText;
console.log(finalText);
}
else if(message.includes('song')){
reply.textContent = "Playing that song";
speech.text = "Playing that song";
var audio = new Audio('song.mp3')
audio.play();
}
else if(message.includes('stop')){
speech.text = "No fucking way, dumb arse.";
reply.textContent = "Never gonna stop the music!";
}
else if(message.includes('help')){
alert("You can try asking me about the weather, how I'm going, or to play a song.");
speech.text = "You primitive. Eh, I'll help."
reply.textContent = "You primitive. Well, at least now you know something.";
}
else if(message.includes('time')){
var dateTime = new Date();
speech.text = dateTime + ". If I can work that out then why the fuck can't you?";
reply.textContent = dateTime + ". If I can work that out then why the fuck can't you?";
}
else if(message.includes('calm')){
rate--;
speech.rate = rate;
speech.text = "Okay, grandma, I'm calm.";
reply.textContent = "Okay grandma. I'm calm."
}
else if(message.includes('hurry up')){
rate++;
rate++;
speech.rate=rate;
speech.text = "Gotcha kiddo. This good?";
reply.textContent = "Gotcha kiddo. This good?";
}
else{
reply.textContent = finalText;
speech.rate=rate;
}
window.speechSynthesis.speak(speech);
console.log("Should be speaking now. Speaking function completed.");
}