From 3fe6b8a50af76b86bfdbd4ebb87f736d5923265b Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Sun, 2 Aug 2026 20:27:39 -0700 Subject: [PATCH] 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 --- .../Cabinet/CabinetVoice.swift | 8 +-- Sources/LyrebirdShoeGaze/Pedal.swift | 8 +++ .../Pedals/AmpVoicePedal.swift | 2 +- .../Pedals/CabinetPedal.swift | 3 +- .../Presets/ShoeGaze+Preset.swift | 45 ++++++++++++---- Tests/LyrebirdShoeGazeTests/PresetTests.swift | 54 +++++++++++++++++++ 6 files changed, 106 insertions(+), 14 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/Cabinet/CabinetVoice.swift b/Sources/LyrebirdShoeGaze/Cabinet/CabinetVoice.swift index 64ad820..b18c691 100644 --- a/Sources/LyrebirdShoeGaze/Cabinet/CabinetVoice.swift +++ b/Sources/LyrebirdShoeGaze/Cabinet/CabinetVoice.swift @@ -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. diff --git a/Sources/LyrebirdShoeGaze/Pedal.swift b/Sources/LyrebirdShoeGaze/Pedal.swift index a50b9f1..29c24e9 100644 --- a/Sources/LyrebirdShoeGaze/Pedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedal.swift @@ -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 / diff --git a/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift b/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift index a72cea3..8a78a3e 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/AmpVoicePedal.swift @@ -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. /// diff --git a/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift index bdac1e7..69e21a2 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/CabinetPedal.swift @@ -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 diff --git a/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Preset.swift b/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Preset.swift index 97cc5dc..981ca60 100644 --- a/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Preset.swift +++ b/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Preset.swift @@ -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 } } diff --git a/Tests/LyrebirdShoeGazeTests/PresetTests.swift b/Tests/LyrebirdShoeGazeTests/PresetTests.swift index 9cd2dce..37bda3e 100644 --- a/Tests/LyrebirdShoeGazeTests/PresetTests.swift +++ b/Tests/LyrebirdShoeGazeTests/PresetTests.swift @@ -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 {