Created
July 25, 2019 20:51
-
-
Save benfoxall/693246e80dfc58a2bf696a45361a9d96 to your computer and use it in GitHub Desktop.
AudioVis
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
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