From 5a3cbeb3722322c76333ebfcde4fc1878b86e08e Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Tue, 4 Aug 2026 16:41:27 -0700 Subject: [PATCH] fix(noise-gate): smooth asymmetric close + Attack/Release knobs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reported bug: a sustained note popped into a different tone mid-decay (~3.63s in the user's recording). Root cause confirmed by rendering the user's rig with their input file: the gate keyed off the raw guitar and, as a note decayed through the threshold, snapped its gain 1.0 -> floor in ~10 ms (symmetric 10 ms Lag). With heavy downstream gain (the rig's Fuzz, ~50x) amplifying that step, the ~-9 dB duck read as an audible pop. Fix: replace the symmetric Lag with an asymmetric LagUpDown — fast open (so a note's attack isn't softened) / slow close (fades the gate shut and rides out envelope chatter at the threshold). Verified against the user's input file: the gate gain now fades 1.0 -> floor smoothly over ~70 ms, monotonic, no snap. Feature (per request): expose Attack (open) and Release (close) time as knobs in the advanced pop-over rather than a separate near-duplicate "Vari" gate. Placed in advancedKnobs so they persist append-only in advancedValues — gates saved before this load with the timing at defaults, no migration (verified by test). Release default 0.08 = the smooth close; range 10 ms..500 ms. Tests: gate close lingers longer with a longer Release and the default lingers well past a fast close (guards the pop fix + the Release control); Attack/ Release exposed in the pop-over (not the face), round-trip through state, and a legacy no-advancedValues preset defaults the timing. 675 tests pass. Co-Authored-By: Claude Opus 4.8 --- .../Pedals/NoiseGatePedal.swift | 52 +++++++++--- .../NoiseGatePedalTests.swift | 81 +++++++++++++++++++ 2 files changed, 124 insertions(+), 9 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/Pedals/NoiseGatePedal.swift b/Sources/LyrebirdShoeGaze/Pedals/NoiseGatePedal.swift index 03f493d..26c06c7 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/NoiseGatePedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/NoiseGatePedal.swift @@ -14,15 +14,17 @@ import LyrebirdUI /// dry ─┬─────────────────────────────────────────────────────────┐ /// └→ AmplitudeFollower → Schmidt(lo: thr, hi: thr×ratio) │ /// → LinLin(0…1 → floor…1) │ -/// → Lag(smooth) ───────────── × ──┴→ mono out +/// → LagUpDown(fast open/slow close) × ─┴→ mono out /// ``` /// /// An `AmplitudeFollower` tracks the dry signal level. A Schmitt trigger turns that /// envelope into an open/closed gate with hysteresis (opens above `threshold × openRatio`, /// closes below `threshold`) so the gate does not chatter around the threshold. The 0/1 -/// gate is mapped to a gain in `[floor, 1]` and smoothed before multiplying the dry signal: -/// when the gate is closed the signal is attenuated to `floor` rather than hard-muted, so -/// `floor` sets how much of the quiet signal/noise tail bleeds through. +/// gate is mapped to a gain in `[floor, 1]` and smoothed **asymmetrically** — opening fast so a +/// note's attack isn't softened, closing slowly so the gate fades shut instead of snapping (a fast +/// close popped audibly when heavy downstream gain amplified the step). When the gate is closed the +/// signal is attenuated to `floor` rather than hard-muted, so `floor` sets how much of the quiet +/// signal/noise tail bleeds through. /// /// The wet/dry mix and bypass switching are handled by the `Pedal` base class. /// @@ -35,6 +37,8 @@ import LyrebirdUI /// the gate. Range `[0, 0.2]`. Defaults to `0.02`. /// - floor: Residual gain applied while the gate is closed, in `[0, 1]`. `0` = full /// mute, `1` = no gating. Range `[0, 1]`. Defaults to `0.0`. +/// - attack: Gate open time in seconds (advanced pop-over). Range `[0.001, 0.05]`. Defaults to `0.005`. +/// - release: Gate close time in seconds (advanced pop-over). Range `[0.01, 0.5]`. Defaults to `0.08`. public final class NoiseGatePedal: Pedal { /// One-line description shown in the picker and info panel. @@ -50,6 +54,15 @@ public final class NoiseGatePedal: Pedal { /// Residual gain while closed, [0, 1]. Range [0, 1]. @UIParam public var floor: Double = 0.0 + /// Gate **Attack** (open time) in seconds — how fast the gate opens on a note. Short keeps + /// transients crisp. Range `[0.001, 0.05]`. Surfaced in the advanced pop-over. + @UIParam public var attack: Double = 0.005 + + /// Gate **Release** (close time) in seconds — how gradually the gate fades shut as a note decays + /// through the threshold. Short = a tight, fast gate; long = a gentle swell-down (and it rides + /// out chatter at the threshold). Range `[0.01, 0.5]`. Surfaced in the advanced pop-over. + @UIParam public var release: Double = 0.08 + // MARK: - Constants /// Default wet/dry blend — fully wet; the gate sits in-line. @@ -65,8 +78,11 @@ public final class NoiseGatePedal: Pedal { /// below `threshold`, giving hysteresis that prevents chatter near the threshold. private let openRatio: Double = 2.0 - /// Gain-smoothing lag time, in seconds — removes the click of an instantaneous 0↔1 gate. - private let gainSmoothTime: Double = 0.01 + // Gate-gain smoothing is **asymmetric** (``attack`` open / ``release`` close) so the gate opens + // promptly (a new note's attack isn't softened) but closes gently. A fast symmetric close + // snapped the gain from unity to `floor` in ~10 ms, which — with heavy downstream gain + // (fuzz/boost) amplifying the step — read as an audible "pop" as a sustained note decayed + // through the threshold. A slower `release` fades the gate shut instead and rides out chatter. /// Gate signal bounds for the `LinLin` floor map. private let gateLo: Double = 0.0 @@ -83,6 +99,12 @@ public final class NoiseGatePedal: Pedal { /// Floor knob range (residual gain). private let floorRange: ClosedRange = 0.0 ... 1.0 + /// Attack (open-time) knob range, seconds. + private let attackRange: ClosedRange = 0.001 ... 0.05 + + /// Release (close-time) knob range, seconds. + private let releaseRange: ClosedRange = 0.01 ... 0.5 + public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Threshold", node: $threshold, range: thresholdRange), @@ -90,6 +112,16 @@ public final class NoiseGatePedal: Pedal { ] } + /// Timing controls, tucked behind the pedal card's advanced pop-over: **Attack** (open time) and + /// **Release** (close time). Live nodes, append-only in `advancedValues` — a gate saved before + /// these existed loads with them at their defaults, no migration. + public override var advancedKnobs: [PedalKnob] { + [ + PedalKnob(label: "Attack", node: $attack, range: attackRange), + PedalKnob(label: "Release", node: $release, range: releaseRange), + ] + } + // MARK: - Init /// Creates a noise-gate pedal. @@ -141,10 +173,12 @@ public final class NoiseGatePedal: Pedal { outHi: openGain ) - // Smooth the gain to remove the click of an instantaneous gate transition. - let smoothed: Lag.Audio = Lag.Audio( + // Smooth the gain asymmetrically: open fast (prompt on a note), close slowly (gentle fade, + // no pop, and it rides out chatter as the envelope hovers at the threshold). + let smoothed: LagUpDown.Audio = LagUpDown.Audio( input: gain, - time: gainSmoothTime + lagUp: $attack, + lagDown: $release ) return dry * smoothed diff --git a/Tests/LyrebirdShoeGazeTests/NoiseGatePedalTests.swift b/Tests/LyrebirdShoeGazeTests/NoiseGatePedalTests.swift index df0f25b..9e8a80f 100644 --- a/Tests/LyrebirdShoeGazeTests/NoiseGatePedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/NoiseGatePedalTests.swift @@ -89,4 +89,85 @@ struct NoiseGatePedalTests { let samples: [Float] = try render(threshold: 0.95, floor: 1.0) #expect(hasNonSilentOutput(samples), "Unity-floor gate muted the signal") } + + /// A sine that steps from loud (0.3, gate open) down to a constant below-threshold level (0.03) + /// over 50 ms and holds there — so the gate opens, then closes, while the source level stays + /// constant. That isolates the gate's close fade (release-dependent) from any source decay. + /// `Line` holds its `end` value after `dur`, giving the sustained low tail. + private final class DecaySineSource: SynthesizerBase { + private let bus: Int + init(bus: Int) { self.bus = bus; super.init() } + override func build() { + output(bus: bus, source: OscSin.Audio(freq: 220.0) * Line.Control(start: 0.3, end: 0.03, dur: 0.05)) + } + } + + /// Energy in the gate's *close window* — the source steps down through the threshold ~50 ms in + /// then holds a constant below-threshold level, so the output energy after that reflects only how + /// long the gate lingers before it finishes fading shut. A longer `release` = more lingering + /// energy; a snap close = little. (floor 0, so once fully closed the tail is silent.) + private func closeWindowEnergy(release: Double) throws -> Double { + let sampleRate: Double = 48000.0 + let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.6, sampleRate: sampleRate) + defer { try? FileManager.default.removeItem(at: url) } + let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) + let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) + defer { engine.busAllocator.releaseAudioBus(srcBus); engine.busAllocator.releaseAudioBus(dstBus) } + let source: DecaySineSource = DecaySineSource(bus: srcBus.index) + let gate: NoiseGatePedal = NoiseGatePedal(inputBus: srcBus.index, outputBus: dstBus.index) + gate.sampleRate = sampleRate + gate.threshold = 0.06 + gate.floor = 0.0 + gate.release = release + let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index) + try engine.add(source) + try engine.add(gate, position: NodePosition.after(source)) + try engine.add(master, position: NodePosition.after(gate)) + try engine.start() + let inter: [Float] = readStereoCAF(url: url, sampleRate: sampleRate) + let mono: [Double] = stride(from: 0, to: inter.count, by: 2).map { Double(inter[$0]) } + let start: Int = Int(0.12 * sampleRate) // after the step-down; the gate is closing/closed + var energy: Double = 0.0 + for i: Int in start.. fast * 1.5, "long Release did not linger longer than short (fast=\(fast) slow=\(slow))") + #expect(deflt > fast * 1.2, "default close is nearly as abrupt as the fast close (\(deflt) vs \(fast))") + } + + @Test func exposesAttackReleaseInAdvancedPopover() throws { + #expect(NoiseGatePedal().advancedKnobs.map { $0.label } == ["Attack", "Release"]) + // Attack/Release are pop-over only — not on the face. + let face: [String] = NoiseGatePedal().effectKnobs.map { $0.label } + #expect(face == ["Threshold", "Floor"]) + } + + @Test func attackReleaseRoundTripThroughState() throws { + let pedal: NoiseGatePedal = NoiseGatePedal() + pedal.attack = 0.02 + pedal.release = 0.3 + let restored: NoiseGatePedal = NoiseGatePedal() + restored.applyState(pedal.captureState()) + #expect(restored.attack == 0.02 && restored.release == 0.3, "Attack/Release did not round-trip") + } + + @Test func legacyGateWithoutAdvancedValuesDefaultsTiming() throws { + // A gate saved before Attack/Release existed has empty advancedValues → timing defaults, and + // Threshold/Floor still restore correctly (append-only, no migration). + let legacy: PedalState = PedalState( + id: UUID(), type: "Noise Gate", + knobValues: [0.03, 0.4, 1.0, 0.7], selectorValues: [], advancedValues: [], bypass: 0.0) + let pedal: NoiseGatePedal = NoiseGatePedal() + pedal.applyState(legacy) + #expect(pedal.threshold == 0.03 && pedal.floor == 0.4, "legacy Threshold/Floor mis-mapped") + #expect(pedal.attack == 0.005 && pedal.release == 0.08, "legacy gate should default Attack/Release") + } }