Skip to content

Instantly share code, notes, and snippets.

@benfoxall
Created July 25, 2019 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfoxall/693246e80dfc58a2bf696a45361a9d96 to your computer and use it in GitHub Desktop.
Save benfoxall/693246e80dfc58a2bf696a45361a9d96 to your computer and use it in GitHub Desktop.
AudioVis
class AudioVis {
constructor() {
this.analyser = …
this.data = new Uint8Array(…)
this.frequencies = new Uint8Array(…)
}
populateArrays() {
// populate data arrays
}
draw(fn) {
const loop = (t) => {
const raf = requestAnimationFrame(loop)
this.populateArrays()
// cancel if an error was thrown
try { fn() } catch (e) {
console.error(e)
cancelAnimationFrame(raf)
}
}
requestAnimationFrame(loop)
}
}
const vis = AudioVis()
vis.draw(() => {
ctx.beginPath()
vis.frequencies.forEach((f, i) => {
ctx.lineTo(i, f)
})
ctx.stroke()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment