From 9daffbbdf3502631de3511f0518cccfe3704ed62 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Mon, 3 Aug 2026 21:34:12 -0700 Subject: [PATCH] =?UTF-8?q?feat(amp):=20decouple=20cabinet=20=E2=80=94=20"?= =?UTF-8?q?None=20(direct)"=20stop=20+=20cabinet=20as=20separate=20block?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase 12. Split AmpVoicePedal.cabinetAndLimit into cabinet(_:) (the ConvolutionReverb, or a straight pass) + outputLimit(_:) (tanh soft-limit → Clip(±1)). The output limit ALWAYS runs on the amp, so the per-amp ±1 guarantee survives cabinet removal. - Cab selector gains a "None (direct)" stop (value = CabinetVoice.allCases .count, appended last so existing preset cab indices stay stable). isCabinetBypassed skips the convolver at that stop. - With Cab = None the amp emits its limited, cab-less power-amp signal; the player drops the existing standalone Cabinet pedal (shared CabinetVoice / CabinetIRFactory, no DSP dup) after it as a real chain block. Default stays cab-on (backward compatible). Tests: None bypasses the convolver, None still limits to ±1 (limiter survived), the None stop is exposed, and an amp(None) → standalone Cabinet chain renders non-silent/finite. Note: a downstream cab can overshoot ±1 like any post-amp pedal — end-of-chain level is the board's stage, not the amp's. Co-Authored-By: Claude Opus 4.8 --- .../Pedals/AmpVoicePedal.swift | 81 ++++++++++++++----- .../AmpVoicePedalTests.swift | 67 +++++++++++++++ 2 files changed, 127 insertions(+), 21 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift b/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift index 4c1ac07..80b33fe 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift @@ -19,8 +19,9 @@ import LyrebirdUI /// → onboardEffects(_:) (subclass hook — e.g. reverb + tremolo; default identity) /// → TriodeStage(power-amp drive) (power-amp warmth) /// → shapeVoice(_:) (subclass signature filter) -/// → ConvolutionReverb(cab IR) (speaker cabinet — voice-selectable, rebuild on change) -/// → TriodeStage(bias: 0) → Clip(±1) (output soft-limit + hard safety ceiling) +/// → ConvolutionReverb(cab IR) (speaker cabinet — voice-selectable, or None for a +/// separate cab block; rebuild on change) +/// → TriodeStage(bias: 0) → Clip(±1) (output soft-limit + hard safety ceiling — always on) /// ``` /// Fully wet (the amp replaces the dry signal); `Level` is the master. Double-width amp-head card. /// @@ -234,11 +235,16 @@ open class AmpVoicePedal: Pedal { /// Range for a signature control that maps [0, 1] (Bright / Presence / Cut amount). let signatureRange: ClosedRange = 0.0 ... 1.0 + /// Cab selector value for the "None (direct)" stop — no speaker convolution. Placed one past the + /// last `CabinetVoice` index so every existing voice keeps its saved index (append-only). + static let cabinetBypassValue: Double = Double(CabinetVoice.allCases.count) + private static let cabStops: [PedalSelectorKnob.Stop] = CabinetVoice.allCases .enumerated() .map { (index: Int, voice: CabinetVoice) in PedalSelectorKnob.Stop(title: voice.displayName, value: Double(index)) } + + [PedalSelectorKnob.Stop(title: "None (direct)", value: cabinetBypassValue)] // MARK: - Chrome @@ -301,21 +307,19 @@ open class AmpVoicePedal: Pedal { // MARK: - Effect - /// Preamp → fixed tone stack → power amp → signature shaping → cabinet → output soft-limit. + /// Preamp → fixed tone stack → power amp → signature shaping → cabinet (or None) → output + /// soft-limit. /// - /// > Note: only the voice named by the live `cab` selector is instantiated, so exactly one - /// > `ConvolutionReverb` runs. The `cab` selector is `rebuildsOnChange`, so switching cabs - /// > rebuilds the board (re-entering `effect(_:)` with the new voice) rather than running every - /// > voice at once. + /// > Note: at most one `ConvolutionReverb` runs — only the voice named by the live `cab` selector + /// > is instantiated, and the "None (direct)" stop instantiates none. The `cab` selector is + /// > `rebuildsOnChange`, so switching cabs (or to/from None) rebuilds the board rather than + /// > running every voice at once. /// - /// The closing `TriodeStage(bias: 0)` is a symmetric `tanh` output limiter: the cab convolution - /// and signature shelves can push peaks past ±1, which a real analog output would soft-compress - /// rather than hard-clip. Unit small-signal gain keeps clean tones intact; peaks soft-limit - /// toward ±1. The trailing `Clip(±1)` is a hard safety clamp for the sub-percent overshoot the - /// `tanh`'s DC-blocker and the halfband filters can leave — so no sample can clip the DAC. + /// The output soft-limit (``outputLimit(_:)``) always runs, cabinet or not, so the ±1 ceiling + /// holds even when the cabinet is bypassed for a separate downstream cab block. /// /// - Parameter dry: Dry mono input signal. - /// - Returns: The amped, cab-convolved, soft-limited, ceiling-clamped mono output. + /// - Returns: The amped, (optionally) cab-convolved, soft-limited, ceiling-clamped mono output. public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { cabinetAndLimit(preCabinetCore(dry)) } @@ -337,27 +341,62 @@ open class AmpVoicePedal: Pedal { return applySag(shapeVoice(powerAmp)) } - /// The per-channel cabinet + output stage: the selected `CabinetVoice` convolver → `tanh` - /// soft-limit → `Clip(±1)` safety clamp. Applied once in mono, or once per channel by a - /// widener amp. + /// True when the `Cab` selector is on the "None (direct)" stop — the amp emits its limited + /// power-amp signal with **no** speaker convolution, so the player can place a separate cabinet + /// block (the standalone `Cabinet` pedal, or a future IR loader) downstream. Read at board-build + /// time; the selector is `rebuildsOnChange`, so toggling it rebuilds the graph. + /// + /// > Warning: with no cabinet, the amp sounds raw and fizzy (the power-amp `tanh` with no speaker + /// > low-pass) — expected; a cabinet is what tames the top end. + var isCabinetBypassed: Bool { + guard cab.isFinite else { return false } + return Int(cab.rounded()) >= CabinetVoice.allCases.count + } + + /// The per-channel cabinet + output stage: cabinet convolver (or a straight pass at the "None" + /// stop) → `tanh` soft-limit → `Clip(±1)` safety clamp. Applied once in mono, or once per channel + /// by a widener amp. /// /// - Parameter signal: The post-core signal for one channel. - /// - Returns: The cab-convolved, soft-limited, ceiling-clamped signal. + /// - Returns: The (optionally) cab-convolved, soft-limited, ceiling-clamped signal. func cabinetAndLimit(_ signal: any ValidGeneratorInput) -> any ValidGeneratorInput { + outputLimit(cabinet(signal)) + } + + /// The selected `CabinetVoice` speaker convolver, or `signal` unchanged when the `Cab` selector + /// is on "None (direct)". Only the voice named by the live `cab` selector is instantiated, so at + /// most one `ConvolutionReverb` runs. + /// + /// - Parameter signal: The post-core signal for one channel. + /// - Returns: The cab-convolved signal, or `signal` unchanged when the cabinet is bypassed. + func cabinet(_ signal: any ValidGeneratorInput) -> any ValidGeneratorInput { + guard !isCabinetBypassed else { return signal } let voice: CabinetVoice = CabinetVoice.voice(atSelectorValue: cab) - let cabinet: ConvolutionReverb.Audio = ConvolutionReverb.Audio( + return ConvolutionReverb.Audio( input: signal, ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sampleRate), mix: cabinetMix ) + } + + /// The output soft-limit + hard `Clip(±1)` safety ceiling — **always** applied on the amp, + /// cabinet or not, so the ±1 guarantee holds even at the "None (direct)" stop. + /// + /// The `TriodeStage(bias: 0)` is a symmetric `tanh` output limiter: cab convolution and signature + /// shelves can push peaks past ±1, which a real analog output soft-compresses rather than + /// hard-clips. Unit small-signal gain keeps clean tones intact; peaks soft-limit toward ±1. The + /// trailing `Clip(±1)` hard-clamps the sub-percent overshoot the `tanh`'s DC-blocker and the + /// halfband filters can leave, so no sample can clip the DAC. + /// + /// - Parameter signal: The (optionally cab-convolved) signal for one channel. + /// - Returns: The soft-limited, ceiling-clamped signal. + func outputLimit(_ signal: any ValidGeneratorInput) -> any ValidGeneratorInput { let softLimited: TriodeStage = TriodeStage( - input: cabinet, + input: signal, drive: outputStageDrive, bias: outputStageBias, oversample: outputStageOversample ) - // Hard safety clamp: the tanh keeps the signal near ±1, but its DC-blocker and the halfband - // filters can leave a sub-percent transient overshoot — clamp it so no sample can clip the DAC. return Clip.Audio(softLimited, lo: -outputCeiling, hi: outputCeiling) } } diff --git a/Tests/LyrebirdShoeGazeTests/AmpVoicePedalTests.swift b/Tests/LyrebirdShoeGazeTests/AmpVoicePedalTests.swift index ec11144..15ea966 100644 --- a/Tests/LyrebirdShoeGazeTests/AmpVoicePedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/AmpVoicePedalTests.swift @@ -259,6 +259,73 @@ struct AmpVoicePedalTests { #expect(restored.sag == 0.7 && restored.bias == 0.2, "Sag/Bias did not round-trip through state") } + // MARK: - Cabinet decoupling + + @Test func cabNoneBypassesConvolution() throws { + for voice in voices { + let withCab: [Float] = try render(voice.make) { $0.cab = 0.0 } + let none: [Float] = try render(voice.make) { $0.cab = AmpVoicePedal.cabinetBypassValue } + #expect(hasNonSilentOutput(none) && none.allSatisfy { $0.isFinite }, + "\(voice.name) None-cab output silent or non-finite") + #expect(windowedSSD(withCab, none) > 0, "\(voice.name) None did not bypass the cabinet") + } + } + + @Test func cabNoneStillLimitsToCeiling() throws { + // With the cabinet bypassed the output soft-limit must still run — the ±1 ceiling has to hold. + let ceilingEpsilon: Double = 1.0e-4 + for voice in voices { + let hot: [Float] = try render(voice.make) { (pedal: AmpVoicePedal) in + pedal.gain = 30.0; pedal.bass = 1.0; pedal.treble = 1.0 + pedal.cab = AmpVoicePedal.cabinetBypassValue + } + let peak: Double = hot.reduce(0.0) { Swift.max($0, abs(Double($1))) } + #expect(peak <= 1.0 + ceilingEpsilon, + "\(voice.name) None-cab peak \(peak) exceeds ±1 (output limiter lost)") + } + } + + @Test func cabSelectorExposesNoneStop() throws { + let stops: [PedalSelectorKnob.Stop] = CaliforniaAmpPedal().effectSelectors + .first { $0.label == "Cab" }?.stops ?? [] + #expect(stops.last?.title == "None (direct)", "Cab selector missing the None stop") + #expect(stops.last?.value == AmpVoicePedal.cabinetBypassValue, "None stop has the wrong value") + } + + @Test func ampWithNoneCabIntoStandaloneCabinetWorks() throws { + // Decoupled chain: amp (Cab = None) → standalone Cabinet pedal. The amp emits its limited, + // cab-less signal; the separate Cabinet block does the speaker convolution downstream. + let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( + duration: 0.2, sampleRate: sampleRate, numAudioBuses: 64) + defer { try? FileManager.default.removeItem(at: url) } + let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) + let midBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) + let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) + defer { + engine.busAllocator.releaseAudioBus(srcBus) + engine.busAllocator.releaseAudioBus(midBus) + engine.busAllocator.releaseAudioBus(dstBus) + } + let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index) + let amp: CaliforniaAmpPedal = CaliforniaAmpPedal(inputBus: srcBus.index, outputBus: midBus.index) + amp.sampleRate = sampleRate + amp.cab = AmpVoicePedal.cabinetBypassValue + let cabinet: CabinetPedal = CabinetPedal(inputBus: midBus.index, outputBus: dstBus.index) + cabinet.sampleRate = sampleRate + let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index) + try engine.add(source) + try engine.add(amp, position: NodePosition.after(source)) + try engine.add(cabinet, position: NodePosition.after(amp)) + try engine.add(master, position: NodePosition.after(cabinet)) + try engine.start() + let out: [Float] = readStereoCAF(url: url, sampleRate: sampleRate) + #expect(hasNonSilentOutput(out) && out.allSatisfy { $0.isFinite }, "decoupled amp+cabinet silent/non-finite") + // Note: the amp guarantees its OWN output ≤ ±1 (see `cabNoneStillLimitsToCeiling`), but a + // downstream standalone Cabinet convolves that signal and — like any post-amp pedal — may + // overshoot ±1. The board's final output stage, not the amp, owns end-of-chain level. So this + // asserts the decoupled chain works (non-silent, finite), not a ±1 ceiling it never promised. + } + // MARK: - Chrome / config @Test func exposesControls() throws {