forked from kellsthepenguin/PopCat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
35 lines (29 loc) · 986 Bytes
/
script.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
const IMG_CLOSED = './resources/close.png'
const IMG_OPENED = './resources/open.png'
const SENSITIVITY = 0.0094 // 0.0094 default, range is 0 ~ 1
const meter = new Tone.Meter({
channels: 1
})
const mic = new Tone.UserMedia().connect(meter)
mic.open().then(() => {
if (Tone.context.state !== 'running') {
Tone.context.resume()
}
meter.normalRange = true
setInterval(() => {
const status = document.getElementById('status')
const statusText = document.getElementById('text_status')
if (meter.getValue() > SENSITIVITY) {
if (status.getAttribute('src') === IMG_CLOSED) {
status.setAttribute('src', IMG_OPENED)
statusText.innerHTML = 'popping'
}
} else if (status.getAttribute('src') === IMG_OPENED) {
status.setAttribute('src', IMG_CLOSED)
statusText.innerHTML = 'not popping'
}
}, 1)
}).catch(e => {
alert('please grant mic permission (if you granted mic permission, see console.)')
console.log(e)
})