Files
Lyrebird/Sources/LyrebirdCLI/Resources/XcodeTemplates/LyrebirdDemandGenerator.xctemplate/___FILEBASENAME___.swift
T
2026-06-01 18:27:40 -07:00

65 lines
2.1 KiB
Swift

//___FILEHEADER___
import Foundation
import LyrebirdSynth
// MARK: - ___FILEBASENAMEASIDENTIFIER___
/// One-line description of ___FILEBASENAMEASIDENTIFIER___.
///
/// A `DemandGenerator` is a pull-based value source — it is **not** a `Generator`
/// and does not appear in the audio graph itself. It is consumed by a demand
/// puller (`Demand.Audio` or `Demand.Control`) which calls `nextValue()` on each
/// `≤ 0 → > 0` trigger edge. Each instance is owned by exactly one puller.
///
/// ## Usage
/// ```swift
/// let demand = ___FILEBASENAMEASIDENTIFIER___()
/// let pitchSeq = Demand.Control(
/// trig: Impulse.Control(freq: 2.0),
/// demand: demand
/// )
/// let osc = OscSin.Audio(freq: pitchSeq)
/// ```
public final class ___FILEBASENAMEASIDENTIFIER___: DemandGenerator {
// MARK: - State
/// Replace these stored properties with whatever state your value source needs
/// (current step, RNG state, table indices, etc.). State is mutated only inside
/// `nextValue()` and `reset()` — both run on the RT thread side of the demand
/// puller, so keep everything allocation-free and lock-free.
private var _step: Int = 0
// MARK: - Init
public init() {
// Initialise state. No allocation paths should run after this point on the
// RT thread.
}
// MARK: - DemandGenerator
/// `true` once this source has produced its final value. Returning `nil` from
/// `nextValue()` is also acceptable — pullers stop pulling when either signals
/// exhaustion.
public var isExhausted: Bool {
// TODO: return true when this source has no more values to produce.
return false
}
/// Returns the next value to emit, or `nil` if exhausted.
public func nextValue() -> Double? {
// TODO: produce the next value. Replace this body with your formula.
let value: Double = Double(_step)
_step += 1
return value
}
/// Reset internal state so the next `nextValue()` produces the source's first
/// value again. Called by demand pullers when their trigger source resets.
public func reset() {
_step = 0
}
}