e697789bd7
Consume Lyrebird 1.6.4, which decouples LookAheadLimiter's release time from its look-ahead window. - LimiterPedal: add a Release knob (0.01–0.5 s) wired to the new LookAheadLimiter.releaseTime. A too-fast release modulated the gain within a low-frequency cycle and distorted sustained notes when driven hard. - CompressorPedal: add an opt-in Safety switch (off by default) that inserts a brick-wall LookAheadLimiter after the compressor to catch transient overshoot that could clip the output. Built as both branches with Select picking live; engaging it adds the limiter's short look-ahead delay. Tests cover the new knob/switch exposure, release-time effect, and that the safety limiter caps peaks. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
112 lines
4.5 KiB
Swift
112 lines
4.5 KiB
Swift
// LimiterPedalTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
@testable import LyrebirdShoeGaze
|
|
|
|
@MainActor
|
|
@Suite("LimiterPedal")
|
|
struct LimiterPedalTests {
|
|
|
|
/// Renders a `LimiterPedal` through an offline engine and returns the interleaved output.
|
|
private func render(
|
|
boost: Double,
|
|
ceiling: Double,
|
|
release: Double = 0.05,
|
|
sampleRate: Double = 44100.0
|
|
) throws -> [Float] {
|
|
let renderDuration: Double = 0.1
|
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
|
|
duration: renderDuration,
|
|
sampleRate: sampleRate
|
|
)
|
|
defer { try? FileManager.default.removeItem(at: url) }
|
|
|
|
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
|
defer {
|
|
engine.busAllocator.releaseAudioBus(srcBus)
|
|
engine.busAllocator.releaseAudioBus(dstBus)
|
|
}
|
|
|
|
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
|
|
let pedal: LimiterPedal = LimiterPedal(
|
|
inputBus: srcBus.index,
|
|
outputBus: dstBus.index
|
|
)
|
|
// Match the engine rate so the look-ahead limiter sizes its ring correctly.
|
|
pedal.sampleRate = sampleRate
|
|
pedal.boost = boost
|
|
pedal.ceiling = ceiling
|
|
pedal.release = release
|
|
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
|
|
|
|
try engine.add(source)
|
|
try engine.add(pedal, position: NodePosition.after(source))
|
|
try engine.add(master, position: NodePosition.after(pedal))
|
|
try engine.start()
|
|
|
|
return readStereoCAF(url: url, sampleRate: sampleRate)
|
|
}
|
|
|
|
@Test func buildsAndProducesNonSilentOutput() throws {
|
|
let samples: [Float] = try render(boost: 2.0, ceiling: 0.95)
|
|
#expect(!samples.isEmpty)
|
|
#expect(hasNonSilentOutput(samples), "LimiterPedal produced silence")
|
|
}
|
|
|
|
@Test func outputNeverExceedsCeiling() throws {
|
|
// Drive hard into the limiter with a low ceiling; the peak must stay at the ceiling.
|
|
// Downstream Pan2/gain in the master only attenuates, so the recorded peak is a
|
|
// conservative upper bound on the limiter output peak.
|
|
let ceiling: Double = 0.5
|
|
let samples: [Float] = try render(boost: 12.0, ceiling: ceiling)
|
|
#expect(hasNonSilentOutput(samples), "Driven limiter produced silence")
|
|
|
|
// Skip the look-ahead fill window before measuring the peak. The limiter's 5 ms
|
|
// look-ahead at 44.1 kHz is ~220 frames; 2048 interleaved samples is a comfortable
|
|
// margin past the window-fill transient.
|
|
let lookAheadFillSamples: Int = 2048
|
|
guard samples.count > lookAheadFillSamples else { return }
|
|
let window: ArraySlice<Float> = samples[lookAheadFillSamples...]
|
|
let peak: Float = window.reduce(0) { Swift.max($0, abs($1)) }
|
|
|
|
let tolerance: Float = 0.05
|
|
#expect(peak <= Float(ceiling) + tolerance, "Limiter overshot ceiling: peak=\(peak)")
|
|
}
|
|
|
|
@Test func exposesBoostCeilingReleaseKnobs() throws {
|
|
let pedal: LimiterPedal = LimiterPedal()
|
|
let labels: [String] = pedal.effectKnobs.map { $0.label }
|
|
#expect(labels == ["Boost", "Ceiling", "Release"])
|
|
}
|
|
|
|
@Test func releaseChangesOutput() throws {
|
|
// Same drive and ceiling, different release times: the gain recovers between peaks
|
|
// at different rates, so the limited output differs. Ceiling still holds for both.
|
|
let ceiling: Double = 0.4
|
|
let fast: [Float] = try render(boost: 12.0, ceiling: ceiling, release: 0.01)
|
|
let slow: [Float] = try render(boost: 12.0, ceiling: ceiling, release: 0.5)
|
|
|
|
#expect(hasNonSilentOutput(fast), "fast-release render was silent")
|
|
#expect(hasNonSilentOutput(slow), "slow-release render was silent")
|
|
|
|
let skip: Int = 2048
|
|
let window: Int = 2048
|
|
guard fast.count >= skip + window, slow.count >= skip + window else { return }
|
|
let fastWindow: [Float] = Array(fast[skip..<(skip + window)])
|
|
let slowWindow: [Float] = Array(slow[skip..<(skip + window)])
|
|
let ssd: Double = zip(fastWindow, slowWindow).reduce(0.0) { acc, pair in
|
|
let diff: Double = Double(pair.0 - pair.1)
|
|
return acc + diff * diff
|
|
}
|
|
#expect(ssd > 0, "release time had no effect on limiter output")
|
|
}
|
|
}
|