From e697789bd77effc8faf792ca685f3c51f1e1f108 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Fri, 31 Jul 2026 18:22:43 -0700 Subject: [PATCH] feat(pedals): limiter Release knob + compressor safety limiter; bump Lyrebird 1.6.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Consume Lyrebird 1.6.4, which decouples LookAheadLimiter's release time from its look-ahead window. - LimiterPedal: add a Release knob (0.01–0.5 s) wired to the new LookAheadLimiter.releaseTime. A too-fast release modulated the gain within a low-frequency cycle and distorted sustained notes when driven hard. - CompressorPedal: add an opt-in Safety switch (off by default) that inserts a brick-wall LookAheadLimiter after the compressor to catch transient overshoot that could clip the output. Built as both branches with Select picking live; engaging it adds the limiter's short look-ahead delay. Tests cover the new knob/switch exposure, release-time effect, and that the safety limiter caps peaks. Co-Authored-By: Claude Opus 4.8 --- Package.resolved | 6 +- Package.swift | 2 +- .../Pedals/CompressorPedal.swift | 52 ++++++++++++- .../Pedals/LimiterPedal.swift | 11 +++ .../CompressorPedalTests.swift | 76 +++++++++++++++++++ .../LimiterPedalTests.swift | 30 ++++++++ 6 files changed, 172 insertions(+), 5 deletions(-) diff --git a/Package.resolved b/Package.resolved index c8c25e6..e134f69 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "2a63dcbef74c280c7062a8342821f694fd71a59d391c302b86b3b86b00481260", + "originHash" : "9365abc9e3bf41483a03dca1c6be0421a5ca41bff1a9db33c8ddc62b2de04ef1", "pins" : [ { "identity" : "linenoise-swift", @@ -15,8 +15,8 @@ "kind" : "remoteSourceControl", "location" : "https://realizedsound.mooo.com/josh/Lyrebird", "state" : { - "revision" : "63d6e0746fdedce626c9eff832e799bad949708e", - "version" : "1.2.4" + "revision" : "d6875da2cb6628c4274454dd55a632dd76212759", + "version" : "1.6.4" } }, { diff --git a/Package.swift b/Package.swift index 4ae39b7..8252996 100644 --- a/Package.swift +++ b/Package.swift @@ -24,7 +24,7 @@ let package = Package( .library(name: "LyrebirdShoeGazeUI", targets: ["LyrebirdShoeGazeUI"]), ], dependencies: [ - .package(url: "https://realizedsound.mooo.com/josh/Lyrebird", from: "1.6.3"), + .package(url: "https://realizedsound.mooo.com/josh/Lyrebird", from: "1.6.4"), ], targets: [ // The core target depends on LyrebirdUI as well as the synthesis core: diff --git a/Sources/LyrebirdShoeGaze/Pedals/CompressorPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/CompressorPedal.swift index feb0d3e..c42ab71 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/CompressorPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/CompressorPedal.swift @@ -32,6 +32,12 @@ import LyrebirdUI /// compressed. Range `[-40, 0]` dB. Defaults to `-12.0`. /// - ratio: Compression ratio `≥ 1.0`. `1.0` = bypass, `4.0` = 4:1 compression. /// Range `[1, 20]`. Defaults to `4.0`. +/// +/// A feed-forward compressor can overshoot on fast transients (its attack is non-zero), +/// so a hot input plus makeup gain can push peaks past full scale and clip the output. +/// The **Safety** switch inserts a brick-wall `LookAheadLimiter` after the compressor to +/// catch those peaks; it is off by default (leave adding a limiter to the user) and adds +/// a short look-ahead output delay when engaged. public final class CompressorPedal: Pedal { /// One-line description shown in the picker and info panel. @@ -50,6 +56,10 @@ public final class CompressorPedal: Pedal { /// Compression ratio. Range [1, 20]. @UIParam public var ratio: Double = 4.0 + /// Safety limiter engage state. `0` = off (compressor may overshoot), `1` = on + /// (brick-wall look-ahead limiter catches transient peaks). + @UIParam public var safetyLimiter: Double = 0.0 + // MARK: - Constants /// Default wet/dry blend — fully wet; the compressor is inserted in-line. @@ -71,6 +81,20 @@ public final class CompressorPedal: Pedal { /// Makeup gain in dB applied after compression to restore perceived loudness. private let makeupGainDB: Double = 4.0 + /// Safety-limiter peak ceiling (linear amplitude), just below full scale. + private let safetyCeiling: Double = 0.98 + + /// Safety-limiter look-ahead / output delay in seconds. Short enough to catch + /// transients without smearing attack; only in circuit when Safety is engaged. + private let safetyLookAheadTime: Double = 0.005 + + /// Safety-limiter gain-release time in seconds. + private let safetyReleaseTime: Double = 0.05 + + /// Safety switch position values. + private let safetyOffValue: Double = 0.0 + private let safetyOnValue: Double = 1.0 + // MARK: - UI knobs /// Input-gain knob range in dB. @@ -90,6 +114,19 @@ public final class CompressorPedal: Pedal { ] } + public override var effectSwitches: [PedalSwitch] { + [ + PedalSwitch( + label: "Safety", + node: $safetyLimiter, + options: [ + PedalSwitch.Option(title: "Off", value: safetyOffValue), + PedalSwitch.Option(title: "On", value: safetyOnValue), + ] + ) + ] + } + // MARK: - Init /// Creates a compressor pedal. @@ -121,7 +158,7 @@ public final class CompressorPedal: Pedal { // Convert the input-gain dB to a linear multiplier (10^(dB/20)) and apply before compression. let inputGainLinear: Exp.Audio = Exp.Audio($inputGain * dbToLinExpScale) let boosted: any ValidGeneratorInput = dry * inputGainLinear - return Compressor.Audio( + let compressed: Compressor.Audio = Compressor.Audio( in: boosted, threshold: $threshold, ratio: $ratio, @@ -130,5 +167,18 @@ public final class CompressorPedal: Pedal { kneeWidth: kneeWidth, makeupGain: makeupGainDB ) + // Brick-wall safety limiter after the compressor. Both branches are always built; + // `Select` picks per the Safety switch (0 = compressor only, 1 = limited). + let limited: LookAheadLimiter = LookAheadLimiter( + in: compressed, + maxAmplitude: safetyCeiling, + lookAheadTime: safetyLookAheadTime, + releaseTime: safetyReleaseTime, + sampleRate: sampleRate + ) + return Select.Audio( + which: $safetyLimiter, + inputs: [compressed, limited] + ) } } diff --git a/Sources/LyrebirdShoeGaze/Pedals/LimiterPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/LimiterPedal.swift index abf8793..1f38099 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/LimiterPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/LimiterPedal.swift @@ -29,6 +29,9 @@ import LyrebirdUI /// drive harder into the limiter. Range `[1, 12]`. Defaults to `1.0`. /// - ceiling: Output peak ceiling as a linear amplitude in `[0, 1]`. The limited output /// never exceeds this absolute value. Range `[0.1, 1.0]`. Defaults to `0.95`. +/// - release: Gain-release time in seconds. Too short a release modulates the gain +/// within a waveform cycle and distorts low notes; longer is cleaner but lets the +/// level swell back more slowly. Range `[0.01, 0.5]`. Defaults to `0.05`. public final class LimiterPedal: Pedal { /// One-line description shown in the picker and info panel. @@ -44,6 +47,9 @@ public final class LimiterPedal: Pedal { /// Output peak ceiling (linear amplitude). Range [0.1, 1.0]. @UIParam public var ceiling: Double = 0.95 + /// Gain-release time in seconds. Range [0.01, 0.5]. + @UIParam public var release: Double = 0.05 + // MARK: - Constants /// Default wet/dry blend — fully wet; the limiter sits in-line. @@ -61,10 +67,14 @@ public final class LimiterPedal: Pedal { /// Ceiling knob range (linear output peak). private let ceilingRange: ClosedRange = 0.1 ... 1.0 + /// Release knob range in seconds. + private let releaseRange: ClosedRange = 0.01 ... 0.5 + public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Boost", node: $boost, range: boostRange), PedalKnob(label: "Ceiling", node: $ceiling, range: ceilingRange), + PedalKnob(label: "Release", node: $release, range: releaseRange), ] } @@ -101,6 +111,7 @@ public final class LimiterPedal: Pedal { in: boosted, maxAmplitude: $ceiling, lookAheadTime: lookAheadTime, + releaseTime: $release, sampleRate: sampleRate ) } diff --git a/Tests/LyrebirdShoeGazeTests/CompressorPedalTests.swift b/Tests/LyrebirdShoeGazeTests/CompressorPedalTests.swift index 04d9724..b10eb57 100644 --- a/Tests/LyrebirdShoeGazeTests/CompressorPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/CompressorPedalTests.swift @@ -105,6 +105,82 @@ struct CompressorPedalTests { #expect(labels == ["Input", "Threshold", "Ratio"]) } + @Test func exposesSafetySwitch() throws { + let pedal: CompressorPedal = CompressorPedal() + let switchLabels: [String] = pedal.effectSwitches.map { $0.label } + #expect(switchLabels == ["Safety"]) + let optionTitles: [String] = pedal.effectSwitches.first?.options.map { $0.title } ?? [] + #expect(optionTitles == ["Off", "On"]) + } + + @Test func safetyLimiterCapsPeaks() throws { + // Drive the compressor hard (hot input + makeup gain) so its non-zero attack lets + // transients overshoot. With the safety limiter engaged the output peak must stay + // near full scale; the two switch states must also produce different output. + let renderDuration: Double = 0.1 + let sampleRate: Double = 44100.0 + + func render(safety: Double) throws -> [Float] { + let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( + duration: renderDuration, + 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: TestLoopSource = TestLoopSource(targetBus: srcBus.index) + let pedal: CompressorPedal = CompressorPedal( + inputBus: srcBus.index, + outputBus: dstBus.index + ) + pedal.sampleRate = sampleRate + pedal.inputGain = 24.0 + pedal.threshold = -30.0 + pedal.ratio = 8.0 + pedal.safetyLimiter = safety + let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index) + try engine.add(source) + try engine.add(pedal, position: NodePosition.after(source)) + try engine.add(master, position: NodePosition.after(pedal)) + try engine.start() + return readStereoCAF(url: url, sampleRate: sampleRate) + } + + let off: [Float] = try render(safety: 0.0) + let on: [Float] = try render(safety: 1.0) + + #expect(hasNonSilentOutput(off), "safety-off render was silent") + #expect(hasNonSilentOutput(on), "safety-on render was silent") + + // Skip the safety limiter's look-ahead fill window before measuring the peak. + let lookAheadFillSamples: Int = 2048 + guard on.count > lookAheadFillSamples else { return } + let onWindow: ArraySlice = on[lookAheadFillSamples...] + let onPeak: Float = onWindow.reduce(0) { Swift.max($0, abs($1)) } + + // Safety ceiling is just under full scale; downstream master only attenuates. + let ceilingTolerance: Float = 0.05 + #expect(onPeak <= 1.0 + ceilingTolerance, "safety limiter failed to cap peak: \(onPeak)") + + // The two switch positions must differ. + let skip: Int = lookAheadFillSamples + let window: Int = 2048 + guard off.count >= skip + window, on.count >= skip + window else { return } + let offWindow: [Float] = Array(off[skip..<(skip + window)]) + let onWindow2: [Float] = Array(on[skip..<(skip + window)]) + let ssd: Double = zip(offWindow, onWindow2).reduce(0.0) { acc, pair in + let diff: Double = Double(pair.0 - pair.1) + return acc + diff * diff + } + #expect(ssd > 0, "safety switch had no effect on output") + } + @Test func inputGainChangesOutput() throws { // The pre-compression input gain stage must alter the output: boosting the input // drives more of the signal past the threshold, changing the compressed result. diff --git a/Tests/LyrebirdShoeGazeTests/LimiterPedalTests.swift b/Tests/LyrebirdShoeGazeTests/LimiterPedalTests.swift index 4f73468..81fedab 100644 --- a/Tests/LyrebirdShoeGazeTests/LimiterPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/LimiterPedalTests.swift @@ -18,6 +18,7 @@ struct LimiterPedalTests { private func render( boost: Double, ceiling: Double, + release: Double = 0.05, sampleRate: Double = 44100.0 ) throws -> [Float] { let renderDuration: Double = 0.1 @@ -43,6 +44,7 @@ struct LimiterPedalTests { pedal.sampleRate = sampleRate pedal.boost = boost pedal.ceiling = ceiling + pedal.release = release let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index) try engine.add(source) @@ -78,4 +80,32 @@ struct LimiterPedalTests { let tolerance: Float = 0.05 #expect(peak <= Float(ceiling) + tolerance, "Limiter overshot ceiling: peak=\(peak)") } + + @Test func exposesBoostCeilingReleaseKnobs() throws { + let pedal: LimiterPedal = LimiterPedal() + let labels: [String] = pedal.effectKnobs.map { $0.label } + #expect(labels == ["Boost", "Ceiling", "Release"]) + } + + @Test func releaseChangesOutput() throws { + // Same drive and ceiling, different release times: the gain recovers between peaks + // at different rates, so the limited output differs. Ceiling still holds for both. + let ceiling: Double = 0.4 + let fast: [Float] = try render(boost: 12.0, ceiling: ceiling, release: 0.01) + let slow: [Float] = try render(boost: 12.0, ceiling: ceiling, release: 0.5) + + #expect(hasNonSilentOutput(fast), "fast-release render was silent") + #expect(hasNonSilentOutput(slow), "slow-release render was silent") + + let skip: Int = 2048 + let window: Int = 2048 + guard fast.count >= skip + window, slow.count >= skip + window else { return } + let fastWindow: [Float] = Array(fast[skip..<(skip + window)]) + let slowWindow: [Float] = Array(slow[skip..<(skip + window)]) + let ssd: Double = zip(fastWindow, slowWindow).reduce(0.0) { acc, pair in + let diff: Double = Double(pair.0 - pair.1) + return acc + diff * diff + } + #expect(ssd > 0, "release time had no effect on limiter output") + } }