Files
ShoeGaze/Sources/LyrebirdShoeGaze/Presets/BoardState.swift
T
josh 947dfe427a Add looper pedal: beat-quantized transport, undo, reverse, timeline
Extend the looper into a double-width tempo-aware pedal:

- Beat-quantized transport: record start and loop close both snap to the
  next beat via a LyrebirdRoutine waiting on the shared clock; loop length
  is an exact whole-beat span. Falls back to immediate/unquantized when no
  clock is present.
- Loop-length read-outs (frames/seconds/beats), transport state, and a
  sample-clock-derived play-head position for the UI timeline.
- Overdub undo (pre-dub snapshot + restore), reverse playback (live rate
  flip), and feedback-decay overdub (overdub preLevel via a Feedback knob).
- Inject the shared LyrebirdClock + SynthesisEngine into Pedal via
  PedalBoard/ChainRouter so tempo-aware pedals can reach them.
- New double-width LooperTimelineView (state badge, length read-out,
  sweeping play-head) wired into PedalView via Pedal.widthUnits.

Concurrency hardening from RT-safety review: a transportGeneration counter
drops superseded deferred beat/undo callbacks that LyrebirdRoutine.stop()
cannot recall once enqueued; the undo restore is deferred past the RT
overdub gate-close; transport routines use non-blocking stop().

Note: this commit also snapshots pre-existing uncommitted work-in-progress
already in the tree (MIDI control refactor, Tuner, Tempo, preset changes),
which is interleaved with the looper changes through shared files and could
not be split into a separately-building commit.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 09:22:20 -07:00

115 lines
4.5 KiB
Swift

// BoardState.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// 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 {
/// Stable per-board identity, persisted so MIDI bindings can address a board by id rather
/// than by position. Survives capture → rebuild round-trips. Legacy presets without an id
/// decode to a fresh `UUID()`.
public let id: UUID
/// 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(
id: UUID = UUID(),
label: String,
segments: [BoardSegmentState]
) {
self.id = id
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(
id: UUID = UUID(),
label: String,
pedals: [PedalState]
) {
self.id = id
self.label = label
self.segments = pedals.map { BoardSegmentState.pedal($0) }
}
private enum CodingKeys: String, CodingKey {
case id
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.id = try container.decodeIfPresent(UUID.self, forKey: .id) ?? UUID()
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(id, forKey: .id)
try container.encode(label, forKey: .label)
try container.encode(segments, forKey: .segments)
}
}