Created
February 25, 2021 22:58
-
-
Save rhom6us/7e28dcc2b2a538c5aa53200479626d02 to your computer and use it in GitHub Desktop.
Draw an audio buffer to a canvas
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
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