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>
204 lines
8.9 KiB
Swift
204 lines
8.9 KiB
Swift
// Out.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/// Bus-write generators with audio-rate (`Out.Audio`) and control-rate (`Out.Control`) variants.
|
|
///
|
|
/// Both variants conform to ``RootRegistering``. Constructed inside
|
|
/// ``SynthesizerBase/build()`` they auto-register as synth roots — no explicit
|
|
/// ``SynthesizerBase/addOutputGenerator(_:)`` or ``SynthesizerBase/output(bus:source:)``
|
|
/// call is required and the return value may be discarded.
|
|
public enum Out {
|
|
|
|
// MARK: - Shared base
|
|
|
|
public class _Base: GeneratorBase {
|
|
public let bus: Int
|
|
let source: any ValidGeneratorInput
|
|
/// Maximum channels to write. `Int.max` = unlimited (plain `Int` bus — write all source channels).
|
|
/// Set to `bus.channelCount` when constructed from an ``AudioBus`` or ``ControlBus``.
|
|
let _maxChannels: Int
|
|
|
|
init(
|
|
bus: Int,
|
|
source: any ValidGeneratorInput,
|
|
maxChannels: Int = Int.max,
|
|
channels: Int = 1
|
|
) {
|
|
let resolvedSource: any ValidGeneratorInput = source._resolvedInput()
|
|
self.bus = bus
|
|
self.source = resolvedSource
|
|
self._maxChannels = maxChannels
|
|
super.init(channels: channels)
|
|
}
|
|
}
|
|
|
|
// MARK: - Audio
|
|
|
|
/// Accumulates into one or more consecutive audio buses using +=.
|
|
/// Multiple ``Synthesizer`` instances writing to the same bus mix additively.
|
|
///
|
|
/// The `bus` parameter takes a plain `Int` index or an ``AudioBus`` allocation (writes are
|
|
/// clamped to `bus.channelCount` channels). Passing a ``ControlBus`` traps — an audio `Out`
|
|
/// addresses the audio-bus pool, so use ``Out/Control`` for a control bus.
|
|
///
|
|
/// ## Multi-channel sources
|
|
///
|
|
/// When `source` returns N channels, `Out.Audio` writes channel 0 to `bus.index`,
|
|
/// channel 1 to `bus.index + 1`, ..., channel N-1 to `bus.index + N - 1`.
|
|
/// Mono sources behave identically to single-bus usage.
|
|
///
|
|
/// ```swift
|
|
/// output(bus: 0, source: Pan2(input: osc, pan: 0.0)) // bus 0 = left, bus 1 = right
|
|
/// output(bus: myAudioBus, source: Pan2(input: osc, pan: 0.0)) // clamped to channelCount
|
|
/// output(bus: 0, source: expand([440.0, 880.0]) { OscSin.Audio(freq: $0) })
|
|
/// ```
|
|
///
|
|
/// - Parameters:
|
|
/// - bus: Destination bus — `Int` index (no channel clamping) or ``AudioBus`` (clamped to
|
|
/// `channelCount`). A ``ControlBus`` traps; use ``Out/Control`` instead.
|
|
/// - source: Signal to write. Multi-channel sources write to consecutive buses starting at
|
|
/// `bus.index`.
|
|
public final class Audio: _Base, AudioGenerator, RootRegistering {
|
|
|
|
/// `Out.Audio` returns `source.calculatedSamples` directly (pass-through), so its
|
|
/// effective output channel count is `source.outputChannelCount`. Read at init-time
|
|
/// only when `Out.Audio` is used as a `BinaryOpGenerator` operand — never on the RT
|
|
/// thread. Non-`Generator` sources (`Double`, `Int`, `Closure`) report 1.
|
|
public override var outputChannelCount: Int {
|
|
(source as? any Generator)?.outputChannelCount ?? 1
|
|
}
|
|
|
|
/// - Parameters:
|
|
/// - bus: Destination bus — `Int` index or ``AudioBus`` (writing clamped to
|
|
/// `bus.channelCount`). A ``ControlBus`` traps; use ``Out/Control`` instead.
|
|
/// - source: Signal to write. Multi-channel sources write to consecutive buses.
|
|
public init(
|
|
bus: any ValidBus,
|
|
source: any ValidGeneratorInput
|
|
) {
|
|
_preconditionAudioRateBus(bus)
|
|
super.init(bus: bus.index, source: source, maxChannels: bus.channelCount)
|
|
_registerAsRootIfBuilding()
|
|
}
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
let sourceSamples: [[Double]] = source.calculatedSamples(graph: graph, count: count)
|
|
let offset: Int = graph.busOffset
|
|
precondition(offset + count <= graph.engine.blockSize)
|
|
for (ch, samples) in sourceSamples.enumerated() {
|
|
guard ch < _maxChannels else { break }
|
|
let targetBus: Int = bus + ch
|
|
guard targetBus >= 0 && targetBus < graph.engine.numAudioBuses else { continue }
|
|
guard samples.count >= count else { continue }
|
|
graph.addSamplesToAudioBus(targetBus, offset: offset, samples: samples, count: count)
|
|
}
|
|
return sourceSamples
|
|
}
|
|
|
|
public func floatValue(graph: SynthGraph) -> Double { 0 }
|
|
}
|
|
|
|
// MARK: - Control
|
|
|
|
/// Accumulates into one or more consecutive control buses using +=.
|
|
///
|
|
/// `Out.Control` is a bus-write effector, not a signal producer — `calculatedSamples`
|
|
/// performs the bus accumulation and returns the source value as a passthrough.
|
|
/// Does not use the default ControlGenerator interpolation (no ramp is appropriate for
|
|
/// a sink whose output is a control bus write). `previousOutputValue` is required by
|
|
/// the ControlGenerator protocol but is not used.
|
|
///
|
|
/// The `bus` parameter takes a plain `Int` index or a ``ControlBus`` allocation (writes are
|
|
/// clamped to `bus.channelCount` channels). Passing an ``AudioBus`` traps — a control `Out`
|
|
/// addresses the control-bus pool, so use ``Out/Audio`` for an audio bus.
|
|
///
|
|
/// ## Multi-channel sources
|
|
///
|
|
/// When `source` returns N channels, `Out.Control` writes channel 0 to `bus.index`,
|
|
/// channel 1 to `bus.index + 1`, ..., channel N-1 to `bus.index + N - 1`. Mono sources
|
|
/// behave identically to single-bus usage.
|
|
///
|
|
/// ```swift
|
|
/// Out.Control(bus: 10, source: lfo) // single-channel write to controlBuses[10]
|
|
/// Out.Control(bus: myCtrlBus, source: stereoSrc) // clamped to myCtrlBus.channelCount
|
|
/// ```
|
|
///
|
|
/// - Parameters:
|
|
/// - bus: Destination bus — `Int` index (1 channel, no clamping) or ``ControlBus`` (clamped
|
|
/// to `channelCount`). An ``AudioBus`` traps; use ``Out/Audio`` instead.
|
|
/// - source: Signal to write. The last sample of each channel block is accumulated into
|
|
/// the corresponding control bus. Multi-channel sources write to consecutive buses.
|
|
public final class Control: _Base, BlockRateControlGenerator, RootRegistering {
|
|
|
|
/// Satisfies ControlGenerator protocol requirement — unused; Control conforms to `BlockRateControlGenerator`.
|
|
public var previousOutputValue: Double = 0.0
|
|
|
|
/// Number of channels to write. Governs `_result` row count.
|
|
private let _channels: Int
|
|
|
|
|
|
public override var outputChannelCount: Int { _channels }
|
|
|
|
/// - Parameters:
|
|
/// - bus: Destination bus — `Int` index or ``ControlBus`` (writing clamped to
|
|
/// `bus.channelCount`). An ``AudioBus`` traps; use ``Out/Audio`` instead.
|
|
/// A plain `Int` writes a single channel.
|
|
/// - source: Signal to write. Multi-channel sources write to consecutive buses.
|
|
public init(
|
|
bus: any ValidBus,
|
|
source: any ValidGeneratorInput
|
|
) {
|
|
_preconditionControlRateBus(bus)
|
|
let ch: Int = _resolvedChannelCount(bus)
|
|
self._channels = ch
|
|
super.init(bus: bus.index, source: source, maxChannels: bus.channelCount, channels: ch)
|
|
_registerAsRootIfBuilding()
|
|
}
|
|
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
let sourceSamples: [[Double]] = source.calculatedSamples(graph: graph, count: count)
|
|
for ch: Int in 0..<_channels {
|
|
let busIdx: Int = bus + ch
|
|
guard busIdx >= 0 && busIdx < graph.engine.numControlBuses else {
|
|
for i: Int in 0..<count { _result[ch][i] = 0 }
|
|
continue
|
|
}
|
|
let row: [Double]
|
|
if ch < sourceSamples.count, !sourceSamples[ch].isEmpty {
|
|
row = sourceSamples[ch]
|
|
} else if let first: [Double] = sourceSamples.first, !first.isEmpty {
|
|
row = first
|
|
} else {
|
|
for i: Int in 0..<count { _result[ch][i] = 0 }
|
|
continue
|
|
}
|
|
let val: Double = row[min(row.count - 1, count - 1)]
|
|
graph.engine.addToControlBus(busIdx, value: Float(val))
|
|
for i: Int in 0..<count { _result[ch][i] = val }
|
|
}
|
|
return _result
|
|
}
|
|
|
|
public func floatValue(graph: SynthGraph) -> Double {
|
|
Double(graph.engine.controlBuses[bus])
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Out: GeneratorEnum {
|
|
public typealias AudioType = Out.Audio
|
|
public typealias ControlType = Out.Control
|
|
}
|