Serialize split-region topology in presets
Replace the flat per-board pedal list with an ordered [BoardSegmentState] (pedal | parallel | stereoSplit | mergeToMono) so parallel and stereo split regions — branch pedal chains plus SplitMode/StereoRoute — round-trip through saveRig/loadRig and rebuildBoard. - PedalBoard.orderedSegments tracks chain structure (add/split/splitStereo/ mergeToMono); captureState() walks it; rebuildSegments() replays it. - BoardState backward-compatible: decodes legacy flat `pedals` arrays; computed `pedals` exposes the linear subset; throws on a file missing both keys. BoardSegmentState wire discriminators documented. - applyState (lightweight recall) extended to branch pedals + routing modes. - Unknown pedal types skipped on load, including inside branches. - Tests: parallel / stereo / mergeToMono file round-trips, legacy decode, unknown-pedal-in-branch, branch-param recall. Suite 116/116; demo builds. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -17,10 +17,7 @@ extension ShoeGaze {
|
||||
///
|
||||
/// - Returns: A `ShoeGazeState` reflecting the current rig.
|
||||
public func captureState() -> ShoeGazeState {
|
||||
let boardStates: [BoardState] = boards.map { board in
|
||||
let pedalStates: [PedalState] = board.pedals.map { $0.captureState() }
|
||||
return BoardState(label: board.label, pedals: pedalStates)
|
||||
}
|
||||
let boardStates: [BoardState] = boards.map { $0.captureState() }
|
||||
return ShoeGazeState(boards: boardStates)
|
||||
}
|
||||
|
||||
@@ -72,20 +69,69 @@ extension ShoeGaze {
|
||||
for boardState: BoardState in state.boards {
|
||||
let board: PedalBoard = try addBoard()
|
||||
board.label = boardState.label
|
||||
|
||||
for pedalState: PedalState in boardState.pedals {
|
||||
guard let pedal: Pedal = PedalRegistry.makePedal(type: pedalState.type) else {
|
||||
// Unknown pedal type — skip without crashing.
|
||||
continue
|
||||
}
|
||||
pedal.applyState(pedalState)
|
||||
try board.add(pedal)
|
||||
}
|
||||
|
||||
try rebuildSegments(boardState.segments, into: board)
|
||||
try finalizeBoard(board)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Segment reconstruction
|
||||
|
||||
/// Rebuilds a board's ordered segments — linear pedals plus parallel and stereo split
|
||||
/// regions — replaying each via the corresponding `PedalBoard` API. Unknown pedal types
|
||||
/// are skipped (logged) so presets with missing custom pedals degrade gracefully.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - segments: The ordered segment snapshots to reconstruct.
|
||||
/// - board: The board to populate (freshly created, before `finalizeBoard`).
|
||||
/// - Throws: `BusAllocatorError.noFreeBus` or `EngineAddError` from any node insertion.
|
||||
private func rebuildSegments(_ segments: [BoardSegmentState], into board: PedalBoard) throws {
|
||||
for segment: BoardSegmentState in segments {
|
||||
switch segment {
|
||||
case .pedal(let state):
|
||||
if let pedal: Pedal = makeAndApply(state) {
|
||||
try board.add(pedal)
|
||||
}
|
||||
|
||||
case .parallel(let parallelState):
|
||||
let section: ParallelSection = try board.split()
|
||||
try addPedals(parallelState.branchA, to: section.branchA)
|
||||
try addPedals(parallelState.branchB, to: section.branchB)
|
||||
try board.merge(section)
|
||||
section.splitter.mode = parallelState.mode
|
||||
|
||||
case .stereoSplit(let stereoState):
|
||||
let section: StereoSplitSection = try board.splitStereo()
|
||||
try addPedals(stereoState.left, to: section.left)
|
||||
try addPedals(stereoState.right, to: section.right)
|
||||
try board.collectStereo(section)
|
||||
section.route = stereoState.route
|
||||
|
||||
case .mergeToMono:
|
||||
try board.mergeToMono()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds each pedal snapshot to a branch chain, skipping unknown types.
|
||||
private func addPedals(_ states: [PedalState], to chain: BranchChain) throws {
|
||||
for state: PedalState in states {
|
||||
if let pedal: Pedal = makeAndApply(state) {
|
||||
try chain.add(pedal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Instantiates a pedal for the snapshot's type and applies its parameters, or returns
|
||||
/// `nil` (logged) for an unknown type.
|
||||
private func makeAndApply(_ state: PedalState) -> Pedal? {
|
||||
guard let pedal: Pedal = PedalRegistry.makePedal(type: state.type) else {
|
||||
print("[ShoeGaze] preset: skipping unknown pedal type \(state.type)")
|
||||
return nil
|
||||
}
|
||||
pedal.applyState(state)
|
||||
return pedal
|
||||
}
|
||||
|
||||
// MARK: - Per-board rebuild
|
||||
|
||||
/// Tears down a single board and recreates it from a saved `BoardState`, preserving
|
||||
@@ -96,9 +142,10 @@ extension ShoeGaze {
|
||||
/// 1. Finds `board` in `boards` by identity (`===`). If absent, the board is treated as
|
||||
/// unmanaged: a fresh board is built, finalised, and returned without reordering.
|
||||
/// 2. Calls `addBoard()` to allocate a new board and sets `newBoard.label = state.label`.
|
||||
/// 3. For each `PedalState` in `state.pedals`, calls `PedalRegistry.makePedal(type:)`;
|
||||
/// unknown types are logged and skipped so presets with missing custom pedals degrade
|
||||
/// gracefully. Known pedals have `applyState(_:)` called before `board.add(_:)`.
|
||||
/// 3. Calls `rebuildSegments(_:into:)` to replay each ordered segment — linear pedals plus
|
||||
/// parallel and stereo split regions (with their branch pedals and routing mode).
|
||||
/// Unknown pedal types are logged and skipped so presets with missing custom pedals
|
||||
/// degrade gracefully.
|
||||
/// 4. Calls `finalizeBoard(_:)` to connect the output master.
|
||||
/// 5. Only after the new board is fully wired does `removeBoard(_:)` tear down the old
|
||||
/// board — so a failure during steps 2–4 leaves the original board intact.
|
||||
@@ -132,17 +179,7 @@ extension ShoeGaze {
|
||||
let newBoard: PedalBoard = try addBoard()
|
||||
do {
|
||||
newBoard.label = state.label
|
||||
|
||||
for pedalState: PedalState in state.pedals {
|
||||
guard let pedal: Pedal = PedalRegistry.makePedal(type: pedalState.type) else {
|
||||
// Unknown pedal type — log for diagnosability, then skip.
|
||||
print("[ShoeGaze] rebuildBoard: skipping unknown pedal type \(pedalState.type)")
|
||||
continue
|
||||
}
|
||||
pedal.applyState(pedalState)
|
||||
try newBoard.add(pedal)
|
||||
}
|
||||
|
||||
try rebuildSegments(state.segments, into: newBoard)
|
||||
try finalizeBoard(newBoard)
|
||||
} catch {
|
||||
// New board is partial — clean it up while the old board is still intact.
|
||||
@@ -168,9 +205,11 @@ extension ShoeGaze {
|
||||
/// rebuilding the engine graph.
|
||||
///
|
||||
/// This is a lightweight "recall settings" path intended for topologically identical
|
||||
/// rigs (same board count, same pedal count and types in the same order). Boards
|
||||
/// and pedals are matched positionally; any count or type mismatch at the board or
|
||||
/// pedal level is silently ignored for that entry — no error is thrown and remaining
|
||||
/// rigs (same board count, same segment order, same pedal types). Boards, segments, and
|
||||
/// pedals — including branch pedals inside parallel and stereo split regions — are matched
|
||||
/// positionally, and split routing modes (`SplitMode` / `StereoRoute`) are restored too.
|
||||
/// Any count or type mismatch at any level is silently ignored for that entry — no error
|
||||
/// is thrown and remaining
|
||||
/// entries continue to be processed.
|
||||
///
|
||||
/// Use `loadRig(from:)` when you need a full topology rebuild.
|
||||
@@ -178,10 +217,45 @@ 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) {
|
||||
for (pedal, pedalState) in zip(board.pedals, boardState.pedals) {
|
||||
guard pedal.displayName == pedalState.type else { continue }
|
||||
pedal.applyState(pedalState)
|
||||
applySegmentParams(boardState.segments, to: board.orderedSegments)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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).
|
||||
private func applySegmentParams(
|
||||
_ states: [BoardSegmentState],
|
||||
to segments: [BoardSegment]
|
||||
) {
|
||||
for (segment, segmentState) in zip(segments, states) {
|
||||
switch (segment, segmentState) {
|
||||
case let (.pedal(pedal), .pedal(pedalState)):
|
||||
if pedal.displayName == pedalState.type {
|
||||
pedal.applyState(pedalState)
|
||||
}
|
||||
|
||||
case let (.parallel(section), .parallel(parallelState)):
|
||||
applyBranchParams(parallelState.branchA, to: section.branchA.pedals)
|
||||
applyBranchParams(parallelState.branchB, to: section.branchB.pedals)
|
||||
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)
|
||||
section.route = stereoState.route
|
||||
|
||||
default:
|
||||
// Topology mismatch at this position — skip.
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Positionally applies branch pedal snapshots to live branch pedals, matching by type.
|
||||
private func applyBranchParams(_ states: [PedalState], to pedals: [Pedal]) {
|
||||
for (pedal, pedalState) in zip(pedals, states) where pedal.displayName == pedalState.type {
|
||||
pedal.applyState(pedalState)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user