feat(postboard): shared post-board chain after the board mix

Add a dedicated linear stereo post-board chain that processes the summed
board mix once, after the board split, before the hardware output — for
global reverb, a bus limiter, a final EQ, and the like.

Routing: when a post-chain exists, every BoardOutputMaster writes a shared
stereo mix bus (parameterized output bus indices) instead of hardware; the
post-chain (PostChain + PostChainOutput) reads that mix, runs its pedals in
order, and copies the tail to the hardware outputs. Always stereo-headed
(dual-mono for a mono rig) so mono pedals auto-stereoize per channel.

The chain's head node is inserted at .tail(of: mainGroup), so it evaluates
after every board master regardless of board add order — no re-order on
board add, and no board dropped from the mix. buildPostChain tracks the
chain before wiring it so a mid-build throw stays reclaimable by teardown.

State: additive ShoeGazeState.postChain (nil-when-empty, decodeIfPresent,
byte-identical old-rig round-trip). Edit API (append/insert/remove/move)
mirrors the pre-board chain: capture-from-live, stop/teardown/rebuild. Undo
treats a post-topology change as a full rebuild via id-sequence compare.
UI: a hide-when-empty post-board strip below the boards, symmetric to the
pre-board strip, with palette routing and per-pedal remove through undo.

Bumps the iOS demo's Lyrebird pin to 2.3.0 (onEditingEnded).

Tests: PostBoardChainTests (render, closed-volume silences output,
post-chain-after-every-master ordering, board-add ordering, insert/move/
remove, save/load round-trip with knob, old-rig decode) + post-pedal undo.
855 tests pass. Reviewed (RT-safety + line sweep); the ordering bug both
reviewers caught is fixed at the root and covered by a deterministic test.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 00:22:39 -08:00
parent 10a6e122bf
commit 52c676605f
13 changed files with 709 additions and 23 deletions
@@ -553,4 +553,76 @@ extension ShoeGaze {
do { try engine.start() } catch { print("[ShoeGaze] engine restart after pre-board edit failed: \(error)") }
}
}
// MARK: - Post-board chain editing
/// Appends a pedal to the end of the post-board chain — the shared stage that processes the
/// summed board mix, after the split, before the hardware output (global reverb, bus limiter).
///
/// - Important: Reshapes the shared mix path, so the whole rig is torn down and rebuilt
/// (ordering-safe); resets transient / looper state like ``setInputSource(_:)``.
public func appendPostPedal(_ kind: PedalType) throws {
let type: String = kind.rawValue
guard PedalRegistry.builtIn[type] != nil else { throw ShoeGazeError.unknownPedalType(type) }
var updated: [PedalState] = capturePostBoardStates()
updated.append(PedalState(type: type, knobValues: [], switchValues: [], bypass: 0.0))
try setPostBoardChain(updated)
}
/// Inserts a pedal at `index` in the post-board chain (insert-before; `index == count` appends).
public func insertPostPedal(_ kind: PedalType, at index: Int) throws {
let type: String = kind.rawValue
guard PedalRegistry.builtIn[type] != nil else { throw ShoeGazeError.unknownPedalType(type) }
var updated: [PedalState] = capturePostBoardStates()
guard index >= 0 && index <= updated.count else {
throw ShoeGazeError.pedalIndexOutOfRange(index: index, count: updated.count)
}
updated.insert(PedalState(type: type, knobValues: [], switchValues: [], bypass: 0.0), at: index)
try setPostBoardChain(updated)
}
/// Removes the post-board pedal at `index`.
public func removePostPedal(at index: Int) throws {
var updated: [PedalState] = capturePostBoardStates()
guard index >= 0 && index < updated.count else {
throw ShoeGazeError.pedalIndexOutOfRange(index: index, count: updated.count)
}
updated.remove(at: index)
try setPostBoardChain(updated)
}
/// Moves the post-board pedal at `sourceIndex` to `destinationIndex` (SwiftUI `onMove` semantics).
public func movePostPedal(from sourceIndex: Int, to destinationIndex: Int) throws {
var updated: [PedalState] = capturePostBoardStates()
try Self.moveWithinArray(&updated, from: sourceIndex, to: destinationIndex)
try setPostBoardChain(updated)
}
/// Replaces the post-board chain with `newStates` and rebuilds the rig (ordering-safe). The
/// masters' output target (mix bus vs hardware) depends on whether a post-chain exists, so the
/// whole rig is rebuilt; mirrors ``setInputSource(_:)``.
///
/// - Note: On a rebuild failure the `postBoardStates` are restored and the engine is restarted,
/// but the graph was already torn down and is left empty — the rig runs silent until the next
/// successful edit. A setup-time edit, so this is acceptable (same contract as the pre-board edit).
private func setPostBoardChain(_ newStates: [PedalState]) throws {
let previous: [PedalState] = postBoardStates
postBoardStates = newStates
let wasRunning: Bool = engine.isRunning
if wasRunning { engine.stop() }
let snapshot: ShoeGazeState = captureState().withPostChain(newStates.isEmpty ? nil : newStates)
teardown()
do {
try rebuildAllBoards(from: snapshot)
} catch {
postBoardStates = previous
if wasRunning {
do { try engine.start() } catch { print("[ShoeGaze] engine restart after failed post-board edit failed: \(error)") }
}
throw error
}
if wasRunning {
do { try engine.start() } catch { print("[ShoeGaze] engine restart after post-board edit failed: \(error)") }
}
}
}