-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
109 lines (93 loc) · 3.38 KB
/
index.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
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
// QUERY SELECTORS //
const beginButton = document.querySelector('.begin');
const speechBubble = document.querySelector('.speech-bubble');
const thoughtBubble = document.querySelector('.thought-bubble');
const changeMood = document.querySelector('.change-mood');
const another = document.querySelector('.another');
const light = document.querySelector('.light');
const eyes = document.querySelectorAll('.eye');
const eyeshine = document.querySelectorAll('.eye .shine');
const mouth = document.querySelector('.mouth');
let words = document.querySelector('.speech-bubble h2');
// SPEECH DEFINITIONS //
let SpeechRecognition;
let SpeechGrammarList;
let SpeechRecognitionEvent;
const grammar = '#JSGF V1.0; grammar moods; public <mood> = happy | sad | anxious | tired | angry | confused | determined ;'
let recognition;
let speechRecognitionList;
let mood;
// EVENT LISTENERS //
window.onload = initializeSpeech;
beginButton.addEventListener('click', () => speak('Why hello, friend! How are you feeling today?', true));
window.speechSynthesis.onvoiceschanged = function () {
window.speechSynthesis.getVoices();
beginButton.disabled = false;
};
another.addEventListener('click', () => speak(getRandomAffirmation(mood)));
changeMood.addEventListener('click', () => robotStopSpeaking(true));
// EVENT HANDLERS //
function initializeSpeech() {
SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;
}
function robotStartSpeaking(text) {
speechBubble.classList.remove('hidden');
thoughtBubble.classList.add('hidden');
words.innerText = text;
mouth.classList.add('speak');
}
function robotStopSpeaking(initial) {
if (initial) {
robotStartListening();
setTimeout(() => {
speechBubble.classList.add('hidden');
thoughtBubble.classList.remove('hidden');
}, 600);
}
mouth.classList.remove('speak');
setTimeout(() => {
changeMood.classList.remove('hidden');
another.classList.remove('hidden');
}, 600)
}
function robotStartListening() {
console.log('listening!')
recognition = new SpeechRecognition() || new webkitSpeechRecognition();
speechRecognitionList = new SpeechGrammarList();
speechRecognitionList.addFromString(grammar, 1);
recognition.grammars = speechRecognitionList;
recognition.continuous = false;
recognition.lang = 'en-US';
recognition.interimResults = false;
recognition.maxAlternatives = 1;
recognition.onresult = function (event) {
mood = event.results[0][0].transcript;
speak(getRandomAffirmation(mood));
}
recognition.onspeechend = function() {
recognition.stop();
}
recognition.start();
}
function wakeUp() {
beginButton.classList.add('hidden');
eyes.forEach(eye => eye.classList.remove('asleep'));
eyeshine.forEach(shine => shine.classList.remove('asleep'));
light.classList.remove('asleep');
}
function speak(text, initial) {
if (initial) wakeUp();
robotStartSpeaking(text);
synthVoice(text, initial)
}
function synthVoice(text, initial) {
const synth = window.speechSynthesis;
const thisVoice = synth.getVoices().find(voice => voice.voiceURI === 'Fred');
const utterance = new SpeechSynthesisUtterance();
utterance.voice = thisVoice;
utterance.text = text;
synth.speak(utterance);
utterance.addEventListener('end', () => robotStopSpeaking(initial));
}