Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/CompressorPedalTests.swift
T
josh e697789bd7 feat(pedals): limiter Release knob + compressor safety limiter; bump Lyrebird 1.6.4
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>
2026-07-31 18:22:43 -07:00

238 lines
10 KiB
Swift

// CompressorPedalTests.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("CompressorPedal")
struct CompressorPedalTests {
@Test func buildsAndProducesNonSilentOutput() throws {
let renderDuration: Double = 0.1
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration)
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: CompressorPedal = CompressorPedal(
inputBus: srcBus.index,
outputBus: dstBus.index
)
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()
let samples: [Float] = readStereoCAF(url: url)
#expect(!samples.isEmpty)
#expect(hasNonSilentOutput(samples), "CompressorPedal produced silence")
}
@Test func ratioChangesOutput() throws {
// A high-ratio render should differ from a low-ratio render (bypass-equivalent).
let renderDuration: Double = 0.1
let numAudioBuses: Int = 64
func renderWithRatio(_ ratioValue: Double) throws -> [Float] {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: numAudioBuses
)
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: CompressorPedal = CompressorPedal(
inputBus: srcBus.index,
outputBus: dstBus.index
)
// Use a low threshold so the test signal (peak ~0.25 linear ≈ -12 dB) is
// well above it, ensuring ratio differences produce measurable gain changes.
pedal.threshold = -24.0
pedal.ratio = ratioValue
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)
}
let lowRatioSamples: [Float] = try renderWithRatio(1.0)
let highRatioSamples: [Float] = try renderWithRatio(20.0)
#expect(hasNonSilentOutput(lowRatioSamples), "ratio=1 render was silent")
#expect(hasNonSilentOutput(highRatioSamples), "ratio=20 render was silent")
let skipSamples: Int = 1024
let windowSize: Int = 2048
guard lowRatioSamples.count >= skipSamples + windowSize,
highRatioSamples.count >= skipSamples + windowSize else { return }
let lowWindow: [Float] = Array(lowRatioSamples[skipSamples..<(skipSamples + windowSize)])
let highWindow: [Float] = Array(highRatioSamples[skipSamples..<(skipSamples + windowSize)])
let ssd: Double = zip(lowWindow, highWindow).reduce(0.0) { acc, pair in
let diff: Double = Double(pair.0 - pair.1)
return acc + diff * diff
}
#expect(ssd > 0, "ratio=1 and ratio=20 produced identical output — ratio knob has no effect")
}
@Test func exposesInputThresholdRatioKnobs() throws {
let pedal: CompressorPedal = CompressorPedal()
let labels: [String] = pedal.effectKnobs.map { $0.label }
#expect(labels == ["Input", "Threshold", "Ratio"])
}
@Test func exposesSafetySwitch() throws {
let pedal: CompressorPedal = CompressorPedal()
let switchLabels: [String] = pedal.effectSwitches.map { $0.label }
#expect(switchLabels == ["Safety"])
let optionTitles: [String] = pedal.effectSwitches.first?.options.map { $0.title } ?? []
#expect(optionTitles == ["Off", "On"])
}
@Test func safetyLimiterCapsPeaks() throws {
// Drive the compressor hard (hot input + makeup gain) so its non-zero attack lets
// transients overshoot. With the safety limiter engaged the output peak must stay
// near full scale; the two switch states must also produce different output.
let renderDuration: Double = 0.1
let sampleRate: Double = 44100.0
func render(safety: Double) throws -> [Float] {
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: CompressorPedal = CompressorPedal(
inputBus: srcBus.index,
outputBus: dstBus.index
)
pedal.sampleRate = sampleRate
pedal.inputGain = 24.0
pedal.threshold = -30.0
pedal.ratio = 8.0
pedal.safetyLimiter = safety
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)
}
let off: [Float] = try render(safety: 0.0)
let on: [Float] = try render(safety: 1.0)
#expect(hasNonSilentOutput(off), "safety-off render was silent")
#expect(hasNonSilentOutput(on), "safety-on render was silent")
// Skip the safety limiter's look-ahead fill window before measuring the peak.
let lookAheadFillSamples: Int = 2048
guard on.count > lookAheadFillSamples else { return }
let onWindow: ArraySlice<Float> = on[lookAheadFillSamples...]
let onPeak: Float = onWindow.reduce(0) { Swift.max($0, abs($1)) }
// Safety ceiling is just under full scale; downstream master only attenuates.
let ceilingTolerance: Float = 0.05
#expect(onPeak <= 1.0 + ceilingTolerance, "safety limiter failed to cap peak: \(onPeak)")
// The two switch positions must differ.
let skip: Int = lookAheadFillSamples
let window: Int = 2048
guard off.count >= skip + window, on.count >= skip + window else { return }
let offWindow: [Float] = Array(off[skip..<(skip + window)])
let onWindow2: [Float] = Array(on[skip..<(skip + window)])
let ssd: Double = zip(offWindow, onWindow2).reduce(0.0) { acc, pair in
let diff: Double = Double(pair.0 - pair.1)
return acc + diff * diff
}
#expect(ssd > 0, "safety switch had no effect on output")
}
@Test func inputGainChangesOutput() throws {
// The pre-compression input gain stage must alter the output: boosting the input
// drives more of the signal past the threshold, changing the compressed result.
let renderDuration: Double = 0.1
let numAudioBuses: Int = 64
func renderWithInputGain(_ gainDB: Double) throws -> [Float] {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: numAudioBuses
)
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: CompressorPedal = CompressorPedal(
inputBus: srcBus.index,
outputBus: dstBus.index
)
pedal.inputGain = gainDB
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)
}
let unitySamples: [Float] = try renderWithInputGain(0.0)
let boostedSamples: [Float] = try renderWithInputGain(24.0)
#expect(hasNonSilentOutput(unitySamples), "0 dB input-gain render was silent")
#expect(hasNonSilentOutput(boostedSamples), "+24 dB input-gain render was silent")
let skipSamples: Int = 1024
let windowSize: Int = 2048
guard unitySamples.count >= skipSamples + windowSize,
boostedSamples.count >= skipSamples + windowSize else { return }
let unityWindow: [Float] = Array(unitySamples[skipSamples..<(skipSamples + windowSize)])
let boostedWindow: [Float] = Array(boostedSamples[skipSamples..<(skipSamples + windowSize)])
let ssd: Double = zip(unityWindow, boostedWindow).reduce(0.0) { acc, pair in
let diff: Double = Double(pair.0 - pair.1)
return acc + diff * diff
}
#expect(ssd > 0, "input gain 0 dB and +24 dB produced identical output — input gain has no effect")
}
}