xxxxxxxxxx
34
const w = 512;
const h = 512;
let fft, mic;
function setup() {
createCanvas(w, h);
pixelDensity(1);
// Initialize microphone
mic = new p5.AudioIn();
mic.start();
// Initialize FFT (Fast Fourier Transform) for audio analysis
fft = new p5.FFT();
fft.setInput(mic);
}
function draw() {
background(255);
// Microphone stuff
let spectrum = fft.analyze();
strokeWeight(3);
beginShape();
for (let i = 0; i < spectrum.length; i++) {
let f = constrain(spectrum[i], 0, 255);
stroke(255 - f, f, 0);
let x = map(i, 0, spectrum.length, 0, width);
let y = map(f, 0, 255, height>>1, 0);
point(x, y);
}
endShape();
}