preload()
preload() runs once before setup(), ensuring that external assets (fonts, images, audio) are fully loaded before the game starts. This is critical when using p5.sound because Web Audio APIs require asynchronous initialization. The sound objects here use p5.sound's disconnect() and connect() methods to chain filters, demonstrating how to build a signal processing pipeline for atmospheric audio.
function preload() {
// Load glitchy sci-fi font from Fontsource CDN
gameFont = loadFont('https://cdn.jsdelivr.net/fontsource/fonts/vt323@latest/latin-400-normal.woff');
// Using p5.Noise for ambient facility hum
ambientSound = new p5.Noise('white');
ambientSound.amp(0); // Start muted, fade in on user gesture
ambientSound.pan(0);
ambientSound.start(); // Noise starts playing continuously
ambientSound.disconnect(); // Disconnect from master output to apply filter
ambientSound.connect(new p5.Filter('lowpass', 200, 1)); // Filter for a deeper, muffled hum
// Using p5.Oscillator for detection alert sound
detectionSound = new p5.Oscillator('sine');
detectionSound.freq(440);
detectionSound.amp(0); // Start muted
detectionSound.start();
detectionSound.disconnect();
detectionSound.connect(new p5.Filter('highpass', 1000, 1)); // Filter for a sharper alert
// Using p5.Noise for footsteps sound (a brief static burst)
footstepsNoise = new p5.Noise('pink');
footstepsNoise.amp(0); // Start muted, envelope will control amplitude
footstepsNoise.start();
footstepsNoise.disconnect();
footstepsNoise.connect(new p5.Filter('lowpass', 500, 1)); // Filter for a softer thump
footstepsEnv = new p5.Env();
// Attack time, attack level, decay time, decay level, release time, release level
// Short attack, quick decay, no sustain, quick release for a "thump"
// Adjusted ADSR to align with PLAYER_MOVE_DURATION for a brief footstep sound
footstepsEnv.setADSR(0.01, PLAYER_MOVE_DURATION / 1000 * 0.5, 0, 0.05, PLAYER_MOVE_DURATION / 1000 * 0.5);
footstepsEnv.setRange(0.1, 0); // Max amplitude 0.1, Min amplitude 0
}
Line-by-line explanation (13 lines)
🔧 Subcomponents:
gameFont = loadFont('https://cdn.jsdelivr.net/fontsource/fonts/vt323@latest/latin-400-normal.woff');
Loads a VT323 retro sci-fi font from a CDN to give the UI a hacker/dystopian aesthetic
ambientSound = new p5.Noise('white');
Creates a white noise generator that will serve as the facility's background hum when filtered
detectionSound = new p5.Oscillator('sine');
Creates a sine wave oscillator that plays an alert tone when the player is caught
footstepsNoise = new p5.Noise('pink');
Creates pink noise that will be shaped into a footstep thump sound via an envelope
footstepsEnv.setADSR(0.01, PLAYER_MOVE_DURATION / 1000 * 0.5, 0, 0.05, PLAYER_MOVE_DURATION / 1000 * 0.5);
Configures attack, decay, sustain, and release times to shape the noise into a brief, punchy footstep sound synchronized with player movement duration
gameFont = loadFont('https://cdn.jsdelivr.net/fontsource/fonts/vt323@latest/latin-400-normal.woff');- Loads a custom retro font (VT323) from a CDN before the sketch runs. This font gives the game a sci-fi hacker aesthetic.
ambientSound = new p5.Noise('white');- Creates a white noise generator—random sound at all frequencies. This will be filtered to create a facility hum.
ambientSound.amp(0);- Sets the noise volume to 0 (silent). It will be faded in later when the player starts the game via a user gesture.
ambientSound.start();- Starts the noise generator playing continuously in the background. It stays silent until its amp is increased.
ambientSound.disconnect();- Removes the noise from the master audio output, allowing us to apply filters before it reaches the speaker.
ambientSound.connect(new p5.Filter('lowpass', 200, 1));- Pipes the noise through a low-pass filter that removes high frequencies, creating a deep, muffled facility hum effect.
detectionSound = new p5.Oscillator('sine');- Creates a sine wave oscillator—a pure, smooth tone used for the detection alert sound.
detectionSound.freq(440);- Sets the oscillator frequency to 440 Hz (the musical note A4), a clear, piercing tone for an alarm.
detectionSound.disconnect();- Removes the oscillator from master output so we can apply filtering for a more ominous alert sound.
detectionSound.connect(new p5.Filter('highpass', 1000, 1));- Pipes the oscillator through a high-pass filter that removes low frequencies, creating a sharper, more electronic alert tone.
footstepsNoise = new p5.Noise('pink');- Creates pink noise (noise with more emphasis on lower frequencies). This sounds more like footsteps than white noise.
footstepsEnv.setADSR(0.01, PLAYER_MOVE_DURATION / 1000 * 0.5, 0, 0.05, PLAYER_MOVE_DURATION / 1000 * 0.5);- Configures the envelope's ADSR (Attack, Decay, Sustain, Release) timings. Attack is nearly instant (0.01s), decay and release align with movement duration to create a quick thump synchronized with player movement.
footstepsEnv.setRange(0.1, 0);- Sets the envelope's amplitude range: 0.1 at peak (max), 0 at quiet (min). This ensures the footstep sound is audible but not overwhelming.