Last active
October 20, 2023 08:30
-
-
Save EtienneDuv/0efc4f9d39eec69c4cfa0088f53ed714 to your computer and use it in GitHub Desktop.
JS metronome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const a = new AudioContext() | |
const getRandInt = () => Math.round(Math.random()*255) | |
k = (w,x,y) => { | |
console.log("Gain:"+w, "Hz:"+x, "ms:"+y) | |
v = a.createOscillator() | |
u = a.createGain() | |
v.connect(u) | |
v.frequency.value = x | |
v.type = "square" | |
u.connect(a.destination) | |
u.gain.value = w * 0.01 | |
v.start(a.currentTime) | |
v.stop(a.currentTime + y *0.001) | |
} | |
setInterval(() => k(getRandInt(),getRandInt(),getRandInt()), 3000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const a = new AudioContext() | |
const beep = (w,x,y) => { | |
v = a.createOscillator() | |
u = a.createGain() | |
v.connect(u) | |
v.frequency.value = x | |
v.type = "square" | |
u.connect(a.destination) | |
u.gain.value = w * 0.01 | |
v.start(a.currentTime) | |
v.stop(a.currentTime + y *0.001) | |
} | |
let last; | |
let current = new Date(); | |
const metronome = (interval = 60) => { | |
setTimeout(() => { | |
beep(150,190,150) | |
metronome(); | |
last = current; | |
current = new Date(); | |
console.log(`seconds since last: ${(current.getTime() - last.getTime())/1000}`); | |
}, interval*1000); | |
}; | |
metronome() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment