// 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 /// 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 } // 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? // MARK: - Constants /// Pan position for the board in the stereo field. Centre = 0.0. Used only for a mono board. private let stereoPan: Double = 0.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). init(boardOutputBusIndex: Int, boardOutputBusRightIndex: Int? = nil) { self.boardOutputBusIndex = boardOutputBusIndex self.boardOutputBusRightIndex = boardOutputBusRightIndex 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) let boardOutLeft: In.Audio = input(bus: boardOutputBusIndex) let gainedLeft: any ValidGeneratorInput = (boardOutLeft * smoothedSelect) * $masterGain 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 gainedRight: any ValidGeneratorInput = (boardOutRight * smoothedSelect) * $masterGain output(bus: 0, source: gainedLeft) output(bus: 1, source: gainedRight) } else { // Mono board: Pan2 spreads the mono output to stereo hardware buses 0 + 1. output(bus: 0, source: Pan2(input: gainedLeft, pan: stereoPan)) } } }