// RateRamp.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT /// Control-rate open-ended linear accumulator. /// /// The accumulator initialises to `start` and grows by `rate` units per second /// indefinitely. On a `≤0 → >0` rising edge of `trigger`, the accumulator is /// reset to the current value of `start` and ramping continues from there at /// the (possibly new) `rate`. The first audio block ramps from `start` to /// `start + rate × blockDuration`. /// /// `rate` is sampled once per audio block (block-constant; reads through any /// `ParameterNode` ramp endpoint via `lastGeneratorSampleOrFloat`). `start` is /// sampled at the first `_computeSamples` call (to seed the accumulator) and /// then only at trigger edges — modulating `start` between triggers has no /// effect. The reset itself is block-quantised: when an edge is detected /// anywhere in the block, the entire block ramps from `start` to /// `start + rate × blockDuration`. Edge *detection* is sample-accurate when /// `trigger` is a Generator (the full block is scanned); when `trigger` is a /// Double/Int literal, the detection is block-rate. At most one reset fires /// per audio block. /// /// Closely modelled on SuperCollider's `Sweep` UGen, with an added modulatable /// `start` value sampled at trigger time. /// /// `RateRamp.Control` is block-rate state — `_acc` advances by exactly one /// per-block delta regardless of `controlBlockSize`. There is no done action; /// the generator is open-ended and runs for the lifetime of its node. /// /// ## Usage /// ```swift /// let trig = Impulse.Control(freq: 1.0) /// let ramp = RateRamp.Control(trigger: trig, rate: 0.5) // 0.0 → 0.5 over 1 s, repeats /// let cutoff = ramp * 1000.0 + 200.0 /// ``` public enum RateRamp { /// Control-rate open-ended linear accumulator with trigger reset. /// /// - Parameters: /// - trigger: Trigger signal. Any `ValidGeneratorInput` is accepted; generator /// inputs are evaluated at audio rate for sample-accurate edge detection. /// A `≤0 → >0` edge resets the accumulator to `start`. /// - rate: Growth rate in units per second. Modulatable continuously; read /// once per audio block. /// - start: Initial accumulator value, also the reset value applied on each /// trigger edge. Sampled at the first `_computeSamples` call and at every /// subsequent edge — modulating `start` between edges has no effect on output. /// Pass a literal or a `ParameterNode` for predictable behaviour; other /// Generator inputs return their last-block value via `floatValue`. /// Defaults to `0.0`. /// - mul: Output multiplier. Defaults to `1.0`. /// - add: Output offset. Defaults to `0.0`. public final class Control: GeneratorBase, BlockRateControlGenerator { let trigInput: any ValidGeneratorInput let rateInput: any ValidGeneratorInput let startInput: any ValidGeneratorInput private let _trigIsGenerator: Bool private let _rateIsGenerator: Bool private let _mulAdd: MulAddSpec public var previousOutputValue: Double = 0.0 private var _acc: Double = 0.0 private var _prevTrig: Double = 0.0 private var _rampTarget: Double = 0.0 private var _started: Bool = false /// - Parameters: /// - trigger: Trigger signal — `≤0 → >0` edge resets accumulator to `start`. /// Generator inputs are scanned across the full block for sample-accurate /// edge detection; literal inputs are read once per block (block-rate). /// - rate: Growth rate in units per second (modulatable per block). /// - start: Initial accumulator value, also the reset value at each trigger /// edge. Sampled on the first `_computeSamples` call and at every edge. /// Defaults to `0.0`. /// - mul: Output multiplier. Defaults to `1.0`. /// - add: Output offset. Defaults to `0.0`. public init( trigger: any ValidGeneratorInput, rate: any ValidGeneratorInput, start: any ValidGeneratorInput = 0.0, mul: any ValidGeneratorInput = 1.0, add: any ValidGeneratorInput = 0.0 ) { let resolvedTrigger: any ValidGeneratorInput = trigger._resolvedInput() let resolvedRate: any ValidGeneratorInput = rate._resolvedInput() let resolvedStart: any ValidGeneratorInput = start._resolvedInput() self.trigInput = resolvedTrigger self.rateInput = resolvedRate self.startInput = resolvedStart self._trigIsGenerator = resolvedTrigger is any Generator self._rateIsGenerator = resolvedRate is any Generator self._mulAdd = MulAddSpec(mul: mul, add: add) super.init() } public func _computeSamples( graph: SynthGraph, count: Int ) -> [[Double]] { _checkCount(count) // First-call seeding: accumulator initialises to `start` so the first // block's ramp begins from `start` (not from 0). Sample rate / engine state // is not available at init time, so we defer to the first RT call. if !_started { _started = true let initialStart: Double = startInput.floatValue(graph: graph) _acc = initialStart _rampTarget = initialStart } // Trigger fast-path: scalar literals are block-constant — one floatValue // read, no per-block allocation. Generator inputs are scanned at audio rate // for sample-accurate edge detection. var edgeFired: Bool = false if _trigIsGenerator { let trigSamples: [Double]? = trigInput.calculatedSamples(graph: graph, count: count).first var localPrev: Double = _prevTrig for i: Int in 0.. 0.0 && localPrev <= 0.0 { edgeFired = true } localPrev = t } _prevTrig = localPrev } else { let t: Double = trigInput.floatValue(graph: graph) if t > 0.0 && _prevTrig <= 0.0 { edgeFired = true } _prevTrig = t } if edgeFired { let resetValue: Double = startInput.floatValue(graph: graph) _acc = resetValue _rampTarget = resetValue // discontinuous reset — next ramp begins from start } let rate: Double = lastGeneratorSampleOrFloat( rateInput, isGenerator: _rateIsGenerator, graph: graph, count: count ) let sampleRate: Double = graph.engine.sampleRate let delta: Double = rate * Double(count) / sampleRate let rampStart: Double = _rampTarget _acc += delta _rampTarget = _acc guard count > 0 else { return _result } previousOutputValue = fillRampedResult( into: &_result, from: rampStart, to: _acc, mulAdd: _mulAdd, graph: graph, count: count ) return _result } } } // MARK: - GeneratorEnum // GeneratorEnum conformance is intentionally omitted for RateRamp. // GeneratorEnum requires distinct AudioType: AudioGenerator and ControlType: ControlGenerator; // RateRamp is Control-rate only — block-quantised reset is its definition, and a per-sample // Audio variant would imply sample-accurate trigger response which the block-rate accumulator // model does not provide.