// Create the Audio Context
var context = new AudioContext();
var analyser = context.createAnalyser();
var WIDTH = 300;
var HEIGHT = 300;
// Create your oscillator, filter and gain node by declaring them as variables
var osc = context.createOscillator();
osc.frequency.value = 500;
// Connect the nodes together
function makeConnection() {
osc.connect(analyser);
}
// Play the sound inside of Chrome
function playSound() {
analyser.connect(context.destination);
osc.start(0);
osc.stop(3);
}
makeConnection();
playSound();
var canvas = document.querySelector('.visualizer');
var myCanvas = canvas.getContext("2d");
analyser.fftSize = 2048;
var dataArray = new Float32Array(analyser.fftSize); // Float32Array needs to be the same length as the fftSize
var dataArray = new Float32Array(bufferLength);
analyser.getByteFrequencyData(dataArray);
console.log(dataArray);
myCanvas.clearRect(0, 0, WIDTH, HEIGHT);
function draw() {
drawVisual = requestAnimationFrame(draw);
analyser.getByteTimeDomainData(dataArray);
myCanvas.fillStyle = 'rgb(200, 200, 200)';
myCanvas.fillRect(0, 0, WIDTH, HEIGHT);
myCanvas.lineWidth = 2;
myCanvas.strokeStyle = 'rgb(0, 0, 0)';
myCanvas.beginPath();
var sliceWidth = WIDTH * 1.0 / bufferLength;
var x = 0;
for(var i = 0; i < bufferLength; i++) {
var v = dataArray[i] / 128.0;
var y = v * HEIGHT/2;
if(i === 0) {
myCanvas.moveTo(x, y);
} else {
myCanvas.lineTo(x, y);
}
x += sliceWidth;
};
myCanvas.lineTo(canvas.width, canvas.height/2);
myCanvas.stroke();
};
draw();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.