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:
@@ -233,6 +233,76 @@ public final class ShoeGaze {
|
||||
return pre.pedals.map { $0.captureState() }
|
||||
}
|
||||
|
||||
// MARK: - Post-board chain
|
||||
|
||||
/// Shared stereo mix bus the board masters write **into** when a post-board chain exists; `nil`
|
||||
/// otherwise (the masters write the hardware outputs directly). Owned here, released with the
|
||||
/// post-chain. The post-chain reads this mix and writes the hardware outputs.
|
||||
private var postMixLeft: AudioBus?
|
||||
private var postMixRight: AudioBus?
|
||||
|
||||
/// The live post-board chain (reads the mix, writes hardware), or `nil` when there is none.
|
||||
private var postChain: PostChain?
|
||||
|
||||
/// Persisted post-board pedal states — the serialization source of truth; the live ``postChain``
|
||||
/// is rebuilt from these. Empty when there is no post-board processing.
|
||||
public internal(set) var postBoardStates: [PedalState] = []
|
||||
|
||||
/// The live pedals in the post-board chain, in signal-flow order; empty when there is none.
|
||||
public var postBoardPedals: [Pedal] { postChain?.pedals ?? [] }
|
||||
|
||||
/// Post-board states captured from the **live** post-pedals (so knob tweaks are included), or the
|
||||
/// stored ``postBoardStates`` when there is no live chain. Use this wherever the post-chain is
|
||||
/// saved or rebuilt, so live edits aren't lost.
|
||||
func capturePostBoardStates() -> [PedalState] {
|
||||
guard let post: PostChain = postChain else { return postBoardStates }
|
||||
return post.pedals.map { $0.captureState() }
|
||||
}
|
||||
|
||||
/// Ensures the stereo mix bus exists when a post-board chain is present, returning its bus
|
||||
/// indices — or `nil` when there is none (the masters then write hardware directly).
|
||||
private func ensurePostMix() throws -> (left: Int, right: Int)? {
|
||||
guard !postBoardStates.isEmpty else { return nil }
|
||||
if let left: AudioBus = postMixLeft, let right: AudioBus = postMixRight {
|
||||
return (left.index, right.index)
|
||||
}
|
||||
let left: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
let right: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
postMixLeft = left
|
||||
postMixRight = right
|
||||
return (left.index, right.index)
|
||||
}
|
||||
|
||||
/// Builds the post-board chain from ``postBoardStates`` — reading the mix, writing hardware,
|
||||
/// inserted at the tail of the main group so it evaluates after **every** board master (order-
|
||||
/// independent). No-op when empty. Call after all boards are finalized.
|
||||
func buildPostChain() throws {
|
||||
guard !postBoardStates.isEmpty, let mix: (left: Int, right: Int) = try ensurePostMix() else { return }
|
||||
let post: PostChain = PostChain(
|
||||
engine: engine, clock: clock, mixLeftBus: mix.left, mixRightBus: mix.right
|
||||
)
|
||||
// Track the chain before wiring it, so a mid-build throw leaves its partial nodes/buses
|
||||
// reachable for teardown (`tearDownPost` / the rebuild catch) instead of orphaned.
|
||||
postChain = post
|
||||
for state: PedalState in postBoardStates {
|
||||
guard let pedal: Pedal = PedalRegistry.makePedal(type: state.type) else { continue }
|
||||
pedal.applyState(state)
|
||||
try post.add(pedal)
|
||||
}
|
||||
try post.finalize()
|
||||
}
|
||||
|
||||
/// Removes the post-board chain nodes and releases the mix bus. Call from ``teardown()`` after the
|
||||
/// masters that write the mix are removed.
|
||||
private func tearDownPost() {
|
||||
postChain?.teardown()
|
||||
postChain = nil
|
||||
if let left: AudioBus = postMixLeft { engine.busAllocator.releaseAudioBus(left) }
|
||||
postMixLeft = nil
|
||||
if let right: AudioBus = postMixRight { engine.busAllocator.releaseAudioBus(right) }
|
||||
postMixRight = nil
|
||||
}
|
||||
|
||||
/// All chain routers created via ``makeChainRouter()``.
|
||||
/// Routers are torn down (in reverse-creation order) by ``teardown()``.
|
||||
///
|
||||
@@ -285,6 +355,7 @@ public final class ShoeGaze {
|
||||
private func allPedals() -> [Pedal] {
|
||||
var result: [Pedal] = []
|
||||
result.append(contentsOf: preBoardPedals)
|
||||
result.append(contentsOf: postBoardPedals)
|
||||
for board: PedalBoard in boards {
|
||||
result.append(contentsOf: pedals(in: board))
|
||||
}
|
||||
@@ -525,10 +596,15 @@ public final class ShoeGaze {
|
||||
/// - Parameter board: A `PedalBoard` returned by `addBoard()` with pedals added.
|
||||
/// - Throws: `EngineAddError` if the engine rejects the master node.
|
||||
public func finalizeBoard(_ board: PedalBoard) throws {
|
||||
// When a post-board chain exists, every master writes the shared mix bus (the post-chain
|
||||
// then reads the mix and writes hardware); otherwise the master writes hardware directly.
|
||||
let mix: (left: Int, right: Int)? = try ensurePostMix()
|
||||
let master: BoardOutputMaster = BoardOutputMaster(
|
||||
boardOutputBusIndex: board.outputBus.index,
|
||||
boardOutputBusRightIndex: board.outputBusRight?.index,
|
||||
dryInputBusIndex: board.headBus.index
|
||||
dryInputBusIndex: board.headBus.index,
|
||||
outputLeftBusIndex: mix?.left ?? 0,
|
||||
outputRightBusIndex: mix?.right ?? 1
|
||||
)
|
||||
// The master evaluates after the entire chain (pedals, splitters, mergers).
|
||||
// `board.tailNode` returns the last inserted node (any Synthesizer), which covers
|
||||
@@ -568,6 +644,11 @@ public final class ShoeGaze {
|
||||
// Bring a newly-finalized board up at the current rig volume / mute.
|
||||
master.rigVolumeNode.update(rigVolume)
|
||||
master.rigMuteNode.update(rigMuted ? 1.0 : 0.0)
|
||||
|
||||
// A board finalized while a post-board chain already exists needs no post-chain rebuild: the
|
||||
// post-chain sits at the tail of the main group, so the new master (spliced nearer the group
|
||||
// head) evaluates before it automatically, and the new master already writes the shared mix
|
||||
// (via the `ensurePostMix()` redirect above). The post-chain keeps its live pedal state.
|
||||
}
|
||||
|
||||
// MARK: - Board selection / crossfade
|
||||
@@ -991,6 +1072,10 @@ public final class ShoeGaze {
|
||||
}
|
||||
outputMasters.removeAll()
|
||||
|
||||
// Remove the post-board chain (reader of the mix, writer of hardware) and release the mix
|
||||
// bus, now that the masters that write it are gone.
|
||||
tearDownPost()
|
||||
|
||||
// Step 2: Remove pedals from the engine and release all board-owned buses.
|
||||
// board.teardown() removes pedals from the engine and then releases buses,
|
||||
// so bus release only happens after all writers have been dequeued.
|
||||
|
||||
Reference in New Issue
Block a user