Pad constructor
The constructor runs once per Pad object when it's created. Setting up an oscillator per pad here (instead of one shared oscillator) lets every pad play its own note independently, even at the same time.
constructor(x, y, size, color, noteMIDI) {
this.x = x;
this.y = y;
this.size = size;
this.color = color;
this.note = noteMIDI;
this.freq = midiToFreq(noteMIDI); // Convert MIDI note number to frequency in Hz
// Create a p5.Oscillator (sine wave by default)
this.oscillator = new p5.Oscillator('sine');
this.oscillator.amp(0); // Start with 0 amplitude (silent)
this.oscillator.freq(this.freq); // Set the oscillator's frequency to the pad's note
this.oscillator.start(); // Start the oscillator, but it won't make sound until amp() is > 0
this.glowAlpha = 0; // Initial glow brightness (0 = fully transparent)
}
Line-by-line explanation (7 lines)
this.x = x; this.y = y; this.size = size;- Stores the pad's position and size so display() and contains() know where to draw and test clicks.
this.freq = midiToFreq(noteMIDI);- Converts a MIDI note number (like 60 for middle C) into a frequency in Hertz that the oscillator can play.
this.oscillator = new p5.Oscillator('sine');- Creates a new sine-wave sound generator from the p5.sound library, one per pad.
this.oscillator.amp(0);- Sets the oscillator's volume to zero so it's completely silent until play() is called.
this.oscillator.freq(this.freq);- Tunes the oscillator to the pad's specific note frequency.
this.oscillator.start();- Starts the oscillator running in the background - it's always 'on' but inaudible at zero amplitude, ready to be triggered instantly.
this.glowAlpha = 0;- Initializes the glow brightness to fully transparent so pads start with no visible flash.