setup()
setup() runs once when the sketch starts. Here we prepare the canvas size, request microphone access, and initialize both the audio input and the FFT analyzer that will power the visualization. The FFT is what breaks audio into frequency bands—without it, we'd only have total volume, not the colorful spectrum bars.
function setup() {
createCanvas(windowWidth, windowHeight);
// Click to start audio
userStartAudio();
mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic);
colorMode(HSB, 360, 100, 100, 100);
}
Line-by-line explanation (7 lines)
🔧 Subcomponents:
mic = new p5.AudioIn();
Creates a new audio input object that will capture sound from the user's microphone
fft = new p5.FFT();
fft.setInput(mic);
Creates an FFT analyzer that breaks the microphone sound into individual frequency bands for visualization
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, so the visualization spans the full screen
userStartAudio();- Requests permission to access the microphone—required by browsers for security before any audio can be captured
mic = new p5.AudioIn();- Creates a new p5.AudioIn object that will listen to and record sound from your microphone
mic.start();- Starts the microphone capturing audio in the background—from this point on, mic data is available to analyze
fft = new p5.FFT();- Creates a Fast Fourier Transform analyzer, which converts audio into frequency data (how loud each pitch range is)
fft.setInput(mic);- Tells the FFT analyzer to listen to the microphone input so it can break that audio into frequency bands
colorMode(HSB, 360, 100, 100, 100);- Switches to HSB (Hue, Saturation, Brightness) color mode so colors can be mapped directly from frequencies—hue 0-360 matches the rainbow