52c676605f
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>
144 lines
5.8 KiB
Swift
144 lines
5.8 KiB
Swift
// PostChain.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import LyrebirdSynth
|
|
|
|
/// The shared **post-board chain**: a linear chain of pedals 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.
|
|
///
|
|
/// Signal flow: every board master writes a shared stereo **mix bus**; this chain reads that mix,
|
|
/// runs its pedals in order, and copies the processed tail to the hardware outputs (via
|
|
/// ``PostChainOutput``). The chain is **always stereo-headed** — the mix carries two channels even
|
|
/// for a mono rig (dual-mono) — so a mono pedal auto-stereoizes per channel while a stereo-native
|
|
/// pedal cross-couples, exactly as in a ``PedalBoard``'s stereo region. It is a *linear* stage:
|
|
/// no split / parallel / stereo regions, which a post-master bus doesn't need.
|
|
///
|
|
/// `PostChain` is deliberately a dedicated type (not a `PedalBoard`): it owns only its inter-node
|
|
/// buses — the mix buses and the hardware outputs are owned elsewhere — and has post-specific
|
|
/// routing (mix in, hardware out). Its pedal buses are wired like `PedalBoard.add`'s width-2 arms.
|
|
///
|
|
/// - Note: Rig-wide volume / mute live on the board masters, *upstream* of this chain, so a panic
|
|
/// mute can let a post-chain reverb tail ring out briefly. Acceptable for a setup-time stage.
|
|
@MainActor
|
|
final class PostChain {
|
|
|
|
private let engine: SynthesisEngine
|
|
private let clock: LyrebirdClock?
|
|
|
|
/// The shared mix bus this chain reads (externally owned — never released here).
|
|
private let mixLeftBus: Int
|
|
private let mixRightBus: Int
|
|
|
|
/// Hardware output buses the processed tail is copied to.
|
|
private let outputLeftBus: Int
|
|
private let outputRightBus: Int
|
|
|
|
/// Inter-node buses this chain allocates and owns (released on ``teardown()``).
|
|
private var allocatedBuses: [AudioBus] = []
|
|
|
|
/// The chain's pedals, in signal-flow order.
|
|
private(set) var pedals: [Pedal] = []
|
|
|
|
/// The current stereo tail — starts at the mix, advances with each pedal.
|
|
private var tailLeft: Int
|
|
private var tailRight: Int
|
|
|
|
/// The last node inserted (for `.after` ordering of the next node).
|
|
private var lastNode: (any Synthesizer)?
|
|
|
|
/// The final node copying the tail to the hardware outputs.
|
|
private var outputNode: PostChainOutput?
|
|
|
|
/// Creates a post-board chain reading `mixLeftBus` / `mixRightBus` and writing the hardware
|
|
/// outputs. The mix and hardware buses are owned by the caller.
|
|
init(
|
|
engine: SynthesisEngine,
|
|
clock: LyrebirdClock?,
|
|
mixLeftBus: Int,
|
|
mixRightBus: Int,
|
|
outputLeftBus: Int = 0,
|
|
outputRightBus: Int = 1
|
|
) {
|
|
self.engine = engine
|
|
self.clock = clock
|
|
self.mixLeftBus = mixLeftBus
|
|
self.mixRightBus = mixRightBus
|
|
self.outputLeftBus = outputLeftBus
|
|
self.outputRightBus = outputRightBus
|
|
self.tailLeft = mixLeftBus
|
|
self.tailRight = mixRightBus
|
|
}
|
|
|
|
/// Appends a pedal to the chain. The first pedal is inserted at the **tail of the main group** so
|
|
/// it evaluates after every board master regardless of board add order; later calls chain after
|
|
/// the previous node automatically.
|
|
///
|
|
/// The pedal is wired at stereo width: both input channels read the current tail, both output
|
|
/// channels write fresh buses. The pedal's `channelKind` decides how its `build()` uses them (a
|
|
/// mono pedal runs `effect(_:)` per channel; a stereo-native pedal cross-couples).
|
|
func add(_ pedal: Pedal) throws {
|
|
pedal.sampleRate = engine.sampleRate
|
|
pedal.hostEngine = engine
|
|
pedal.clock = clock
|
|
|
|
let outLeft: AudioBus = try allocateInterNodeBus()
|
|
let outRight: AudioBus = try allocateInterNodeBus()
|
|
pedal.inputBus = tailLeft
|
|
pedal.inputBusRight = tailRight
|
|
pedal.outputBus = outLeft.index
|
|
pedal.outputBusRight = outRight.index
|
|
try insertNode(pedal)
|
|
tailLeft = outLeft.index
|
|
tailRight = outRight.index
|
|
pedals.append(pedal)
|
|
}
|
|
|
|
/// Adds the output node that copies the chain tail to the hardware outputs. Call once, after all
|
|
/// pedals have been added.
|
|
func finalize() throws {
|
|
let output: PostChainOutput = PostChainOutput(
|
|
leftInputBus: tailLeft,
|
|
rightInputBus: tailRight,
|
|
leftOutputBus: outputLeftBus,
|
|
rightOutputBus: outputRightBus
|
|
)
|
|
try insertNode(output)
|
|
outputNode = output
|
|
}
|
|
|
|
/// Removes every node this chain added and releases its inter-node buses. Call before the mix /
|
|
/// hardware buses it references are released.
|
|
func teardown() {
|
|
if let output: PostChainOutput = outputNode { engine.remove(output) }
|
|
outputNode = nil
|
|
for pedal: Pedal in pedals { engine.remove(pedal) }
|
|
pedals.removeAll()
|
|
for bus: AudioBus in allocatedBuses { engine.busAllocator.releaseAudioBus(bus) }
|
|
allocatedBuses.removeAll()
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func allocateInterNodeBus() throws -> AudioBus {
|
|
let bus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
allocatedBuses.append(bus)
|
|
return bus
|
|
}
|
|
|
|
private func insertNode(_ node: any Synthesizer) throws {
|
|
if let previous: any Synthesizer = lastNode {
|
|
try engine.add(node, position: .after(previous))
|
|
} else {
|
|
// Head node: tail of the main group → after every board master already there, and
|
|
// order-independent, so a board added later (spliced near the group head) still
|
|
// evaluates before this chain. No re-order needed on board add.
|
|
try engine.add(node, position: .tail(of: engine.mainGroup))
|
|
}
|
|
lastNode = node
|
|
}
|
|
}
|