Skip to content

Instantly share code, notes, and snippets.

@EtienneDuv
Last active October 20, 2023 08:30
Show Gist options
  • Save EtienneDuv/0efc4f9d39eec69c4cfa0088f53ed714 to your computer and use it in GitHub Desktop.
Save EtienneDuv/0efc4f9d39eec69c4cfa0088f53ed714 to your computer and use it in GitHub Desktop.
JS metronome
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)
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