diff --git a/RELEASES.md b/RELEASES.md index b1f04e3..7da3d98 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -7,6 +7,29 @@ functionality in a backward-compatible way. --- +## 1.6.4 — `LookAheadLimiter` release time decoupled from look-ahead + +A patch release. One backward-compatible library addition. + +`LookAheadLimiter` gains a `releaseTime` parameter, independent of +`lookAheadTime`: + +```swift +LookAheadLimiter(in:maxAmplitude:lookAheadTime:releaseTime:sampleRate:) +``` + +Previously the gain-release one-pole coefficient was derived from +`lookAheadTime` (default 5 ms), so the release was fast enough to modulate the +gain within a single cycle of a low-frequency signal — audible as distortion on +sustained bass when the limiter was driven hard. `releaseTime` (default +`0.050`) is now a separate control, read per block from a `ValidGeneratorInput` +so it can be driven by a live parameter / knob. The look-ahead window size and +the no-overshoot ceiling guarantee are unchanged. + +The new parameter has a default, so existing call sites are source-compatible. + +--- + ## 1.6.3 — Notarization-ready `macos-ui-xcode` template A patch release. Project-scaffolding fixes; no library API changes. diff --git a/Sources/Lyrebird/Generators/Dynamics/LookAheadLimiter.swift b/Sources/Lyrebird/Generators/Dynamics/LookAheadLimiter.swift index caa3458..c387116 100644 --- a/Sources/Lyrebird/Generators/Dynamics/LookAheadLimiter.swift +++ b/Sources/Lyrebird/Generators/Dynamics/LookAheadLimiter.swift @@ -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 + diff --git a/Sources/LyrebirdGraphDSL/Generated/GeneratedDSLRegistry.swift b/Sources/LyrebirdGraphDSL/Generated/GeneratedDSLRegistry.swift index 78cffd1..e497d7e 100644 --- a/Sources/LyrebirdGraphDSL/Generated/GeneratedDSLRegistry.swift +++ b/Sources/LyrebirdGraphDSL/Generated/GeneratedDSLRegistry.swift @@ -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") ) } diff --git a/Tests/LyrebirdTests/Generators/Dynamics/DynamicsTests.swift b/Tests/LyrebirdTests/Generators/Dynamics/DynamicsTests.swift index 9b5e7c3..53e70b8 100644 --- a/Tests/LyrebirdTests/Generators/Dynamics/DynamicsTests.swift +++ b/Tests/LyrebirdTests/Generators/Dynamics/DynamicsTests.swift @@ -480,6 +480,34 @@ struct LookAheadLimiterTests { let tailMax: Double = tail.map { abs($0) }.max() ?? 0 #expect(tailMax < maxAmp * 10.0) // generous bound; really we want "small" } + + /// `releaseTime` is decoupled from `lookAheadTime`: with the look-ahead window held + /// fixed, two different release times must produce different gain-recovery behaviour + /// (hence different output) on a low-frequency signal that crosses the ceiling on its + /// peaks and dips below between them — while both still respect the ceiling. + @Test func releaseTimeIndependentOfLookAhead() { + let maxAmp: Double = 0.3 + func run(_ releaseTime: Double) -> [Double] { + let sine: OscSin.Audio = OscSin.Audio(freq: 60.0, mul: 1.0) + let lim: LookAheadLimiter = LookAheadLimiter( + in: sine, maxAmplitude: maxAmp, + lookAheadTime: 0.005, releaseTime: releaseTime, sampleRate: sr) + return collectBlocks(lim, engine: engine, blocks: 100) + } + let fast: [Double] = run(0.005) + let slow: [Double] = run(0.200) + // Ceiling holds for both release settings. + #expect(fast.allSatisfy { abs($0) <= maxAmp + 0.001 }) + #expect(slow.allSatisfy { abs($0) <= maxAmp + 0.001 }) + // Different release → different waveform. + let tailFast: [Double] = Array(fast.suffix(engine.blockSize * 50)) + let tailSlow: [Double] = Array(slow.suffix(engine.blockSize * 50)) + let ssd: Double = zip(tailFast, tailSlow).reduce(0.0) { acc, pair in + let diff: Double = pair.0 - pair.1 + return acc + diff * diff + } + #expect(ssd > 0, "releaseTime had no effect on limiter output") + } } // MARK: - Compressor Tests diff --git a/docs/stdlib/dynamics.md b/docs/stdlib/dynamics.md index 63d961f..6e6fe17 100644 --- a/docs/stdlib/dynamics.md +++ b/docs/stdlib/dynamics.md @@ -105,7 +105,7 @@ File: `Sources/Lyrebird/Generators/Dynamics/LookAheadLimiter.swift` Delays the output by `lookAheadTime` seconds and uses the look-ahead window to detect peaks before they arrive, guaranteeing the output never exceeds `±maxAmplitude`. No overshoot. ```swift -LookAheadLimiter(in:maxAmplitude:lookAheadTime:sampleRate:) +LookAheadLimiter(in:maxAmplitude:lookAheadTime:releaseTime:sampleRate:) ``` | Parameter | Type | Default | Description | @@ -113,6 +113,7 @@ LookAheadLimiter(in:maxAmplitude:lookAheadTime:sampleRate:) | `in` | `ValidGeneratorInput` | — | Signal to limit | | `maxAmplitude` | `ValidGeneratorInput` | `1.0` | Peak ceiling (absolute value) | | `lookAheadTime` | `Double` | `0.005` | Delay in seconds. Set at init. | +| `releaseTime` | `ValidGeneratorInput` | `0.050` | Gain-release time constant (seconds), read per block; decoupled from `lookAheadTime`. Clamped to ≥ 0. Keep it above the period of the lowest expected frequency to avoid low-frequency distortion. | | `sampleRate` | `Double` | `44100.0` | Used to size ring buffers at init. | **Output:** single channel, delayed by `lookAheadSamples = Int(lookAheadTime × sampleRate)`. @@ -122,7 +123,7 @@ LookAheadLimiter(in:maxAmplitude:lookAheadTime:sampleRate:) 2. Scan the look-ahead window (`lookAheadSamples`) for peak. 3. Required gain = `min(1, maxAmplitude / peak)`. 4. Attack is **instantaneous** (gain can always be reduced within the window). -5. Release smoothed: `releaseCoeff = exp(−1 / (sr × lookAheadTime))`. +5. Release smoothed: `releaseCoeff = exp(−1 / (sr × releaseTime))`, recomputed per block (so `releaseTime` may track a live parameter) rather than fixed at init. 6. Output = delayed sample × current gain. **Pre-allocated buffers:** @@ -134,7 +135,8 @@ LookAheadLimiter(in:maxAmplitude:lookAheadTime:sampleRate:) **Usage:** ```swift -let mastered = LookAheadLimiter(in: mix, maxAmplitude: 0.99, lookAheadTime: 0.005) +let mastered = LookAheadLimiter(in: mix, maxAmplitude: 0.99, lookAheadTime: 0.005, + releaseTime: 0.050, sampleRate: engine.sampleRate) ``` ---