diff --git a/Sources/LyrebirdShoeGaze/Cabinet/CabinetVoice.swift b/Sources/LyrebirdShoeGaze/Cabinet/CabinetVoice.swift index e338cec..64ad820 100644 --- a/Sources/LyrebirdShoeGaze/Cabinet/CabinetVoice.swift +++ b/Sources/LyrebirdShoeGaze/Cabinet/CabinetVoice.swift @@ -24,6 +24,18 @@ public enum CabinetVoice: String, CaseIterable, Sendable { case fullRange case loFiBox + /// The voice at a selector value (an index into ``allCases``), rounded and clamped to the valid + /// range. Used by the cab selector to pick the single voice to instantiate on a board rebuild. + /// + /// - Parameter selectorValue: The live selector node value (nominally an integer index). + /// - Returns: The corresponding voice, clamped to `allCases`. + public static func voice(atSelectorValue selectorValue: Double) -> CabinetVoice { + let all: [CabinetVoice] = allCases + let index: Int = Int(selectorValue.rounded()) + let clamped: Int = Swift.min(Swift.max(index, 0), all.count - 1) + return all[clamped] + } + /// Human-readable name for pickers. public var displayName: String { switch self { diff --git a/Sources/LyrebirdShoeGaze/PedalSelectorKnob.swift b/Sources/LyrebirdShoeGaze/PedalSelectorKnob.swift index 76ff364..50e3684 100644 --- a/Sources/LyrebirdShoeGaze/PedalSelectorKnob.swift +++ b/Sources/LyrebirdShoeGaze/PedalSelectorKnob.swift @@ -49,20 +49,31 @@ public struct PedalSelectorKnob { /// contain at least two. public let stops: [Stop] + /// When `true`, committing a new stop asks the host to rebuild the board (as the Neural Amp + /// model loader does) instead of relying on the live graph to react to the node value. Use for + /// selectors whose choice changes the *graph topology* — e.g. a cabinet whose voice picks which + /// single convolver is instantiated — rather than a value an always-running node reads live. + /// Defaults to `false`. + public let rebuildsOnChange: Bool + /// Creates a selector-knob descriptor. /// /// - Parameters: /// - label: Short label shown beneath the knob. /// - node: The `UIParamNode` projected value of an `@UIParam` property (`pedal.$voice1`, …). /// - stops: Ordered stops, CCW → CW. At least two. + /// - rebuildsOnChange: Ask the host to rebuild the board when a new stop is committed. Use for + /// selectors that change graph topology. Defaults to `false`. public init( label: String, node: UIParamNode, - stops: [Stop] + stops: [Stop], + rebuildsOnChange: Bool = false ) { precondition(stops.count >= 2, "PedalSelectorKnob requires at least two stops") self.label = label self.node = node self.stops = stops + self.rebuildsOnChange = rebuildsOnChange } } diff --git a/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift b/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift index 81b9929..a72cea3 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift @@ -120,7 +120,8 @@ open class AmpVoicePedal: Pedal { public override var effectSelectors: [PedalSelectorKnob] { [ - PedalSelectorKnob(label: "Cab", node: $cab, stops: AmpVoicePedal.cabStops), + PedalSelectorKnob( + label: "Cab", node: $cab, stops: AmpVoicePedal.cabStops, rebuildsOnChange: true), ] } @@ -128,9 +129,10 @@ open class AmpVoicePedal: Pedal { /// Preamp → fixed tone stack → power amp → signature shaping → cabinet (voice-selected). /// - /// > Note: the cabinet is a `Select` over one `ConvolutionReverb` per `CabinetVoice`. Per - /// > `Select`'s SC-parity contract every input is evaluated each block, so all cab convolvers - /// > run continuously regardless of the `cab` selection — a bounded, fixed CPU cost. + /// > 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. /// /// - Parameter dry: Dry mono input signal. /// - Returns: The amped, cab-convolved mono output. @@ -142,13 +144,11 @@ open class AmpVoicePedal: Pedal { let powerAmp: TriodeStage = TriodeStage( input: toned, drive: powerAmpDrive, bias: powerAmpBias, oversample: 2) let shaped: any ValidGeneratorInput = shapeVoice(powerAmp) - let cabinets: [any ValidGeneratorInput] = CabinetVoice.allCases.map { (voice: CabinetVoice) in - ConvolutionReverb.Audio( - input: shaped, - ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr), - mix: cabinetMix - ) - } - return Select.Audio(which: $cab, inputs: cabinets) + let voice: CabinetVoice = CabinetVoice.voice(atSelectorValue: cab) + return ConvolutionReverb.Audio( + input: shaped, + ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr), + mix: cabinetMix + ) } } diff --git a/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift index ba3ba71..bdac1e7 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift @@ -10,14 +10,15 @@ import LyrebirdUI /// Speaker-cabinet simulation via impulse-response convolution. /// /// Convolves the input against a cabinet impulse response using Lyrebird's partitioned FFT -/// convolver (`ConvolutionReverb`). One convolver per built-in ``CabinetVoice`` is built at -/// `build()` time (each voicing's IR is synthesized in code — our own MIT content, see -/// ``CabinetIRFactory``); a `Select` driven by the ``cabinet`` knob picks the active one, so -/// switching cabs is live and round-trips through the rig save format like every other pedal. +/// convolver (`ConvolutionReverb`). A single convolver is built at `build()` time for the voice the +/// ``cabinet`` selector currently names (each voicing's IR is synthesized in code — our own MIT +/// content, see ``CabinetIRFactory``). Changing the selector asks the host to rebuild the board so +/// only the selected voice's convolver ever runs — one partitioned-FFT convolver, not one per +/// voice. The selection round-trips through the rig save format like every other pedal. /// /// Signal path (inside `effect(_:)`): /// ``` -/// dry → [ConvolutionReverb(ir: voice0), …] → Select(which: cabinet) → mono out +/// dry → ConvolutionReverb(ir: selectedVoice) → mono out /// ``` /// /// The wet/dry mix and bypass are handled by the `Pedal` base class. @@ -82,31 +83,30 @@ public final class CabinetPedal: Pedal { public override var effectSelectors: [PedalSelectorKnob] { [ - PedalSelectorKnob(label: "Cab", node: $cabinet, stops: CabinetPedal.voiceStops), + PedalSelectorKnob( + label: "Cab", node: $cabinet, stops: CabinetPedal.voiceStops, rebuildsOnChange: true), ] } // MARK: - Effect - /// Convolves the dry signal against every built-in cabinet IR and selects the active one. + /// Convolves the dry signal against the single selected cabinet IR. /// - /// All voices' convolvers are built at `build()` time; `Select` picks per the live `cabinet` - /// index each block. Every convolver keeps running (SC `Select` semantics), so their internal - /// state stays in sync across a switch — but the output changes on the switch sample, so a - /// hard cab change can click; cabs are normally set-and-forget, and a smooth crossfade is a - /// possible later refinement. Synthesizing the short IRs happens off the RT thread in `build()`. + /// Only the voice named by the live `cabinet` selector is instantiated, so exactly one + /// partitioned-FFT convolver runs. Changing the selector triggers a board rebuild (the selector + /// is `rebuildsOnChange`), which re-enters `effect(_:)` with the new voice — so switching cabs + /// costs a brief rebuild rather than running every voice at once. Synthesizing the short IR + /// happens off the RT thread in `build()`. /// /// - Parameter dry: Dry mono input signal. /// - Returns: The cabinet-convolved mono output. public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { let sr: Double = sampleRate - let convolvers: [any ValidGeneratorInput] = CabinetVoice.allCases.map { (voice: CabinetVoice) in - ConvolutionReverb.Audio( - input: dry, - ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr), - mix: convolutionMix - ) - } - return Select.Audio(which: $cabinet, inputs: convolvers) + let voice: CabinetVoice = CabinetVoice.voice(atSelectorValue: cabinet) + return ConvolutionReverb.Audio( + input: dry, + ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr), + mix: convolutionMix + ) } } diff --git a/Sources/LyrebirdShoeGazeUI/PedalSelectorKnobView.swift b/Sources/LyrebirdShoeGazeUI/PedalSelectorKnobView.swift index d9bfbba..33efadd 100644 --- a/Sources/LyrebirdShoeGazeUI/PedalSelectorKnobView.swift +++ b/Sources/LyrebirdShoeGazeUI/PedalSelectorKnobView.swift @@ -29,6 +29,9 @@ struct PedalSelectorKnobView: View { // MARK: - Input private let selector: PedalSelectorKnob + /// Called once when a drag ends on a different stop than it began — used to trigger a board + /// rebuild for `rebuildsOnChange` selectors. `nil` for plain live-value selectors. + private let onCommit: (() -> Void)? // MARK: - Drag state @@ -37,8 +40,9 @@ struct PedalSelectorKnobView: View { // MARK: - Init - init(_ selector: PedalSelectorKnob) { + init(_ selector: PedalSelectorKnob, onCommit: (() -> Void)? = nil) { self.selector = selector + self.onCommit = onCommit } // MARK: - Body @@ -100,7 +104,11 @@ struct PedalSelectorKnobView: View { selector.node.update(value) } } - .onEnded { _ in dragInProgress = false } + .onEnded { _ in + let changed: Bool = currentIndex != dragStartIndex + dragInProgress = false + if changed { onCommit?() } + } } // MARK: - Helpers diff --git a/Sources/LyrebirdShoeGazeUI/PedalView.swift b/Sources/LyrebirdShoeGazeUI/PedalView.swift index 27320b4..1d1c690 100644 --- a/Sources/LyrebirdShoeGazeUI/PedalView.swift +++ b/Sources/LyrebirdShoeGazeUI/PedalView.swift @@ -777,7 +777,12 @@ public struct PedalView: View { HStack(alignment: .top, spacing: PedalView.knobRowItemSpacing) { Spacer(minLength: 0) ForEach(Array(selectors.enumerated()), id: \.offset) { pair in - PedalSelectorKnobView(pair.element) + PedalSelectorKnobView( + pair.element, + onCommit: pair.element.rebuildsOnChange + ? { [pedal, pedalRebuildAction] in pedalRebuildAction(pedal) } + : nil + ) } Spacer(minLength: 0) } diff --git a/Tests/LyrebirdShoeGazeTests/AmpVoicePedalTests.swift b/Tests/LyrebirdShoeGazeTests/AmpVoicePedalTests.swift index a2d6a86..12d940d 100644 --- a/Tests/LyrebirdShoeGazeTests/AmpVoicePedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/AmpVoicePedalTests.swift @@ -126,6 +126,8 @@ struct AmpVoicePedalTests { for voice in voices { let pedal: AmpVoicePedal = voice.make() #expect(pedal.effectSelectors.map { $0.label } == ["Cab"], "\(voice.name) selectors") + #expect(pedal.effectSelectors.first?.rebuildsOnChange == true, + "\(voice.name) cab selector should rebuild on change") #expect(pedal.widthUnits == 2, "\(voice.name) amp head should use a double-width card") #expect(pedal.showsMixControl == false, "\(voice.name) should be fully wet") } diff --git a/Tests/LyrebirdShoeGazeTests/CabinetPedalTests.swift b/Tests/LyrebirdShoeGazeTests/CabinetPedalTests.swift index 807b046..e4ebb02 100644 --- a/Tests/LyrebirdShoeGazeTests/CabinetPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/CabinetPedalTests.swift @@ -68,6 +68,21 @@ struct CabinetPedalTests { #expect(stops == CabinetVoice.allCases.map { $0.displayName }) } + @Test func cabinetSelectorRebuildsOnChange() throws { + // Only the selected voice's convolver is built, so changing it must rebuild the board. + let pedal: CabinetPedal = CabinetPedal() + #expect(pedal.effectSelectors.first?.rebuildsOnChange == true) + } + + @Test func voiceAtSelectorValueClampsAndRounds() throws { + let all: [CabinetVoice] = CabinetVoice.allCases + #expect(CabinetVoice.voice(atSelectorValue: 0.0) == all[0]) + #expect(CabinetVoice.voice(atSelectorValue: 2.0) == all[2]) + #expect(CabinetVoice.voice(atSelectorValue: 1.4) == all[1], "should round to nearest stop") + #expect(CabinetVoice.voice(atSelectorValue: -5.0) == all[0], "clamp below 0") + #expect(CabinetVoice.voice(atSelectorValue: 999.0) == all[all.count - 1], "clamp above max") + } + @Test func cabinetSelectionRoundTripsThroughState() throws { // The selector value must survive capture/apply (preset round-trip). let pedal: CabinetPedal = CabinetPedal()