-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdemoAudioDetectionListeners.js
229 lines (159 loc) · 6.26 KB
/
demoAudioDetectionListeners.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* eslint-env browser */
const dB = (signal) => - Math.round( 20 * Math.log10( 1 / signal ) )
/**
* speechDetectionListeners
*
*/
function hystogramLine( value ) {
const maxCharsperLine = 200
const valueInChars = maxCharsperLine * value
const char = '█'
return char.repeat(valueInChars)
}
function showConfiguration() {
document.querySelector('#SAMPLE_POLLING_MSECS').textContent = SAMPLE_POLLING_MSECS
document.querySelector('#MAX_INTERSPEECH_SILENCE_MSECS').textContent = MAX_INTERSPEECH_SILENCE_MSECS
document.querySelector('#MIN_SIGNAL_DURATION').textContent = MIN_SIGNAL_DURATION
document.querySelector('#VOLUME_SIGNAL').textContent = VOLUME_SIGNAL
document.querySelector('#VOLUME_SILENCE').textContent = VOLUME_SILENCE
document.querySelector('#VOLUME_MUTE').textContent = VOLUME_MUTE
document.querySelector('#MIN_AVERAGE_SIGNAL_VOLUME').textContent = MIN_AVERAGE_SIGNAL_VOLUME
}
//
// signal handler
//
document.addEventListener('signal', event => {
const volume = event.detail.volume.toFixed(9)
const timestamp = event.detail.timestamp
const items = event.detail.items.toString().padEnd(3)
const dBV = dB(event.detail.volume)
const line = hystogramLine(volume)
if (debuglog)
console.log(`signal ${timestamp} ${items} ${volume} ${dBV} ${line}`)
document.querySelector('#audiostatuscell').style.background = 'green'
document.querySelector('#audiostatuscell').style.color = 'black'
document.querySelector('#audiostatus').style.background = 'green'
document.querySelector('#audiostatus').textContent = 'signal'
//const theDiv = document.getElementById('log')
//const content = document.createTextNode(text)
//theDiv.appendChild(content)
})
//
// silence handler
//
document.addEventListener('silence', event => {
const volume = event.detail.volume.toFixed(9)
const timestamp = event.detail.timestamp
const items = event.detail.items.toString().padEnd(3)
const dBV = dB(event.detail.volume)
if (debuglog)
console.log(`silence ${timestamp} ${items} ${volume} ${dBV}`)
document.querySelector('#audiostatuscell').style.background = 'black'
document.querySelector('#audiostatuscell').style.color = 'white'
document.querySelector('#audiostatus').style.background = 'black'
document.querySelector('#audiostatus').textContent = 'silence'
})
//
// mute handler
//
document.addEventListener('mute', event => {
const volume = event.detail.volume.toFixed(9)
const timestamp = event.detail.timestamp
const dBV = dB(event.detail.volume)
if (debuglog)
console.log(`mute ${timestamp} ${volume} ${dBV}`)
document.querySelector('#audiostatus').textContent = 'mute'
})
//
// prespeechstart handler
//
document.addEventListener('prespeechstart', event => {
if (debuglog) {
//const volume = event.detail.volume.toFixed(9)
const timestamp = event.detail.timestamp
//const dBV = dB(event.detail.volume)
//console.log(`%cPRE SPEECH START ${timestamp} ${volume} ${dBV}`, 'color:yellow')
console.log(`%cPRE SPEECH START ${timestamp}`, 'color:blue')
}
restartRecording()
})
//
// speechstart handler
//
document.addEventListener('speechstart', event => {
if (debuglog) {
//speechstartTime = event.detail.timestamp
console.log('%cSPEECH START', 'color:greenyellow')
}
document.querySelector('#recordingcell').style.background = 'green'
document.querySelector('#recordingcell').style.color = 'white'
document.querySelector('#recording').style.background = 'green'
document.querySelector('#recording').style.color = 'white'
document.querySelector('#recording').textContent = 'start'
//startRecording()
})
//
// speechstop handler
//
document.addEventListener('speechstop', event => {
const duration = event.detail.duration
if (debuglog) {
const averageSignalLevel = averageSignal()
console.log('%cSPEECH STOP', 'color:lime')
console.log(`Total Duration in msecs : ${duration}`)
console.log(`Signal Duration in msecs : ${duration - MAX_INTERSPEECH_SILENCE_MSECS }`)
console.log(`Average Signal level : ${averageSignalLevel}`)
console.log(`Average Signal dB : ${dB(averageSignalLevel)}`)
console.log(' ')
}
document.querySelector('#recordingcell').style.color = 'white'
document.querySelector('#recordingcell').style.background = 'black'
document.querySelector('#recording').style.color = 'white'
document.querySelector('#recording').style.background = 'black'
document.querySelector('#recording').textContent = `stop. len: ${duration} msecs`
stopRecording()
})
//
// speechabort handler
//
document.addEventListener('speechabort', event => {
const abort = event.detail.abort
if (debuglog) {
const duration = event.detail.duration
const averageSignalLevel = averageSignal()
console.log('%cSPEECH ABORT', 'color:red')
console.log(`Abort reason : ${abort}`)
console.log(`Total Duration in msecs : ${duration}`)
console.log(`Signal Duration in msecs : ${duration - MAX_INTERSPEECH_SILENCE_MSECS }`)
console.log(`Average Signal level : ${averageSignalLevel}`)
console.log(`Average Signal dB : ${dB(averageSignalLevel)}`)
console.log(' ')
}
document.querySelector('#recordingcell').style.color = 'white'
document.querySelector('#recordingcell').style.background = 'red'
document.querySelector('#recording').style.color = 'white'
document.querySelector('#recording').style.background = 'red'
document.querySelector('#recording').textContent = `abort. ${abort}`
abortRecording()
})
//
// mutedmic handler
//
document.addEventListener('mutedmic', event => {
document.querySelector('#microphonestatus').textContent = 'muted (off)'
document.querySelector('#microphonestatus').style.background = 'red'
document.querySelector('#microphonestatuscell').style.background = 'red'
console.log('%cMICROPHONE MUTED', 'color:red')
console.log(' ')
})
//
// unmutedmic handler
//
document.addEventListener('unmutedmic', event => {
document.querySelector('#microphonestatus').textContent = 'unmuted (on)'
document.querySelector('#microphonestatus').style.background = 'green'
document.querySelector('#microphonestatuscell').style.background = 'green'
console.log('%cMICROPHONE UNMUTED', 'color:green')
console.log(' ')
})
showConfiguration()