preload()
preload() runs once before setup() and is the ideal place to initialize audio objects and other resources. p5.sound oscillators must be created and started here so they're ready when the game begins. The ADSR envelope (Attack, Decay, Sustain, Release) shapes how a sound fades in and out, crucial for realistic audio effects.
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, 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);
scrapCollectOsc = new p5.Oscillator('square');
scrapCollectOsc.amp(0);
scrapCollectOsc.start();
scrapCollectEnv = new p5.Envelope();
scrapCollectEnv.setADSR(0.01, 0.08, 0, 0.15);
scrapCollectEnv.setRange(0.7, 0);
scrapCollectOsc.amp(scrapCollectEnv);
buyItemOsc = new p5.Oscillator('sine');
buyItemOsc.amp(0);
buyItemOsc.start();
buyItemEnv = new p5.Envelope();
buyItemEnv.setADSR(0.01, 0.1, 0, 0.2);
buyItemEnv.setRange(0.8, 0);
buyItemOsc.amp(buyItemEnv);
sellScrapOsc = new p5.Oscillator('triangle');
sellScrapOsc.amp(0);
sellScrapOsc.start();
sellScrapEnv = new p5.Envelope();
sellScrapEnv.setADSR(0.01, 0.08, 0, 0.15);
sellScrapEnv.setRange(0.6, 0);
sellScrapOsc.amp(sellScrapEnv);
loadingSoundOsc = new p5.Oscillator('sine');
loadingSoundOsc.freq(100);
loadingSoundOsc.amp(0);
loadingSoundOsc.start();
loadingSoundEnv = new p5.Envelope();
loadingSoundEnv.setADSR(0.1, 0.5, 0, 0.5);
loadingSoundEnv.setRange(0.5, 0);
loadingSoundOsc.amp(loadingSoundEnv);
boombox = {
osc: new p5.Oscillator('triangle'),
delay: new p5.Delay(),
nearbyThreshold: 200,
fadeZone: 100
};
boombox.osc.freq(220);
boombox.osc.amp(0);
boombox.osc.start();
boombox.osc.disconnect();
boombox.delay.process(boombox.osc, 0.5, 0.7, 2300);
boombox.delay.amp(0);
boombox.delay.connect();
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
ambientSoundOsc = new p5.Oscillator('sine'); ambientSoundOsc.freq(50); ambientSoundOsc.start();
Creates a low-frequency sine wave drone (50 Hz) that runs continuously to provide an ominous atmospheric hum
ambientSoundNoise.disconnect(); ambientSoundNoise.connect(ambientFilter);
Connects white noise through a low-pass filter to create a subtle hiss/static effect without harsh highs
jumpscareEnvelope.setADSR(0.01, 0.2, 0.0, 0.5); jumpscareEnvelope.setRange(1.0, 0);
Defines how the jumpscare noise fades in quickly (10ms attack) and decays dramatically for a startling effect
boombox.delay.process(boombox.osc, 0.5, 0.7, 2300);
Adds a 0.5-second delayed echo with 70% feedback to create a room-like reverb effect for the music
ambientSoundOsc = new p5.Oscillator('sine');- Creates a sine wave oscillator for the ambient background drone—sine waves are smooth and good for low-frequency tones
ambientSoundOsc.freq(50);- Sets the oscillator frequency to 50 Hz, which is below human hearing but felt as vibration, creating unease
ambientSoundOsc.amp(0); ambientSoundOsc.start();- Starts the oscillator silently (amp 0)—it will fade in later when the game begins
ambientSoundNoise = new p5.Noise('white'); ambientSoundNoise.start();- Creates white noise (all frequencies equally loud) and starts it, which will be filtered to reduce harshness
ambientFilter = new p5.Filter('lowpass'); ambientFilter.freq(500);- Creates a low-pass filter set to 500 Hz, allowing only frequencies below 500 Hz through—removes high hissing
ambientSoundNoise.disconnect(); ambientSoundNoise.connect(ambientFilter);- Disconnects noise from the default speaker output and reroutes it through the filter instead
jumpscareOsc = new p5.Oscillator('sawtooth'); jumpscareOsc.freq(1);- Creates a harsh sawtooth wave oscillator for the jumpscare screech—sawtooth contains many harmonics for a terrifying sound
jumpscareEnvelope.setADSR(0.01, 0.2, 0.0, 0.5);- Sets up the jumpscare envelope: 10ms attack (very fast), 200ms decay, no sustain, 500ms release—for a sudden shock
boombox.delay.process(boombox.osc, 0.5, 0.7, 2300);- Creates a delay effect with 0.5-second delay time and 70% feedback, making the music echo convincingly