From 53b506adeb0d0e0032bd0cc492c24ac961822045 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Sun, 15 Nov 2026 12:11:06 -0800 Subject: [PATCH] fix(pedal): audio-rate linear bypass crossfade to kill on/off click MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The engaged↔bypassed crossfade smoother was a one-pole Lag.Control, whose exponential curve is front-loaded (steepest slope at the very start). That clicked when the two crossfade legs are decorrelated and full-scale — e.g. a wet-only pitch shifter swapping against the dry note — and was only masked when wet ≈ dry. A block-rate smoother does not fix it: at the engine's 512-sample block a short fade is just a few coarse steps, each its own click. Switch to an audio-rate linear Slew.Audio, which ramps one step per sample regardless of block size, driving an equal-power crossfade. The old `private let bypassLagTime` becomes an overridable `open var bypassFadeTime`, clamped to a positive floor before taking its reciprocal so a subclass override of 0 or negative cannot reintroduce an instantaneous (clicking) or inverted slew. Affects every pedal's on/off transition. Co-Authored-By: Claude Opus 4.8 --- Sources/LyrebirdShoeGaze/Pedal.swift | 46 +++++++++++++++++++++------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/Pedal.swift b/Sources/LyrebirdShoeGaze/Pedal.swift index 07857cc..92c4350 100644 --- a/Sources/LyrebirdShoeGaze/Pedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedal.swift @@ -19,7 +19,7 @@ import LyrebirdUI /// inputBus → dry → effect(_:) → wet /// ↕ mix (equal-power crossfade) /// blended -/// ↕ bypass (lagged equal-power crossfade with dry) +/// ↕ bypass (linear equal-power crossfade with dry) /// switched → × level → outputBus /// ``` /// @@ -32,7 +32,7 @@ import LyrebirdUI /// /// - `mix`: Wet/dry blend in `[0, 1]`. `0` = fully dry, `1` = fully wet. /// - `bypass`: Bypass state. `0` = engaged (effect active), `1` = bypassed (pure dry). -/// Transitions are smoothed with a 20 ms lag to avoid clicks. +/// Transitions are smoothed with a linear crossfade (see ``bypassFadeTime``) to avoid clicks. /// - `level`: Output gain as a linear multiplier. `1.0` = unity gain. /// /// - Parameters: @@ -47,7 +47,7 @@ open class Pedal: SynthesizerBase { @UIParam public var mix: Double = 1.0 /// Bypass state. `0` = engaged (effect active), `1` = bypassed (dry signal only). - /// Transitions are smoothed by `bypassLagTime` seconds to prevent clicks. + /// Transitions are smoothed by a linear crossfade over ``bypassFadeTime`` seconds to prevent clicks. @UIParam public var bypass: Double = 0.0 /// Output gain as a linear multiplier. `1.0` = unity gain. @@ -156,9 +156,22 @@ open class Pedal: SynthesizerBase { // MARK: - Constants - /// Lag time constant in seconds (60 dB convergence) for the bypass crossfade. - /// Smooths engaged ↔ bypassed transitions to prevent audible clicks. - private let bypassLagTime: Double = 0.02 + /// Duration in seconds of the engaged ↔ bypassed crossfade. The bypass control is slewed + /// **linearly** and at **audio rate** from `0` to `1` (or back) over this time, so the + /// equal-power crossfade sweeps smoothly, one step per sample. Two earlier approaches clicked + /// here when the crossfade legs are decorrelated and full-scale (a wet-only pitch shifter such as + /// the Whammy swapping against the dry note; masked when wet ≈ dry): a one-pole `Lag`, whose + /// exponential curve is front-loaded (steepest slope at the very start); and a block-rate slew, + /// which steps only once per audio block — at the engine's 512-sample block a short fade is just + /// a few coarse jumps, each its own click. An audio-rate linear slew has neither flaw. + /// Overridable for pedals needing a gentler fade. + open var bypassFadeTime: Double { 0.03 } + + /// Lower bound the effective fade time is clamped to before taking its reciprocal for the slew + /// rate. Guards against an override returning `0` (reciprocal `.infinity` → instantaneous slew, + /// which reintroduces the click this fade removes) or a negative value (which would invert the + /// slew). One sample at 48 kHz, so a legitimate small fade is left untouched. + private let minBypassFadeTime: Double = 1.0 / 48_000.0 // Bipolar-span constant used to remap a [0, 1] value to [-1, +1] // for CrossFade's equal-power pan parameter. @@ -421,7 +434,7 @@ open class Pedal: SynthesizerBase { /// Builds the signal graph for one channel: dry → wet/dry blend → bypass switch → × level. /// /// Returns the final post-`level` source for the channel. The `mix`, `bypass`, and `level` - /// `@UIParam` nodes are shared across channels; each channel gets its own `Lag.Control` + /// `@UIParam` nodes are shared across channels; each channel gets its own `Slew.Audio` /// for the bypass crossfade so smoothing is per-channel state driven by the same target. /// /// - Parameters: @@ -442,14 +455,25 @@ open class Pedal: SynthesizerBase { pan: mixPan ) - // Smooth bypass transitions with a 20 ms lag to prevent clicks. - let bypassLagged: Lag.Control = Lag.Control( + // Smooth bypass transitions with a linear crossfade to prevent clicks. The slew runs at + // AUDIO rate, not control rate: a block-rate smoother takes one step per audio block, so at + // the engine's 512-sample block a short fade is only a handful of coarse steps — each a + // click of its own. `Slew.Audio` ramps per sample, so the fade is smooth regardless of + // block size. Linear slope (not a front-loaded one-pole lag) keeps it click-free even when + // the two legs are decorrelated and full-scale, as for a wet-only pitch shifter. + // Rate is in bypass-units per second: 0 → 1 traverses in exactly `bypassFadeTime` seconds. + // Clamp to a positive floor first — a subclass override returning 0 or negative would + // otherwise yield an infinite or inverted slew rate (see `minBypassFadeTime`). + let effectiveFadeTime: Double = max(bypassFadeTime, minBypassFadeTime) + let bypassFadeRate: Double = 1.0 / effectiveFadeTime + let bypassSlewed: Slew.Audio = Slew.Audio( input: $bypass, - time: bypassLagTime + up: bypassFadeRate, + dn: bypassFadeRate ) // Map bypass [0, 1] → pan [-1, +1]: −1 = blended (engaged), +1 = dry (bypassed). - let bypassPan: any ValidGeneratorInput = (bypassLagged * bipolarSpan) - bipolarOffset + let bypassPan: any ValidGeneratorInput = (bypassSlewed * bipolarSpan) - bipolarOffset // Equal-power crossfade: engaged → blended; bypassed → dry. let switched: CrossFade.Audio = CrossFade.Audio(