204 lines
9.0 KiB
Swift
204 lines
9.0 KiB
Swift
// FreeVerbTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("FreeVerb — stereo reverb")
|
|
struct FreeVerbTests {
|
|
|
|
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: FreeVerb.Audio = FreeVerb.Audio(source: ConstantGeneratorInput(0.5))
|
|
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64)
|
|
#expect(out.count == 2, "FreeVerb must produce exactly 2 channels")
|
|
#expect(out[0].count >= 64)
|
|
#expect(out[1].count >= 64)
|
|
}
|
|
|
|
// MARK: - Dry pass-through
|
|
|
|
@Test func mixZeroPassesDryThrough() {
|
|
let input: Double = 0.6
|
|
let verb: FreeVerb.Audio = FreeVerb.Audio(source: ConstantGeneratorInput(input), mix: 0.0)
|
|
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64)
|
|
for i: Int in 0..<64 {
|
|
#expect(abs(out[0][i] - input) < 1e-10,
|
|
"L ch at mix=0 must equal dry input at sample \(i)")
|
|
#expect(abs(out[1][i] - input) < 1e-10,
|
|
"R ch at mix=0 must equal dry input at sample \(i)")
|
|
}
|
|
}
|
|
|
|
// MARK: - Wet output differs from dry
|
|
|
|
@Test func mixOneWetDiffersFromDry() {
|
|
// Run constant-input blocks until delay lines fill; wet output must differ from input.
|
|
let verb: FreeVerb.Audio = FreeVerb.Audio(source: ConstantGeneratorInput(1.0), roomSize: 0.8,
|
|
mix: 1.0, sampleRate: 44100)
|
|
for _ in 0..<10 {
|
|
_ = verb.calculatedSamples(graph: graph, count: 512)
|
|
advanceBlock(engine)
|
|
}
|
|
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 512)
|
|
let anyDiffersL: Bool = out[0].prefix(512).contains { abs($0 - 1.0) > 1e-6 }
|
|
#expect(anyDiffersL, "Wet output must differ from dry input once delay lines are filled")
|
|
}
|
|
|
|
// MARK: - Impulse response decays
|
|
|
|
@Test func impulseResponseDecays() {
|
|
let impulse: ImpulseSource = ImpulseSource()
|
|
let verb: FreeVerb.Audio = FreeVerb.Audio(source: impulse, roomSize: 0.8, mix: 1.0, sampleRate: 44100)
|
|
|
|
// Block 0: inject impulse — writes into comb delay lines, output is 0 (nothing to
|
|
// read back yet; smallest comb size is 1116 > 512 samples/block).
|
|
_ = verb.calculatedSamples(graph: graph, count: 512)
|
|
advanceBlock(engine)
|
|
|
|
// Blocks 1-2: advance until the first comb reflection wraps around.
|
|
// Smallest comb (1116 samples) first echoes at sample 1116 → block 2, offset 92.
|
|
_ = verb.calculatedSamples(graph: graph, count: 512)
|
|
advanceBlock(engine)
|
|
|
|
// Capture early tail energy (block 2 — first reflections audible).
|
|
let earlyOut: [[Double]] = verb.calculatedSamples(graph: graph, count: 512)
|
|
let earlyEnergy: Double = earlyOut[0].prefix(512).reduce(0.0) { $0 + $1 * $1 }
|
|
advanceBlock(engine)
|
|
|
|
// Advance 30 more blocks, then capture late tail.
|
|
// At roomSize=0.8, feedback ≈ 0.924; after 30 blocks each comb decays to ~34%.
|
|
var lateOut: [[Double]] = [[]]
|
|
for _ in 0..<30 {
|
|
lateOut = verb.calculatedSamples(graph: graph, count: 512)
|
|
advanceBlock(engine)
|
|
}
|
|
let lateEnergy: Double = lateOut[0].prefix(512).reduce(0.0) { $0 + $1 * $1 }
|
|
|
|
#expect(earlyEnergy > 0, "Early tail must have non-zero energy after first reflections")
|
|
#expect(lateEnergy < earlyEnergy, "Reverb tail must decay over time")
|
|
}
|
|
|
|
// MARK: - Stereo channels are distinct
|
|
|
|
@Test func leftAndRightAreDistinct() {
|
|
let verb: FreeVerb.Audio = FreeVerb.Audio(source: ConstantGeneratorInput(1.0), roomSize: 0.5,
|
|
mix: 1.0, spread: 23, sampleRate: 44100)
|
|
for _ in 0..<5 {
|
|
_ = 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 due to spread detuning")
|
|
}
|
|
|
|
// MARK: - roomSize affects decay rate
|
|
|
|
@Test func largerRoomSizeSlowerDecay() {
|
|
let impSml: ImpulseSource = ImpulseSource()
|
|
let impLrg: ImpulseSource = ImpulseSource()
|
|
let vSml: FreeVerb.Audio = FreeVerb.Audio(source: impSml, roomSize: 0.2, mix: 1.0, sampleRate: 44100)
|
|
let vLrg: FreeVerb.Audio = FreeVerb.Audio(source: impLrg, roomSize: 0.9, mix: 1.0, sampleRate: 44100)
|
|
|
|
// Both receive impulse on block 0.
|
|
_ = vSml.calculatedSamples(graph: graph, count: 512)
|
|
_ = vLrg.calculatedSamples(graph: graph, count: 512)
|
|
advanceBlock(engine)
|
|
|
|
// Advance 30 blocks; capture late tail.
|
|
var tailSml: [[Double]] = [[]]
|
|
var tailLrg: [[Double]] = [[]]
|
|
for _ in 0..<30 {
|
|
tailSml = vSml.calculatedSamples(graph: graph, count: 512)
|
|
tailLrg = vLrg.calculatedSamples(graph: graph, count: 512)
|
|
advanceBlock(engine)
|
|
}
|
|
let energySml: Double = tailSml[0].prefix(512).reduce(0.0) { $0 + $1 * $1 }
|
|
let energyLrg: Double = tailLrg[0].prefix(512).reduce(0.0) { $0 + $1 * $1 }
|
|
#expect(energyLrg > energySml,
|
|
"Larger roomSize must produce more energy in the late tail")
|
|
}
|
|
|
|
// MARK: - Deterministic sample pin
|
|
|
|
@Test func firstCombEchoIsExact() {
|
|
// With roomSize=0 (feedback=0.7), damping=0 (LP transparent: damp1=0 → state=out),
|
|
// mix=1, spread=23 at 44100 Hz:
|
|
//
|
|
// Comb echo: smallest comb = 1116 samples. First echo at absolute sample 1116
|
|
// = block 2, index 92. Only that one comb has wrapped; others are larger → sumL = out0.
|
|
//
|
|
// Allpass chain: all 4 allpass buffers are still zero at index 92 (confirmed by
|
|
// tracing: each allpass size ≤ 556 samples; every position written before sample
|
|
// 1116 received apL=0 input, so all positions hold 0). With zero-filled buffers,
|
|
// each stage computes apOut = -input + 0 = -input. Four negations: (-1)^4 = +1.
|
|
// Net allpass effect is identity, value preserved exactly.
|
|
//
|
|
// Expected: comb[0] reads buf[0] = freeVerbFixedGain (0.015), LP state = 0.015
|
|
// (damp1=0 → no smoothing), sumL = 0.015, allpass identity → result = 0.015.
|
|
let impulse: ImpulseSource = ImpulseSource()
|
|
let verb: FreeVerb.Audio = FreeVerb.Audio(source: impulse, roomSize: 0.0, damping: 0.0,
|
|
mix: 1.0, spread: 23, sampleRate: 44100)
|
|
|
|
// Block 0: impulse at sample 0 written into comb delay positions.
|
|
_ = verb.calculatedSamples(graph: graph, count: 512)
|
|
advanceBlock(engine)
|
|
|
|
// Block 1: no comb has wrapped yet (smallest comb = 1116 > 1023).
|
|
_ = verb.calculatedSamples(graph: graph, count: 512)
|
|
advanceBlock(engine)
|
|
|
|
// Block 2: first comb wraps at sample 1116 = index 92 within this block.
|
|
let block2: [[Double]] = verb.calculatedSamples(graph: graph, count: 512)
|
|
#expect(abs(block2[0][92] - 0.015) < 1e-12,
|
|
"First echo from 1116-sample comb must equal freeVerbFixedGain at block-2 index 92")
|
|
}
|
|
|
|
// MARK: - Channel aliases
|
|
|
|
@Test func channelAliasesMatchSubscripts() {
|
|
// All three calls share the same segment; fan-out cache ensures FreeVerb
|
|
// computes once and left/right extract from the cached 2-channel result.
|
|
let verb: FreeVerb.Audio = FreeVerb.Audio(source: ConstantGeneratorInput(1.0), 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)
|
|
#expect(abs(rightOut[0][i] - fullOut[1][i]) < 1e-10)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Test helpers
|
|
|
|
/// Emits 1.0 at sample 0 of the first block; 0.0 on every subsequent block.
|
|
private final class ImpulseSource: 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, "ImpulseSource: count \(count) exceeds buffer size \(_result[0].count)")
|
|
// _fired is permanent — set on first _computeSamples call, never reset.
|
|
// advanceBlock() increments segment serial so subsequent blocks get fresh calls here.
|
|
_result[0][0] = _fired ? 0.0 : 1.0
|
|
for i: Int in 1..<count { _result[0][i] = 0.0 }
|
|
_fired = true
|
|
return _result
|
|
}
|
|
}
|