5a07a6d4b4
A pre-compression input gain knob (Input, dB, range [-12, +24], default 0) boosts/cuts the signal before it hits the compressor, so driving the input harder pushes more of the signal past the threshold. The dB value is converted to a linear multiplier via Exp(dB * ln(10)/20) and applied to the dry signal. Tests: knob list is [Input, Threshold, Ratio]; input gain changes the output. The preset recall test now finds the Threshold knob by label, not position. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
162 lines
6.9 KiB
Swift
162 lines
6.9 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 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")
|
|
}
|
|
}
|