Published
Edited
May 19, 2019
1 fork
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
Insert cell
Insert cell
analyser = audioCtx.createAnalyser();
Insert cell
Insert cell
data ={
return navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then(function(stream) {
const src = audioCtx.createMediaStreamSource(stream)
src.connect(analyser)
analyser.fftSize = 256
const bufferLength = analyser.fftSize
const dataArray = new Float32Array(bufferLength)
return dataArray
})
}
Insert cell
{
while(true) {
analyser.getFloatFrequencyData(data)
yield data
}
}
Insert cell
Insert cell
Insert cell
{
const canvasCtx = canvas.getContext('2d');
canvasCtx.clearRect(0, 0, canvas.width, canvas.height);

function draw() {
//Schedule next redraw
requestAnimationFrame(draw);

//Draw black background
canvasCtx.fillStyle = 'rgb(110, 110, 110)';
canvasCtx.fillRect(0, 0, canvas.width, canvas.height);

//Draw spectrum
const barWidth = (canvas.width / 256) * 2.5;
let posX = 0;
for (let i = 0; i < 256; i++) {
const barHeight = (data[i] + 140) * 2;
canvasCtx.fillStyle = 'rgb(' + Math.floor(barHeight + 100) + ', 50, 50)';
canvasCtx.fillRect(posX, canvas.height - barHeight / 2, barWidth, barHeight / 2);
posX += barWidth + 1;
}
};

draw();
}
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more