setup()
setup() runs once when the sketch starts. It configures the canvas size, drawing modes, and audio engine. The audio must be initialized here, but won't actually produce sound until userStartAudio() is called by first user interaction (a browser security requirement).
function setup() {
createCanvas(windowWidth, windowHeight);
textAlign(CENTER, CENTER);
rectMode(CENTER);
// Setup Audio (will start on first interaction)
osc = new p5.Oscillator('sine');
osc2 = new p5.Oscillator('sine');
sirenOsc = new p5.Oscillator('square');
env = new p5.Envelope();
env.setADSR(0.01, 0.1, 0.5, 0.1);
env.setRange(0.5, 0);
osc.amp(env);
osc2.amp(env);
sirenOsc.amp(0);
createKeypad();
}
Line-by-line explanation (11 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a full-window canvas that adapts to any screen size
textAlign(CENTER, CENTER);
rectMode(CENTER);
Centers all text and rectangles around their x,y coordinates—essential for positioning UI elements
osc = new p5.Oscillator('sine');
osc2 = new p5.Oscillator('sine');
sirenOsc = new p5.Oscillator('square');
Creates three tone generators: two sine waves for beeps, one square wave for the siren
env = new p5.Envelope();
env.setADSR(0.01, 0.1, 0.5, 0.1);
env.setRange(0.5, 0);
Defines the attack/decay/sustain/release of beep sounds—making them feel punchy and retro
createCanvas(windowWidth, windowHeight);- Fills the entire browser window with a canvas. windowWidth and windowHeight are p5.js variables that update if the window resizes.
textAlign(CENTER, CENTER);- Centers all text horizontally and vertically around the x,y coordinates you give to text(). Without this, text would be left-aligned and top-aligned.
rectMode(CENTER);- Makes all rectangles draw from their center point instead of from the top-left corner. This makes positioning UI buttons much simpler.
osc = new p5.Oscillator('sine');- Creates a sine wave oscillator—the smoothest sounding tone, good for beeps. You need p5.sound library for this.
osc2 = new p5.Oscillator('sine');- Creates a second sine oscillator at a slightly different frequency—when played together, they create a richer, slightly dissonant retro beep sound.
sirenOsc = new p5.Oscillator('square');- Creates a square wave oscillator with a harsher, more aggressive tone—perfect for police sirens.
env = new p5.Envelope();- Creates an envelope object that will control how sound fades in and out over time—instead of just clicking on/off abruptly.
env.setADSR(0.01, 0.1, 0.5, 0.1);- Sets Attack (0.01s fade-in), Decay (0.1s drop), Sustain (0.5 volume level), Release (0.1s fade-out). This makes beeps sound snappy.
env.setRange(0.5, 0);- Sets the envelope's peak volume (0.5) and minimum volume (0). The envelope will scale between these values.
osc.amp(env);- Connects the envelope to control the first oscillator's volume—so when env.play() is called, osc will fade in and out.
createKeypad();- Calls a helper function that builds the 12 buttons (0-9, *, #) for the phone keypad.