setup()
setup() runs once when the sketch starts and is where you initialize all p5.sound objects BEFORE calling any methods on them. This prevents errors when trying to set ADSR values on envelopes that haven't been created yet. The structure here—creating noise generators, filters, and envelopes, then chaining them together with disconnect() and connect()—is the fundamental pattern for building custom audio in p5.sound.
function setup() {
createCanvas(windowWidth, windowHeight);
noiseDetail(8, 0.5); // Set noise detail for smoother visuals
// --- Initialize all p5.sound related objects at the start of setup() ---
// This helps prevent the 'setADSR' error by ensuring objects are defined
// before any methods are called on them.
noiseGen = new p5.Noise('white');
waveFilter = new p5.Filter('lowpass');
crashNoise = new p5.Noise('white');
crashEnvelope = new p5.Envelope();
crashFilter = new p5.Filter('bandpass');
// --- Continuous Wave Audio Setup ---
noiseGen.start();
noiseGen.amp(0); // Start silent
waveFilter.freq(200);
waveFilter.res(1.5);
noiseGen.disconnect();
noiseGen.connect(waveFilter);
waveFilter.connect();
// --- Crashing Wave Audio Setup ---
crashNoise.start();
crashNoise.amp(0); // Start silent
// Adjusted ADSR parameters for a 5-second crash
crashEnvelope.setADSR(0.01, 1, 0.1, 4); // Attack, Decay, Sustain, Release
crashEnvelope.setRange(1, 0); // Max amplitude, Min amplitude
crashFilter.freq(2000); // Higher frequency for splash sound
crashFilter.res(2); // Some resonance
crashNoise.disconnect(); // Disconnect crashNoise from master
crashNoise.connect(crashFilter); // Connect crashNoise to crashFilter
crashFilter.connect(); // Connect crashFilter to master output
// Important: User must interact with the page for audio to start in most browsers
userStartAudio();
// --- UI Elements ---
// Volume slider for continuous wave
volumeSlider = createSlider(0, 1, 0.5, 0.01);
volumeSlider.position(10, 10);
volumeSlider.style('width', '180px');
volLabel = createDiv('Continuous Vol');
volLabel.position(200, 10);
volLabel.style('color', 'black');
volLabel.style('font-size', '14px');
// Filter frequency slider (Wave Depth)
pitchSlider = createSlider(50, 1000, 200, 1);
pitchSlider.position(10, 40);
pitchSlider.style('width', '180px');
pitchLabel = createDiv('Wave Depth / Filter Freq');
pitchLabel.position(200, 40);
pitchLabel.style('color', 'black');
pitchLabel.style('font-size', '14px');
// Crash Interval slider - default increased to avoid immediate overlap
crashIntervalSlider = createSlider(60, 300, 360, 10); // Min frames, Max frames, Initial, Step
crashIntervalSlider.position(10, 70);
crashIntervalSlider.style('width', '180px');
crashIntervalLabel = createDiv('Crash Interval (frames)');
crashIntervalLabel.position(200, 70);
crashIntervalLabel.style('color', 'black');
crashIntervalLabel.style('font-size', '14px');
// Crash Volume slider
crashVolumeSlider = createSlider(0, 1, 0.7, 0.01);
crashVolumeSlider.position(10, 100);
crashVolumeSlider.style('width', '180px');
crashVolumeLabel = createDiv('Crash Volume');
crashVolumeLabel.position(200, 100);
crashVolumeLabel.style('color', 'black');
crashVolumeLabel.style('font-size', '14px');
// Instructions for audio start
audioInstructions = createDiv('Click or press a key to start audio');
audioInstructions.position(10, 130);
audioInstructions.style('color', 'black');
audioInstructions.style('font-size', '14px');
// Hide instructions after first user interaction (for audio)
document.addEventListener('mousedown', hideAudioInstructions);
document.addEventListener('keydown', hideAudioInstructions);
}
Line-by-line explanation (10 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
noiseDetail(8, 0.5);
Creates a full-window canvas and configures Perlin noise to produce smooth, organic wave patterns
noiseGen = new p5.Noise('white');
waveFilter = new p5.Filter('lowpass');
noiseGen.start();
noiseGen.amp(0);
waveFilter.freq(200);
waveFilter.res(1.5);
noiseGen.disconnect();
noiseGen.connect(waveFilter);
waveFilter.connect();
Creates and chains a white noise generator through a lowpass filter to produce the continuous wave sound effect
crashNoise = new p5.Noise('white');
crashEnvelope = new p5.Envelope();
crashFilter = new p5.Filter('bandpass');
crashNoise.start();
crashNoise.amp(0);
crashEnvelope.setADSR(0.01, 1, 0.1, 4);
crashEnvelope.setRange(1, 0);
crashFilter.freq(2000);
crashFilter.res(2);
crashNoise.disconnect();
crashNoise.connect(crashFilter);
crashFilter.connect();
Creates a separate noise generator, envelope controller, and bandpass filter for the crash sounds with a 5-second fade-out decay
volumeSlider = createSlider(0, 1, 0.5, 0.01);
volumeSlider.position(10, 10);
pitchSlider = createSlider(50, 1000, 200, 1);
pitchSlider.position(10, 40);
crashIntervalSlider = createSlider(60, 300, 360, 10);
crashIntervalSlider.position(10, 70);
crashVolumeSlider = createSlider(0, 1, 0.7, 0.01);
crashVolumeSlider.position(10, 100);
Creates four interactive sliders to control continuous volume, wave depth, crash interval, and crash volume, positioning them in the top-left corner
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window, making the sketch responsive to window size
noiseDetail(8, 0.5);- Configures Perlin noise to use 8 octaves of detail with 0.5 falloff, creating smoother and more natural-looking waves
noiseGen = new p5.Noise('white');- Creates a white noise generator object (unfiltered noise containing all frequencies equally) for the continuous wave sound
waveFilter = new p5.Filter('lowpass');- Creates a lowpass filter that will remove high frequencies, making the noise sound deeper and more wave-like
noiseGen.start();- Starts the noise generator, making it ready to produce sound (though initially silenced with amp(0))
waveFilter.freq(200);- Sets the filter's cutoff frequency to 200 Hz, allowing only frequencies below 200 Hz to pass through, creating deep ocean tones
noiseGen.connect(waveFilter);- Routes the noise generator output through the filter instead of directly to the speaker, shaping its tone
crashEnvelope.setADSR(0.01, 1, 0.1, 4);- Configures the crash sound's attack (0.01s), decay (1s), sustain (0.1 amplitude), and release (4s) envelope—the sound fades out over 4 seconds
crashFilter.freq(2000);- Sets the crash filter's center frequency to 2000 Hz, emphasizing mid-range frequencies for a crisp splashing sound
userStartAudio();- Initializes the audio context and allows audio to start when the user clicks or presses a key (required by most browsers for security)