xxxxxxxxxx
43
let fft;
let mic;
function setup() {
createCanvas(400, 400);
fft = new p5.FFT();
mic = new p5.AudioIn();
mic.start();
fft.setInput(mic);
}
function draw() {
background(220);
// Visualize the mic level with an ellipse
// let level = mic.getLevel()*100;
// ellipse(width/2, height/2, level, level);
// This is the waveform
let waveform = fft.waveform();
noFill();
stroke('red');
beginShape();
for (let w = 0; w < waveform.length; w++) {
let wh = waveform[w];
let y = map(wh, -1, 1, 0, height);
let x = map(w, 0, waveform.length, 0, width);
vertex(x, y);
}
endShape();
// Analyze the waveform
let spectrum = fft.analyze();
for (let s = 0; s < spectrum.length; s++) {
let amplitude = spectrum[s];
let y = map(amplitude, 0, 255, height, 0);
let x = map(s, 0, spectrum.length, 0, width);
line(x, y, x, height);
}
}