Files
Lyrebird/Tests/LyrebirdTests/CompressorParameterRampingTests.swift
T
2026-06-01 18:27:40 -07:00

82 lines
3.9 KiB
Swift

// CompressorParameterRampingTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("Compressor parameter ramping")
struct CompressorParameterRampingTests {
@Test func thresholdGeneratorInputIsRead() throws {
// Verify wiring a ParameterNode to threshold produces valid output.
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
try engine.start()
defer { engine.stop() }
let threshNode: ParameterNode = ParameterNode(-6.0)
let signal: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.5)
let comp: Compressor.Audio = Compressor.Audio(in: signal, threshold: threshNode)
let g: SynthGraph = makeSynthGraph(engine: engine)
let out: [[Double]] = comp.calculatedSamples(graph: g, count: 64)
let samples: [Double] = Array((out.first ?? []).prefix(64))
#expect(samples.allSatisfy { $0.isFinite },
"Compressor.Audio output must be finite with ParameterNode threshold")
}
@Test func thresholdParameterNodeOutputDiffersByBlock() throws {
// Verify that the ParameterNode-wired threshold is read (not ignored).
// Two separate Compressor instances with instant attack (attack=0):
// - comp1: threshold = -20 dB → signal at -6 dBFS is well above threshold → heavy compression
// - comp2: threshold = 0 dB → signal is below threshold → bypass (no compression)
let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
defer { engine.stop() }
let signal1: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.5)
let signal2: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.5)
// threshNode1: -20 dB → signal at ~-6 dBFS is 14 dB above threshold → compression
let threshNode1: ParameterNode = ParameterNode(-20.0)
let comp1: Compressor.Audio = Compressor.Audio(in: signal1, threshold: threshNode1,
ratio: 100.0, attack: 0.0, release: 0.0)
// threshNode2: 0 dB → signal is below threshold → no compression
let threshNode2: ParameterNode = ParameterNode(0.0)
let comp2: Compressor.Audio = Compressor.Audio(in: signal2, threshold: threshNode2,
ratio: 100.0, attack: 0.0, release: 0.0)
let g: SynthGraph = makeSynthGraph(engine: engine)
let out1: [[Double]] = comp1.calculatedSamples(graph: g, count: 64)
let out2: [[Double]] = comp2.calculatedSamples(graph: g, count: 64)
let rms1: Double = rmsValue(Array((out1.first ?? []).prefix(64)))
let rms2: Double = rmsValue(Array((out2.first ?? []).prefix(64)))
// comp1 compresses aggressively (14 dB above threshold, ratio 100:1, instant attack).
// comp2 passes through unchanged. rms1 should be significantly less than rms2.
#expect(rms1 < rms2 * 0.5,
"ParameterNode threshold at -20dB with ratio=100 should heavily compress vs threshold=0dB: rms1=\(rms1) rms2=\(rms2)")
}
@Test func controlThresholdGeneratorInputIsRead() throws {
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
try engine.start()
defer { engine.stop() }
let threshNode: ParameterNode = ParameterNode(-6.0)
let signal: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.5)
let comp: Compressor.Control = Compressor.Control(in: signal, threshold: threshNode)
let g: SynthGraph = makeSynthGraph(engine: engine)
let out: [[Double]] = comp.calculatedSamples(graph: g, count: 64)
let samples: [Double] = Array((out.first ?? []).prefix(64))
#expect(samples.allSatisfy { $0.isFinite },
"Compressor.Control output must be finite with ParameterNode threshold")
}
}