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:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user