// 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 private let boardOutputBusIndex: Int // MARK: - Constants /// Pan position for the board in the stereo field. Centre = 0.0. private let stereoPan: Double = 0.0 // MARK: - Init init(boardOutputBusIndex: Int) { self.boardOutputBusIndex = boardOutputBusIndex super.init() } // MARK: - Build override func build() { let boardOut: In.Audio = input(bus: boardOutputBusIndex) // 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 gained: any ValidGeneratorInput = (boardOut * smoothedSelect) * $masterGain // Pan2 spreads the mono pedalboard output to stereo hardware buses 0 + 1. output(bus: 0, source: Pan2(input: gained, pan: stereoPan)) } }