prep for 1.2.0 release updates

This commit is contained in:
2026-06-21 11:45:58 -07:00
parent 73191c958f
commit 032b7c39d3
286 changed files with 21116 additions and 121 deletions
@@ -0,0 +1,228 @@
// SpringReverbTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("SpringReverb.Audio — spring-tank reverb")
struct SpringReverbTests {
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: SpringReverb.Audio = SpringReverb.Audio(input: ConstantGeneratorInput(0.0))
#expect(verb.rate == .audio)
}
// MARK: - 2. Output count
@Test func outputCountMatchesRequest() {
let verb: SpringReverb.Audio = SpringReverb.Audio(input: ConstantGeneratorInput(0.5))
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 512)
#expect(Array(out[0].prefix(512)).count == 512)
}
// MARK: - 3. Mono output by default
@Test func monoOutputByDefault() {
let verb: SpringReverb.Audio = SpringReverb.Audio(input: ConstantGeneratorInput(0.0), stereo: false)
#expect(verb.outputChannelCount == 1)
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64)
#expect(out.count == 1)
}
// MARK: - 4. Stereo output when flagged
@Test func stereoOutputWhenFlagged() {
let verb: SpringReverb.Audio = SpringReverb.Audio(input: ConstantGeneratorInput(0.0), stereo: true)
#expect(verb.outputChannelCount == 2)
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64)
#expect(out.count == 2)
}
// MARK: - 5. Silent input → silent output
@Test func silentInputProducesSilentOutput() {
// mix: 1.0, silent input → all wet, but wet is also silent (zero in → zero out)
let verb: SpringReverb.Audio = SpringReverb.Audio(input: ConstantGeneratorInput(0.0), mix: 1.0)
// Advance a few blocks to ensure any transient settles
for _ in 0..<5 {
_ = verb.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
}
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 512)
let maxAbs: Double = out[0].prefix(512).map { abs($0) }.max() ?? 0.0
#expect(maxAbs < 1e-12, "Silent input must produce silent output; got maxAbs=\(maxAbs)")
}
// MARK: - 6. Mix=0 → passthrough
@Test func mixZeroIsPassthrough() {
let inputValue: Double = 0.75
let verb: SpringReverb.Audio = SpringReverb.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,
"At mix=0, output must equal dry input at sample \(i)")
}
}
// MARK: - 7. First sample deterministic
@Test func firstSampleDeterministic() {
// Silent input + identity mul/add → first sample is 0 (nothing in tank yet)
let v1: SpringReverb.Audio = SpringReverb.Audio(input: ConstantGeneratorInput(0.0))
let out1: [[Double]] = v1.calculatedSamples(graph: graph, count: 64)
#expect(out1[0][0] == 0.0)
// With mul: 2.0, add: 1.0 and silent input:
// dry=0, wet=0, output = (1-mix)*0 + mix*0 = 0, then mul*0+add = 0*2+1 = 1
advanceBlock(engine)
let v2: SpringReverb.Audio = SpringReverb.Audio(input: ConstantGeneratorInput(0.0), mul: 2.0, add: 1.0)
let out2: [[Double]] = v2.calculatedSamples(graph: graph, count: 64)
#expect(abs(out2[0][0] - 1.0) < 1e-12,
"mul=2, add=1 on silent source must give first sample 1.0, got \(out2[0][0])")
}
// MARK: - 8. Decay produces a tail
@Test func decayProducesTail() {
let impulse: SpringImpulseSource = SpringImpulseSource()
let verb: SpringReverb.Audio = SpringReverb.Audio(
input: impulse,
decay: 2.0,
mix: 1.0,
sampleRate: 44100
)
// Block 0: inject impulse
_ = verb.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
// Advance ~0.5 s (≈22 blocks at 512 samples/block) and accumulate energy
let blocksFor0_5s: Int = Int(44100.0 * 0.5 / 512) + 1
var totalEnergy: Double = 0.0
for _ in 0..<blocksFor0_5s {
let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 512)
let blockEnergy: Double = out[0].prefix(512).reduce(0.0) { $0 + $1 * $1 }
totalEnergy += blockEnergy
advanceBlock(engine)
}
#expect(totalEnergy > 1e-10, "After an impulse, tail energy must remain > epsilon for at least 0.5 s; got \(totalEnergy)")
}
// MARK: - 9. Larger decay → longer tail
@Test func largerDecayMakesLongerTail() {
let impShort: SpringImpulseSource = SpringImpulseSource()
let impLong: SpringImpulseSource = SpringImpulseSource()
let vShort: SpringReverb.Audio = SpringReverb.Audio(input: impShort, decay: 0.5, mix: 1.0, sampleRate: 44100)
let vLong: SpringReverb.Audio = SpringReverb.Audio(input: impLong, decay: 4.0, mix: 1.0, sampleRate: 44100)
// Block 0: inject impulse in both
_ = 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..<blocksFor1s {
tailShort = vShort.calculatedSamples(graph: graph, count: 512)
tailLong = vLong.calculatedSamples(graph: graph, count: 512)
advanceBlock(engine)
}
let rmsShort: Double = rmsValue(Array(tailShort[0].prefix(512)))
let rmsLong: Double = rmsValue(Array(tailLong[0].prefix(512)))
#expect(rmsLong > rmsShort,
"Longer decay must produce larger RMS tail at 1 s; short=\(rmsShort), long=\(rmsLong)")
}
// MARK: - 10. Fan-out: single evaluation per segment
@Test func fanOutSingleEvaluation() {
let verb: SpringReverb.Audio = SpringReverb.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 calls in same segment must return identical samples at index \(i)")
}
}
// MARK: - 11. mulAdd applied
@Test func mulAddApplied() {
// Silent input: output before mulAdd = 0. After mul=2, add=1 → 1.0
let verb: SpringReverb.Audio = SpringReverb.Audio(input: ConstantGeneratorInput(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 input must produce 1.0; got \(out[0][0])")
}
/// Branchless ring-buffer wrap exercised across many wraps.
/// Render long enough that the main delay line (≈ 50 ms × 44100 ≈ 2205 samples)
/// wraps multiple times. Drive a quiet but non-zero input to keep ring values
/// non-trivial and verify output stays finite + bounded over the full render.
@Test func denormalLongTailStaysFinite() {
let drive: ConstantGeneratorInput = ConstantGeneratorInput(1e-3)
let verb: SpringReverb.Audio = SpringReverb.Audio(
input: drive, decay: 4.0, tension: 0.5, damping: 0.3,
mix: 0.5, preDelay: 0.005, sampleRate: 44100.0)
// ~3 s render — many wraps of the 50 ms main delay. Single shared engine;
// `advanceBlock` bumps `segmentSerial` so the generator's fan-out cache
// does not freeze on the first block's output.
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 v: Double in block[0] {
if !v.isFinite { sawNonFinite = true }
let a: Double = abs(v)
if a > maxAbs { maxAbs = a }
}
advanceBlock(loopEngine)
}
#expect(!sawNonFinite, "SpringReverb 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 SpringImpulseSource: 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,
"SpringImpulseSource: 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
}
}