Files
josh 52c676605f 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>
2026-11-26 00:22:39 -08:00

186 lines
8.9 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// BoardOutputMaster.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Internal synthesizer that reads a pedalboard's output bus, applies a master
/// gain, pans it to stereo, and writes to hardware output buses 0 (left) and 1 (right).
///
/// Ordered `.after` the last pedal in the board's chain so it evaluates after the
/// full signal chain is complete.
final class BoardOutputMaster: SynthesizerBase {
// MARK: - Parameters
/// Master output gain in [0, 1]. The user-facing volume knob; applied directly.
@UIParam var masterGain: Double = 0.8
/// Board-select gain in [0, 1]. `1` = this board is audible, `0` = silenced.
/// Driven by `ShoeGaze.selectBoard(_:crossfadeSeconds:)`; lagged by `crossfadeTime`
/// so switching boards crossfades instead of clicking. Defaults to `1` (audible).
@UIParam var selectGain: Double = 1.0
/// Lag time in seconds for `selectGain` transitions — the board crossfade duration.
/// Defaults to a short 20 ms so normal use is click-free; raise it for a slow crossfade.
@UIParam var crossfadeTime: Double = 0.02
/// Per-board full-bypass state. `0` = board engaged (processed output), `1` = bypassed
/// (dry input passes straight through, skipping every pedal). Smoothed by `bypassLagTime`.
@UIParam var boardBypass: Double = 0.0
/// Rig-wide full-bypass state, driven across every board at once. `0` = engaged,
/// `1` = bypassed (dry). Independent of `boardBypass`; either engaged routes to dry.
@UIParam var rigBypass: Double = 0.0
/// The live `UIParamNode` backing `masterGain`, exposed so the board (and its UI)
/// can bind a master-volume knob to this stage.
var gainNode: UIParamNode { $masterGain }
/// The live `UIParamNode` backing `selectGain` (board-select crossfade gain).
var selectGainNode: UIParamNode { $selectGain }
/// The live `UIParamNode` backing `crossfadeTime` (board crossfade seconds).
var crossfadeTimeNode: UIParamNode { $crossfadeTime }
/// The live `UIParamNode` backing `boardBypass` (per-board full bypass).
var boardBypassNode: UIParamNode { $boardBypass }
/// The live `UIParamNode` backing `rigBypass` (rig-wide full bypass).
var rigBypassNode: UIParamNode { $rigBypass }
/// Rig-wide output volume in [0, 1], applied to the final output. Driven across every board by
/// `ShoeGaze.setRigVolume(_:)`. Defaults to `1` (unity).
@UIParam var rigVolume: Double = 1.0
/// Rig-wide output mute — `0` = audible, `1` = silenced (a hard gate, not a dry bypass). Driven
/// across every board by `ShoeGaze.setRigMuted(_:)`. Defaults to `0`.
@UIParam var rigMute: Double = 0.0
/// The live `UIParamNode` backing `rigVolume` (rig-wide output volume).
var rigVolumeNode: UIParamNode { $rigVolume }
/// The live `UIParamNode` backing `rigMute` (rig-wide output mute).
var rigMuteNode: UIParamNode { $rigMute }
// MARK: - Routing
/// Left / mono board output bus index.
private let boardOutputBusIndex: Int
/// Right board output bus index when the board tail is stereo (width 2), else `nil`.
/// When set, the left bus drives hardware 0 and the right bus drives hardware 1 directly,
/// bypassing the mono `Pan2` spread.
private let boardOutputBusRightIndex: Int?
/// The board's dry head-bus index (the mono input feeding the first pedal). Read as the
/// bypass source so a bypassed board passes its untouched input straight through.
private let dryInputBusIndex: Int
/// Destination buses for the master's stereo output — hardware 0 / 1 by default. When a
/// post-board chain exists, the masters are pointed at a shared **mix bus** instead, and the
/// post-chain reads that mix and writes hardware. All boards share the same destination.
private let outputLeftBusIndex: Int
private let outputRightBusIndex: Int
// MARK: - Constants
/// Lag time constant in seconds for the bypass crossfades — smooths engaged ↔ bypassed
/// transitions to prevent clicks. Matches the per-pedal bypass lag.
private let bypassLagTime: Double = 0.02
/// Remaps a `[0, 1]` bypass value to CrossFade's `[-1, +1]` equal-power pan.
private let bipolarSpan: Double = 2.0
private let bipolarOffset: Double = 1.0
// MARK: - Init
/// Creates a board output master.
///
/// - Parameters:
/// - boardOutputBusIndex: Left / mono board output bus index.
/// - boardOutputBusRightIndex: Right board output bus index for a stereo (width-2) board,
/// or `nil` for a mono board (the signal is panned to centre instead).
/// - dryInputBusIndex: The board's head-bus index, read as the dry bypass source.
init(
boardOutputBusIndex: Int,
boardOutputBusRightIndex: Int? = nil,
dryInputBusIndex: Int,
outputLeftBusIndex: Int = 0,
outputRightBusIndex: Int = 1
) {
self.boardOutputBusIndex = boardOutputBusIndex
self.boardOutputBusRightIndex = boardOutputBusRightIndex
self.dryInputBusIndex = dryInputBusIndex
self.outputLeftBusIndex = outputLeftBusIndex
self.outputRightBusIndex = outputRightBusIndex
super.init()
}
// MARK: - Build
override func build() {
// Lag the board-select gain so switching boards crossfades over `crossfadeTime`
// seconds rather than clicking. The user master-gain knob is applied directly.
let smoothedSelect: Lag.Control = Lag.Control(input: $selectGain, time: $crossfadeTime)
// Rig-wide output volume × mute, shared across both channels (like `smoothedSelect`).
let rigOutputGain: any ValidGeneratorInput = rigOutputGainStage()
// The dry input tap — read once, used as the bypass source for both channels (the head
// bus is mono, so a bypassed stereo board passes the same dry signal to both sides).
let dry: In.Audio = input(bus: dryInputBusIndex)
let boardOutLeft: In.Audio = input(bus: boardOutputBusIndex)
let switchedLeft: any ValidGeneratorInput = applyBypass(wet: boardOutLeft, dry: dry)
let gainedLeft: any ValidGeneratorInput =
((switchedLeft * smoothedSelect) * $masterGain) * rigOutputGain
if let rightIndex: Int = boardOutputBusRightIndex {
// Stereo board: left tail → hardware 0, right tail → hardware 1, no pan spread.
let boardOutRight: In.Audio = input(bus: rightIndex)
let switchedRight: any ValidGeneratorInput = applyBypass(wet: boardOutRight, dry: dry)
let gainedRight: any ValidGeneratorInput =
((switchedRight * smoothedSelect) * $masterGain) * rigOutputGain
output(bus: outputLeftBusIndex, source: gainedLeft)
output(bus: outputRightBusIndex, source: gainedRight)
} else {
// Mono board: write the mono signal to both hardware buses at unity (0 dB). Using an
// equal-power `Pan2` to centre would drop each channel by 3 dB (×0.707) — power-correct
// for a true pan, but a mono guitar rig should come out at the same level it went in, so
// the signal is copied to left and right directly instead.
output(bus: outputLeftBusIndex, source: gainedLeft)
output(bus: outputRightBusIndex, source: gainedLeft)
}
}
/// Rig-wide output gain: the smoothed rig volume gated by the smoothed mute. Mute `0` → gain 1
/// (audible), mute `1` → gain 0 (silent). Both lagged to avoid clicks.
private func rigOutputGainStage() -> any ValidGeneratorInput {
let rigVol: Lag.Control = Lag.Control(input: $rigVolume, time: bypassLagTime)
let muteLagged: Lag.Control = Lag.Control(input: $rigMute, time: bypassLagTime)
// (mute − 1) × −1 ⇒ 0→1, 1→0, using only generator−scalar and generator×scalar.
let muteGain: any ValidGeneratorInput = (muteLagged - 1.0) * -1.0
return rigVol * muteGain
}
/// Routes `wet` to `dry` when either the per-board or rig-wide bypass is engaged. Two cascaded
/// equal-power crossfades (each lagged to prevent clicks) give an independent "OR" of the two
/// bypass sources: rig bypass overrides even a board that is otherwise engaged.
private func applyBypass(
wet: any ValidGeneratorInput,
dry: any ValidGeneratorInput
) -> any ValidGeneratorInput {
let boardLagged: Lag.Control = Lag.Control(input: $boardBypass, time: bypassLagTime)
let boardPan: any ValidGeneratorInput = (boardLagged * bipolarSpan) - bipolarOffset
let afterBoard: CrossFade.Audio = CrossFade.Audio(inA: wet, inB: dry, pan: boardPan)
let rigLagged: Lag.Control = Lag.Control(input: $rigBypass, time: bypassLagTime)
let rigPan: any ValidGeneratorInput = (rigLagged * bipolarSpan) - bipolarOffset
return CrossFade.Audio(inA: afterBoard, inB: dry, pan: rigPan)
}
}