preload()
preload() runs once before setup(), and is where you load images, audio, and initialize p5.sound objects. All audio synthesis in this sketch happens here, creating reusable oscillators, noise generators, and envelopes that the game will trigger later.
function preload() {
ambientSoundOsc = new p5.Oscillator('sine');
ambientSoundOsc.freq(50);
ambientSoundOsc.amp(0);
ambientSoundOsc.start();
ambientSoundNoise = new p5.Noise('white');
ambientSoundNoise.amp(0);
ambientSoundNoise.start();
ambientFilter = new p5.Filter('lowpass');
ambientFilter.freq(500);
ambientFilter.res(2);
ambientSoundNoise.disconnect();
ambientSoundNoise.connect(ambientFilter);
ambientFilter.connect();
jumpscareNoise = new p5.Noise('white');
jumpscareNoise.amp(0);
jumpscareNoise.start();
jumpscareOsc = new p5.Oscillator('sawtooth');
jumpscareOsc.freq(1);
jumpscareOsc.amp(0);
jumpscareOsc.start();
jumpscareEnvelope = new p5.Envelope();
jumpscareEnvelope.setADSR(0.01, 0.2, 0, 0.5);
jumpscareEnvelope.setRange(1.0, 0);
jumpscareNoise.amp(jumpscareEnvelope);
footstepOsc = new p5.Oscillator('sine');
footstepOsc.amp(0);
footstepOsc.start();
footstepEnvelope = new p5.Envelope();
footstepEnvelope.setADSR(0.01, 0.1, 0, 0.1);
footstepOsc.amp(footstepEnvelope);
collectOsc = new p5.Oscillator('triangle');
collectOsc.amp(0);
collectOsc.start();
collectEnvelope = new p5.Envelope();
collectEnvelope.setADSR(0.01, 0.05, 0, 0.2);
collectOsc.amp(collectEnvelope);
}
Line-by-line explanation (12 lines)
🔧 Subcomponents:
ambientSoundOsc = new p5.Oscillator('sine');
ambientSoundOsc.freq(50);
ambientSoundOsc.amp(0);
ambientSoundOsc.start();
Creates a low-frequency drone oscillator (50 Hz sine wave) that will play ambient background audio
ambientSoundNoise = new p5.Noise('white');
ambientSoundNoise.amp(0);
ambientSoundNoise.start();
ambientFilter = new p5.Filter('lowpass');
ambientFilter.freq(500);
ambientFilter.res(2);
ambientSoundNoise.disconnect();
ambientSoundNoise.connect(ambientFilter);
ambientFilter.connect();
Creates filtered white noise (only frequencies below 500 Hz pass through) for a subtle hiss effect
jumpscareNoise = new p5.Noise('white');
jumpscareNoise.amp(0);
jumpscareNoise.start();
jumpscareOsc = new p5.Oscillator('sawtooth');
jumpscareOsc.freq(1);
jumpscareOsc.amp(0);
jumpscareOsc.start();
jumpscareEnvelope = new p5.Envelope();
jumpscareEnvelope.setADSR(0.01, 0.2, 0, 0.5);
jumpscareEnvelope.setRange(1.0, 0);
jumpscareNoise.amp(jumpscareEnvelope);
Creates a harsh sawtooth oscillator and noise burst with an envelope that controls their volume over time (attack, decay, sustain, release)
ambientSoundOsc = new p5.Oscillator('sine');- Creates a sine-wave oscillator object which will generate a smooth, pure tone
ambientSoundOsc.freq(50);- Sets the oscillator frequency to 50 Hz, a very low frequency that feels like a distant rumble
ambientSoundOsc.amp(0);- Sets amplitude to 0 so the oscillator is silent until we fade it in later
ambientSoundOsc.start();- Starts the oscillator running (but silently, since amp is 0)
ambientSoundNoise = new p5.Noise('white');- Creates a white noise generator (random noise across all frequencies)
ambientFilter = new p5.Filter('lowpass');- Creates a low-pass filter that will only let low frequencies through, removing harsh high-frequency hiss
ambientFilter.freq(500);- Configures the filter to cut off frequencies above 500 Hz
ambientSoundNoise.disconnect();- Removes the noise from the default audio output so we can route it through the filter first
ambientSoundNoise.connect(ambientFilter);- Connects noise to the filter, so it becomes filtered before reaching the speakers
ambientFilter.connect();- Connects the filtered noise to the master audio output (the speakers)
jumpscareEnvelope.setADSR(0.01, 0.2, 0, 0.5);- Configures the jumpscare envelope: 0.01s attack (quick fade-in), 0.2s decay, 0s sustain (stays at peak), 0.5s release (slow fade-out)
jumpscareEnvelope.setRange(1.0, 0);- Sets the envelope to scale from full volume (1.0) down to silent (0)