// Merger.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Sums two branch buses to a single output bus with independent per-branch levels. /// /// Intended as the downstream companion to ``Splitter`` when combining two parallel /// signal paths back into a single chain. Each branch contributes its signal scaled /// by its level parameter; the two scaled signals are summed into `outputBus`. /// /// ## Signal graph /// /// ``` /// inputBusA → In.Audio × levelA ──┐ /// + → outputBus /// inputBusB → In.Audio × levelB ──┘ /// ``` /// /// ## Parameters /// /// - `levelA`: Gain applied to branch A before summing. `1.0` = unity gain, `0.0` = muted. /// - `levelB`: Gain applied to branch B before summing. `1.0` = unity gain, `0.0` = muted. /// /// > Note: With both defaults at `1.0`, two branches carrying the same signal sum to /// > approximately 2× amplitude. In normal use the ``Splitter`` routes independent bus /// > paths to each branch, so they carry different signals and this doubling does not /// > occur. For an empty (pass-through) branch with `levelA = levelB = 1.0`, lower /// > one of the levels or attenuate in the signal chain to avoid the additive boost. /// /// - Parameters: /// - inputBusA: Audio bus index from which branch A's signal is read. /// - inputBusB: Audio bus index from which branch B's signal is read. /// - outputBus: Audio bus index to which the summed output is written. public final class Merger: SynthesizerBase { // MARK: - Parameters /// Level for branch A. `1.0` = unity gain, `0.0` = muted. @UIParam public var levelA: Double = 1.0 /// Level for branch B. `1.0` = unity gain, `0.0` = muted. @UIParam public var levelB: Double = 1.0 // MARK: - Routing /// Audio bus index from which branch A's signal is read. public internal(set) var inputBusA: Int /// Audio bus index from which branch B's signal is read. public internal(set) var inputBusB: Int /// Audio bus index to which the summed output is written. public internal(set) var outputBus: Int // MARK: - Init /// Creates a merger that sums two branch buses into one output bus. /// /// - Parameters: /// - inputBusA: Audio bus index from which branch A's signal is read. /// - inputBusB: Audio bus index from which branch B's signal is read. /// - outputBus: Audio bus index to which the summed output is written. public init( inputBusA: Int, inputBusB: Int, outputBus: Int ) { self.inputBusA = inputBusA self.inputBusB = inputBusB self.outputBus = outputBus super.init() } // MARK: - Build /// Builds the signal graph: (inputBusA × levelA) + (inputBusB × levelB) → outputBus. public override func build() { let sigA: In.Audio = input(bus: inputBusA) let sigB: In.Audio = input(bus: inputBusB) output(bus: outputBus, source: (sigA * $levelA) + (sigB * $levelB)) } }