130 lines
4.5 KiB
Swift
130 lines
4.5 KiB
Swift
// Splitter.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import LyrebirdSynth
|
||
import LyrebirdUI
|
||
|
||
/// Routes one input bus to two independent output buses with click-free per-branch enables.
|
||
///
|
||
/// Both branches read the same input signal. Each branch level is lagged through a
|
||
/// ``Lag.Control`` so enable/disable transitions are artefact-free (no abrupt cuts).
|
||
///
|
||
/// Use ``mode`` as a convenience setter to configure common routing patterns:
|
||
/// - `.a` → branch A on, branch B muted
|
||
/// - `.b` → branch B on, branch A muted
|
||
/// - `.both` → both branches active (full parallel routing)
|
||
///
|
||
/// ## Signal graph
|
||
///
|
||
/// ```
|
||
/// inputBus → In.Audio
|
||
/// ├── × Lag(branchAEnabled) → outputBusA
|
||
/// └── × Lag(branchBEnabled) → outputBusB
|
||
/// ```
|
||
///
|
||
/// ## Parameters
|
||
///
|
||
/// - `branchAEnabled`: Branch A level. `1.0` = passing, `0.0` = muted.
|
||
/// - `branchBEnabled`: Branch B level. `1.0` = passing, `0.0` = muted.
|
||
///
|
||
/// - Parameters:
|
||
/// - inputBus: Audio bus index to read the input signal from.
|
||
/// - outputBusA: Audio bus index to write branch A's output to.
|
||
/// - outputBusB: Audio bus index to write branch B's output to.
|
||
public final class Splitter: SynthesizerBase {
|
||
|
||
// MARK: - Parameters
|
||
|
||
/// Branch A level. `1.0` = passing, `0.0` = muted. Transitions are click-free.
|
||
@UIParam public var branchAEnabled: Double = 1.0
|
||
|
||
/// Branch B level. `1.0` = passing, `0.0` = muted. Transitions are click-free.
|
||
@UIParam public var branchBEnabled: Double = 1.0
|
||
|
||
// MARK: - Routing
|
||
|
||
/// Audio bus index from which the input signal is read.
|
||
public internal(set) var inputBus: Int
|
||
|
||
/// Audio bus index to which branch A's output is written.
|
||
public internal(set) var outputBusA: Int
|
||
|
||
/// Audio bus index to which branch B's output is written.
|
||
public internal(set) var outputBusB: Int
|
||
|
||
// MARK: - Constants
|
||
|
||
/// Lag time in seconds for branch enable transitions.
|
||
/// Prevents audible clicks when toggling a branch on or off.
|
||
private let switchLagTime: Double = 0.02
|
||
|
||
/// Threshold above which an enable value is treated as "on" for ``mode`` getter.
|
||
private let enableThreshold: Double = 0.5
|
||
|
||
// MARK: - Init
|
||
|
||
/// Creates a splitter that routes one input bus to two branch buses.
|
||
///
|
||
/// - Parameters:
|
||
/// - inputBus: Audio bus index to read the input signal from.
|
||
/// - outputBusA: Audio bus index to write branch A's output to.
|
||
/// - outputBusB: Audio bus index to write branch B's output to.
|
||
public init(
|
||
inputBus: Int,
|
||
outputBusA: Int,
|
||
outputBusB: Int
|
||
) {
|
||
self.inputBus = inputBus
|
||
self.outputBusA = outputBusA
|
||
self.outputBusB = outputBusB
|
||
super.init()
|
||
}
|
||
|
||
// MARK: - Mode convenience
|
||
|
||
/// Routing mode derived from the current branch enable values.
|
||
///
|
||
/// - Getting: Returns `.both` if both enables are ≥ 0.5; `.a` if only branch A
|
||
/// is ≥ 0.5; `.b` if only branch B is ≥ 0.5. Returns `.a` if both are below 0.5
|
||
/// (neither branch active — `.a` is the least-surprising fallback).
|
||
/// - Setting: Updates `branchAEnabled` and `branchBEnabled` to match the chosen mode.
|
||
/// `.a` → (1, 0); `.b` → (0, 1); `.both` → (1, 1). Transitions are click-free.
|
||
public var mode: SplitMode {
|
||
get {
|
||
let aOn: Bool = branchAEnabled >= enableThreshold
|
||
let bOn: Bool = branchBEnabled >= enableThreshold
|
||
if aOn && bOn { return .both }
|
||
if aOn { return .a }
|
||
if bOn { return .b }
|
||
return .a
|
||
}
|
||
set {
|
||
switch newValue {
|
||
case .a:
|
||
branchAEnabled = 1.0
|
||
branchBEnabled = 0.0
|
||
case .b:
|
||
branchAEnabled = 0.0
|
||
branchBEnabled = 1.0
|
||
case .both:
|
||
branchAEnabled = 1.0
|
||
branchBEnabled = 1.0
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - Build
|
||
|
||
/// Builds the signal graph: input → lagged per-branch gain → two output buses.
|
||
public override func build() {
|
||
let inSig: In.Audio = input(bus: inputBus)
|
||
let gainA: Lag.Control = Lag.Control(input: $branchAEnabled, time: switchLagTime)
|
||
let gainB: Lag.Control = Lag.Control(input: $branchBEnabled, time: switchLagTime)
|
||
output(bus: outputBusA, source: inSig * gainA)
|
||
output(bus: outputBusB, source: inSig * gainB)
|
||
}
|
||
}
|