Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/BoardState.swift
T
josh 712f049f96 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>
2026-06-21 17:12:46 -07:00

101 lines
4.0 KiB
Swift

// BoardState.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// Serialisable snapshot of one `PedalBoard`'s label and full signal-chain topology.
///
/// `label` is the board's display name. `segments` captures the chain in signal-flow order:
/// linear pedals interleaved with parallel and stereo split regions (each region carries its
/// branch pedal chains and routing mode). Unknown pedal types are skipped silently on load.
///
/// ## Backward compatibility
///
/// Presets written before topology capture stored a flat `pedals` array instead of `segments`.
/// Such files still decode: the legacy `pedals` array is wrapped as `.pedal` segments. New
/// files encode `segments` only. The computed ``pedals`` property exposes the linear pedal
/// subset (the whole chain for boards with no split regions) for callers that only need it.
public struct BoardState: Codable, Equatable {
/// The board's display label (e.g. `"Board 1"`).
public let label: String
/// Ordered signal-chain segments (pedals + split regions) in signal-flow order.
public let segments: [BoardSegmentState]
/// The linear pedal subset of the chain — the `.pedal` segments only, in order. For a
/// board with no split regions this is the entire chain.
public var pedals: [PedalState] {
segments.compactMap { segment in
if case .pedal(let state) = segment { return state }
return nil
}
}
/// Creates a board state snapshot from an ordered segment list.
///
/// - Parameters:
/// - label: The board's display label.
/// - segments: Ordered signal-chain segments.
public init(
label: String,
segments: [BoardSegmentState]
) {
self.label = label
self.segments = segments
}
/// Creates a board state snapshot from a flat pedal list (no split regions).
///
/// Each pedal becomes a `.pedal` segment. Convenience for linear boards and the legacy
/// call shape.
///
/// - Parameters:
/// - label: The board's display label.
/// - pedals: Ordered list of pedal state snapshots.
public init(
label: String,
pedals: [PedalState]
) {
self.label = label
self.segments = pedals.map { BoardSegmentState.pedal($0) }
}
private enum CodingKeys: String, CodingKey {
case label
case segments
case pedals
}
/// Decodes a snapshot, tolerating legacy presets that stored a flat `pedals` array by
/// wrapping each entry as a `.pedal` segment.
public init(from decoder: any Decoder) throws {
let container: KeyedDecodingContainer<CodingKeys> = try decoder.container(keyedBy: CodingKeys.self)
self.label = try container.decode(String.self, forKey: .label)
if let decodedSegments: [BoardSegmentState] =
try container.decodeIfPresent([BoardSegmentState].self, forKey: .segments) {
self.segments = decodedSegments
} else if let legacyPedals: [PedalState] =
try container.decodeIfPresent([PedalState].self, forKey: .pedals) {
// Legacy file: a flat pedal list with no split regions.
self.segments = legacyPedals.map { BoardSegmentState.pedal($0) }
} else {
// Neither key present — a malformed file. Fail loudly rather than silently
// producing an empty board.
throw DecodingError.dataCorruptedError(
forKey: CodingKeys.segments,
in: container,
debugDescription: "BoardState requires either 'segments' or legacy 'pedals'."
)
}
}
/// Encodes the snapshot using the current `segments` representation.
public func encode(to encoder: any Encoder) throws {
var container: KeyedEncodingContainer<CodingKeys> = encoder.container(keyedBy: CodingKeys.self)
try container.encode(label, forKey: .label)
try container.encode(segments, forKey: .segments)
}
}