Skip to content

Instantly share code, notes, and snippets.

@rhom6us
Created February 25, 2021 22:58
Show Gist options
  • Save rhom6us/7e28dcc2b2a538c5aa53200479626d02 to your computer and use it in GitHub Desktop.
Save rhom6us/7e28dcc2b2a538c5aa53200479626d02 to your computer and use it in GitHub Desktop.
Draw an audio buffer to a canvas
function drawBuffer( width, height, context, buffer ) {
var data = buffer.getChannelData( 0 );
var step = Math.ceil( data.length / width );
var amp = height / 2;
for(var i=0; i < width; i++){
var min = 1.0;
var max = -1.0;
for (var j=0; j<step; j++) {
var datum = data[(i*step)+j];
if (datum < min)
min = datum;
if (datum > max)
max = datum;
}
context.fillRect(i,(1+min)*amp,1,Math.max(1,(max-min)*amp));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment