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>
199 lines
7.1 KiB
Swift
199 lines
7.1 KiB
Swift
// Demand.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/// Pull-based demand-rate signal generators.
|
|
///
|
|
/// `Demand.Audio` and `Demand.Control` pull one value from a `DemandGenerator` source
|
|
/// each time the trigger signal crosses from ≤0 to >0. Between triggers the held value
|
|
/// is output unchanged. When the source is exhausted the last held value is output
|
|
/// indefinitely (no wrap, no `doneAction`).
|
|
///
|
|
/// **Trigger edge ordering** — within any single sample, a reset edge is processed before
|
|
/// a trigger edge. A simultaneous reset and trigger in the same sample resets the source
|
|
/// first, then pulls the first value of the freshly-reset sequence.
|
|
///
|
|
/// **Ownership** — The `demand` source must be owned by exactly one puller. Sharing a
|
|
/// `DemandGenerator` across multiple `Demand` instances is not thread-safe.
|
|
///
|
|
/// ```swift
|
|
/// // Step through a pitch sequence at 4 Hz (control rate)
|
|
/// let pitchSeq = Demand.Control(
|
|
/// trig: Impulse.Control(freq: 4),
|
|
/// demand: DemandSequence(values: [261.63, 293.66, 329.63, 349.23])
|
|
/// )
|
|
/// addOutputGenerator(Out.Audio(bus: 0, source: OscSin.Audio(freq: pitchSeq)))
|
|
///
|
|
/// // Same idea at audio rate — sample-accurate trigger timing
|
|
/// let audioSeq = Demand.Audio(
|
|
/// trig: Impulse.Audio(freq: 4),
|
|
/// demand: DemandSequence(values: [0.25, 0.5, 0.75, 1.0])
|
|
/// )
|
|
/// ```
|
|
public enum Demand {
|
|
|
|
// MARK: - Shared base
|
|
|
|
public class _Base: GeneratorBase {
|
|
let _trigInput: any ValidGeneratorInput
|
|
let _resetInput: any ValidGeneratorInput
|
|
// nonisolated(unsafe): owned by exactly one Demand.Audio or Demand.Control instance
|
|
// on the RT thread. Sharing across instances is not thread-safe.
|
|
nonisolated(unsafe) let _demand: any DemandGenerator
|
|
|
|
var _heldValue: Double = 0.0
|
|
var _prevTrig: Double = 0.0
|
|
var _prevReset: Double = 0.0
|
|
let _zeroBlock: [Double]
|
|
|
|
init(
|
|
trig: any ValidGeneratorInput,
|
|
reset: any ValidGeneratorInput,
|
|
demand: any DemandGenerator
|
|
) {
|
|
let resolvedTrig: any ValidGeneratorInput = trig._resolvedInput()
|
|
let resolvedReset: any ValidGeneratorInput = reset._resolvedInput()
|
|
_trigInput = resolvedTrig
|
|
_resetInput = resolvedReset
|
|
_demand = demand
|
|
_zeroBlock = makeSampleBuffer()
|
|
}
|
|
}
|
|
|
|
// MARK: - Audio-rate puller
|
|
|
|
/// Sample-accurate demand-rate puller at audio rate.
|
|
///
|
|
/// Evaluates `trig` sample-by-sample; each ≤0→>0 crossing pulls one value from
|
|
/// `demand` and holds it until the next crossing. Output is a single-channel audio
|
|
/// block at the engine sample rate.
|
|
public final class Audio: _Base, AudioGenerator {
|
|
|
|
public override init(
|
|
trig: any ValidGeneratorInput,
|
|
reset: any ValidGeneratorInput = 0.0,
|
|
demand: any DemandGenerator
|
|
) {
|
|
super.init(trig: trig, reset: reset, demand: demand)
|
|
}
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
|
|
let trigSamples: [Double] = _trigInput.calculatedSamples(graph: graph, count: count).first
|
|
?? _zeroBlock
|
|
let resetSamples: [Double] = _resetInput.calculatedSamples(graph: graph, count: count).first
|
|
?? _zeroBlock
|
|
|
|
for i: Int in 0..<count {
|
|
let r: Double = resetSamples[i]
|
|
if r > 0 && _prevReset <= 0 {
|
|
_demand.reset()
|
|
_heldValue = 0
|
|
}
|
|
_prevReset = r
|
|
|
|
let t: Double = trigSamples[i]
|
|
if t > 0 && _prevTrig <= 0 {
|
|
if let v: Double = _demand.nextValue() {
|
|
_heldValue = v
|
|
}
|
|
}
|
|
_prevTrig = t
|
|
|
|
_result[0][i] = _heldValue
|
|
}
|
|
return _result
|
|
}
|
|
}
|
|
|
|
// MARK: - Control-rate puller
|
|
|
|
/// Block-rate demand-rate puller with interpolated output.
|
|
///
|
|
/// Scans the full trigger block per-sample to detect ≤0→>0 crossings from audio-rate
|
|
/// trigger sources. A per-block `didTrigger` gate ensures at most one `nextValue()` call
|
|
/// per block — the first crossing wins. A reset edge clears the gate, so one additional
|
|
/// pull is allowed after a reset within the same block (matching the sample-level
|
|
/// reset-before-trigger ordering in `Demand.Audio`). Output ramps from the previous
|
|
/// block's target to the new held value, reaching the target at the last sample. Matches
|
|
/// the standard `ControlGenerator` ramp convention.
|
|
///
|
|
/// `floatValue(graph:)` returns `previousOutputValue` without advancing the sequence —
|
|
/// use the full `calculatedSamples` path for demand evaluation.
|
|
public final class Control: _Base, BlockRateControlGenerator {
|
|
public var previousOutputValue: Double = 0.0
|
|
|
|
private var _rampTarget: Double = 0.0
|
|
|
|
public override init(
|
|
trig: any ValidGeneratorInput,
|
|
reset: any ValidGeneratorInput = 0.0,
|
|
demand: any DemandGenerator
|
|
) {
|
|
super.init(trig: trig, reset: reset, demand: demand)
|
|
}
|
|
|
|
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
|
|
let trigSamples: [Double] = _trigInput.calculatedSamples(graph: graph, count: count).first
|
|
?? _zeroBlock
|
|
let resetSamples: [Double] = _resetInput.calculatedSamples(graph: graph, count: count).first
|
|
?? _zeroBlock
|
|
|
|
var localPrevTrig: Double = _prevTrig
|
|
var localPrevReset: Double = _prevReset
|
|
var didTrigger: Bool = false
|
|
|
|
for i: Int in 0..<count {
|
|
let r: Double = resetSamples[i]
|
|
if r > 0 && localPrevReset <= 0 {
|
|
_demand.reset()
|
|
_heldValue = 0
|
|
didTrigger = false // allow one pull after reset, even if already triggered
|
|
}
|
|
localPrevReset = r
|
|
|
|
let t: Double = trigSamples[i]
|
|
if !didTrigger && t > 0 && localPrevTrig <= 0 {
|
|
if let v: Double = _demand.nextValue() { _heldValue = v }
|
|
didTrigger = true
|
|
}
|
|
localPrevTrig = t
|
|
}
|
|
|
|
_prevTrig = localPrevTrig
|
|
_prevReset = localPrevReset
|
|
|
|
guard count > 0 else {
|
|
previousOutputValue = _heldValue
|
|
return _result
|
|
}
|
|
|
|
fillScalarRampedResult(
|
|
into: &_result,
|
|
previousOutputValue: &_rampTarget,
|
|
target: _heldValue,
|
|
count: count
|
|
)
|
|
previousOutputValue = _heldValue
|
|
return _result
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Demand: GeneratorEnum {
|
|
public typealias AudioType = Demand.Audio
|
|
public typealias ControlType = Demand.Control
|
|
}
|