Why does my Web Audio gain click when a MIDI fader moves?
If a click follows each fader jump, test your gain parameter before changing the oscillator or the MIDI timing. A gain step can abruptly change the amplitude of the audio at a nonzero sample. Doing that repeatedly can ma
If a click follows each fader jump, test your gain parameter before changing the oscillator or the MIDI timing. A gain step can abruptly change the amplitude of the audio at a nonzero sample. Doing that repeatedly can make a smooth hand motion sound stepped, especially when input values arrive in discrete packets.
Give the fader its own smoothed gain stage
Put a GainNode in series for the live fader. Keep the note envelope on a separate stage so the controller doesnβt rewrite attack or release automation. Each incoming value changes the target, and the fader approaches that target continuously rather than jumping to it.
const fader = context.createGain();
fader.gain.setValueAtTime(0.2, context.currentTime);
// Connect fader in series between your existing voice bus and mix bus.
// Call this with the data byte from your already-selected MIDI CC.
function onFaderCC(value) {
if (!Number.isInteger(value) || value < 0 || value > 127) return;
fader.gain.setTargetAtTime(value / 127, context.currentTime, 0.005);
}
The W3C Web Audio 1.0 Recommendation defines the third argument in seconds. Itβs a time constant, not the time to finish: the parameter traverses about 63% of the remaining difference in one time constant and about 95% in three. Use the audio contextβs clock. This example assumes a live fader on its own AudioParam, with no queued automation from another controller. A linear mapping is used here to isolate smoothing; change the faderβs mapping separately if you need another feel.
Compare the parameter, not just the click
Start at gain 0.2 and request 0.8. A direct step changes gain by 0.6 immediately. With a 5 ms time constant, the reference model changes gain by about 0.002495 over the first 48 kHz sample interval and reaches about 0.770 at 15 ms.
Command Elapsed Gain
Step 0.2 β 0.8 Immediately 0.800000
Target 0.8, tau = 5 ms 1 sample 0.202495
Target 0.8, tau = 5 ms 10 ms 0.718799
Target 0.8, tau = 5 ms 15 ms 0.770128
Target 0.8, tau = 20 ms 20 ms 0.579272
In an offline calculation using the specβs equation, 768 cases checked that retargeting starts from the current value and the next step stays between it and the new target. These are parameter values, not measured loudness, browser latency, or a listening test. On a real patch, try the shortest smoothing that takes the edge off without making the fader feel slow. For a button or a drum trigger, keep the trigger discrete and handle its envelope separately.
Sonari for Mac can send MIDI from gestures. If you route a continuous control to a browser patch, log the incoming values before smoothing; smoothing can ease a gain step, but it wonβt repair a missing or misrouted input. The example does not describe Sonariβs own signal processing.
Disclosure: Sonari Studio makes Sonari. AI generated this guide and its offline example.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.