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>
101 lines
5.0 KiB
Swift
101 lines
5.0 KiB
Swift
// GeneratorBase.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/// Concrete base class for all Lyrebird generators.
|
|
///
|
|
/// Provides the four stored properties required by the `Generator` and `ControlGenerator`
|
|
/// protocols, eliminating boilerplate across every conformer. Subclass `GeneratorBase` (or
|
|
/// use a `_Base: GeneratorBase` pattern) instead of declaring these properties manually.
|
|
///
|
|
/// ## Properties provided
|
|
/// - `_segmentSerial` / `_cachedResult` — fan-out cache state (`Generator` protocol)
|
|
/// - `_subBlockBuffer` — sub-block assembly buffer (`ControlGenerator` protocol)
|
|
/// - `_result` — pre-allocated, zero-filled mono output buffer
|
|
/// (`lyrebirdDefaultResultBufferSize` samples)
|
|
///
|
|
/// **Mono generators** simply inherit `_result` — no declaration, no init line needed.
|
|
/// **Multi-row generators** call `super.init(channels: N)` from their designated `init`
|
|
/// to allocate N rows in one shot — no `_result` redeclaration in the subclass, no
|
|
/// post-super reassignment. `outputChannelCount` must be overridden to return the
|
|
/// fixed channel count.
|
|
///
|
|
/// `Audio` conformers inherit `_subBlockBuffer` as `[[]]` (no allocation); sub-blocking
|
|
/// `Control` conformers resize it to `lyrebirdDefaultResultBufferSize` via
|
|
/// `_setupSubBlockBuffer()` in `init`. `BlockRateControlGenerator` conformers never
|
|
/// sub-block and leave it unallocated.
|
|
open class GeneratorBase {
|
|
public var _segmentSerial: UInt64 = 0
|
|
public var _cachedResult: [[Double]] = [[]]
|
|
public var _subBlockBuffer: [[Double]] = [[]]
|
|
|
|
/// Pre-allocated output buffer.
|
|
///
|
|
/// Allocated at `init` time, sized to `lyrebirdDefaultResultBufferSize` samples per
|
|
/// channel. Mono generators inherit a 1-row default; multi-row generators pass
|
|
/// `channels: N` to `super.init` to allocate N rows in one shot — no post-super
|
|
/// reassignment, no `_result` redeclaration in the subclass. Buffer rows never
|
|
/// resize after `init`; never touch this from the RT thread except to write samples.
|
|
public var _result: [[Double]]
|
|
|
|
/// Optional human-readable label for this generator, for debugging only.
|
|
///
|
|
/// Set from `build()` (or right after construction) to identify an inner node in the
|
|
/// debugger — an LLDB conditional breakpoint can then key on the tag instead of a raw
|
|
/// pointer, e.g. break in `_computeSamples` when `self.debugTag == "mod"`. Never read
|
|
/// on the RT thread by framework code; it exists purely so a debugger and hand-written
|
|
/// debug taps can recognise a specific instance.
|
|
///
|
|
/// `nonisolated(unsafe)`: a plain optional written on the main thread before the add
|
|
/// barrier and inspected only from a debugger — never read on the RT thread. Present in
|
|
/// all build configurations (a nil `String?` costs one pointer per instance) so that
|
|
/// `.debugTag` references and DocC symbol links resolve unconditionally.
|
|
///
|
|
/// See <doc:Debugging> for the breakpoint recipes that key on this tag.
|
|
public nonisolated(unsafe) var debugTag: String?
|
|
|
|
/// Default channel count — `1`. Multi-row generators MUST override.
|
|
///
|
|
/// See `Generator.outputChannelCount` for the contract. The default returns mono so
|
|
/// existing single-channel generators need no change. Multi-row generators (Pan2 et al.)
|
|
/// must override to return their fixed channel count, otherwise `+ - * /` operators will
|
|
/// treat the generator as mono and silently drop channels 1..N-1.
|
|
open var outputChannelCount: Int { 1 }
|
|
|
|
/// Designated initializer. Allocates `_result` with `channels` rows.
|
|
///
|
|
/// Multi-row generators call `super.init(channels: N)` from their own designated `init`
|
|
/// to size the output buffer in one place. Mono generators inherit the default `1`.
|
|
public init(channels: Int = 1) {
|
|
_result = makeResultBuffer(channels: channels)
|
|
}
|
|
|
|
/// Call from every `ControlGenerator` conformer's designated `init` to allocate the
|
|
/// sub-block assembly buffer. `channels` must equal the generator's output channel count.
|
|
public func _setupSubBlockBuffer(channels: Int = 1) {
|
|
_subBlockBuffer = Array(
|
|
repeating: makeSampleBuffer(),
|
|
count: channels
|
|
)
|
|
}
|
|
|
|
/// Trap if a caller asks for more samples than `_result[0]` can hold.
|
|
///
|
|
/// Every `_computeSamples` should open with `_checkCount(count)`. The buffer is
|
|
/// pre-allocated to `lyrebirdDefaultResultBufferSize` (512) and `SynthesisEngine.init`
|
|
/// enforces `blockSize <= lyrebirdDefaultResultBufferSize`, so this is a defensive
|
|
/// check against a misconfigured engine — not a hot-path expectation.
|
|
///
|
|
/// - Parameter count: Sample count requested by the caller.
|
|
@inlinable
|
|
public func _checkCount(_ count: Int) {
|
|
precondition(
|
|
count <= _result[0].count,
|
|
"count \(count) exceeds pre-allocated buffer (\(_result[0].count)). "
|
|
+ "Ensure SynthesisEngine blockSize <= lyrebirdDefaultResultBufferSize."
|
|
)
|
|
}
|
|
}
|