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

305 lines
12 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// GVerbTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("GVerb — Griesinger algorithmic stereo reverb")
struct GVerbTests {
let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
var graph: SynthGraph { makeSynthGraph(engine: engine) }
init() throws {
try engine.start()
}
// MARK: - Output shape
@Test func outputIsStereo() {
let verb: GVerb.Audio = GVerb.Audio(source: ConstantGeneratorInput(0.5), sampleRate: 44100)
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64)
#expect(out.count == 2, "GVerb must produce exactly 2 channels")
#expect(out[0].count >= 64)
#expect(out[1].count >= 64)
}
// MARK: - Dry pass-through at earlyLevel=tailLevel=0
@Test func zeroLevelsProduceSilence() {
let verb: GVerb.Audio = GVerb.Audio(
source: ConstantGeneratorInput(0.8),
earlyLevel: 0.0,
tailLevel: 0.0,
sampleRate: 44100
)
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64)
for i: Int in 0..<64 {
#expect(abs(out[0][i]) < 1e-10,
"L ch must be silent when earlyLevel=tailLevel=0 (sample \(i))")
#expect(abs(out[1][i]) < 1e-10,
"R ch must be silent when earlyLevel=tailLevel=0 (sample \(i))")
}
}
// MARK: - Impulse RT60 within expected range at reverbTime=3.0
@Test func impulseRT60WithinRange() {
// Inject a single impulse, capture energy evolution over many blocks.
// With reverbTime=3.0 s, the tail energy should still be non-zero at 1 s
// and significantly decayed by 6 s.
let impulse: GVerbImpulseSource = GVerbImpulseSource()
let verb: GVerb.Audio = GVerb.Audio(
source: impulse,
roomSize: 10.0,
reverbTime: 3.0,
damping: 0.3,
earlyLevel: 0.0, // isolate late tail
tailLevel: 1.0,
sampleRate: 44100
)
// Drain enough blocks for early reflections to settle.
let blockSize: Int = 512
let blocksPerSecond: Int = 44100 / blockSize // ~86 blocks/s
// Warm up — inject impulse on block 0, advance.
_ = verb.calculatedSamples(graph: graph, count: blockSize)
advanceBlock(engine)
// Accumulate energy at ~1 s (expect significant tail).
var energyAt1s: Double = 0.0
let warmupBlocks: Int = blocksPerSecond
for _ in 0..<warmupBlocks {
let blk: [[Double]] = verb.calculatedSamples(graph: graph, count: blockSize)
energyAt1s += blk[0].prefix(blockSize).reduce(0.0) { $0 + $1 * $1 }
advanceBlock(engine)
}
// Accumulate energy at ~5–6 s (expect substantially lower).
var energyAt5s: Double = 0.0
let laterBlocks: Int = blocksPerSecond * 4 // advance another 4 seconds
for _ in 0..<laterBlocks {
let blk: [[Double]] = verb.calculatedSamples(graph: graph, count: blockSize)
energyAt5s += blk[0].prefix(blockSize).reduce(0.0) { $0 + $1 * $1 }
advanceBlock(engine)
}
// Reverb tail should have non-zero energy at 1 s.
#expect(energyAt1s > 0.0, "Late tail must have energy ~1 s after impulse (reverbTime=3.0)")
// And substantially less energy at 5 s (tail must decay).
#expect(energyAt5s < energyAt1s,
"Late tail must decay: energy at 5 s must be less than at 1 s")
}
// MARK: - roomSize change affects tail without crash
@Test func roomSizeChangeNoCrash() {
let verb: GVerb.Audio = GVerb.Audio(source: ConstantGeneratorInput(0.5), sampleRate: 44100)
// Warm up at default roomSize.
for _ in 0..<10 {
_ = verb.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
}
// Change roomSize mid-stream via a new ConstantGeneratorInput — no crash expected.
// (In practice roomSize is read each block via floatValue.)
let verbLarge: GVerb.Audio = GVerb.Audio(
source: ConstantGeneratorInput(0.5),
roomSize: 80.0,
sampleRate: 44100
)
for _ in 0..<10 {
_ = verbLarge.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
}
// If we reach here without trap/crash the test passes.
#expect(Bool(true), "roomSize change must not crash or exceed buffer bounds")
}
// MARK: - Early and tail levels independently audible (NRT energy check)
@Test func earlyAndTailLevelsAreIndependent() {
let blockSize: Int = 512
let warmup: Int = 20
// Early only
let impulseE: GVerbImpulseSource = GVerbImpulseSource()
let verbEarly: GVerb.Audio = GVerb.Audio(
source: impulseE,
roomSize: 10.0, reverbTime: 3.0,
earlyLevel: 1.0, tailLevel: 0.0,
sampleRate: 44100
)
// Tail only
let impulseT: GVerbImpulseSource = GVerbImpulseSource()
let verbTail: GVerb.Audio = GVerb.Audio(
source: impulseT,
roomSize: 10.0, reverbTime: 3.0,
earlyLevel: 0.0, tailLevel: 1.0,
sampleRate: 44100
)
var earlyEnergy: Double = 0.0
var tailEnergy: Double = 0.0
for _ in 0..<warmup {
let blkE: [[Double]] = verbEarly.calculatedSamples(graph: graph, count: blockSize)
let blkT: [[Double]] = verbTail.calculatedSamples(graph: graph, count: blockSize)
earlyEnergy += blkE[0].prefix(blockSize).reduce(0.0) { $0 + $1 * $1 }
tailEnergy += blkT[0].prefix(blockSize).reduce(0.0) { $0 + $1 * $1 }
advanceBlock(engine)
}
#expect(earlyEnergy > 0.0, "Early reflections must produce non-zero energy")
#expect(tailEnergy > 0.0, "Tail reverb must produce non-zero energy")
}
// MARK: - Stereo L/R are distinct
@Test func stereoLRDistinct() {
let verb: GVerb.Audio = GVerb.Audio(
source: ConstantGeneratorInput(1.0),
roomSize: 15.0, reverbTime: 3.0,
earlyLevel: 0.5, tailLevel: 0.5,
sampleRate: 44100
)
// Warm up.
for _ in 0..<15 {
_ = verb.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
}
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 512)
let anyDiff: Bool = zip(out[0].prefix(512), out[1].prefix(512))
.contains { abs($0 - $1) > 1e-8 }
#expect(anyDiff, "L and R channels must differ (asymmetric post-sum allpass diffuser chains)")
}
// MARK: - Parameters update mid-stream (no RT alloc — no crash)
@Test func parametersUpdateMidStream() {
// Use ConstantGeneratorInput values — replaced by different instances each block
// to ensure parameter reads go through the protocol path every block.
let verb: GVerb.Audio = GVerb.Audio(
source: ConstantGeneratorInput(0.3),
roomSize: ConstantGeneratorInput(10.0),
reverbTime: ConstantGeneratorInput(2.0),
damping: ConstantGeneratorInput(0.5),
sampleRate: 44100
)
for _ in 0..<5 {
_ = verb.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
}
// Create new verb with different params — simulates mid-stream param change.
let verb2: GVerb.Audio = GVerb.Audio(
source: ConstantGeneratorInput(0.3),
roomSize: ConstantGeneratorInput(50.0),
reverbTime: ConstantGeneratorInput(5.0),
damping: ConstantGeneratorInput(0.8),
sampleRate: 44100
)
for _ in 0..<5 {
_ = verb2.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
}
#expect(Bool(true), "Parameter changes must not crash or allocate on the RT thread")
}
// MARK: - dryLevel passes direct signal at unity
@Test func dryLevelPassesDirectSignal() {
// With earlyLevel=0 and tailLevel=0, only the dry bypass path is active.
// dryLevel=1.0 on cold buffers: first output sample must equal the first input sample
// (after the one-pole input damper with inputBandwidth=1.0, which is pass-through).
let blockSize: Int = 64
let verb: GVerb.Audio = GVerb.Audio(
source: ConstantGeneratorInput(0.5),
inputBandwidth: 1.0,
earlyLevel: 0.0,
tailLevel: 0.0,
dryLevel: 1.0,
sampleRate: 44100
)
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: blockSize)
// All samples should equal 0.5 (dry bypass, no early/tail contribution).
for i: Int in 0..<blockSize {
#expect(abs(out[0][i] - 0.5) < 1e-10,
"L ch dry bypass must equal input 0.5 (sample \(i))")
#expect(abs(out[1][i] - 0.5) < 1e-10,
"R ch dry bypass must equal input 0.5 (sample \(i))")
}
}
// MARK: - Channel aliases match subscripts
@Test func channelAliasesMatchSubscripts() {
let verb: GVerb.Audio = GVerb.Audio(source: ConstantGeneratorInput(1.0), earlyLevel: 0.4, tailLevel: 0.4,
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,
"verb.left[i] must equal verb[0][i]")
#expect(abs(rightOut[0][i] - fullOut[1][i]) < 1e-10,
"verb.right[i] must equal verb[1][i]")
}
}
/// Phase 175 — branchless ring-buffer wrap exercised across many wraps of the
/// tap delay (44000 samples ≈ 1 s) and 4 FDN delay lines. 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: GVerb.Audio = GVerb.Audio(
source: drive, roomSize: 20.0, reverbTime: 5.0,
damping: 0.3, earlyLevel: 0.4, tailLevel: 0.5,
sampleRate: 44100)
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..<blocks {
let g: SynthGraph = makeSynthGraph(engine: loopEngine)
let block: [[Double]] = verb.calculatedSamples(graph: g, count: 64)
for ch: Int in 0..<2 {
for v: Double in block[ch] {
if !v.isFinite { sawNonFinite = true }
let a: Double = abs(v)
if a > maxAbs { maxAbs = a }
}
}
advanceBlock(loopEngine)
}
#expect(!sawNonFinite, "GVerb output must remain finite across many ring wraps")
#expect(maxAbs < 1.0, "quiet-input output must remain bounded")
}
}
// MARK: - Test helpers
/// Emits 1.0 at sample 0 of the first block; 0.0 on every subsequent block.
private final class GVerbImpulseSource: GeneratorBase, AudioGenerator {
private var _fired: Bool = false
public func floatValue(graph: SynthGraph) -> Double { 0.0 }
public func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
precondition(count <= _result[0].count, "GVerbImpulseSource: count \(count) exceeds buffer size \(_result[0].count)")
_result[0][0] = _fired ? 0.0 : 1.0
for i: Int in 1..<count { _result[0][i] = 0.0 }
_fired = true
return _result
}
}