feat(rigs): persist chain routers + add Garcia two-board split rig
Add ChainRouter persistence so routed multi-board rigs survive save/load —
previously captureState/loadRig only handled plain boards, so any A/B router
rig was silently lost on save (a latent gap this fixes for user rigs too).
- New ChainRouterState (Codable): each router's two linear branches as
[PedalState] plus splitter branch levels and merger output levels.
- ShoeGazeState gains an additive `routers` field decoded with
decodeIfPresent (nil for old rigs) — no format-version bump; routerless
rigs stay byte-identical (nil, not []).
- captureState snapshots routers; rebuildAllBoards reconstructs each via
makeChainRouter/addTo{A,B}/finalize and restores the blend.
FactoryRig gains a session-scoped builder variant (FactoryRigBuild enum) so
a rig can create its own boards/routers; makeEntries branches on it.
Ships the Garcia OBEL Clean/Effects Split: an effects/lead board (octave,
envelope filter, overdrive, phaser, delay, amp) vs a clean direct board,
selectable via the router — showcasing the multi-board capability.
Tests: full router capture -> JSON -> load round-trip with structure and
branch-level assertions plus a render check, backward-compat (no routers
key when routerless), and the Garcia rig via the factory load+render suite.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,59 @@
|
|||||||
|
// ChainRouterState.swift
|
||||||
|
// Lyrebird
|
||||||
|
//
|
||||||
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
/// Serialisable snapshot of one ``ChainRouter`` — its two linear board chains and its blend state.
|
||||||
|
///
|
||||||
|
/// A router splits one input to two independent boards and blends their outputs. Its branches are
|
||||||
|
/// built linearly (via ``ChainRouter/addToA(_:)`` / ``ChainRouter/addToB(_:)``), so each is captured
|
||||||
|
/// as a flat list of ``PedalState``. The splitter's two branch-enable levels capture the A / B / both
|
||||||
|
/// selection (and any continuous blend between them); the merger's two levels capture the output mix.
|
||||||
|
///
|
||||||
|
/// Persisted as part of ``ShoeGazeState/routers`` so a routed multi-board rig survives save / load.
|
||||||
|
public struct ChainRouterState: Codable, Equatable {
|
||||||
|
|
||||||
|
/// Pedals on board A (the first branch), in chain order.
|
||||||
|
public let boardA: [PedalState]
|
||||||
|
|
||||||
|
/// Pedals on board B (the second branch), in chain order.
|
||||||
|
public let boardB: [PedalState]
|
||||||
|
|
||||||
|
/// Splitter branch-A enable level in `[0, 1]`. `1` = A audible, `0` = A muted.
|
||||||
|
public let branchA: Double
|
||||||
|
|
||||||
|
/// Splitter branch-B enable level in `[0, 1]`. `1` = B audible, `0` = B muted.
|
||||||
|
public let branchB: Double
|
||||||
|
|
||||||
|
/// Merger board-A output level in `[0, 1]`.
|
||||||
|
public let mergerLevelA: Double
|
||||||
|
|
||||||
|
/// Merger board-B output level in `[0, 1]`.
|
||||||
|
public let mergerLevelB: Double
|
||||||
|
|
||||||
|
/// Creates a router snapshot.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - boardA: Board A's pedals in chain order.
|
||||||
|
/// - boardB: Board B's pedals in chain order.
|
||||||
|
/// - branchA: Splitter branch-A enable level `[0, 1]`.
|
||||||
|
/// - branchB: Splitter branch-B enable level `[0, 1]`.
|
||||||
|
/// - mergerLevelA: Merger board-A output level `[0, 1]`.
|
||||||
|
/// - mergerLevelB: Merger board-B output level `[0, 1]`.
|
||||||
|
public init(
|
||||||
|
boardA: [PedalState],
|
||||||
|
boardB: [PedalState],
|
||||||
|
branchA: Double,
|
||||||
|
branchB: Double,
|
||||||
|
mergerLevelA: Double,
|
||||||
|
mergerLevelB: Double
|
||||||
|
) {
|
||||||
|
self.boardA = boardA
|
||||||
|
self.boardB = boardB
|
||||||
|
self.branchA = branchA
|
||||||
|
self.branchB = branchB
|
||||||
|
self.mergerLevelA = mergerLevelA
|
||||||
|
self.mergerLevelB = mergerLevelB
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -40,13 +40,14 @@ public struct FactoryRig: Identifiable, Sendable {
|
|||||||
/// Sonic family this rig belongs to, for grouping in a picker. Presentational only.
|
/// Sonic family this rig belongs to, for grouping in a picker. Presentational only.
|
||||||
public let category: FactoryRigCategory
|
public let category: FactoryRigCategory
|
||||||
|
|
||||||
/// Builds the rig's pedal chain into `board`, an empty board freshly returned by
|
/// How this rig builds its graph at seed time.
|
||||||
/// `session.addBoard()`. Set pedal parameters via the typed `configure` closures of
|
public let build: FactoryRigBuild
|
||||||
/// ``FactoryRigLibrary``'s helpers. Do **not** call `finalizeBoard` / `captureEntry` here — the
|
|
||||||
/// seeding driver owns that lifecycle.
|
|
||||||
public let build: @MainActor (_ session: ShoeGaze, _ board: PedalBoard) throws -> Void
|
|
||||||
|
|
||||||
/// Creates a factory rig descriptor.
|
/// Creates a single-board factory rig.
|
||||||
|
///
|
||||||
|
/// The `build` closure receives a fresh, empty ``PedalBoard`` (already returned by
|
||||||
|
/// `session.addBoard()`) and appends pedals to it. Do **not** call `finalizeBoard` /
|
||||||
|
/// `captureEntry` here — the seeding driver owns that lifecycle.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - id: Stable identifier (a fixed literal per rig — see ``id``).
|
/// - id: Stable identifier (a fixed literal per rig — see ``id``).
|
||||||
@@ -65,6 +66,46 @@ public struct FactoryRig: Identifiable, Sendable {
|
|||||||
self.name = name
|
self.name = name
|
||||||
self.blurb = blurb
|
self.blurb = blurb
|
||||||
self.category = category
|
self.category = category
|
||||||
self.build = build
|
self.build = .singleBoard(build)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a session-scoped factory rig that builds its own boards and/or routers.
|
||||||
|
///
|
||||||
|
/// Use this for rigs the single-board form can't express — e.g. a two-board signal split via
|
||||||
|
/// ``ShoeGaze/makeChainRouter()``. The `sessionBuild` closure gets the whole session and owns the
|
||||||
|
/// full lifecycle for what it creates: it must call `addBoard`/`finalizeBoard` (for plain boards)
|
||||||
|
/// and/or `makeChainRouter`/`addToA`/`addToB`/`finalize` (for routers) itself. Do **not** call
|
||||||
|
/// `captureEntry` here — the seeding driver owns that.
|
||||||
|
///
|
||||||
|
/// - Parameters:
|
||||||
|
/// - id: Stable identifier (a fixed literal per rig — see ``id``).
|
||||||
|
/// - name: Rig name (picker title + saved entry name).
|
||||||
|
/// - blurb: User-facing description of the sound.
|
||||||
|
/// - category: Sonic family for picker grouping.
|
||||||
|
/// - sessionBuild: Graph-building closure run against the session at seed time.
|
||||||
|
public init(
|
||||||
|
id: UUID,
|
||||||
|
name: String,
|
||||||
|
blurb: String,
|
||||||
|
category: FactoryRigCategory,
|
||||||
|
sessionBuild: @escaping @MainActor (_ session: ShoeGaze) throws -> Void
|
||||||
|
) {
|
||||||
|
self.id = id
|
||||||
|
self.name = name
|
||||||
|
self.blurb = blurb
|
||||||
|
self.category = category
|
||||||
|
self.build = .session(sessionBuild)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// How a ``FactoryRig`` builds its graph at seed time.
|
||||||
|
public enum FactoryRigBuild: Sendable {
|
||||||
|
|
||||||
|
/// Appends pedals to a single pre-made, empty board; the seeding driver calls `addBoard` before
|
||||||
|
/// and `finalizeBoard` after.
|
||||||
|
case singleBoard(@MainActor (_ session: ShoeGaze, _ board: PedalBoard) throws -> Void)
|
||||||
|
|
||||||
|
/// Builds the whole graph against the session (boards and/or routers), owning its own
|
||||||
|
/// `addBoard`/`finalizeBoard` and `makeChainRouter`/`finalize` lifecycle.
|
||||||
|
case session(@MainActor (_ session: ShoeGaze) throws -> Void)
|
||||||
|
}
|
||||||
|
|||||||
@@ -35,6 +35,28 @@ extension PedalBoard {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension ChainRouter {
|
||||||
|
|
||||||
|
/// Constructs a concrete pedal, applies `configure`, and appends it to board A of the router.
|
||||||
|
///
|
||||||
|
/// The concrete-type `configure` closure keeps every parameter compile-checked, exactly like
|
||||||
|
/// ``PedalBoard/append(_:configure:)``. Uses ``addToA(_:)`` so ``finalize()`` ordering stays correct.
|
||||||
|
@discardableResult
|
||||||
|
func appendToA<P: Pedal>(_ pedal: P, configure: (P) -> Void) throws -> P {
|
||||||
|
configure(pedal)
|
||||||
|
try addToA(pedal)
|
||||||
|
return pedal
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructs a concrete pedal, applies `configure`, and appends it to board B of the router.
|
||||||
|
@discardableResult
|
||||||
|
func appendToB<P: Pedal>(_ pedal: P, configure: (P) -> Void) throws -> P {
|
||||||
|
configure(pedal)
|
||||||
|
try addToB(pedal)
|
||||||
|
return pedal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Factory rig catalog
|
// MARK: - Factory rig catalog
|
||||||
|
|
||||||
/// The built-in starter rigs shipped with the app and seeded into an empty rig library on first
|
/// The built-in starter rigs shipped with the app and seeded into an empty rig library on first
|
||||||
@@ -82,6 +104,7 @@ public enum FactoryRigLibrary {
|
|||||||
psychVibeLead,
|
psychVibeLead,
|
||||||
comfortablyNumbLead,
|
comfortablyNumbLead,
|
||||||
doomRiffWall,
|
doomRiffWall,
|
||||||
|
garciaOBELSplit,
|
||||||
]
|
]
|
||||||
|
|
||||||
// MARK: Shoegaze walls
|
// MARK: Shoegaze walls
|
||||||
@@ -600,6 +623,69 @@ public enum FactoryRigLibrary {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: Two-board split
|
||||||
|
|
||||||
|
/// A two-board split — a full effects chain vs a clean direct path — selectable like an on-board
|
||||||
|
/// effects loop. Showcases the multi-board router.
|
||||||
|
static let garciaOBELSplit: FactoryRig = FactoryRig(
|
||||||
|
id: stableID("Garcia OBEL Split"),
|
||||||
|
name: "Garcia OBEL Clean/Effects Split",
|
||||||
|
blurb: "Two boards, one footswitch: an effects/lead chain (octave, envelope filter, "
|
||||||
|
+ "overdrive, phaser, delay) versus a clean direct path — switch between them like an "
|
||||||
|
+ "on-board effects loop. In the spirit of a certain improvising guitarist's late-'80s "
|
||||||
|
+ "rig. Flip the router to A for effects, B for clean.",
|
||||||
|
category: .classic,
|
||||||
|
sessionBuild: { session in
|
||||||
|
let router: ChainRouter = try session.makeChainRouter()
|
||||||
|
|
||||||
|
// Board A — the effects / lead chain (effects loop engaged).
|
||||||
|
try router.appendToA(BassOctaverPedal()) { p in
|
||||||
|
p.oct1 = 0.5
|
||||||
|
p.direct = 1.0
|
||||||
|
p.mode = 1.0
|
||||||
|
}
|
||||||
|
try router.appendToA(MutantPedal()) { p in
|
||||||
|
p.sensitivity = 2.5
|
||||||
|
p.resonance = 5.0
|
||||||
|
}
|
||||||
|
try router.appendToA(OverdrivePedal()) { p in
|
||||||
|
p.drive = 5.0
|
||||||
|
p.tone = 4200.0
|
||||||
|
}
|
||||||
|
try router.appendToA(PhaserPedal()) { p in
|
||||||
|
p.rate = 0.4
|
||||||
|
p.feedback = 0.4
|
||||||
|
}
|
||||||
|
try router.appendToA(DelayPedal()) { p in
|
||||||
|
p.time = 0.35
|
||||||
|
p.feedback = 1.8
|
||||||
|
}
|
||||||
|
try router.appendToA(CaliforniaAmpPedal()) { p in
|
||||||
|
p.gain = 5.0
|
||||||
|
p.bass = 0.55
|
||||||
|
p.mid = 0.6
|
||||||
|
p.treble = 0.6
|
||||||
|
}
|
||||||
|
|
||||||
|
// Board B — the clean / direct path (effects loop bypassed).
|
||||||
|
try router.appendToB(CompressorPedal()) { p in
|
||||||
|
p.inputGain = 2.0
|
||||||
|
p.threshold = -16.0
|
||||||
|
p.ratio = 3.0
|
||||||
|
}
|
||||||
|
try router.appendToB(CaliforniaAmpPedal()) { p in
|
||||||
|
p.gain = 3.5
|
||||||
|
p.bass = 0.5
|
||||||
|
p.mid = 0.55
|
||||||
|
p.treble = 0.6
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default to the effects board; the router's A/B control switches paths.
|
||||||
|
router.mode = .a
|
||||||
|
try router.finalize()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
// MARK: - Seeding
|
// MARK: - Seeding
|
||||||
|
|
||||||
/// Builds every factory rig into a ``RigEntry`` snapshot suitable for the rig library.
|
/// Builds every factory rig into a ``RigEntry`` snapshot suitable for the rig library.
|
||||||
@@ -620,12 +706,19 @@ public enum FactoryRigLibrary {
|
|||||||
entries.reserveCapacity(all.count)
|
entries.reserveCapacity(all.count)
|
||||||
for rig: FactoryRig in all {
|
for rig: FactoryRig in all {
|
||||||
let session: ShoeGaze = try makeSession()
|
let session: ShoeGaze = try makeSession()
|
||||||
// Tear down on every exit from this iteration, including a throw from `build` /
|
// Tear down on every exit from this iteration, including a throw from a builder, so a
|
||||||
// `finalizeBoard`, so a failing rig never leaks its throwaway engine's buses/nodes.
|
// failing rig never leaks its throwaway engine's buses/nodes.
|
||||||
defer { session.teardown() }
|
defer { session.teardown() }
|
||||||
let board: PedalBoard = try session.addBoard()
|
switch rig.build {
|
||||||
try rig.build(session, board)
|
case .singleBoard(let build):
|
||||||
try session.finalizeBoard(board)
|
let board: PedalBoard = try session.addBoard()
|
||||||
|
try build(session, board)
|
||||||
|
try session.finalizeBoard(board)
|
||||||
|
case .session(let build):
|
||||||
|
// The builder owns its own board/router lifecycle (addBoard/finalizeBoard,
|
||||||
|
// makeChainRouter/finalize).
|
||||||
|
try build(session)
|
||||||
|
}
|
||||||
entries.append(session.captureEntry(name: rig.name, id: rig.id))
|
entries.append(session.captureEntry(name: rig.name, id: rig.id))
|
||||||
}
|
}
|
||||||
return entries
|
return entries
|
||||||
|
|||||||
@@ -18,6 +18,16 @@ extension ShoeGaze {
|
|||||||
/// - Returns: A `ShoeGazeState` reflecting the current rig.
|
/// - Returns: A `ShoeGazeState` reflecting the current rig.
|
||||||
public func captureState() -> ShoeGazeState {
|
public func captureState() -> ShoeGazeState {
|
||||||
let boardStates: [BoardState] = boards.map { $0.captureState() }
|
let boardStates: [BoardState] = boards.map { $0.captureState() }
|
||||||
|
let routerStates: [ChainRouterState] = routers.map { router in
|
||||||
|
ChainRouterState(
|
||||||
|
boardA: router.boardA.pedals.map { $0.captureState() },
|
||||||
|
boardB: router.boardB.pedals.map { $0.captureState() },
|
||||||
|
branchA: router.splitter.branchAEnabled,
|
||||||
|
branchB: router.splitter.branchBEnabled,
|
||||||
|
mergerLevelA: router.merger?.levelA ?? 1.0,
|
||||||
|
mergerLevelB: router.merger?.levelB ?? 1.0
|
||||||
|
)
|
||||||
|
}
|
||||||
return ShoeGazeState(
|
return ShoeGazeState(
|
||||||
boards: boardStates,
|
boards: boardStates,
|
||||||
midiMap: midiMap,
|
midiMap: midiMap,
|
||||||
@@ -25,7 +35,10 @@ extension ShoeGaze {
|
|||||||
tempoEnabled: tempoEnabled,
|
tempoEnabled: tempoEnabled,
|
||||||
inputSource: inputSource,
|
inputSource: inputSource,
|
||||||
numInputChannels: engine.numInputChannels,
|
numInputChannels: engine.numInputChannels,
|
||||||
numOutputChannels: engine.numOutputChannels
|
numOutputChannels: engine.numOutputChannels,
|
||||||
|
// `nil` rather than `[]` when there are no routers, so a routerless rig's JSON is
|
||||||
|
// unchanged and byte-identical to one saved before router persistence existed.
|
||||||
|
routers: routerStates.isEmpty ? nil : routerStates
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,6 +175,9 @@ extension ShoeGaze {
|
|||||||
try rebuildSegments(boardState.segments, into: board)
|
try rebuildSegments(boardState.segments, into: board)
|
||||||
try finalizeBoard(board)
|
try finalizeBoard(board)
|
||||||
}
|
}
|
||||||
|
for routerState: ChainRouterState in snapshot.routers ?? [] {
|
||||||
|
try rebuildRouter(routerState)
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Leave no partial rig behind: tear down whatever was built, then rethrow.
|
// Leave no partial rig behind: tear down whatever was built, then rethrow.
|
||||||
teardown()
|
teardown()
|
||||||
@@ -169,6 +185,32 @@ extension ShoeGaze {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reconstructs one ``ChainRouter`` from a saved snapshot: creates the router, replays both linear
|
||||||
|
/// branches (skipping unknown pedal types), finalizes, then restores the splitter branch levels
|
||||||
|
/// and merger output levels so the saved A / B / blend selection is preserved.
|
||||||
|
///
|
||||||
|
/// - Parameter state: The router snapshot to reconstruct.
|
||||||
|
/// - Throws: `BusAllocatorError`, `EngineAddError`, or `ShoeGazeError` from router construction.
|
||||||
|
private func rebuildRouter(_ state: ChainRouterState) throws {
|
||||||
|
let router: ChainRouter = try makeChainRouter()
|
||||||
|
for pedalState: PedalState in state.boardA {
|
||||||
|
if let pedal: Pedal = makeAndApply(pedalState) {
|
||||||
|
try router.addToA(pedal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for pedalState: PedalState in state.boardB {
|
||||||
|
if let pedal: Pedal = makeAndApply(pedalState) {
|
||||||
|
try router.addToB(pedal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try router.finalize()
|
||||||
|
// Restore the blend after finalize: the splitter exists from init, the merger only after.
|
||||||
|
router.splitter.branchAEnabled = state.branchA
|
||||||
|
router.splitter.branchBEnabled = state.branchB
|
||||||
|
router.merger?.levelA = state.mergerLevelA
|
||||||
|
router.merger?.levelB = state.mergerLevelB
|
||||||
|
}
|
||||||
|
|
||||||
/// Loads a rig from a JSON preset file and rebuilds the engine graph.
|
/// Loads a rig from a JSON preset file and rebuilds the engine graph.
|
||||||
///
|
///
|
||||||
/// This method:
|
/// This method:
|
||||||
|
|||||||
@@ -57,6 +57,11 @@ public struct ShoeGazeState: Codable, Equatable {
|
|||||||
/// persistence. Reapplied on load under the same conditions as ``numInputChannels``.
|
/// persistence. Reapplied on load under the same conditions as ``numInputChannels``.
|
||||||
public let numOutputChannels: Int?
|
public let numOutputChannels: Int?
|
||||||
|
|
||||||
|
/// The rig's chain routers (two-board split/blend units), or `nil` for a rig saved before router
|
||||||
|
/// persistence existed. Additive: decoded with `decodeIfPresent`, so older rigs — which never had
|
||||||
|
/// routers — decode cleanly to `nil` without a format-version bump.
|
||||||
|
public let routers: [ChainRouterState]?
|
||||||
|
|
||||||
/// Creates a rig state snapshot.
|
/// Creates a rig state snapshot.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
@@ -68,6 +73,7 @@ public struct ShoeGazeState: Codable, Equatable {
|
|||||||
/// - inputSource: The rig's input selection, or `nil` to not persist one. Defaults to `nil`.
|
/// - inputSource: The rig's input selection, or `nil` to not persist one. Defaults to `nil`.
|
||||||
/// - numInputChannels: The engine input-channel count, or `nil`. Defaults to `nil`.
|
/// - numInputChannels: The engine input-channel count, or `nil`. Defaults to `nil`.
|
||||||
/// - numOutputChannels: The engine output-channel count, or `nil`. Defaults to `nil`.
|
/// - numOutputChannels: The engine output-channel count, or `nil`. Defaults to `nil`.
|
||||||
|
/// - routers: The rig's chain routers, or `nil` when there are none. Defaults to `nil`.
|
||||||
public init(
|
public init(
|
||||||
version: Int = presetFormatVersion,
|
version: Int = presetFormatVersion,
|
||||||
boards: [BoardState],
|
boards: [BoardState],
|
||||||
@@ -76,7 +82,8 @@ public struct ShoeGazeState: Codable, Equatable {
|
|||||||
tempoEnabled: Bool? = nil,
|
tempoEnabled: Bool? = nil,
|
||||||
inputSource: InputSource? = nil,
|
inputSource: InputSource? = nil,
|
||||||
numInputChannels: Int? = nil,
|
numInputChannels: Int? = nil,
|
||||||
numOutputChannels: Int? = nil
|
numOutputChannels: Int? = nil,
|
||||||
|
routers: [ChainRouterState]? = nil
|
||||||
) {
|
) {
|
||||||
self.version = version
|
self.version = version
|
||||||
self.boards = boards
|
self.boards = boards
|
||||||
@@ -86,6 +93,7 @@ public struct ShoeGazeState: Codable, Equatable {
|
|||||||
self.inputSource = inputSource
|
self.inputSource = inputSource
|
||||||
self.numInputChannels = numInputChannels
|
self.numInputChannels = numInputChannels
|
||||||
self.numOutputChannels = numOutputChannels
|
self.numOutputChannels = numOutputChannels
|
||||||
|
self.routers = routers
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
@@ -97,6 +105,7 @@ public struct ShoeGazeState: Codable, Equatable {
|
|||||||
case inputSource
|
case inputSource
|
||||||
case numInputChannels
|
case numInputChannels
|
||||||
case numOutputChannels
|
case numOutputChannels
|
||||||
|
case routers
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decodes a rig snapshot, tolerating an absent, malformed, OR legacy-format `midiMap`.
|
/// Decodes a rig snapshot, tolerating an absent, malformed, OR legacy-format `midiMap`.
|
||||||
@@ -129,5 +138,6 @@ public struct ShoeGazeState: Codable, Equatable {
|
|||||||
self.inputSource = try container.decodeIfPresent(InputSource.self, forKey: .inputSource)
|
self.inputSource = try container.decodeIfPresent(InputSource.self, forKey: .inputSource)
|
||||||
self.numInputChannels = try container.decodeIfPresent(Int.self, forKey: .numInputChannels)
|
self.numInputChannels = try container.decodeIfPresent(Int.self, forKey: .numInputChannels)
|
||||||
self.numOutputChannels = try container.decodeIfPresent(Int.self, forKey: .numOutputChannels)
|
self.numOutputChannels = try container.decodeIfPresent(Int.self, forKey: .numOutputChannels)
|
||||||
|
self.routers = try container.decodeIfPresent([ChainRouterState].self, forKey: .routers)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -199,7 +199,10 @@ public final class ShoeGaze {
|
|||||||
|
|
||||||
/// All chain routers created via ``makeChainRouter()``.
|
/// All chain routers created via ``makeChainRouter()``.
|
||||||
/// Routers are torn down (in reverse-creation order) by ``teardown()``.
|
/// Routers are torn down (in reverse-creation order) by ``teardown()``.
|
||||||
private var routers: [ChainRouter] = []
|
///
|
||||||
|
/// `internal` (not `private`) so the preset capture/rebuild path in `ShoeGaze+Preset.swift` can
|
||||||
|
/// snapshot and reconstruct routers.
|
||||||
|
var routers: [ChainRouter] = []
|
||||||
|
|
||||||
// MARK: - Init
|
// MARK: - Init
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
// ChainRouterPersistenceTests.swift
|
||||||
|
// Lyrebird
|
||||||
|
//
|
||||||
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
import Testing
|
||||||
|
import Foundation
|
||||||
|
import LyrebirdSynth
|
||||||
|
import LyrebirdUI
|
||||||
|
@testable import LyrebirdShoeGaze
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
@Suite("ChainRouter persistence")
|
||||||
|
struct ChainRouterPersistenceTests {
|
||||||
|
|
||||||
|
@Test func routerRigRoundTripsThroughJSONAndRenders() throws {
|
||||||
|
// Build a routed session: two-pedal board A, one-pedal board B, selecting board B.
|
||||||
|
let (engineA, urlA): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
|
||||||
|
defer { try? FileManager.default.removeItem(at: urlA) }
|
||||||
|
let sessionA: ShoeGaze = ShoeGaze(engine: engineA)
|
||||||
|
let router: ChainRouter = try sessionA.makeChainRouter()
|
||||||
|
try router.addToA(FuzzPedal())
|
||||||
|
try router.addToA(OverdrivePedal())
|
||||||
|
try router.addToB(ReverbPedal())
|
||||||
|
router.mode = .b
|
||||||
|
try router.finalize()
|
||||||
|
|
||||||
|
let state: ShoeGazeState = sessionA.captureState()
|
||||||
|
#expect(state.routers?.count == 1, "router was not captured")
|
||||||
|
#expect(state.boards.isEmpty, "router rig should have no plain boards")
|
||||||
|
|
||||||
|
// JSON round-trip.
|
||||||
|
let data: Data = try JSONEncoder().encode(state)
|
||||||
|
let decoded: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data)
|
||||||
|
#expect(decoded == state, "router state did not survive a JSON round-trip")
|
||||||
|
|
||||||
|
// Load into a fresh session and verify the router was rebuilt structurally.
|
||||||
|
let (engineB, urlB): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.2, numAudioBuses: 128)
|
||||||
|
defer { try? FileManager.default.removeItem(at: urlB) }
|
||||||
|
let sessionB: ShoeGaze = ShoeGaze(engine: engineB)
|
||||||
|
try sessionB.loadRig(from: decoded)
|
||||||
|
|
||||||
|
#expect(sessionB.routers.count == 1, "router was not rebuilt on load")
|
||||||
|
guard let rebuilt: ChainRouter = sessionB.routers.first else {
|
||||||
|
Issue.record("no rebuilt router")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
#expect(rebuilt.boardA.pedals.count == 2, "board A pedal count not restored")
|
||||||
|
#expect(rebuilt.boardB.pedals.count == 1, "board B pedal count not restored")
|
||||||
|
#expect(rebuilt.boardA.pedals.map { $0.displayName } == ["Fuzz", "Overdrive"])
|
||||||
|
#expect(rebuilt.boardB.pedals.map { $0.displayName } == ["Reverb"])
|
||||||
|
// mode == .b → branch A muted, branch B active.
|
||||||
|
#expect(rebuilt.splitter.branchAEnabled == 0.0, "branch A level not restored")
|
||||||
|
#expect(rebuilt.splitter.branchBEnabled == 1.0, "branch B level not restored")
|
||||||
|
|
||||||
|
// The rebuilt router should render audio (board B is the active path).
|
||||||
|
try engineB.start()
|
||||||
|
let samples: [Float] = readStereoCAF(url: urlB)
|
||||||
|
#expect(hasNonSilentOutput(samples), "rebuilt router produced silence")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test func routerlessRigStoresNoRoutersKey() throws {
|
||||||
|
// A plain single-board session must not gain a `routers` payload (byte-compat with rigs
|
||||||
|
// saved before router persistence existed).
|
||||||
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 64)
|
||||||
|
defer { try? FileManager.default.removeItem(at: url) }
|
||||||
|
let session: ShoeGaze = ShoeGaze(engine: engine)
|
||||||
|
let board: PedalBoard = try session.addBoard()
|
||||||
|
try board.add(FuzzPedal())
|
||||||
|
try session.finalizeBoard(board)
|
||||||
|
|
||||||
|
let state: ShoeGazeState = session.captureState()
|
||||||
|
#expect(state.routers == nil, "routerless rig should capture nil routers")
|
||||||
|
|
||||||
|
let json: String = String(data: try JSONEncoder().encode(state), encoding: .utf8) ?? ""
|
||||||
|
#expect(!json.contains("\"routers\""), "routerless JSON should omit the routers key")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -71,9 +71,12 @@ struct FactoryRigLibraryTests {
|
|||||||
for (entry, rig): (RigEntry, FactoryRig) in zip(entries, rigs) {
|
for (entry, rig): (RigEntry, FactoryRig) in zip(entries, rigs) {
|
||||||
#expect(entry.id == rig.id, "entry id must be the rig's stable id")
|
#expect(entry.id == rig.id, "entry id must be the rig's stable id")
|
||||||
#expect(entry.name == rig.name)
|
#expect(entry.name == rig.name)
|
||||||
#expect(entry.state.boards.count == 1, "\(rig.name) should build exactly one board")
|
// A rig builds either plain boards or a router (or both) — never nothing.
|
||||||
let pedalCount: Int = entry.state.boards.first?.pedals.count ?? 0
|
let routerStates: [ChainRouterState] = entry.state.routers ?? []
|
||||||
#expect(pedalCount >= 3, "\(rig.name) should build a non-trivial chain")
|
#expect(!entry.state.boards.isEmpty || !routerStates.isEmpty, "\(rig.name) built nothing")
|
||||||
|
let boardPedals: Int = entry.state.boards.reduce(0) { $0 + $1.pedals.count }
|
||||||
|
let routerPedals: Int = routerStates.reduce(0) { $0 + $1.boardA.count + $1.boardB.count }
|
||||||
|
#expect(boardPedals + routerPedals >= 3, "\(rig.name) should build a non-trivial chain")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stable ids: re-seeding yields identical ids (so a re-seed overwrites in place).
|
// Stable ids: re-seeding yields identical ids (so a re-seed overwrites in place).
|
||||||
|
|||||||
Reference in New Issue
Block a user