fix(cab): rebuild board on lightweight recall of a topology selector

Review follow-up to the single-convolver cab change. Now that only the selected
cabinet voice's convolver is built, any path that changes the selector value
without rebuilding leaves audio on the stale convolver. The UI drag already
rebuilds; the gap was ShoeGaze.applyState (lightweight scene recall), which
applies param snapshots to live pedals without a rebuild.

- Pedal.rebuildSelectorValues: values of effectSelectors flagged rebuildsOnChange.
- applyState/applySegmentParams/applyBranchParams: detect when a rebuildsOnChange
  selector value actually changes during recall and rebuild only those boards;
  boards without such a selector keep the cheap live-apply path.
- CabinetVoice.voice(atSelectorValue:): guard non-finite and clamp in Double
  space so Int() can never trap on a corrupt preset value.
- Doc fixes: CabinetPedal.cabinet and AmpVoicePedal chain diagram no longer claim
  a live Select.

MIDI CC is unaffected: selectors are not in ccMappableControls (knobs + sliders
only). Tests: recall changing the cab voice rebuilds and applies; recall with an
unchanged voice does not rebuild.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-08-02 20:27:39 -07:00
parent 59370ee5dd
commit 3fe6b8a50a
6 changed files with 106 additions and 14 deletions
@@ -31,9 +31,11 @@ public enum CabinetVoice: String, CaseIterable, Sendable {
/// - 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]
guard selectorValue.isFinite else { return all[0] }
// Clamp in Double space first so a huge/NaN-adjacent value can never trap `Int(_:)`.
let bounded: Double = Swift.min(Swift.max(selectorValue, 0.0), Double(all.count - 1))
let index: Int = Int(bounded.rounded())
return all[index]
}
/// Human-readable name for pickers.
+8
View File
@@ -237,6 +237,14 @@ open class Pedal: SynthesizerBase {
[]
}
/// Current values of every ``effectSelectors`` entry flagged `rebuildsOnChange` — the selectors
/// whose choice changes the graph topology (e.g. a cabinet voice). A lightweight recall compares
/// this before/after applying a snapshot to decide whether the board must be rebuilt (a plain
/// node write does not re-enter `effect(_:)`). Empty for pedals with no such selectors.
public var rebuildSelectorValues: [Double] {
effectSelectors.filter { $0.rebuildsOnChange }.map { $0.node.value }
}
/// Effect-specific transport controls, rendered as a row directly below the knobs — above
/// ``effectButtons``. Use for the persistent state toggles and transport stops that read as
/// a group distinct from the momentary "do it now" actions (e.g. a looper's Loop / Stop /
@@ -16,7 +16,7 @@ import LyrebirdUI
/// → ToneStack(bass, mid, treble, model) (fixed per amp)
/// → TriodeStage(power-amp drive) (power-amp warmth)
/// → shapeVoice(_:) (subclass signature filter)
/// → ConvolutionReverb(cab IR) (speaker cabinet — live-selectable voice)
/// → ConvolutionReverb(cab IR) (speaker cabinet — voice-selectable, rebuild on change)
/// ```
/// Fully wet (the amp replaces the dry signal); `Level` is the master. Double-width amp-head card.
///
@@ -42,7 +42,8 @@ public final class CabinetPedal: Pedal {
// MARK: - Parameters
/// Selected cabinet voicing as an index into ``CabinetVoice/allCases``. Snaps to integer
/// stops via the ``cabinet`` selector; read live by the `Select` in `effect(_:)`.
/// stops via the ``cabinet`` selector; read at board-build time by `effect(_:)`, which
/// instantiates only that voice's convolver (the selector is `rebuildsOnChange`).
@UIParam public var cabinet: Double = 0.0
// MARK: - Constants
@@ -350,7 +350,17 @@ extension ShoeGaze {
/// - Parameter state: The `ShoeGazeState` whose parameter values should be applied.
public func applyState(_ state: ShoeGazeState) {
for (board, boardState) in zip(boards, state.boards) {
applySegmentParams(boardState.segments, to: board.orderedSegments)
let topologyChanged: Bool = applySegmentParams(boardState.segments, to: board.orderedSegments)
// A `rebuildsOnChange` selector (e.g. a cabinet voice) picks which nodes exist in the
// graph, so a plain node write during recall is invisible until the board is rebuilt.
// Rebuild only the boards where such a selector actually changed value.
if topologyChanged {
do {
_ = try rebuildBoard(board, from: board.captureState())
} catch {
print("[ShoeGaze] board rebuild after recall failed: \(error)")
}
}
}
// A recall restores each pedal's tempo-sync state but not its derived values (no clock is
// consulted in `Pedal.applyState`), so recompute them against the rig's current tempo —
@@ -363,30 +373,39 @@ extension ShoeGaze {
/// Positionally applies segment parameter snapshots to live board segments, including
/// branch pedals and split routing modes. Type or topology mismatches at any position are
/// skipped silently (lightweight recall is for topologically identical rigs).
/// - Returns: `true` if any pedal's `rebuildsOnChange` selector value changed, meaning the board
/// must be rebuilt for the new selection to take effect.
@discardableResult
private func applySegmentParams(
_ states: [BoardSegmentState],
to segments: [BoardSegment]
) {
) -> Bool {
var topologyChanged: Bool = false
for (segment, segmentState) in zip(segments, states) {
switch (segment, segmentState) {
case let (.pedal(pedal), .pedal(pedalState)):
if pedal.displayName == pedalState.type {
let before: [Double] = pedal.rebuildSelectorValues
pedal.applyState(pedalState)
if pedal.rebuildSelectorValues != before { topologyChanged = true }
}
case let (.parallel(section), .parallel(parallelState)):
applyBranchParams(parallelState.branchA, to: section.branchA.pedals)
applyBranchParams(parallelState.branchB, to: section.branchB.pedals)
let a: Bool = applyBranchParams(parallelState.branchA, to: section.branchA.pedals)
let b: Bool = applyBranchParams(parallelState.branchB, to: section.branchB.pedals)
if a || b { topologyChanged = true }
section.splitter.mode = parallelState.mode
case let (.stereoSplit(section), .stereoSplit(stereoState)):
applyBranchParams(stereoState.left, to: section.left.pedals)
applyBranchParams(stereoState.right, to: section.right.pedals)
let l: Bool = applyBranchParams(stereoState.left, to: section.left.pedals)
let r: Bool = applyBranchParams(stereoState.right, to: section.right.pedals)
if l || r { topologyChanged = true }
section.route = stereoState.route
case let (.stereoUnzip(section), .stereoUnzip(unzipState)):
applyBranchParams(unzipState.left, to: section.left.pedals)
applyBranchParams(unzipState.right, to: section.right.pedals)
let l: Bool = applyBranchParams(unzipState.left, to: section.left.pedals)
let r: Bool = applyBranchParams(unzipState.right, to: section.right.pedals)
if l || r { topologyChanged = true }
section.route = unzipState.route
default:
@@ -394,12 +413,20 @@ extension ShoeGaze {
continue
}
}
return topologyChanged
}
/// Positionally applies branch pedal snapshots to live branch pedals, matching by type.
private func applyBranchParams(_ states: [PedalState], to pedals: [Pedal]) {
///
/// - Returns: `true` if any branch pedal's `rebuildsOnChange` selector value changed.
@discardableResult
private func applyBranchParams(_ states: [PedalState], to pedals: [Pedal]) -> Bool {
var topologyChanged: Bool = false
for (pedal, pedalState) in zip(pedals, states) where pedal.displayName == pedalState.type {
let before: [Double] = pedal.rebuildSelectorValues
pedal.applyState(pedalState)
if pedal.rebuildSelectorValues != before { topologyChanged = true }
}
return topologyChanged
}
}
@@ -275,6 +275,60 @@ struct PresetTests {
#expect(section.route == .stereo, "Route should be recalled")
}
@Test func applyStateRebuildsBoardWhenCabinetVoiceChanges() throws {
// A cabinet voice is a `rebuildsOnChange` selector: only the selected voice's convolver is
// built, so a lightweight recall that changes it must rebuild the board (a plain node write
// would leave audio on the previously-built convolver).
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine)
let board: PedalBoard = try sg.addBoard()
try board.add(CabinetPedal())
try sg.finalizeBoard(board)
guard let original: CabinetPedal = board.pedals.first as? CabinetPedal else {
Issue.record("No CabinetPedal in board")
return
}
// Snapshot at voice 3, then move the live pedal to voice 0 and recall the snapshot.
original.cabinet = 3.0
let snapshot: ShoeGazeState = sg.captureState()
original.cabinet = 0.0
sg.applyState(snapshot)
// The rebuild replaces the instance — the recalled voice must be reflected on the new one.
guard let rebuilt: CabinetPedal = sg.boards[0].pedals.first as? CabinetPedal else {
Issue.record("No CabinetPedal after recall")
return
}
#expect(rebuilt !== original, "board should rebuild when the cabinet voice changes")
#expect(rebuilt.cabinet == 3.0, "recalled cabinet voice should be applied")
}
@Test func applyStateDoesNotRebuildWhenCabinetVoiceUnchanged() throws {
// Recall with the same cabinet voice must NOT rebuild — the lightweight path stays cheap.
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine)
let board: PedalBoard = try sg.addBoard()
try board.add(CabinetPedal())
try sg.finalizeBoard(board)
guard let original: CabinetPedal = board.pedals.first as? CabinetPedal else {
Issue.record("No CabinetPedal in board")
return
}
original.cabinet = 2.0
let snapshot: ShoeGazeState = sg.captureState()
sg.applyState(snapshot)
#expect(sg.boards[0].pedals.first === original, "unchanged cabinet voice should not rebuild")
}
// MARK: - In-memory round-trip
@Test func captureStateRoundTripEquality() throws {