constructor(x, y, r, baseFreq)
The constructor is called when you create a new Bongo object. It sets up all the properties and audio components. The oscillator runs continuously but stays silent (amplitude 0) until the envelope is triggered by a hit. This is more efficient than starting/stopping the oscillator repeatedly.
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
}
๐ง Subcomponents:
this.osc = new p5.Oscillator('sine');
Creates a sine wave oscillator that will generate the drum sound
this.env = new p5.Envelope();
Creates an amplitude envelope to shape the percussive attack and decay of the sound
this.env.setADSR(0.001, 0.15, 0.0, 0.18);
Sets Attack (0.001s), Decay (0.15s), Sustain (0.0), Release (0.18s) for percussive sound
this.env.setRange(0.9, 0);
Sets volume from 0.9 (peak) to 0 (silent) for the envelope
Line by Line:
this.x = x;- Stores the drum's horizontal center position on the canvas
this.y = y;- Stores the drum's vertical center position on the canvas
this.r = r;- Stores the drum head radius, used for both hit detection and drawing size
this.baseFreq = baseFreq;- Stores the base frequency in Hz (180 for low drum, 270 for high drum)
this.hitAmount = 0;- Initializes visual depression amount to 0 (no depression initially)
this.osc = new p5.Oscillator('sine');- Creates a sine wave oscillator that will produce the drum tone
this.env = new p5.Envelope();- Creates an envelope to control how the sound starts, sustains, and ends
this.env.setADSR(0.001, 0.15, 0.0, 0.18);- Configures the envelope: very fast attack (0.001s), quick decay (0.15s), no sustain, short release (0.18s) for a percussive hit
this.env.setRange(0.9, 0);- Sets the envelope to go from 0.9 volume (loud) to 0 (silent), creating the percussive effect
this.osc.freq(this.baseFreq);- Sets the oscillator's frequency to the base frequency (180 Hz or 270 Hz)
this.osc.start();- Starts the oscillator running continuously
this.osc.amp(0);- Sets oscillator amplitude to 0 so it's silent until the envelope plays