v1.6.4 release: LookAheadLimiter releaseTime decoupled from lookAheadTime

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-31 18:10:54 -07:00
parent 5e6534a615
commit d6875da2cb
5 changed files with 89 additions and 23 deletions
@@ -11,7 +11,11 @@ import Darwin
/// the output never exceeds ±`maxAmplitude` regardless of transients.
///
/// The gain attack is instantaneous within the look-ahead window. Gain release uses
/// a one-pole smoother with a time constant equal to `lookAheadTime`.
/// a one-pole smoother whose time constant is `releaseTime`, decoupled from
/// `lookAheadTime`. A too-fast release modulates the gain within a single cycle of a
/// low-frequency signal, which distorts sustained bass; keeping release slow (tens of
/// ms) while the look-ahead window stays short (a few ms) catches transients without
/// that distortion.
///
/// ## Latency
/// Output is delayed by exactly `lookAheadSamples = Int(lookAheadTime × sampleRate)`.
@@ -42,18 +46,22 @@ import Darwin
/// - maxAmplitude: Peak ceiling (absolute value). Output `|y|` never exceeds
/// this value once the look-ahead window has filled. Default `1.0`.
/// - lookAheadTime: Look-ahead and output delay in seconds. Fixed at init —
/// determines ring buffer size and release time constant.
/// Range `[0, ∞)`; values above ~50 ms waste memory.
/// Default `0.005` (5 ms).
/// determines ring buffer size. Range `[0, ∞)`; values above
/// ~50 ms waste memory. Default `0.005` (5 ms).
/// - releaseTime: Gain-release time constant in seconds. Clamped to ≥ 0. Read per block,
/// so it may be driven by a live parameter. Independent of `lookAheadTime`.
/// Keep it well above the period of the lowest frequency you expect
/// (tens of ms) to avoid low-frequency distortion. Default `0.050`.
/// - sampleRate: Engine sample rate in Hz. **Must match `SynthesisEngine.sampleRate`.**
/// Required — no default. Ring buffer sizes and release coefficient
/// are computed at init from this value and cannot be updated at runtime.
/// Required — no default. Ring buffer sizes are computed at init from
/// this value and cannot be updated at runtime.
public final class LookAheadLimiter: GeneratorBase, AudioGenerator {
// MARK: - Parameters
private let _input: any ValidGeneratorInput
private let _maxAmpInput: any ValidGeneratorInput
private let _releaseTimeInput: any ValidGeneratorInput
private let _lookAheadSamples: Int
// MARK: - Pre-allocated delay ring
@@ -80,8 +88,6 @@ public final class LookAheadLimiter: GeneratorBase, AudioGenerator {
private var _writePos: Int = 0
private var _gain: Double = 1.0
private let _releaseCoeff: Double
private let _attackComp: Double
// MARK: - Init
@@ -90,30 +96,30 @@ public final class LookAheadLimiter: GeneratorBase, AudioGenerator {
/// - maxAmplitude: Peak ceiling (absolute value). Output `|y|` never exceeds
/// this value once the look-ahead window has filled. Default `1.0`.
/// - lookAheadTime: Look-ahead and output delay in seconds. Fixed at init —
/// determines ring buffer size and release time constant.
/// Range `[0, ∞)`. Default `0.005` (5 ms).
/// determines ring buffer size. Range `[0, ∞)`. Default `0.005` (5 ms).
/// - releaseTime: Gain-release time constant in seconds. Clamped to ≥ 0. Read per block, so it may be
/// driven by a live parameter. Independent of `lookAheadTime`. Default `0.050`.
/// - sampleRate: Engine sample rate in Hz. **Must match `SynthesisEngine.sampleRate`.**
/// Required — no default. Ring buffer sizes and the release coefficient
/// are computed at init from this value and cannot be updated at runtime.
/// Required — no default. Ring buffer sizes are computed at init from this
/// value and cannot be updated at runtime.
public init(
in input: any ValidGeneratorInput,
maxAmplitude: any ValidGeneratorInput = 1.0,
lookAheadTime: Double = 0.005,
releaseTime: any ValidGeneratorInput = 0.050,
sampleRate: Double
) {
precondition(lookAheadTime >= 0, "LookAheadLimiter: lookAheadTime must be ≥ 0")
let resolvedInput: any ValidGeneratorInput = input._resolvedInput()
let resolvedMaxAmp: any ValidGeneratorInput = maxAmplitude._resolvedInput()
let resolvedInput: any ValidGeneratorInput = input._resolvedInput()
let resolvedMaxAmp: any ValidGeneratorInput = maxAmplitude._resolvedInput()
let resolvedRelease: any ValidGeneratorInput = releaseTime._resolvedInput()
_input = resolvedInput
_maxAmpInput = resolvedMaxAmp
_releaseTimeInput = resolvedRelease
_lookAheadSamples = Int(lookAheadTime * sampleRate)
let bufferSize: Int = _lookAheadSamples + lyrebirdDefaultResultBufferSize + 1
_ringSize = bufferSize
_delayRing = [Double](repeating: 0.0, count: bufferSize)
_releaseCoeff = lookAheadTime > 0
? exp(-1.0 / (sampleRate * lookAheadTime))
: 0.0
_attackComp = 1.0 - _releaseCoeff
// Deque capacity = max-live (_lookAheadSamples + 1) + 1 slot of slack
// so push-before-front-pop on a wraparound step does not collide.
_dequeCapacity = max(2, _lookAheadSamples + 2)
@@ -136,9 +142,14 @@ public final class LookAheadLimiter: GeneratorBase, AudioGenerator {
let maxAmp: Double = max(0.0, Double(_maxAmpInput.floatValue(graph: graph)))
let inputSamples: [[Double]] = _input.calculatedSamples(graph: graph, count: count)
// Release coefficient recomputed per block from the (possibly live) releaseTime.
// Decoupled from lookAheadTime — a slow release avoids modulating the waveform.
let sr: Double = graph.engine.sampleRate
let releaseT: Double = max(0.0, Double(_releaseTimeInput.floatValue(graph: graph)))
let releaseCoeff: Double = releaseT > 0 ? exp(-1.0 / (sr * releaseT)) : 0.0
let attackComp: Double = 1.0 - releaseCoeff
let ringSize: Int = _ringSize
let releaseCoeff: Double = _releaseCoeff
let attackComp: Double = _attackComp
guard let ins: [Double] = inputSamples.first else {
// Pathological branch: upstream returned a 0-row matrix. Advance ring +
@@ -3332,6 +3332,7 @@ extension DSLBuiltinRegistry {
DSLParameter(label: "in", hasDefault: false),
DSLParameter(label: "maxAmplitude", hasDefault: true),
DSLParameter(label: "lookAheadTime", hasDefault: true),
DSLParameter(label: "releaseTime", hasDefault: true),
DSLParameter(label: "sampleRate", hasDefault: false),
],
make: { args in
@@ -3339,6 +3340,7 @@ extension DSLBuiltinRegistry {
in: try DSLArgument.input(args["in"], "LookAheadLimiter", "in"),
maxAmplitude: try DSLArgument.input(args["maxAmplitude"] ?? .signal(1.0), "LookAheadLimiter", "maxAmplitude"),
lookAheadTime: try DSLArgument.double(args["lookAheadTime"] ?? .signal(0.005), "LookAheadLimiter", "lookAheadTime"),
releaseTime: try DSLArgument.input(args["releaseTime"] ?? .signal(0.050), "LookAheadLimiter", "releaseTime"),
sampleRate: try DSLArgument.double(args["sampleRate"], "LookAheadLimiter", "sampleRate")
)
}