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 {