1.0.0 release

This commit is contained in:
2026-06-01 18:27:40 -07:00
parent b1ab0f102c
commit 1f20fcab20
1151 changed files with 230703 additions and 14 deletions
+494
View File
@@ -0,0 +1,494 @@
// MuTronIIITests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Darwin
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - MuTronIIITests
@Suite("MuTronIII")
struct MuTronIIITests {
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 wah: MuTronIII.Audio = MuTronIII.Audio(input: 0.0)
#expect(wah.rate == .audio)
}
@Test func controlRate() {
let wah: MuTronIII.Control = MuTronIII.Control(input: 0.0)
#expect(wah.rate == .control)
}
// MARK: 2. Output count matches request
@Test func audioOutputCount() {
let wah: MuTronIII.Audio = MuTronIII.Audio(input: 0.0)
let g: SynthGraph = graph
let result: [[Double]] = wah.calculatedSamples(graph: g, count: blockSize)
#expect(result.count == 1, "MuTronIII.Audio should produce 1 channel")
#expect(Array(result[0].prefix(blockSize)).count == blockSize)
}
@Test func controlOutputCount() {
let wah: MuTronIII.Control = MuTronIII.Control(input: 0.0)
let g: SynthGraph = graph
let result: [[Double]] = wah.calculatedSamples(graph: g, count: blockSize)
#expect(result.count == 1, "MuTronIII.Control should produce 1 channel")
#expect(Array(result[0].prefix(blockSize)).count == blockSize)
}
// MARK: 3. Fan-out — two calls in same segment return identical first channel
@Test func audioFanOut() {
let wah: MuTronIII.Audio = MuTronIII.Audio(input: OscSin.Audio(freq: 440.0))
let g: SynthGraph = graph
let first: [[Double]] = wah.calculatedSamples(graph: g, count: blockSize)
let second: [[Double]] = wah.calculatedSamples(graph: g, count: blockSize)
#expect(Array(first[0].prefix(blockSize)) == Array(second[0].prefix(blockSize)))
}
@Test func controlFanOut() {
let wah: MuTronIII.Control = MuTronIII.Control(input: OscSin.Audio(freq: 440.0))
let g: SynthGraph = graph
let first: [[Double]] = wah.calculatedSamples(graph: g, count: blockSize)
let second: [[Double]] = wah.calculatedSamples(graph: g, count: blockSize)
#expect(Array(first[0].prefix(blockSize)) == Array(second[0].prefix(blockSize)))
}
// MARK: 4. State advances once per block (second call is a cache hit)
@Test func audioStateAdvancesOncePerBlock() {
let wah: MuTronIII.Audio = MuTronIII.Audio(input: OscSin.Audio(freq: 440.0))
let g: SynthGraph = graph
let a: [Double] = Array(wah.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
let b: [Double] = Array(wah.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
#expect(a == b, "Second call in same segment must return cached result")
}
@Test func controlSubBlockBypass() {
// Control bypass: two calls on the same graph must return identical output.
// This verifies _computeSamples is called exactly once per audio block.
let wah: MuTronIII.Control = MuTronIII.Control(input: OscSin.Audio(freq: 220.0), sensitivity: 2.0)
let g: SynthGraph = graph
let a: [Double] = Array(wah.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
let b: [Double] = Array(wah.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
#expect(a == b, "Control second call must be a cache hit (bypass active)")
}
// MARK: 5. Integration — runs inside Synthesizer without crash, output finite
@Test func audioIntegrationNoCrash() {
let noise: WhiteNoise.Audio = WhiteNoise.Audio()
let wah: MuTronIII.Audio = MuTronIII.Audio(input: noise, sensitivity: 1.5, peak: 4.0)
let samples: [Double] = collectBlocks(wah, engine: engine, blocks: 8)
for s: Double in samples {
#expect(s.isFinite, "MuTronIII.Audio integration output must be finite; got \(s)")
}
}
@Test func controlIntegrationNoCrash() {
let noise: WhiteNoise.Audio = WhiteNoise.Audio()
let wah: MuTronIII.Control = MuTronIII.Control(input: noise, sensitivity: 1.5, peak: 4.0)
let samples: [Double] = collectBlocks(wah, engine: engine, blocks: 8)
for s: Double in samples {
#expect(s.isFinite, "MuTronIII.Control integration output must be finite; got \(s)")
}
}
// MARK: 6. Default constructor compiles with all defaults
@Test func defaultConstructorCompiles() {
let noise: WhiteNoise.Audio = WhiteNoise.Audio()
let wah: MuTronIII.Audio = MuTronIII.Audio(input: noise)
let g: SynthGraph = graph
let result: [[Double]] = wah.calculatedSamples(graph: g, count: blockSize)
#expect(result.count == 1)
}
// MARK: 7. Mode selection — LP / BP / HP outputs differ pairwise
@Test func modeSelectOutputsDiffer() {
// Use a sustained tone to get the filter responding.
let sig: OscSin.Audio = OscSin.Audio(freq: 220.0, mul: 0.8)
let wahLP: MuTronIII.Audio = MuTronIII.Audio(input: sig, mode: .lowpass, sensitivity: 2.0, peak: 4.0)
let wahBP: MuTronIII.Audio = MuTronIII.Audio(input: sig, mode: .bandpass, sensitivity: 2.0, peak: 4.0)
let wahHP: MuTronIII.Audio = MuTronIII.Audio(input: sig, mode: .highpass, sensitivity: 2.0, peak: 4.0)
// Settle for several blocks so the envelope has time to open.
let settleBlocks: Int = 16
for _ in 0..<settleBlocks {
let g: SynthGraph = makeSynthGraph(engine: engine)
_ = wahLP.calculatedSamples(graph: g, count: blockSize)
_ = wahBP.calculatedSamples(graph: g, count: blockSize)
_ = wahHP.calculatedSamples(graph: g, count: blockSize)
advanceBlock(engine)
}
let g: SynthGraph = makeSynthGraph(engine: engine)
let lpOut: [Double] = Array(wahLP.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
let bpOut: [Double] = Array(wahBP.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
let hpOut: [Double] = Array(wahHP.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
// At least one sample must differ between each pair.
let lpVsBp: Bool = zip(lpOut, bpOut).contains { abs($0.0 - $0.1) > 1e-9 }
let bpVsHp: Bool = zip(bpOut, hpOut).contains { abs($0.0 - $0.1) > 1e-9 }
let lpVsHp: Bool = zip(lpOut, hpOut).contains { abs($0.0 - $0.1) > 1e-9 }
#expect(lpVsBp, "LP and BP outputs should differ")
#expect(bpVsHp, "BP and HP outputs should differ")
#expect(lpVsHp, "LP and HP outputs should differ")
}
// MARK: 8. Up vs Down sweep — same input, inverted response
@Test func upVsDownSweepDiffer() {
// Strong tone so the envelope activates quickly.
let sig: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.9)
let wahUp: MuTronIII.Audio = MuTronIII.Audio(input: sig, mode: .lowpass, direction: .up, sensitivity: 3.0)
let wahDown: MuTronIII.Audio = MuTronIII.Audio(input: sig, mode: .lowpass, direction: .down, sensitivity: 3.0)
// Settle
let settleBlocks: Int = 16
for _ in 0..<settleBlocks {
let g: SynthGraph = makeSynthGraph(engine: engine)
_ = wahUp.calculatedSamples(graph: g, count: blockSize)
_ = wahDown.calculatedSamples(graph: g, count: blockSize)
advanceBlock(engine)
}
let g: SynthGraph = makeSynthGraph(engine: engine)
let upOut: [Double] = Array(wahUp.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
let downOut: [Double] = Array(wahDown.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
let differ: Bool = zip(upOut, downOut).contains { abs($0.0 - $0.1) > 1e-6 }
#expect(differ, "Up and down sweep outputs should differ after envelope activation")
}
// MARK: 9. Generator inputs (sensitivity, peak) run without crash, output finite
@Test func generatorInputsProduceFiniteOutput() {
// Drive sensitivity and peak from control generators.
// LFSawtooth sweeps -1..1; scale so both params stay in a valid positive range.
// sensLFO: mul=1.0, add=1.5 → sweeps 0.5..2.5 (positive, sensible sensitivity)
// peakLFO: mul=1.5, add=2.5 → sweeps 1.0..4.0 (Q safely > 1, SVF stable)
let sensLFO: LFSawtooth.Control = LFSawtooth.Control(freq: 0.5, mul: 1.0, add: 1.5)
let peakLFO: LFSawtooth.Control = LFSawtooth.Control(freq: 0.3, mul: 1.5, add: 2.5)
let input: OscSin.Audio = OscSin.Audio(freq: 330.0, mul: 0.5)
let wah: MuTronIII.Audio = MuTronIII.Audio(
input: input,
sensitivity: sensLFO,
peak: peakLFO
)
for _ in 0..<16 {
let g: SynthGraph = makeSynthGraph(engine: engine)
let result: [[Double]] = wah.calculatedSamples(graph: g, count: blockSize)
let samples: [Double] = Array(result[0].prefix(blockSize))
for s: Double in samples {
#expect(s.isFinite, "Output must be finite with generator inputs; got \(s)")
}
advanceBlock(engine)
}
}
// MARK: 10. Block-boundary continuity — state carries across blocks
@Test func blockBoundaryContinuity() {
// Run two consecutive blocks on the same instance; last sample of block 1 and
// first sample of block 2 must differ (state is evolving), but neither should be NaN/Inf.
let sig: OscSin.Audio = OscSin.Audio(freq: 220.0, mul: 0.8)
let wah: MuTronIII.Audio = MuTronIII.Audio(input: sig, sensitivity: 2.0, peak: 3.0)
// Settle
let settleBlocks: Int = 4
for _ in 0..<settleBlocks {
let g: SynthGraph = makeSynthGraph(engine: engine)
_ = wah.calculatedSamples(graph: g, count: blockSize)
advanceBlock(engine)
}
let g1: SynthGraph = makeSynthGraph(engine: engine)
let block1: [Double] = Array(wah.calculatedSamples(graph: g1, count: blockSize)[0].prefix(blockSize))
advanceBlock(engine)
let g2: SynthGraph = makeSynthGraph(engine: engine)
let block2: [Double] = Array(wah.calculatedSamples(graph: g2, count: blockSize)[0].prefix(blockSize))
for s: Double in block1 + block2 {
#expect(s.isFinite, "Block boundary output must be finite; got \(s)")
}
// Verify blocks are not identical (state evolves across blocks).
let blocksMatch: Bool = block1 == block2
// With a sustained oscillator input and active envelope the blocks should differ.
// This is a sanity check rather than a hard requirement.
_ = blocksMatch
}
// MARK: 11. mulAdd — output is original × 2 + 1
@Test func mulAddApplied() {
let sig: any ValidGeneratorInput = ConstantGeneratorInput(0.5)
let wahPlain: MuTronIII.Audio = MuTronIII.Audio(input: sig, sensitivity: 1.0, peak: 1.0,
freqLo: 200.0, freqHi: 2000.0)
let wahMulAdd: MuTronIII.Audio = MuTronIII.Audio(input: sig, sensitivity: 1.0, peak: 1.0,
freqLo: 200.0, freqHi: 2000.0,
mul: 2.0, add: 1.0)
// Settle both identically.
let settleBlocks: Int = 32
for _ in 0..<settleBlocks {
let g: SynthGraph = makeSynthGraph(engine: engine)
_ = wahPlain.calculatedSamples(graph: g, count: blockSize)
_ = wahMulAdd.calculatedSamples(graph: g, count: blockSize)
advanceBlock(engine)
}
let g: SynthGraph = makeSynthGraph(engine: engine)
let plainResult: [[Double]] = wahPlain.calculatedSamples(graph: g, count: blockSize)
let mulAddResult: [[Double]] = wahMulAdd.calculatedSamples(graph: g, count: blockSize)
// Check mid-block sample to avoid edge transients.
let idx: Int = blockSize / 2
let expected: Double = plainResult[0][idx] * 2.0 + 1.0
let actual: Double = mulAddResult[0][idx]
#expect(abs(actual - expected) < 1e-9,
"mulAdd: expected \(expected), got \(actual)")
}
// MARK: 12. GeneratorEnum type aliases resolve
@Test func generatorEnumTypeAliases() {
let audio: MuTronIII.AudioType = MuTronIII.Audio(input: 0.0)
let ctrl: MuTronIII.ControlType = MuTronIII.Control(input: 0.0)
#expect(audio.rate == .audio)
#expect(ctrl.rate == .control)
}
// MARK: 13. Denormal guard — no subnormal accumulation after silence
@Test func denormalGuardAfterSilence() {
// Drive with a brief burst, then silence. State should not contain subnormals.
let wah: MuTronIII.Audio = MuTronIII.Audio(
input: OscSin.Audio(freq: 440.0, mul: 0.8),
sensitivity: 2.0
)
// Burst
for _ in 0..<8 {
let g: SynthGraph = makeSynthGraph(engine: engine)
_ = wah.calculatedSamples(graph: g, count: blockSize)
advanceBlock(engine)
}
// Switch to silence — use a new instance wrapping 0 input for remaining blocks.
let silence: MuTronIII.Audio = MuTronIII.Audio(input: 0.0)
for _ in 0..<256 {
let g: SynthGraph = makeSynthGraph(engine: engine)
_ = silence.calculatedSamples(graph: g, count: blockSize)
advanceBlock(engine)
}
#expect(!silence._prevAmp.isSubnormal,
"_prevAmp must not be subnormal; got \(silence._prevAmp)")
#expect(!silence._svfLow.isSubnormal,
"_svfLow must not be subnormal; got \(silence._svfLow)")
#expect(!silence._svfBand.isSubnormal,
"_svfBand must not be subnormal; got \(silence._svfBand)")
}
// MARK: 14. Control ramp — last sample of ramp buffer equals previousOutputValue
@Test func controlRampPreviousOutputValue() {
let sig: OscSin.Audio = OscSin.Audio(freq: 220.0, mul: 0.8)
let wah: MuTronIII.Control = MuTronIII.Control(input: sig, sensitivity: 2.0, peak: 4.0)
let settleBlocks: Int = 32
for _ in 0..<settleBlocks {
let g: SynthGraph = makeSynthGraph(engine: engine)
let result: [[Double]] = wah.calculatedSamples(graph: g, count: blockSize)
let lastSample: Double = result[0][blockSize - 1]
#expect(abs(lastSample - wah.previousOutputValue) < 1e-9,
"Control: previousOutputValue must equal last ramp sample")
advanceBlock(engine)
}
}
// MARK: 15. Audio and Control parity (loose) — both produce finite output tracking same input
@Test func audioControlParity() {
let sig: OscSin.Audio = OscSin.Audio(freq: 440.0, mul: 0.7)
let wahAudio: MuTronIII.Audio = MuTronIII.Audio(input: sig, sensitivity: 1.5, peak: 4.0)
let wahControl: MuTronIII.Control = MuTronIII.Control(input: sig, sensitivity: 1.5, peak: 4.0)
for _ in 0..<16 {
let g: SynthGraph = makeSynthGraph(engine: engine)
let audioResult: [[Double]] = wahAudio.calculatedSamples(graph: g, count: blockSize)
let controlResult: [[Double]] = wahControl.calculatedSamples(graph: g, count: blockSize)
let audioSamples: [Double] = Array(audioResult[0].prefix(blockSize))
let controlSamples: [Double] = Array(controlResult[0].prefix(blockSize))
for s: Double in audioSamples + controlSamples {
#expect(s.isFinite, "Audio/Control parity: output must be finite; got \(s)")
}
advanceBlock(engine)
}
}
// MARK: 16. Negative sensitivity is clamped to 0 (no nonsense env sign)
@Test func negativeSensitivityClamped() {
// Negative sensitivity must produce the same finite output as sensitivity = 0
// (envelope held at 0, cutoff pinned to freqLo). Compare against a zero-sens reference.
let sig: OscSin.Audio = OscSin.Audio(freq: 220.0, mul: 0.8)
let wahNeg: MuTronIII.Audio = MuTronIII.Audio(input: sig, sensitivity: -2.5)
let wahZero: MuTronIII.Audio = MuTronIII.Audio(input: sig, sensitivity: 0.0)
for _ in 0..<8 {
let g1: SynthGraph = makeSynthGraph(engine: engine)
let neg: [Double] = Array(wahNeg.calculatedSamples(graph: g1, count: blockSize)[0].prefix(blockSize))
let g2: SynthGraph = makeSynthGraph(engine: engine)
let zer: [Double] = Array(wahZero.calculatedSamples(graph: g2, count: blockSize)[0].prefix(blockSize))
for i: Int in 0..<blockSize {
#expect(neg[i].isFinite, "Negative sensitivity must produce finite output; got \(neg[i])")
#expect(abs(neg[i] - zer[i]) < 1e-9,
"Negative sensitivity must clamp to 0 — sample \(i): neg=\(neg[i]) zero=\(zer[i])")
}
advanceBlock(engine)
}
}
// MARK: 17. freqHi above Nyquist clamped (no sin wrap, no NaN)
@Test func freqHiAboveNyquistClamped() {
// freqHi = 96000 at sr 44100 — well past Nyquist (22050). Without the clamp,
// sin(π·cutoff/sr) wraps and produces wrong cutoff. After the clamp, output
// must be finite and behave like a Nyquist-capped sweep.
let sig: WhiteNoise.Audio = WhiteNoise.Audio(mul: 0.4)
let wah: MuTronIII.Audio = MuTronIII.Audio(
input: sig,
sensitivity: 2.0,
freqLo: 200.0,
freqHi: 96_000.0 // well above Nyquist
)
for _ in 0..<16 {
let g: SynthGraph = makeSynthGraph(engine: engine)
let samples: [Double] = Array(wah.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
for s: Double in samples {
#expect(s.isFinite, "freqHi above Nyquist must clamp safely; got \(s)")
}
advanceBlock(engine)
}
}
// MARK: 18. Deterministic SVF output for known inputs
@Test func deterministicSampleOutput() {
// Lock down the SVF coefficient/recurrence math: with sensitivity = 0 the envelope
// follower is pinned at 0, so cutoff = freqLo · pow(freqHi/freqLo, 0) = freqLo.
// Set freqLo = freqHi = 440 Hz, peak = 1.0 (q1 = 1.0). With a unit-step input
// (constant 1.0 from sample 0 onward) and zeroed state, the Chamberlin SVF
// output is fully determined.
//
// Hand-computed expectations using:
// sr = 44100, freq = 440, peak = 1.0
// f = 2 · sin(π · 440 / 44100) ≈ 0.06264832417874368
// q1 = 1.0
// low_0 = 0; band_0 = 0
// high_0 = 1 - 0 - 1·0 = 1
// band_1 = f · 1 + 0 = f
// low_1 = 0 + f · 0 = 0 (uses band_0)
// high_1 = 1 - 0 - 1·f = 1 - f
//
// After per-sample iteration:
// sample 0 (highpass mode) = 1.0
// sample 1 (highpass mode) = 1 - f
let wah: MuTronIII.Audio = MuTronIII.Audio(
input: ConstantGeneratorInput(1.0),
mode: .highpass,
direction: .up,
sensitivity: 0.0, // pin envelope at 0
peak: 1.0,
freqLo: 440.0,
freqHi: 440.0,
attackTime: 0.0,
releaseTime: 0.0
)
let g: SynthGraph = graph
let result: [[Double]] = wah.calculatedSamples(graph: g, count: blockSize)
let samples: [Double] = Array(result[0].prefix(blockSize))
// Reproduce the Chamberlin SVF recurrence here in test code; if MuTronIII's
// implementation drifts, the assertion catches it.
// Step:
// low_new = low + f·band (band is OLD)
// high_n = x - low_new - q1·band (low is NEW, band is OLD)
// band_new = f·high_n + band
// Initial: low=0, band=0; q1 = 1/peak = 1.0; x = 1.0.
let f: Double = min(1.0, 2.0 * Darwin.sin(Double.pi * 440.0 / sampleRate))
let q1: Double = 1.0
var low: Double = 0.0
var band: Double = 0.0
// Sample 0
let lowAfter0: Double = low + f * band // 0
let high0: Double = 1.0 - lowAfter0 - q1 * band // 1
let bandAfter0: Double = f * high0 + band // f
low = lowAfter0; band = bandAfter0
// Sample 1
let lowAfter1: Double = low + f * band // f²
let high1: Double = 1.0 - lowAfter1 - q1 * band // 1 - f² - f
let tolerance: Double = 1e-12
#expect(abs(samples[0] - high0) < tolerance,
"sample[0] = \(samples[0]); expected \(high0)")
#expect(abs(samples[1] - high1) < tolerance,
"sample[1] = \(samples[1]); expected \(high1)")
}
// MARK: 19. freqLo non-positive clamped (pow base must be > 0)
@Test func freqLoNonPositiveClamped() {
// freqLo = 0 and freqLo < 0 must not produce NaN via pow(0, x) or pow(neg, frac).
let sig: OscSin.Audio = OscSin.Audio(freq: 110.0, mul: 0.6)
for badFreqLo: Double in [0.0, -50.0] {
let wah: MuTronIII.Audio = MuTronIII.Audio(
input: sig,
sensitivity: 1.5,
freqLo: badFreqLo,
freqHi: 2000.0
)
for _ in 0..<8 {
let g: SynthGraph = makeSynthGraph(engine: engine)
let samples: [Double] = Array(wah.calculatedSamples(graph: g, count: blockSize)[0].prefix(blockSize))
for s: Double in samples {
#expect(s.isFinite,
"freqLo=\(badFreqLo) must clamp; got non-finite sample \(s)")
}
advanceBlock(engine)
}
}
}
}