perf(cab): instantiate only the selected cabinet convolver

The cab selector was a Select over one ConvolutionReverb per CabinetVoice, and
Select evaluates every input each block — so all 8 voices' partitioned-FFT
convolvers ran continuously (7/8 discarded), per CabinetPedal and per amp voice.

Build only the selected voice's convolver. The cab selector is now
rebuildsOnChange: committing a new stop asks the host to rebuild the board
(same mechanism the Neural Amp model loader uses), which re-enters effect()
with the new voice. One convolver runs instead of N; cost moves to a brief
rebuild on cab switch (cabs are set-and-forget).

- PedalSelectorKnob: add rebuildsOnChange flag.
- PedalSelectorKnobView: fire onCommit on drag-end when the stop changed.
- PedalView: wire onCommit → pedalRebuildAction for rebuildsOnChange selectors.
- CabinetVoice.voice(atSelectorValue:): rounded/clamped index → voice.
- CabinetPedal + AmpVoicePedal: single ConvolutionReverb for the selected voice.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-08-02 20:16:42 -07:00
parent 8a266a33dc
commit 59370ee5dd
8 changed files with 89 additions and 36 deletions
@@ -24,6 +24,18 @@ public enum CabinetVoice: String, CaseIterable, Sendable {
case fullRange case fullRange
case loFiBox 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. /// Human-readable name for pickers.
public var displayName: String { public var displayName: String {
switch self { switch self {
@@ -49,20 +49,31 @@ public struct PedalSelectorKnob {
/// contain at least two. /// contain at least two.
public let stops: [Stop] 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. /// Creates a selector-knob descriptor.
/// ///
/// - Parameters: /// - Parameters:
/// - label: Short label shown beneath the knob. /// - label: Short label shown beneath the knob.
/// - node: The `UIParamNode` projected value of an `@UIParam` property (`pedal.$voice1`, …). /// - node: The `UIParamNode` projected value of an `@UIParam` property (`pedal.$voice1`, …).
/// - stops: Ordered stops, CCW → CW. At least two. /// - 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( public init(
label: String, label: String,
node: UIParamNode, node: UIParamNode,
stops: [Stop] stops: [Stop],
rebuildsOnChange: Bool = false
) { ) {
precondition(stops.count >= 2, "PedalSelectorKnob requires at least two stops") precondition(stops.count >= 2, "PedalSelectorKnob requires at least two stops")
self.label = label self.label = label
self.node = node self.node = node
self.stops = stops self.stops = stops
self.rebuildsOnChange = rebuildsOnChange
} }
} }
@@ -120,7 +120,8 @@ open class AmpVoicePedal: Pedal {
public override var effectSelectors: [PedalSelectorKnob] { 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). /// Preamp → fixed tone stack → power amp → signature shaping → cabinet (voice-selected).
/// ///
/// > Note: the cabinet is a `Select` over one `ConvolutionReverb` per `CabinetVoice`. Per /// > Note: only the voice named by the live `cab` selector is instantiated, so exactly one
/// > `Select`'s SC-parity contract every input is evaluated each block, so all cab convolvers /// > `ConvolutionReverb` runs. The `cab` selector is `rebuildsOnChange`, so switching cabs
/// > run continuously regardless of the `cab` selection — a bounded, fixed CPU cost. /// > rebuilds the board (re-entering `effect(_:)` with the new voice) rather than running every
/// > voice at once.
/// ///
/// - Parameter dry: Dry mono input signal. /// - Parameter dry: Dry mono input signal.
/// - Returns: The amped, cab-convolved mono output. /// - Returns: The amped, cab-convolved mono output.
@@ -142,13 +144,11 @@ open class AmpVoicePedal: Pedal {
let powerAmp: TriodeStage = TriodeStage( let powerAmp: TriodeStage = TriodeStage(
input: toned, drive: powerAmpDrive, bias: powerAmpBias, oversample: 2) input: toned, drive: powerAmpDrive, bias: powerAmpBias, oversample: 2)
let shaped: any ValidGeneratorInput = shapeVoice(powerAmp) let shaped: any ValidGeneratorInput = shapeVoice(powerAmp)
let cabinets: [any ValidGeneratorInput] = CabinetVoice.allCases.map { (voice: CabinetVoice) in let voice: CabinetVoice = CabinetVoice.voice(atSelectorValue: cab)
ConvolutionReverb.Audio( return ConvolutionReverb.Audio(
input: shaped, input: shaped,
ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr), ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr),
mix: cabinetMix mix: cabinetMix
) )
} }
return Select.Audio(which: $cab, inputs: cabinets)
}
} }
@@ -10,14 +10,15 @@ import LyrebirdUI
/// Speaker-cabinet simulation via impulse-response convolution. /// Speaker-cabinet simulation via impulse-response convolution.
/// ///
/// Convolves the input against a cabinet impulse response using Lyrebird's partitioned FFT /// Convolves the input against a cabinet impulse response using Lyrebird's partitioned FFT
/// convolver (`ConvolutionReverb`). One convolver per built-in ``CabinetVoice`` is built at /// convolver (`ConvolutionReverb`). A single convolver is built at `build()` time for the voice the
/// `build()` time (each voicing's IR is synthesized in code — our own MIT content, see /// ``cabinet`` selector currently names (each voicing's IR is synthesized in code — our own MIT
/// ``CabinetIRFactory``); a `Select` driven by the ``cabinet`` knob picks the active one, so /// content, see ``CabinetIRFactory``). Changing the selector asks the host to rebuild the board so
/// switching cabs is live and round-trips through the rig save format like every other pedal. /// 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(_:)`): /// 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. /// 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] { 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 // 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` /// Only the voice named by the live `cabinet` selector is instantiated, so exactly one
/// index each block. Every convolver keeps running (SC `Select` semantics), so their internal /// partitioned-FFT convolver runs. Changing the selector triggers a board rebuild (the selector
/// state stays in sync across a switch — but the output changes on the switch sample, so a /// is `rebuildsOnChange`), which re-enters `effect(_:)` with the new voice — so switching cabs
/// hard cab change can click; cabs are normally set-and-forget, and a smooth crossfade is a /// costs a brief rebuild rather than running every voice at once. Synthesizing the short IR
/// possible later refinement. Synthesizing the short IRs happens off the RT thread in `build()`. /// happens off the RT thread in `build()`.
/// ///
/// - Parameter dry: Dry mono input signal. /// - Parameter dry: Dry mono input signal.
/// - Returns: The cabinet-convolved mono output. /// - Returns: The cabinet-convolved mono output.
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
let sr: Double = sampleRate let sr: Double = sampleRate
let convolvers: [any ValidGeneratorInput] = CabinetVoice.allCases.map { (voice: CabinetVoice) in let voice: CabinetVoice = CabinetVoice.voice(atSelectorValue: cabinet)
ConvolutionReverb.Audio( return ConvolutionReverb.Audio(
input: dry, input: dry,
ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr), ir: CabinetIRFactory.makeIR(voice: voice, sampleRate: sr),
mix: convolutionMix mix: convolutionMix
) )
} }
return Select.Audio(which: $cabinet, inputs: convolvers)
}
} }
@@ -29,6 +29,9 @@ struct PedalSelectorKnobView: View {
// MARK: - Input // MARK: - Input
private let selector: PedalSelectorKnob 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 // MARK: - Drag state
@@ -37,8 +40,9 @@ struct PedalSelectorKnobView: View {
// MARK: - Init // MARK: - Init
init(_ selector: PedalSelectorKnob) { init(_ selector: PedalSelectorKnob, onCommit: (() -> Void)? = nil) {
self.selector = selector self.selector = selector
self.onCommit = onCommit
} }
// MARK: - Body // MARK: - Body
@@ -100,7 +104,11 @@ struct PedalSelectorKnobView: View {
selector.node.update(value) selector.node.update(value)
} }
} }
.onEnded { _ in dragInProgress = false } .onEnded { _ in
let changed: Bool = currentIndex != dragStartIndex
dragInProgress = false
if changed { onCommit?() }
}
} }
// MARK: - Helpers // MARK: - Helpers
+6 -1
View File
@@ -777,7 +777,12 @@ public struct PedalView: View {
HStack(alignment: .top, spacing: PedalView.knobRowItemSpacing) { HStack(alignment: .top, spacing: PedalView.knobRowItemSpacing) {
Spacer(minLength: 0) Spacer(minLength: 0)
ForEach(Array(selectors.enumerated()), id: \.offset) { pair in 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) Spacer(minLength: 0)
} }
@@ -126,6 +126,8 @@ struct AmpVoicePedalTests {
for voice in voices { for voice in voices {
let pedal: AmpVoicePedal = voice.make() let pedal: AmpVoicePedal = voice.make()
#expect(pedal.effectSelectors.map { $0.label } == ["Cab"], "\(voice.name) selectors") #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.widthUnits == 2, "\(voice.name) amp head should use a double-width card")
#expect(pedal.showsMixControl == false, "\(voice.name) should be fully wet") #expect(pedal.showsMixControl == false, "\(voice.name) should be fully wet")
} }
@@ -68,6 +68,21 @@ struct CabinetPedalTests {
#expect(stops == CabinetVoice.allCases.map { $0.displayName }) #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 { @Test func cabinetSelectionRoundTripsThroughState() throws {
// The selector value must survive capture/apply (preset round-trip). // The selector value must survive capture/apply (preset round-trip).
let pedal: CabinetPedal = CabinetPedal() let pedal: CabinetPedal = CabinetPedal()