From e88db0468664c5e449950fff2116d6204090da04 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Mon, 3 Aug 2026 13:10:23 -0700 Subject: [PATCH] fix(amps): recall rebuilds on a rebuildsOnChange switch (channel) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review blocker: lightweight recall (ShoeGaze.applyState) detected topology change only via Pedal.rebuildSelectorValues (effectSelectors), so a channel switch — the first rebuildsOnChange *switch* — flipped during a scene recall wrote the node value but never rebuilt, leaving the stale channel topology (wrong preamp). - Rename/extend to Pedal.rebuildTopologyValues: folds in effectSwitches flagged rebuildsOnChange alongside the selectors (both NaN-sanitized). Recall call sites in ShoeGaze+Preset updated. - FullertonDrive: rename channel constants to generic switchOff/On (used by the Bright switch too). - Test: applyStateRebuildsBoardWhenChannelSwitchChanges — recall flipping the channel rebuilds the board and applies the recalled channel. Co-Authored-By: Claude Opus 4.8 --- Sources/LyrebirdShoeGaze/Pedal.swift | 17 +++++++---- .../Pedals/FullertonDriveAmpPedal.swift | 12 ++++---- .../Presets/ShoeGaze+Preset.swift | 8 ++--- Tests/LyrebirdShoeGazeTests/PresetTests.swift | 30 +++++++++++++++++++ 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/Sources/LyrebirdShoeGaze/Pedal.swift b/Sources/LyrebirdShoeGaze/Pedal.swift index b060e53..aa7f074 100644 --- a/Sources/LyrebirdShoeGaze/Pedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedal.swift @@ -237,16 +237,21 @@ 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] { + /// Current values of every ``effectSelectors`` and ``effectSwitches`` entry flagged + /// `rebuildsOnChange` — the controls whose choice changes the graph topology (e.g. a cabinet + /// voice or a clean/drive channel). 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 controls. + public var rebuildTopologyValues: [Double] { // Sanitize non-finite to a fixed sentinel: the before/after compare in a recall uses `!=`, // and `NaN != NaN` would force a spurious rebuild on every recall. - effectSelectors + let selectorValues: [Double] = effectSelectors .filter { $0.rebuildsOnChange } .map { $0.node.value.isFinite ? $0.node.value : 0.0 } + let switchValues: [Double] = effectSwitches + .filter { $0.rebuildsOnChange } + .map { $0.node.value.isFinite ? $0.node.value : 0.0 } + return selectorValues + switchValues } /// Effect-specific transport controls, rendered as a row directly below the knobs — above diff --git a/Sources/LyrebirdShoeGaze/Pedals/FullertonDriveAmpPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/FullertonDriveAmpPedal.swift index 6fc39d4..0e3bda4 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/FullertonDriveAmpPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/FullertonDriveAmpPedal.swift @@ -36,8 +36,8 @@ public final class FullertonDriveAmpPedal: AmpVoicePedal { private let reverbRange: ClosedRange = 0.0 ... 1.0 private let driveRange: ClosedRange = 1.0 ... 30.0 - private let cleanValue: Double = 0.0 - private let driveValue: Double = 1.0 + private let switchOffValue: Double = 0.0 + private let switchOnValue: Double = 1.0 public override var toneStackModel: ToneStackModel { .fender } public override var summaryText: String { @@ -59,16 +59,16 @@ public final class FullertonDriveAmpPedal: AmpVoicePedal { label: "Bright", node: $bright, options: [ - PedalSwitch.Option(title: "Off", value: cleanValue), - PedalSwitch.Option(title: "On", value: driveValue), + PedalSwitch.Option(title: "Off", value: switchOffValue), + PedalSwitch.Option(title: "On", value: switchOnValue), ] ), PedalSwitch( label: "Channel", node: $channel, options: [ - PedalSwitch.Option(title: "Clean", value: cleanValue), - PedalSwitch.Option(title: "Drive", value: driveValue), + PedalSwitch.Option(title: "Clean", value: switchOffValue), + PedalSwitch.Option(title: "Drive", value: switchOnValue), ], rebuildsOnChange: true ), diff --git a/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Preset.swift b/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Preset.swift index 95965ea..0a9f292 100644 --- a/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Preset.swift +++ b/Sources/LyrebirdShoeGaze/Presets/ShoeGaze+Preset.swift @@ -391,9 +391,9 @@ extension ShoeGaze { switch (segment, segmentState) { case let (.pedal(pedal), .pedal(pedalState)): if pedal.displayName == pedalState.type { - let before: [Double] = pedal.rebuildSelectorValues + let before: [Double] = pedal.rebuildTopologyValues pedal.applyState(pedalState) - if pedal.rebuildSelectorValues != before { topologyChanged = true } + if pedal.rebuildTopologyValues != before { topologyChanged = true } } case let (.parallel(section), .parallel(parallelState)): @@ -429,9 +429,9 @@ extension ShoeGaze { 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 + let before: [Double] = pedal.rebuildTopologyValues pedal.applyState(pedalState) - if pedal.rebuildSelectorValues != before { topologyChanged = true } + if pedal.rebuildTopologyValues != before { topologyChanged = true } } return topologyChanged } diff --git a/Tests/LyrebirdShoeGazeTests/PresetTests.swift b/Tests/LyrebirdShoeGazeTests/PresetTests.swift index 37bda3e..ec5b022 100644 --- a/Tests/LyrebirdShoeGazeTests/PresetTests.swift +++ b/Tests/LyrebirdShoeGazeTests/PresetTests.swift @@ -329,6 +329,36 @@ struct PresetTests { #expect(sg.boards[0].pedals.first === original, "unchanged cabinet voice should not rebuild") } + @Test func applyStateRebuildsBoardWhenChannelSwitchChanges() throws { + // A rebuildsOnChange *switch* (an amp's Clean/Drive channel) changes graph topology, so a + // lightweight recall that flips it must rebuild the board — not just the cab selector case. + 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(FullertonDriveAmpPedal()) + try sg.finalizeBoard(board) + + guard let original: FullertonDriveAmpPedal = board.pedals.first as? FullertonDriveAmpPedal else { + Issue.record("No FullertonDriveAmpPedal in board") + return + } + // Snapshot on the Drive channel, move the live pedal back to Clean, then recall. + original.channel = 1.0 + let snapshot: ShoeGazeState = sg.captureState() + original.channel = 0.0 + + sg.applyState(snapshot) + + guard let rebuilt: FullertonDriveAmpPedal = sg.boards[0].pedals.first as? FullertonDriveAmpPedal else { + Issue.record("No FullertonDriveAmpPedal after recall") + return + } + #expect(rebuilt !== original, "board should rebuild when the channel switch changes") + #expect(rebuilt.channel == 1.0, "recalled channel should be applied") + } + // MARK: - In-memory round-trip @Test func captureStateRoundTripEquality() throws {