// ResonatorBankTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // MARK: - Local DFT helper private func dftMag(samples: [Double], freq: Double, sampleRate: Double) -> Double { let n: Int = samples.count guard n > 0 else { return 0 } var re: Double = 0.0, im = 0.0 for i: Int in 0.. LyrebirdBuffer { let buf: LyrebirdBuffer = LyrebirdBuffer(count: triplets.count, channelCount: resonatorBankChannelCount) for (i, t) in triplets.enumerated() { buf.setValue(t.0, frame: i, channel: resonatorBankFreqChannel) buf.setValue(t.1, frame: i, channel: resonatorBankDecayChannel) buf.setValue(t.2, frame: i, channel: resonatorBankAmpChannel) } return buf } @Suite("ResonatorBank") struct ResonatorBankTests { let engine: SynthesisEngine let sr: Double = 44100 init() throws { engine = makeTestEngine(blockSize: 64, sampleRate: 44100) try engine.start() } @Test func dftPeaksAtAllModes() { // Two-mode bell-like bank driven by impulse: DFT energy should peak at both // resonant frequencies. let bank: LyrebirdBuffer = makeBank([ (440.0, 0.5, 1.0), (1320.0, 0.5, 1.0), ]) let impulse: Impulse.Audio = Impulse.Audio(freq: 1.0) let voice: ResonatorBank.Audio = ResonatorBank.Audio(input: impulse, buffer: bank) let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 200) let tail: [Double] = Array(samples.suffix(100 * engine.blockSize)) let mag440: Double = dftMag(samples: tail, freq: 440, sampleRate: sr) let mag1320: Double = dftMag(samples: tail, freq: 1320, sampleRate: sr) let mag200: Double = dftMag(samples: tail, freq: 200, sampleRate: sr) let mag800: Double = dftMag(samples: tail, freq: 800, sampleRate: sr) #expect(mag440 > mag200) #expect(mag440 > mag800) #expect(mag1320 > mag200) #expect(mag1320 > mag800) #expect(samples.allSatisfy { $0.isFinite }) } @Test func longerDecayRingsLonger() throws { let bank1: LyrebirdBuffer = makeBank([(440.0, 0.05, 1.0)]) let bank2: LyrebirdBuffer = makeBank([(440.0, 0.5, 1.0)]) let imp1: Impulse.Audio = Impulse.Audio(freq: 1.0) let r1: ResonatorBank.Audio = ResonatorBank.Audio(input: imp1, buffer: bank1) let s1: [Double] = collectBlocks(r1, engine: engine, blocks: 30) let rms1: Double = rmsValue(Array(s1.suffix(10 * engine.blockSize))) let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: sr) try engine2.start() let imp2: Impulse.Audio = Impulse.Audio(freq: 1.0) let r2: ResonatorBank.Audio = ResonatorBank.Audio(input: imp2, buffer: bank2) let s2: [Double] = collectBlocks(r2, engine: engine2, blocks: 30) let rms2: Double = rmsValue(Array(s2.suffix(10 * engine2.blockSize))) #expect(rms2 > rms1) } @Test func ampScalesOutput() throws { let bank1: LyrebirdBuffer = makeBank([(440.0, 0.5, 0.25)]) let bank2: LyrebirdBuffer = makeBank([(440.0, 0.5, 1.0)]) let imp1: Impulse.Audio = Impulse.Audio(freq: 1.0) let r1: ResonatorBank.Audio = ResonatorBank.Audio(input: imp1, buffer: bank1) let s1: [Double] = collectBlocks(r1, engine: engine, blocks: 40) let rms1: Double = rmsValue(s1) let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: sr) try engine2.start() let imp2: Impulse.Audio = Impulse.Audio(freq: 1.0) let r2: ResonatorBank.Audio = ResonatorBank.Audio(input: imp2, buffer: bank2) let s2: [Double] = collectBlocks(r2, engine: engine2, blocks: 40) let rms2: Double = rmsValue(s2) // amp=1.0 should be ~4× louder than amp=0.25 (single resonator, scalar gain on b0). #expect(rms2 > rms1 * 2.0) } @Test func deterministicFirstSampleMatchesFormula() { // Single resonator, step input, zero state. // First output = b0 = (1 − R) · amp where R = exp(log(0.001) / (decay · sr)). let decay: Double = 1.0 let amp: Double = 0.7 let bank: LyrebirdBuffer = makeBank([(440.0, decay, amp)]) let log0001: Double = log(0.001) let R: Double = exp(log0001 / (decay * sr)) let expected: Double = (1.0 - R) * amp let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let voice: ResonatorBank.Audio = ResonatorBank.Audio(input: dc, buffer: bank) let g: SynthGraph = makeSynthGraph(engine: engine) let samples: [[Double]] = voice.calculatedSamples(graph: g, count: engine.blockSize) #expect(abs(Double(samples[0][0]) - expected) < 1e-10) } @Test func sumOfTwoResonatorsEqualsSumOfTwoFilters() { // Bank of [(f1, d, a1), (f2, d, a2)] driven by impulse should equal // (a1·Ringz(f1, d) + a2·Ringz(f2, d)) sample-for-sample under the same exciter. // Using ResonatingFilter as the trusted reference, scaled by amp. let f1: Double = 300.0; let f2: Double = 1100.0; let d: Double = 0.3 let a1: Double = 0.8; let a2: Double = 0.4 let bank: LyrebirdBuffer = makeBank([(f1, d, a1), (f2, d, a2)]) let impulse: ConstantGeneratorInput = ConstantGeneratorInput(1.0) // DC step matches first sample test let voice: ResonatorBank.Audio = ResonatorBank.Audio(input: impulse, buffer: bank) let g: SynthGraph = makeSynthGraph(engine: engine) let bankOut: [Double] = voice.calculatedSamples(graph: g, count: engine.blockSize)[0] let log0001: Double = log(0.001) let R: Double = exp(log0001 / (d * sr)) let b0_1: Double = (1.0 - R) * a1 let b0_2: Double = (1.0 - R) * a2 let na1_1: Double = -2.0 * R * cos(2.0 * .pi * f1 / sr) let na1_2: Double = -2.0 * R * cos(2.0 * .pi * f2 / sr) let na2: Double = R * R var y1_1: Double = 0.0, y2_1 = 0.0, y1_2 = 0.0, y2_2 = 0.0 for i: Int in 0.. 100 · 64 / 44100 ≈ 0.145 s of decay-tail evaluation). _ = collectBlocks(voice, engine: engine, blocks: 100) let postEdit: [Double] = collectBlocks(voice, engine: engine, blocks: 200) let mag440Second: Double = dftMag( samples: Array(postEdit.suffix(100 * engine.blockSize)), freq: 440, sampleRate: sr ) let mag1500Second: Double = dftMag( samples: Array(postEdit.suffix(100 * engine.blockSize)), freq: 1500, sampleRate: sr ) #expect(mag1500Second > mag440Second) #expect(mag440First > 0) } @Test func denormalsDoNotAccumulate() { let bank: LyrebirdBuffer = makeBank([ (440.0, 0.01, 1.0), (1100.0, 0.01, 1.0), ]) let impulse: Impulse.Audio = Impulse.Audio(freq: 1.0) let voice: ResonatorBank.Audio = ResonatorBank.Audio(input: impulse, buffer: bank) let samples: [Double] = collectBlocks(voice, engine: engine, blocks: 200) #expect(samples.allSatisfy { $0.isFinite }) #expect(samples.allSatisfy { !$0.isSubnormal }) } @Test func participatesInFanOutCache() { let bank: LyrebirdBuffer = makeBank([(440.0, 0.5, 1.0)]) let noise: WhiteNoise.Audio = WhiteNoise.Audio() let voice: ResonatorBank.Audio = ResonatorBank.Audio(input: noise, buffer: bank) let g: SynthGraph = makeSynthGraph(engine: engine) let r1: [[Double]] = voice.calculatedSamples(graph: g, count: engine.blockSize) let r2: [[Double]] = voice.calculatedSamples(graph: g, count: engine.blockSize) #expect(r1[0].prefix(engine.blockSize).elementsEqual(r2[0].prefix(engine.blockSize))) } @Test func invalidateProducesSilence() { let bank: LyrebirdBuffer = makeBank([(440.0, 0.5, 1.0)]) let dc: ConstantGeneratorInput = ConstantGeneratorInput(1.0) let voice: ResonatorBank.Audio = ResonatorBank.Audio(input: dc, buffer: bank) // Warm-up: one block of normal output. _ = collectBlocks(voice, engine: engine, blocks: 1) bank.invalidate() let after: [Double] = collectBlocks(voice, engine: engine, blocks: 2) #expect(after.allSatisfy { $0 == 0 }) } @Test func mulAddAppliesAfterSummation() throws { let bank: LyrebirdBuffer = makeBank([(440.0, 0.5, 1.0), (880.0, 0.5, 1.0)]) let imp1: Impulse.Audio = Impulse.Audio(freq: 1.0) let baseline: ResonatorBank.Audio = ResonatorBank.Audio(input: imp1, buffer: bank) let bSamples: [Double] = collectBlocks(baseline, engine: engine, blocks: 4) let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: sr) try engine2.start() let imp2: Impulse.Audio = Impulse.Audio(freq: 1.0) let scaled: ResonatorBank.Audio = ResonatorBank.Audio(input: imp2, buffer: bank, mul: 2.0, add: 0.5) let sSamples: [Double] = collectBlocks(scaled, engine: engine2, blocks: 4) let n: Int = min(bSamples.count, sSamples.count) for i: Int in 0..