// PlateReverbTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("PlateReverb.Audio — Dattorro plate reverb") struct PlateReverbTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100) var graph: SynthGraph { makeSynthGraph(engine: engine) } init() throws { try engine.start() } // MARK: - 1. Rate @Test func rateIsAudio() { let verb: PlateReverb.Audio = PlateReverb.Audio(input: ConstantGeneratorInput(0.0)) #expect(verb.rate == .audio) } // MARK: - 2. Output count @Test func outputCountMatchesRequest() { let verb: PlateReverb.Audio = PlateReverb.Audio(input: ConstantGeneratorInput(0.5)) let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 512) #expect(Array(out[0].prefix(512)).count == 512) } // MARK: - 3. outputChannelCount is 2 @Test func outputChannelCountIsTwo() { let verb: PlateReverb.Audio = PlateReverb.Audio(input: ConstantGeneratorInput(0.0)) #expect(verb.outputChannelCount == 2) let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64) #expect(out.count == 2) } // MARK: - 4. Silent input → silent output @Test func silentInputProducesSilentOutput() { let verb: PlateReverb.Audio = PlateReverb.Audio(input: ConstantGeneratorInput(0.0), mix: 1.0) for _ in 0..<5 { _ = verb.calculatedSamples(graph: graph, count: 512) advanceBlock(engine) } let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 512) let maxAbsL: Double = out[0].prefix(512).map { abs($0) }.max() ?? 0.0 let maxAbsR: Double = out[1].prefix(512).map { abs($0) }.max() ?? 0.0 #expect(maxAbsL < 1e-12, "Silent input must produce silent L output; got maxAbsL=\(maxAbsL)") #expect(maxAbsR < 1e-12, "Silent input must produce silent R output; got maxAbsR=\(maxAbsR)") } // MARK: - 5. Mix=0 → passthrough (L and R both equal dry) @Test func mixZeroIsPassthrough() { let inputValue: Double = 0.6 let verb: PlateReverb.Audio = PlateReverb.Audio( input: ConstantGeneratorInput(inputValue), mix: 0.0 ) let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64) for i: Int in 0..<64 { #expect(abs(out[0][i] - inputValue) < 1e-12, "L ch at mix=0 must equal dry input at sample \(i)") #expect(abs(out[1][i] - inputValue) < 1e-12, "R ch at mix=0 must equal dry input at sample \(i)") } } // MARK: - 6. First sample deterministic @Test func firstSampleDeterministic() { // Silent in, mix=0 → both channels should be exactly 0 let verb: PlateReverb.Audio = PlateReverb.Audio(input: ConstantGeneratorInput(0.0), mix: 0.0) let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64) #expect(out[0][0] == 0.0, "L first sample must be 0 for silent input at mix=0") #expect(out[1][0] == 0.0, "R first sample must be 0 for silent input at mix=0") } // MARK: - 7. Left/right aliases return individual channels @Test func leftRightAliasesReturnIndividualChannels() { let verb: PlateReverb.Audio = PlateReverb.Audio( input: ConstantGeneratorInput(0.5), mix: 0.5, sampleRate: 44100 ) let leftOut: [[Double]] = verb.left.calculatedSamples(graph: graph, count: 64) let rightOut: [[Double]] = verb.right.calculatedSamples(graph: graph, count: 64) let fullOut: [[Double]] = verb.calculatedSamples(graph: graph, count: 64) for i: Int in 0..<64 { #expect(abs(leftOut[0][i] - fullOut[0][i]) < 1e-10, "left alias must match channel 0 at index \(i)") #expect(abs(rightOut[0][i] - fullOut[1][i]) < 1e-10, "right alias must match channel 1 at index \(i)") } } // MARK: - 8. Decay produces a tail @Test func decayProducesTail() { let impulse: PlateImpulseSource = PlateImpulseSource() let verb: PlateReverb.Audio = PlateReverb.Audio( input: impulse, decay: 0.7, mix: 1.0, sampleRate: 44100 ) // Block 0: inject impulse _ = verb.calculatedSamples(graph: graph, count: 512) advanceBlock(engine) // Accumulate energy over ~1 s post-impulse let blocksFor1s: Int = Int(44100.0 / 512) + 1 var totalEnergy: Double = 0.0 for _ in 0.. 1e-10, "After an impulse, tail energy must remain > epsilon for 1 s; got \(totalEnergy)") } // MARK: - 9. Larger decay → longer tail @Test func largerDecayMakesLongerTail() { let impShort: PlateImpulseSource = PlateImpulseSource() let impLong: PlateImpulseSource = PlateImpulseSource() let vShort: PlateReverb.Audio = PlateReverb.Audio(input: impShort, decay: 0.3, mix: 1.0, sampleRate: 44100) let vLong: PlateReverb.Audio = PlateReverb.Audio(input: impLong, decay: 0.85, mix: 1.0, sampleRate: 44100) // Block 0: inject impulse _ = vShort.calculatedSamples(graph: graph, count: 512) _ = vLong.calculatedSamples(graph: graph, count: 512) advanceBlock(engine) // Advance to ≈1 s post-impulse let blocksFor1s: Int = Int(44100.0 / 512) + 1 var tailShort: [[Double]] = [[]] var tailLong: [[Double]] = [[]] for _ in 0.. rmsShort, "Higher decay must produce larger RMS tail at 1 s; short=\(rmsShort), long=\(rmsLong)") } // MARK: - 10. Non-zero modulation breaks periodicity @Test func nonZeroModProducesTimeVaryingTail() { let imp1: PlateImpulseSource = PlateImpulseSource() let imp2: PlateImpulseSource = PlateImpulseSource() let vNoMod: PlateReverb.Audio = PlateReverb.Audio( input: imp1, decay: 0.85, modDepth: 0.0, mix: 1.0, sampleRate: 44100 ) let vMod: PlateReverb.Audio = PlateReverb.Audio( input: imp2, decay: 0.85, modDepth: 1.0, mix: 1.0, sampleRate: 44100 ) // Inject impulse on block 0 _ = vNoMod.calculatedSamples(graph: graph, count: 512) _ = vMod.calculatedSamples(graph: graph, count: 512) advanceBlock(engine) // Advance to let the tank fill — ~0.5 s let blocksFor0_5s: Int = Int(44100.0 * 0.5 / 512) + 1 for _ in 0.. Double { guard a.count == b.count, !a.isEmpty else { return 0 } var num: Double = 0.0 var den: Double = 0.0 for i: Int in 0.. 0 ? num / den : 0 } let corrNoMod: Double = lag1Autocorr(noModA, noModB) let corrMod: Double = lag1Autocorr(modA, modB) // Modulated version should have lower block-to-block autocorrelation // (modulation decorrelates consecutive blocks in the tail) #expect(abs(corrMod - corrNoMod) > 1e-6, "Modulated tail must differ from unmodulated in inter-block autocorrelation") } // MARK: - 11. Fan-out: single evaluation per segment @Test func fanOutSingleEvaluation() { let verb: PlateReverb.Audio = PlateReverb.Audio(input: ConstantGeneratorInput(0.5), mix: 0.5) let out1: [[Double]] = verb.calculatedSamples(graph: graph, count: 64) let out2: [[Double]] = verb.calculatedSamples(graph: graph, count: 64) for i: Int in 0..<64 { #expect(out1[0][i] == out2[0][i], "Fan-out: both L calls in same segment must be identical at index \(i)") #expect(out1[1][i] == out2[1][i], "Fan-out: both R calls in same segment must be identical at index \(i)") } } // MARK: - 12. mulAdd applied @Test func mulAddApplied() { // Silent in, mix=0 → output=0 before mulAdd; mul=2, add=1 → 1.0 let verb: PlateReverb.Audio = PlateReverb.Audio(input: ConstantGeneratorInput(0.0), mix: 0.0, mul: 2.0, add: 1.0) let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64) #expect(abs(out[0][0] - 1.0) < 1e-12, "mulAdd(2,1) on silent/passthrough must produce 1.0; got \(out[0][0])") #expect(abs(out[1][0] - 1.0) < 1e-12, "mulAdd(2,1) on silent/passthrough R must produce 1.0; got \(out[1][0])") } /// Phase 175 — branchless ring-buffer wrap exercised across many wraps of all /// internal delay lines (preDelay + dl1L/R + dl2L/R + 4 input diffusers + /// 2 modulated tank allpasses + 2 fixed tank allpasses). Drive a quiet /// non-zero input over ~3 s and assert output stays finite + bounded. /// Single shared engine; `advanceBlock` bumps `segmentSerial` so the /// generator's fan-out cache does not freeze on the first block's output. @Test func denormalLongTailStaysFinite() { let drive: ConstantGeneratorInput = ConstantGeneratorInput(1e-3) let verb: PlateReverb.Audio = PlateReverb.Audio( input: drive, decay: 0.6, bandwidth: 0.99, damping: 0.3, mix: 0.5) let loopEngine: SynthesisEngine = makeTestEngine(blockSize: 64) let blocks: Int = (Int(44100.0 * 3.0) / 64) + 1 var sawNonFinite: Bool = false var maxAbs: Double = 0.0 for _: Int in 0.. maxAbs { maxAbs = a } } } advanceBlock(loopEngine) } #expect(!sawNonFinite, "PlateReverb output must remain finite across many ring wraps") #expect(maxAbs < 1.0, "quiet-input output must remain bounded") } } // MARK: - Test helper — fires an impulse on first block only private final class PlateImpulseSource: GeneratorBase, AudioGenerator { private var _fired: Bool = false func floatValue(graph: SynthGraph) -> Double { 0.0 } func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] { precondition(count <= _result[0].count, "PlateImpulseSource: count \(count) exceeds buffer size \(_result[0].count)") _result[0][0] = _fired ? 0.0 : 1.0 for i: Int in 1..