constructor(x, y, r, baseFreq)
The constructor runs once per Bongo object and is where you set up everything that object needs to remember and use later - its position, its sound engine, and its default state.
constructor(x, y, r, baseFreq) {
this.x = x; // center of drum head
this.y = y;
this.r = r; // radius of drum head (for hit detection / drawing)
this.baseFreq = baseFreq;
this.hitAmount = 0; // 0..1, used to animate skin depression
// Sound: p5.Oscillator + p5.Envelope
// Docs: https://p5js.org/reference/#/p5.Oscillator
this.osc = new p5.Oscillator('sine');
this.env = new p5.Envelope(); // https://p5js.org/reference/#/p5.Envelope
// Percussive envelope: fast attack, short decay, no sustain, short release
this.env.setADSR(0.001, 0.15, 0.0, 0.18);
this.env.setRange(0.9, 0);
this.osc.freq(this.baseFreq);
this.osc.start();
this.osc.amp(0); // silence until envelope plays
}
Line-by-line explanation (8 lines)
this.x = x; this.y = y; this.r = r;- Stores the drum's center position and radius so hit-detection and drawing can use them later.
this.hitAmount = 0;- Starts the 'how depressed is the skin right now' value at 0, meaning no visual dent yet.
this.osc = new p5.Oscillator('sine');- Creates a sine-wave sound generator - the actual source of the drum's tone.
this.env = new p5.Envelope();- Creates an amplitude envelope, which will shape the volume of the oscillator over time to sound like a struck drum instead of a continuous tone.
this.env.setADSR(0.001, 0.15, 0.0, 0.18);- Sets Attack/Decay/Sustain/Release times in seconds - a near-instant attack and short decay with zero sustain is what makes this sound percussive rather than sustained like an organ.
this.env.setRange(0.9, 0);- Sets the envelope's peak and resting amplitude - it will jump to 0.9 volume then fall back to 0.
this.osc.start();- Starts the oscillator running continuously in the background (silently, since amp is 0) so it's ready to be triggered instantly on a hit.
this.osc.amp(0);- Mutes the oscillator directly - the envelope will control volume from now on instead of this base amplitude.