Files
Lyrebird/Sources/Lyrebird/Buses/ValidBus.swift
T

89 lines
4.3 KiB
Swift

// ValidBus.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
/// A type that can identify a bus by index and channel count.
///
/// `Int`, ``AudioBus``, and ``ControlBus`` all conform. Pass any of these wherever a
/// bus parameter is accepted — `In.Audio`, `In.Control`, `Out.Audio`, `Out.Control`, and
/// the ``SynthesizerBase`` convenience helpers (`output(bus:source:)`,
/// `controlOutput(bus:source:)`, `input(bus:)`, `controlInput(bus:)`).
///
/// ## Channel-count semantics
///
/// | Type | `channelCount` | Effect |
/// |---|---|---|
/// | `Int` | `Int.max` | No clamping for writes; reads default to 1 channel |
/// | `AudioBus` | explicit | Writes clamped to that many channels; reads use that channel count |
/// | `ControlBus` | explicit | Writes clamped to that many channels; reads use that channel count |
///
/// A plain `Int` bus carries no channel-count intent — write all source channels through,
/// read single channel. Pass an ``AudioBus`` or ``ControlBus`` allocation when you want
/// the channel count enforced automatically.
public protocol ValidBus {
/// Raw bus index used by the engine.
var index: Int { get }
/// Number of channels in this bus block.
/// `Int.max` means "no explicit count" — only plain `Int` buses return this value.
var channelCount: Int { get }
}
extension Int: ValidBus {
public var index: Int { self }
/// Plain `Int` bus carries no channel-count intent. Default to 1
public var channelCount: Int { Int.max }
}
/// Resolves the channel count for reads and buffer pre-allocation.
/// A plain `Int` bus (channelCount == `Int.max`) resolves to 1. All other buses use their
/// explicit `channelCount`, clamped to a minimum of 1.
@inline(__always)
func _resolvedChannelCount(_ bus: any ValidBus) -> Int {
bus.channelCount == Int.max ? 1 : max(1, bus.channelCount)
}
/// Traps at graph-build time when a ``ControlBus`` is handed to an **audio**-rate bus generator
/// (`Out.Audio` / `ReplaceOut.Audio` / `In.Audio`).
///
/// Those generators address the **audio**-bus pool by index. A `ControlBus`'s `index` lives in the
/// separate control-bus index space, so writing/reading it through an audio-rate generator hits
/// audio memory at that number — silently colliding with unrelated audio buses (including the
/// hardware output buses at low indices). There is no correct use for it: reach for the control-rate
/// variant (`Out.Control` / `ReplaceOut.Control` / `In.Control`, or `SynthesizerBase`'s
/// `controlOutput(bus:source:)` / `controlInput(bus:)`).
///
/// Runs only at `init` (graph construction, main thread) — never on the RT render path. A plain
/// `Int` bus is the deliberate raw-index escape hatch and is intentionally not checked.
@inline(__always)
func _preconditionAudioRateBus(_ bus: any ValidBus) {
precondition(
!(bus is ControlBus),
"A ControlBus was passed to an audio-rate bus generator (Out.Audio / ReplaceOut.Audio / "
+ "In.Audio), which addresses the audio-bus pool at that index and collides with unrelated "
+ "audio buses. Use the control-rate variant (Out.Control / ReplaceOut.Control / In.Control, "
+ "or controlOutput(bus:source:) / controlInput(bus:))."
)
}
/// Traps at graph-build time when an ``AudioBus`` is handed to a **control**-rate bus generator
/// (`Out.Control` / `ReplaceOut.Control` / `In.Control`).
///
/// The mirror of ``_preconditionAudioRateBus(_:)``: control-rate generators address the
/// **control**-bus pool by index, so an `AudioBus`'s `index` reaches control memory at that number
/// and collides with unrelated control buses. Use the audio-rate variant (`Out.Audio` /
/// `ReplaceOut.Audio` / `In.Audio`, or `output(bus:source:)` / `input(bus:)`). `Int` buses are not
/// checked. `init`-time only, never RT.
@inline(__always)
func _preconditionControlRateBus(_ bus: any ValidBus) {
precondition(
!(bus is AudioBus),
"An AudioBus was passed to a control-rate bus generator (Out.Control / ReplaceOut.Control / "
+ "In.Control), which addresses the control-bus pool at that index and collides with "
+ "unrelated control buses. Use the audio-rate variant (Out.Audio / ReplaceOut.Audio / "
+ "In.Audio, or output(bus:source:) / input(bus:))."
)
}