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>
151 lines
5.6 KiB
Swift
151 lines
5.6 KiB
Swift
// Line.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Synchronization
|
|
|
|
/// Control-rate linear ramp from `start` to `end` over `dur` seconds.
|
|
///
|
|
/// Outputs `start` at time 0, ramps linearly toward `end`, and reaches `end` after
|
|
/// `dur` seconds. After completion the generator dispatches its `doneAction`:
|
|
/// - `.nothing` — freeze at `end`.
|
|
/// - `.freeNode` — freeze at `end` and set `needsFree = true` (RT-safe atomic store).
|
|
/// Read `needsFree` from a non-RT context after `processBlock` returns and post a
|
|
/// remove event to the engine.
|
|
/// - `.loop` — reset to `start` and run again indefinitely.
|
|
///
|
|
/// `Line.Control` is block-rate state — `_pos` advances by exactly one block per audio
|
|
/// block regardless of `controlBlockSize`. For per-sample resolution use
|
|
/// `EnvelopeGen.Audio` with a two-segment linear envelope.
|
|
///
|
|
/// ## Usage
|
|
/// ```swift
|
|
/// let env = Line.Control(start: 0.0, end: 1.0, dur: 2.0, doneAction: .freeNode)
|
|
/// let osc = OscSin.Audio(freq: 440.0) * env
|
|
/// ```
|
|
public enum Line {
|
|
|
|
/// Control-rate linear ramp over a fixed duration.
|
|
///
|
|
/// - Parameters:
|
|
/// - start: Initial output value (units arbitrary — Hz, amplitude, normalized).
|
|
/// - end: Final output value reached after `dur` seconds.
|
|
/// - dur: Ramp duration in seconds. Must be `>= 0`. A duration of `0` snaps
|
|
/// immediately to `end` and dispatches `doneAction` on the first block.
|
|
/// - doneAction: Action taken on completion. Defaults to `.nothing`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public final class Control: GeneratorBase, BlockRateControlGenerator, DoneActionWatchable {
|
|
let start: Double
|
|
let end: Double
|
|
let dur: Double
|
|
private let _doneAction: DoneAction
|
|
private let _mulAdd: MulAddSpec
|
|
public var previousOutputValue: Double = 0.0
|
|
|
|
// Lazy: _durSamples = dur * sampleRate, computed on first _computeSamples
|
|
// because sampleRate is not known until the engine is bound.
|
|
private var _durSamples: Double = 0.0
|
|
private var _durInitialized: Bool = false
|
|
private var _pos: Int = 0
|
|
private var _isDone: Bool = false
|
|
private var _rampTarget: Double = 0.0
|
|
|
|
private let _needsFree: Atomic<Bool> = Atomic(false)
|
|
public var needsFree: Bool { _needsFree.load(ordering: .acquiring) }
|
|
|
|
/// - Parameters:
|
|
/// - start: Initial output value.
|
|
/// - end: Final output value reached after `dur` seconds.
|
|
/// - dur: Ramp duration in seconds. Must be `>= 0`.
|
|
/// - doneAction: Action taken on completion. Defaults to `.nothing`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public init(
|
|
start: Double,
|
|
end: Double,
|
|
dur: Double,
|
|
doneAction: DoneAction = .nothing,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
precondition(dur >= 0.0, "Line: dur must be >= 0 (got \(dur)).")
|
|
self.start = start
|
|
self.end = end
|
|
self.dur = dur
|
|
self._doneAction = doneAction
|
|
self._mulAdd = MulAddSpec(mul: mul, add: add)
|
|
self._rampTarget = start
|
|
self.previousOutputValue = start
|
|
super.init()
|
|
_registerAutoFreeIfNeeded(doneAction)
|
|
}
|
|
|
|
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
|
|
if !_durInitialized {
|
|
_durSamples = dur * graph.engine.sampleRate
|
|
_durInitialized = true
|
|
}
|
|
|
|
let target: Double
|
|
var dispatchDone: Bool = false
|
|
if _isDone || _durSamples <= 0.0 {
|
|
target = end
|
|
if !_isDone { dispatchDone = true } // dur == 0 path
|
|
} else {
|
|
_pos += count
|
|
let t: Double = min(Double(_pos) / _durSamples, 1.0)
|
|
target = start + (end - start) * t
|
|
if t >= 1.0 { dispatchDone = true }
|
|
}
|
|
|
|
let rampStart: Double = _rampTarget
|
|
_rampTarget = target
|
|
guard count > 0 else { return _result }
|
|
previousOutputValue = fillRampedResult(
|
|
into: &_result,
|
|
from: rampStart,
|
|
to: target,
|
|
mulAdd: _mulAdd,
|
|
graph: graph,
|
|
count: count
|
|
)
|
|
|
|
// Done dispatch is post-ramp so .loop's _rampTarget reset takes effect on
|
|
// the NEXT block, not this one.
|
|
if dispatchDone { handleDone() }
|
|
return _result
|
|
}
|
|
|
|
private func handleDone() {
|
|
switch _doneAction {
|
|
case .nothing:
|
|
_isDone = true
|
|
case .freeNode:
|
|
_isDone = true
|
|
_needsFree.store(true, ordering: .releasing)
|
|
case .loop:
|
|
_pos = 0
|
|
_isDone = false
|
|
_rampTarget = start
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - GeneratorEnum
|
|
// GeneratorEnum conformance is intentionally omitted for Line.
|
|
// GeneratorEnum requires distinct AudioType: AudioGenerator and ControlType: ControlGenerator;
|
|
// Line is Control-rate only — a per-sample Audio variant of a single-segment linear ramp
|
|
// adds no meaningful resolution over the block-rate Control variant. Use EnvelopeGen.Audio
|
|
// with a two-level Envelope when sample-accurate ramping is required.
|