-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
189 lines (153 loc) · 3.95 KB
/
app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
if (!('SpeechRecognition' in window) && !('webkitSpeechRecognition' in window)) {
const noSupportMessage = "Your browser doesn't support SpeechRecognition."
document.getElementById('js-nosupport').innerText = noSupportMessage
throw new Error(noSupportMessage)
}
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition
const speechInstance = new SpeechRecognition()
const Speak = msg => {
let supportSpeaking = false
if (!('SpeechSynthesisUtterance' in window) && !('speechSynthesis' in window)) {
console.warn("Your browser doesn't support Speech.")
} else {
supportSpeaking = true
}
if (!supportSpeaking) {
return undefined
}
const { speechSynthesis, SpeechSynthesisUtterance } = window
const speakInstance = new SpeechSynthesisUtterance()
speakInstance.text = msg
speakInstance.volumen = 1
speakInstance.rate = 0.8
speakInstance.pitch = 0.3
speechSynthesis.speak(speakInstance)
}
const Trigger = {
node: document.getElementById('js-talk-trigger'),
enable: function () {
this.node.classList.add('is-active')
},
disable: function () {
this.node.classList.remove('is-active')
}
}
const STATUS = {
INITIAL: 'Click the button to talk. \n(Say help to see the available commands)',
ACTIVE: 'Listening...',
NO_SPEECH: 'No speech.'
}
const Status = {
node: document.getElementById('js-status'),
setInitial: function () {
this.node.innerText = STATUS.INITIAL
},
setActive: function () {
this.node.innerText = STATUS.ACTIVE
},
setNoSpeach: function () {
this.node.innerText = STATUS.NO_SPEECH
}
}
const Response = {
node: document.getElementById('js-response'),
show: function () {
this.node.classList.add('is-visible')
},
hide: function () {
this.node.classList.remove('is-visible')
},
setContent: function (childNode) {
this.node.textContent = null
this.show()
this.node.append(childNode)
},
clear: function () {
this.hide()
this.node.textContent = null
}
}
const Input = {
node: document.getElementById('js-input'),
show: function () {
this.node.classList.add('is-visible')
},
hide: function () {
this.node.classList.remove('is-visible')
},
setText: function (msg) {
this.show()
this.node.innerText = msg
},
clear: function () {
this.hide()
this.node.innerText = null
}
}
const COMMAND_LIST = ['hello', 'help']
const ACTION = {
HELLO: {
COMMAND: 'hello',
ANSWER: 'Hi, I am Jarvis. How can i help you?',
RESPONSE: () => {
Response.clear()
}
},
HELP: {
COMMAND: 'help',
ANSWER: 'Are you looking for help? These are the available commands.',
RESPONSE: function () {
const children = document.createElement('ul')
COMMAND_LIST.map(command => {
const elLine = document.createElement('li')
elLine.innerText = command
children.append(elLine)
})
Response.setContent(children)
}
}
}
let isListening = false
Status.setInitial()
Input.hide()
Trigger.node.addEventListener('click', () => {
if (!isListening) {
speechInstance.start()
}
})
speechInstance.onstart = () => {
isListening = true
Trigger.enable()
Status.setActive()
Response.clear()
}
speechInstance.onresult = event => {
isListening = false
Trigger.disable()
Status.setInitial()
let isCommandMatched = false
const { resultIndex, results } = event
const transcript = results[resultIndex][0].transcript
Input.setText(transcript)
COMMAND_LIST.map(command => {
if (transcript.includes(command)) {
const cmd = command.toUpperCase()
isCommandMatched = true
Speak(ACTION[`${cmd}`].ANSWER)
ACTION[`${cmd}`].RESPONSE()
}
})
if (!isCommandMatched) {
Speak(ACTION.HELP.ANSWER)
ACTION.HELP.RESPONSE()
}
}
speechInstance.onerror = () => {
Status.setNoSpeach()
Speak('Hey, are you there?')
}
speechInstance.onend = () => {
isListening = false
Trigger.disable()
Status.setInitial()
}