47 lines
1.9 KiB
Swift
47 lines
1.9 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)
|
|
}
|
|
|