f8878324fd
Mirror of Lyrebird dev HEAD (9f791c7c) minus maintainer-only paths. Patch release: fixes the allpass fractional-delay click (DelayLine/CombFilter/AllpassFilter/ DelayLineFeedback under a modulated delay) and lands the internal code-quality consolidation pass (~160 deduped sites + shared base classes/helpers, bit-identical output). No public API removed or changed. See RELEASES.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
238 lines
10 KiB
Swift
238 lines
10 KiB
Swift
// In.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/// Bus-read generators with audio-rate (`In.Audio`) and control-rate (`In.Control`) variants.
|
|
public enum In {
|
|
|
|
// MARK: - Shared base
|
|
|
|
public class _Base: GeneratorBase {
|
|
public let bus: Int
|
|
/// Number of channels (consecutive buses) this generator reads. Fixed at init.
|
|
public let channels: Int
|
|
|
|
public override var outputChannelCount: Int { channels }
|
|
|
|
init(
|
|
bus: Int,
|
|
channels: Int
|
|
) {
|
|
self.bus = bus
|
|
self.channels = channels
|
|
super.init(channels: channels)
|
|
}
|
|
}
|
|
|
|
// MARK: - Audio
|
|
|
|
/// Reads from one or more consecutive audio buses.
|
|
///
|
|
/// The `bus` parameter takes a plain `Int` index (1 channel) or an ``AudioBus`` allocation
|
|
/// (channel count taken from the bus). A ``ControlBus`` traps — `In.Audio` reads the audio-bus
|
|
/// pool; use ``In/Control`` for a control bus.
|
|
///
|
|
/// ```swift
|
|
/// In.Audio(bus: 5) // reads audioBuses[5], returns 1 channel
|
|
/// In.Audio(bus: myAudioBus) // reads all channels of the AudioBus allocation
|
|
/// In.Audio(bus: 4, channels: 2) // explicit multi-channel override
|
|
/// ```
|
|
///
|
|
/// `floatValue` always returns the value of the first channel (bus).
|
|
///
|
|
/// - Parameters:
|
|
/// - bus: Source bus — `Int` index (1 channel) or ``AudioBus`` (reads `channelCount`
|
|
/// consecutive buses). A ``ControlBus`` traps; use ``In/Control``.
|
|
/// - channels: Only available on the `Int`-index overload. Number of consecutive buses
|
|
/// to read. Defaults to `1`. Fixed at init — no RT allocation.
|
|
public final class Audio: _Base, AudioGenerator {
|
|
|
|
/// Reads from a ``ValidBus``.
|
|
///
|
|
/// - Parameters:
|
|
/// - bus: Source bus — `Int` index (1 channel) or ``AudioBus`` (uses `channelCount`).
|
|
/// A ``ControlBus`` traps; use ``In/Control``.
|
|
public init(bus: any ValidBus) {
|
|
_preconditionAudioRateBus(bus)
|
|
let ch: Int = _resolvedChannelCount(bus)
|
|
super.init(bus: bus.index, channels: ch)
|
|
}
|
|
|
|
/// Reads from a raw bus index with an explicit channel count.
|
|
///
|
|
/// - Parameters:
|
|
/// - bus: Raw audio bus index.
|
|
/// - channels: Number of consecutive buses to read. Fixed at init; no RT allocation.
|
|
public override init(
|
|
bus: Int,
|
|
channels: Int = 1
|
|
) {
|
|
super.init(bus: bus, channels: channels)
|
|
}
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
precondition(bus >= 0 && bus < graph.engine.numAudioBuses)
|
|
let offset: Int = graph.busOffset
|
|
precondition(offset + count <= graph.engine.blockSize)
|
|
for ch: Int in 0..<channels {
|
|
let busIdx: Int = bus + ch
|
|
guard busIdx < graph.engine.numAudioBuses else { break }
|
|
// No `let busSlice = ...[busIdx]` — that binding triggers an ARC retain on
|
|
// the inner [Float] storage on the RT thread. Calling `withUnsafeBufferPointer`
|
|
// directly on the subscript result lets the compiler emit an `_read` borrow
|
|
// access (no retain).
|
|
graph.engine.audioBuses[busIdx].withUnsafeBufferPointer { buf in
|
|
guard let base: UnsafePointer<Float> = buf.baseAddress else { return }
|
|
// Restore Array's bounds-check semantics that the raw-pointer path drops.
|
|
precondition(offset + count <= buf.count)
|
|
for i: Int in 0..<count {
|
|
_result[ch][i] = Double(base[offset + i])
|
|
}
|
|
}
|
|
}
|
|
return _result
|
|
}
|
|
|
|
public func floatValue(graph: SynthGraph) -> Double {
|
|
precondition(bus >= 0 && bus < graph.engine.numAudioBuses)
|
|
// Avoid the chained `[bus][offset]` subscript — the intermediate `[Float]`
|
|
// would retain the inner storage on the RT thread.
|
|
// Bus storage is pre-allocated at engine.start() and never empty — the
|
|
// baseAddress trap surfaces the unreachable empty-bus state rather than
|
|
// silently returning 0.
|
|
return graph.engine.audioBuses[bus].withUnsafeBufferPointer { buf in
|
|
guard let base: UnsafePointer<Float> = buf.baseAddress else {
|
|
preconditionFailure("In.Audio.floatValue: bus storage empty (engine not started?)")
|
|
}
|
|
return Double(base[graph.busOffset])
|
|
}
|
|
}
|
|
|
|
/// Creates a `GeneratorArray` of independent single-channel `In.Audio` generators,
|
|
/// one per consecutive bus starting at `bus`.
|
|
///
|
|
/// Use when you need separate generator instances that can be wired independently.
|
|
/// For a single multi-output generator prefer `In.Audio(bus:)`.
|
|
public static func multi(
|
|
bus: Int,
|
|
channels: Int
|
|
) -> GeneratorArray {
|
|
GeneratorArray((0..<channels).map { In.Audio(bus: bus + $0) })
|
|
}
|
|
|
|
/// Creates a `GeneratorArray` of independent single-channel generators, one per channel of `bus`.
|
|
/// Prefer `In.Audio(bus:)` when a single multi-output generator is sufficient.
|
|
///
|
|
/// Takes an `Int` index or an ``AudioBus``; a ``ControlBus`` traps (this reads the audio-bus
|
|
/// pool). The `Int` delegate below can't see the original typed bus, so the guard is applied
|
|
/// here before delegating.
|
|
public static func multi(bus: any ValidBus) -> GeneratorArray {
|
|
_preconditionAudioRateBus(bus)
|
|
let ch: Int = _resolvedChannelCount(bus)
|
|
return multi(bus: bus.index, channels: ch)
|
|
}
|
|
}
|
|
|
|
// MARK: - Control
|
|
|
|
/// Reads from one or more consecutive control buses and delivers interpolated audio-rate
|
|
/// ramps to downstream generators.
|
|
///
|
|
/// The `bus` parameter takes a plain `Int` index (1 channel) or a ``ControlBus`` allocation
|
|
/// (channel count taken from the bus). An ``AudioBus`` traps — `In.Control` reads the
|
|
/// control-bus pool; use ``In/Audio`` for an audio bus.
|
|
///
|
|
/// ```swift
|
|
/// In.Control(bus: 10) // reads controlBuses[10], interpolated ramp
|
|
/// In.Control(bus: myCtrlBus) // reads all channels of the ControlBus allocation
|
|
/// In.Control(bus: 10, channels: 3) // explicit multi-channel override
|
|
/// ```
|
|
///
|
|
/// Each channel maintains its own ramp state (`previousOutputValue` exposes channel 0).
|
|
///
|
|
/// ## RT-allocation model
|
|
/// `_result` and `_previousOutputValues` are pre-allocated in `init`. No heap allocation
|
|
/// in steady state.
|
|
///
|
|
/// - Parameters:
|
|
/// - bus: Source bus — `Int` index (1 channel) or ``ControlBus`` (reads `channelCount`
|
|
/// consecutive buses with independent ramp state per channel). An ``AudioBus`` traps; use ``In/Audio``.
|
|
/// - channels: Only available on the `Int`-index overload. Number of consecutive buses
|
|
/// to read. Defaults to `1`. Fixed at init — no RT allocation.
|
|
public final class Control: _Base, BlockRateControlGenerator {
|
|
|
|
/// Per-channel ramp state. Updated by `_computeSamples` after each block.
|
|
private var _previousOutputValues: [Double]
|
|
|
|
/// Ramp start for channel 0 — satisfies `ControlGenerator` protocol.
|
|
/// For multi-channel use, each channel's state is in `_previousOutputValues`.
|
|
public var previousOutputValue: Double {
|
|
get { _previousOutputValues[0] }
|
|
set { _previousOutputValues[0] = newValue }
|
|
}
|
|
|
|
/// Reads from a ``ValidBus``.
|
|
///
|
|
/// - Parameters:
|
|
/// - bus: Source bus — `Int` index (1 channel) or ``ControlBus`` (uses `channelCount`).
|
|
/// An ``AudioBus`` traps; use ``In/Audio``.
|
|
public init(bus: any ValidBus) {
|
|
_preconditionControlRateBus(bus)
|
|
let ch: Int = _resolvedChannelCount(bus)
|
|
_previousOutputValues = [Double](repeating: 0.0, count: ch)
|
|
super.init(bus: bus.index, channels: ch)
|
|
}
|
|
|
|
/// Reads from a raw bus index with an explicit channel count.
|
|
///
|
|
/// - Parameters:
|
|
/// - bus: Raw control bus index.
|
|
/// - channels: Number of consecutive buses to read. Fixed at init; no RT allocation.
|
|
public override init(
|
|
bus: Int,
|
|
channels: Int = 1
|
|
) {
|
|
_previousOutputValues = [Double](repeating: 0.0, count: channels)
|
|
super.init(bus: bus, channels: channels)
|
|
}
|
|
|
|
public func floatValue(graph: SynthGraph) -> Double {
|
|
precondition(bus >= 0 && bus < graph.engine.numControlBuses)
|
|
return Double(graph.engine.controlBuses[bus])
|
|
}
|
|
|
|
|
|
/// Produces a linearly-interpolated block for each channel, ramping from the previous
|
|
/// block's end value to the current control bus value.
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
for ch: Int in 0..<channels {
|
|
let busIdx: Int = bus + ch
|
|
guard busIdx < graph.engine.numControlBuses else { break }
|
|
let target: Double = Double(graph.engine.controlBuses[busIdx])
|
|
let prev: Double = _previousOutputValues[ch]
|
|
let step: Double = (target - prev) / Double(count)
|
|
for i: Int in 0..<count {
|
|
_result[ch][i] = prev + step * Double(i + 1)
|
|
}
|
|
_previousOutputValues[ch] = target
|
|
}
|
|
return _result
|
|
}
|
|
}
|
|
}
|
|
|
|
extension In: GeneratorEnum {
|
|
public typealias AudioType = In.Audio
|
|
public typealias ControlType = In.Control
|
|
}
|