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

203 lines
9.0 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.
// MoogLadderTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - MoogLadderTests
@Suite("MoogLadder")
struct MoogLadderTests {
let engine: SynthesisEngine
let blockSize: Int = 64
let sampleRate: Double = 44100
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
var graph: SynthGraph { makeSynthGraph(engine: engine) }
// MARK: 1. Rate
@Test func audioRate() {
let f: MoogLadder.Audio = MoogLadder.Audio(input: 0.0, freq: 1000.0, resonance: 0.0)
#expect(f.rate == .audio)
}
// MARK: 2. Output channel count — mono
@Test func audioOutputChannelCount() {
let f: MoogLadder.Audio = MoogLadder.Audio(input: 0.0, freq: 1000.0)
#expect(f.outputChannelCount == 1)
let g: SynthGraph = graph
let result: [[Double]] = f.calculatedSamples(graph: g, count: blockSize)
#expect(result.count == 1)
#expect(Array(result[0].prefix(blockSize)).count == blockSize)
}
// MARK: 3. Fan-out cache
@Test func audioFanOutCacheHit() {
let f: MoogLadder.Audio = MoogLadder.Audio(input: 1.0, freq: 1000.0, resonance: 0.5)
let g: SynthGraph = graph
let first: [[Double]] = f.calculatedSamples(graph: g, count: blockSize)
let second: [[Double]] = f.calculatedSamples(graph: g, count: blockSize)
#expect(Array(first[0].prefix(blockSize)) == Array(second[0].prefix(blockSize)))
}
// MARK: 4. Integration — runs without producing NaN/Inf
@Test func integrationNoNaN() {
let f: MoogLadder.Audio = MoogLadder.Audio(input: WhiteNoise.Audio(), freq: 800.0, resonance: 0.7)
let samples: [Double] = collectBlocks(f, engine: engine, blocks: 8)
for s: Double in samples {
#expect(s.isFinite, "MoogLadder output must be finite; got \(s)")
}
}
// MARK: 5. Lowpass passes DC after settle
@Test func lowpassPassesDC() {
// Constant DC input — after settle, output should approach input level.
// 4-pole ladder has substantial settling time at low cutoffs, so use moderate cutoff.
let f: MoogLadder.Audio = MoogLadder.Audio(input: 0.5, freq: 2000.0, resonance: 0.0)
for _ in 0..<128 {
let g: SynthGraph = makeSynthGraph(engine: engine)
_ = f.calculatedSamples(graph: g, count: blockSize)
advanceBlock(engine)
}
let g: SynthGraph = makeSynthGraph(engine: engine)
let result: [[Double]] = f.calculatedSamples(graph: g, count: blockSize)
let mean: Double = Array(result[0].prefix(blockSize)).reduce(0.0, +) / Double(blockSize)
// tanh saturation at the input gain compresses DC slightly; expect within 25%.
#expect(abs(mean - 0.5) < 0.125,
"Lowpass DC steady state should be near input (0.5); got \(mean)")
}
// MARK: 6. Highs are attenuated more than lows
@Test func cutoffAttenuatesHigherFrequencies() {
// White noise through low cutoff ladder vs high cutoff ladder.
// Low cutoff should produce significantly less RMS energy.
let noise: WhiteNoise.Audio = WhiteNoise.Audio()
let fLow: MoogLadder.Audio = MoogLadder.Audio(input: noise, freq: 200.0, resonance: 0.0)
let fHigh: MoogLadder.Audio = MoogLadder.Audio(input: noise, freq: 10000.0, resonance: 0.0)
let settleBlocks: Int = 32
let measureBlocks: Int = 64
var sumSqLow: Double = 0.0
var sumSqHigh: Double = 0.0
for _ in 0..<settleBlocks {
let g: SynthGraph = makeSynthGraph(engine: engine)
_ = fLow.calculatedSamples(graph: g, count: blockSize)
_ = fHigh.calculatedSamples(graph: g, count: blockSize)
advanceBlock(engine)
}
for _ in 0..<measureBlocks {
let g: SynthGraph = makeSynthGraph(engine: engine)
let lo: [Double] = Array(fLow.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
let hi: [Double] = Array(fHigh.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
sumSqLow += lo.map { $0 * $0 }.reduce(0.0, +)
sumSqHigh += hi.map { $0 * $0 }.reduce(0.0, +)
advanceBlock(engine)
}
let rmsLow: Double = (sumSqLow / Double(measureBlocks * blockSize)).squareRoot()
let rmsHigh: Double = (sumSqHigh / Double(measureBlocks * blockSize)).squareRoot()
#expect(rmsHigh > rmsLow * 2.0,
"High-cutoff RMS (\(rmsHigh)) should exceed low-cutoff RMS (\(rmsLow)) by ≥2×")
}
// MARK: 7. High resonance amplifies impulse response vs low resonance
@Test func highResonanceRingsLouderThanLow() {
// For an impulse input, a high-resonance ladder rings substantially
// longer / louder than a near-zero-resonance one.
let lo: MoogLadder.Audio = MoogLadder.Audio(input: Impulse.Audio(freq: 1.0), freq: 1000.0, resonance: 0.0)
let hi: MoogLadder.Audio = MoogLadder.Audio(input: Impulse.Audio(freq: 1.0), freq: 1000.0, resonance: 1.2)
let loSamples: [Double] = collectBlocks(lo, engine: engine, blocks: 16)
let hiSamples: [Double] = collectBlocks(hi, engine: engine, blocks: 16)
for s: Double in hiSamples { #expect(s.isFinite) }
let rmsLo: Double = rmsValue(loSamples)
let rmsHi: Double = rmsValue(hiSamples)
#expect(rmsHi > rmsLo * 2.0,
"High-resonance impulse RMS (\(rmsHi)) should exceed low-resonance RMS (\(rmsLo)) by ≥2×")
}
// MARK: 8. Generator cutoff input is honored
@Test func generatorCutoffInputResponds() {
// LFO-modulated cutoff should produce a time-varying output.
let lfo: LFTriangle.Audio = LFTriangle.Audio(freq: 4.0, mul: 1500.0, add: 2000.0)
let f: MoogLadder.Audio = MoogLadder.Audio(input: WhiteNoise.Audio(), freq: lfo, resonance: 0.3)
let samples: [Double] = collectBlocks(f, engine: engine, blocks: 16)
for s: Double in samples { #expect(s.isFinite) }
let rms: Double = (samples.map { $0 * $0 }.reduce(0.0, +) / Double(samples.count)).squareRoot()
#expect(rms > 0.0)
}
// MARK: 9. Generator resonance input is honored
@Test func generatorResonanceInputResponds() {
let resLfo: LFTriangle.Audio = LFTriangle.Audio(freq: 0.5, mul: 0.4, add: 0.6) // sweeps roughly [0.2, 1.0]
let f: MoogLadder.Audio = MoogLadder.Audio(input: WhiteNoise.Audio(), freq: 600.0, resonance: resLfo)
let samples: [Double] = collectBlocks(f, engine: engine, blocks: 16)
for s: Double in samples { #expect(s.isFinite) }
}
// MARK: 10. Negative cutoff is clamped, not exploded
@Test func negativeCutoffClampedToMinimum() {
let f: MoogLadder.Audio = MoogLadder.Audio(input: WhiteNoise.Audio(), freq: -500.0, resonance: 0.0)
let samples: [Double] = collectBlocks(f, engine: engine, blocks: 4)
for s: Double in samples { #expect(s.isFinite) }
}
// MARK: 11. Resonance clamp at maxResonance
@Test func resonanceClampedAtMax() {
// Exceeding moogLadderMaxResonance shouldn't NaN out.
let f: MoogLadder.Audio = MoogLadder.Audio(input: WhiteNoise.Audio(), freq: 1000.0, resonance: 5.0)
let samples: [Double] = collectBlocks(f, engine: engine, blocks: 8)
for s: Double in samples { #expect(s.isFinite) }
}
// MARK: 12. mul/add applied to output
@Test func mulAddApplied() {
let f: MoogLadder.Audio = MoogLadder.Audio(input: 0.5, freq: 5000.0, resonance: 0.0, mul: 2.0, add: 0.1)
// Settle
for _ in 0..<128 {
let g: SynthGraph = makeSynthGraph(engine: engine)
_ = f.calculatedSamples(graph: g, count: blockSize)
advanceBlock(engine)
}
let g: SynthGraph = makeSynthGraph(engine: engine)
let result: [[Double]] = f.calculatedSamples(graph: g, count: blockSize)
let mean: Double = Array(result[0].prefix(blockSize)).reduce(0.0, +) / Double(blockSize)
// res=0 → xfb = x = 0.5. Each stage settles to the value where
// tanh(yN) = tanh(driver). Stage 1 driver is tanh(0.5) → y1 settles to
// atanh(tanh(0.5)) = 0.5. The same identity propagates through stages
// 2–4, so settled raw output = 0.5. mul=2, add=0.1 → expected = 1.1.
let expected: Double = 2.0 * 0.5 + 0.1
#expect(abs(mean - expected) < 0.01,
"mul/add output should be ≈ \(expected); got \(mean)")
}
// MARK: 13. Reset-on-init: state starts at zero (silent input → silent output)
@Test func silentInputProducesSilentOutput() {
let f: MoogLadder.Audio = MoogLadder.Audio(input: 0.0, freq: 1000.0, resonance: 0.5)
let samples: [Double] = collectBlocks(f, engine: engine, blocks: 4)
for s: Double in samples {
#expect(s == 0.0, "Silent input + zero-init state must produce silence; got \(s)")
}
}
}