The core of PianoMitra's audio engine is the **Web Audio API**. As discussed in our manifesto, this allows us to generate sound waves—Sine, Triangle, Sawtooth—instantly through synthesis, avoiding the latency and file size issues of sampling. However, a raw waveform, no matter how pure, often sounds thin and unpolished. To achieve the rich, dynamic sound profiles available in our Sound Lab, we must process that raw signal through a series of specialized nodes. This is where the true power of the Web Audio API lies: its ability to create complex signal chains, just like a professional Digital Audio Workstation (DAW).
Think of the audio signal as water flowing through a series of pipes, where each pipe (or **AudioNode**) modifies the water (the sound wave) in a specific way. Understanding this signal flow is the key to creating unique and professional-sounding patches in PianoMitra.
The Signal Chain: From Source to Speaker
In PianoMitra, the audio stream follows a path that is remarkably similar to a hardware synthesizer: **Source → Effects → Destination**.
- **Source Node:** This is where the sound originates, typically an `OscillatorNode` (for the waveform) or a `BufferSourceNode` (for a sample).
- **Processing Nodes:** This is the heart of the effects engine. Nodes like `GainNode`, `BiquadFilterNode`, `DelayNode`, and `DynamicsCompressorNode` are inserted here.
- **Destination Node:** This is the final output node, always `AudioContext.destination`, which sends the processed sound to your speakers or headphones.
Node Deep Dive: Shaping the Timbre with Filtering
The most critical component for shaping the tonal quality (timbre) of a sound is the **`BiquadFilterNode`**. A biquad filter is a second-order filter that can be used to create Low-Pass, High-Pass, Bandpass, and various other filter types that are essential for sound design. When you see a "Tone" or "Cutoff" knob in our Sound Lab, you are interacting directly with the parameters of a `BiquadFilterNode`.
Key `BiquadFilterNode` Parameters:
- **`type`:** Defines the kind of filter. Common values include `lowpass` (cuts high frequencies), `highpass` (cuts low frequencies), and `bandpass` (allows a band of frequencies through).
- **`frequency`:** The **cutoff frequency** in Hertz. This is the point at which the filter begins to affect the signal. Adjusting this value dynamically is what makes "wah" effects possible.
- **`Q` (Quality Factor):** Controls the resonance, or the sharpness of the peak at the cutoff frequency. High Q values create a distinct, ringing tone often used in electronic dance music synths.
- **`gain`:** Used for specialized filters like shelving or peaking, controlling how much a certain frequency range is boosted or cut.
By connecting an `OscillatorNode` output into a `BiquadFilterNode` and then routing the filter output to the destination, we can transform a harsh Sawtooth wave into a mellow, warm pad sound or a punchy, resonant bass.
Adding Depth and Space: Delay and Reverb
To make a sound feel real or spacious, we must simulate the reflections that occur in a physical environment. This is accomplished using **Time-Based Effects**, primarily **Delay** and **Reverb**.
The `DelayNode`
The `DelayNode` is the simplest of these, taking an input and delaying it by a specific amount of time before sending it to the output. Its core parameter is `delayTime`, measured in seconds. For musical effects, this time is often synchronized with the tempo of the music (e.g., 0.5 seconds for a half note delay at 60 BPM).
To create a true echo or looping effect (known as **feedback**), the output of the `DelayNode` is subtly routed back to its own input. This requires a precise `GainNode` inserted into the feedback loop to control the decay of the echoes. If the gain is too high, the sound will infinitely loop and quickly distort. A small amount of gain (e.g., 0.4) allows the echoes to naturally fade out.
Implementing Reverb with Convolution
While basic reverb can be approximated with multiple short delays, professional-grade reverb relies on the **`ConvolverNode`**. This node uses a technique called **convolution** to apply an **Impulse Response (IR)** to the audio signal. An IR is essentially a short recording of a sound (like a click or a balloon pop) made within a specific acoustic space (e.g., a cathedral, a small room, or a spring reverb tank).
The `ConvolverNode` mathematically "stamps" the acoustic signature of that space onto the synthetic sound, making the sound feel as if it were played in the cathedral itself. PianoMitra utilizes a small library of pre-recorded impulse responses, allowing users to select ambient spaces without incurring the computational cost of a real-time algorithmic reverb.
const delay = audioCtx.createDelay(1.0); // Max delay of 1 second
delay.delayTime.setValueAtTime(0.3, audioCtx.currentTime); // Set delay to 300ms
const feedbackGain = audioCtx.createGain();
feedbackGain.gain.setValueAtTime(0.4, audioCtx.currentTime);
// Create the feedback loop
delay.connect(feedbackGain).connect(delay);
Mastering the Dynamics: Compression
The final, crucial step in achieving a polished sound is controlling the dynamics (the difference between the loudest and quietest parts). This is the job of the **`DynamicsCompressorNode`**, which is applied as a master effect to the entire output or to individual instrument lines (like a kick drum).
How Compression Works:
Compression reduces the dynamic range of a signal by turning down the volume of the loudest parts. This makes the overall sound appear louder and more consistent, helping it "sit" better in a mix. The `DynamicsCompressorNode` has four key parameters:
- **`threshold`:** The volume level (in decibels, typically negative) above which the compressor starts working.
- **`ratio`:** How much the sound is turned down once it exceeds the threshold (e.g., a ratio of 4:1 means that for every 4dB the signal goes over the threshold, only 1dB is allowed through).
- **`attack`:** How quickly the compressor reacts to an incoming signal above the threshold (fast attacks are punchy, slow attacks let the initial transient through).
- **`release`:** How quickly the compressor stops working after the signal drops back below the threshold.
By carefully tuning the attack and release times, PianoMitra's compressor ensures that high-velocity MIDI notes don't clip the audio output, maintaining a consistent and clean listening experience across all devices.
Conclusion: Building Your Custom Synth Patch
The true art of synthesis in PianoMitra is understanding how these nodes interact. Start with a simple Sawtooth wave. Add a `BiquadFilterNode` with a lowpass setting to warm it up. Then, connect that to a `DelayNode` to give it space, and finally, run the whole sound through a `DynamicsCompressorNode` to level out the performance. This complex but logical signal chain is what transforms simple math into rich, professional music, all powered by the capabilities of your modern web browser.