Created
September 18, 2022 16:08
-
-
Save jango-blockchained/44babe17e637a5f1271a61d6e00f516f to your computer and use it in GitHub Desktop.
Draw a 2d canvas from audio input
This file contains hidden or 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
| var audioContext = new webkitAudioContext(); | |
| 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)); | |
| } | |
| } | |
| function initAudio() { | |
| var audioRequest = new XMLHttpRequest(); | |
| audioRequest.open("GET", "sounds/fightclub.ogg", true); | |
| audioRequest.responseType = "arraybuffer"; | |
| audioRequest.onload = function() { | |
| audioContext.decodeAudioData( audioRequest.response, | |
| function(buffer) { | |
| var canvas = document.getElementById("view1"); | |
| drawBuffer( canvas.width, canvas.height, canvas.getContext('2d'), buffer ); | |
| } ); | |
| } | |
| audioRequest.send(); | |
| } | |
| window.addEventListener('load', initAudio ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment