Skip to content

Instantly share code, notes, and snippets.

@tripulse
Last active April 14, 2019 15:57
Show Gist options
  • Select an option

  • Save tripulse/ab0b1c05b1b6f9b782d2a5bee1378fbc to your computer and use it in GitHub Desktop.

Select an option

Save tripulse/ab0b1c05b1b6f9b782d2a5bee1378fbc to your computer and use it in GitHub Desktop.
I don't what I'm doing
var audioCtx = new AudioContext();
var source = audioCtx.createMediaElementSource(document.querySelector('video'));
var analyser = audioCtx.createAnalyser();
source.connect(analyser);
analyser.connect(audioCtx.destination);
// FFTSize to analyse
analyser.fftSize = 8192;
// analyser.smoothingTimeConstant = 0.2;
Math.map = function(val, v_min, v_max, m_min, m_max) {
// Make this range into (0..1)
val = (val - v_min) / (v_max - v_min);
// Convert the range to (m_min..m_max)
return m_min + val * (m_max - m_min);
}
document.querySelector('#secondary').innerHTML = `<canvas id='cv'></canvas>`;
var ctx = document.querySelector('#cv').getContext('2d');
var WIDTH = ctx.canvas.width,
HEIGHT = ctx.canvas.height;
var bl_w = 0.5;
var n_bins = analyser.fftSize; // Analysed fftSize
var fftData = new Float32Array(n_bins); // The arrray to store (-1.0..1.0) values.
var sliceWidth = WIDTH / n_bins;
(function() {
requestAnimationFrame(arguments.callee);
// 'X', 'Y' coordinate
var x = 0;
var y;
analyser.getFloatTimeDomainData(fftData);
// ctx.clearRect(0, 0, WIDTH, HEIGHT);
// ctx.beginPath();
// fftData.forEach((bin, idx) => {
// y = (0.5 + bin) * HEIGHT;
// if(idx === 0)
// ctx.moveTo(x, y);
// else
// ctx.lineTo(x, y);
// x+= sliceWidth;
// });
// ctx.stroke();
ctx.clearRect(0, 0, WIDTH, HEIGHT);
fftData.forEach((bin) => {
// y = Math.map(bin, 0, 255, 0, HEIGHT);
var tmp = (bin < 0) ? Math.abs(bin) : bin;
y = (bin + tmp) * HEIGHT;
// ctx.fillRect(x, HEIGHT-y, sliceWidth, y);
ctx.fillRect(x, HEIGHT/2 - y/2, sliceWidth, y);
ctx.fillRect(x, HEIGHT/2 - bl_w/2, sliceWidth, bl_w);
x+= sliceWidth;
});
})();
@tripulse
Copy link
Copy Markdown
Author

MANAAA

@tripulse
Copy link
Copy Markdown
Author

Masterpiece which got bootleg'd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment