// Create the Audio Context
var context = new AudioContext();
var analyser = context.createAnalyser();
var WIDTH = 300;
var HEIGHT = 100;
// Create your oscillator, filter and gain node by declaring them as variables
var osc = context.createOscillator();
var filter = context.createBiquadFilter();
filter.type = 'lowpass';
filter.frequency.value = 350;
var oscGain = context.createGain();
oscGain.value = 0.3;
// Connect the nodes together
function makeConnection() {
osc.connect(filter);
filter.connect(oscGain);
oscGain.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 bufferLength = analyser.frequencyBinCount; //an unsigned long value half that of the FFT size. This generally equates to the number of data values you will have to play with for the visualization
var dataArray = new Uint8Array(bufferLength);
analyser.getByteTimeDomainData(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.