// 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 } }