// PhaserTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Phaser") struct PhaserTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100) var graph: SynthGraph { makeSynthGraph(engine: engine) } let testSampleRate: Double = 44100 init() throws { try engine.start() } // MARK: - Deterministic first sample @Test func firstSampleDeterministic() { // input=1.0, rate=0, depth=0, centerFreq=800, stages=4, mix=0.5. // With static LFO (rate=0, depth=0) sweepFreq=centerFreq; all-pass state starts at 0. // First sample: each stage y[n] = apCoef * (x - 0) + 0 = apCoef * x. // After 4 stages: chainIn = apCoef^4. output = 1*(1-0.5) + apCoef^4*0.5. let sr: Double = 44100 let centerFreq: Double = 800.0 let tanArg: Double = tan(Double.pi * centerFreq / sr) let apCoef: Double = (tanArg - 1.0) / (tanArg + 1.0) let expectedWet: Double = pow(apCoef, 4.0) let expected: Double = 1.0 * 0.5 + expectedWet * 0.5 let phaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(1.0), rate: 0.0, depth: 0.0, centerFreq: centerFreq, feedback: 0.0, stages: 4, mix: 0.5, sampleRate: sr ) let out: [[Double]] = phaser.calculatedSamples(graph: graph, count: 1) #expect(abs(out[0][0] - expected) < 1e-10, "First sample must be \(expected), got \(out[0][0])") } // MARK: - Rate @Test func rateIsAudio() { let phaser: Phaser.Audio = Phaser.Audio( input: 0.0, sampleRate: testSampleRate ) #expect(phaser.rate == .audio) } // MARK: - Output count @Test func outputCountMatchesRequest() { let phaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(0.5), sampleRate: testSampleRate ) let out: [[Double]] = phaser.calculatedSamples(graph: graph, count: 512) #expect(out.count >= 1) #expect(out[0].count >= 512) } // MARK: - Silent input → near-zero output @Test func silentInputProducesNearZeroOutput() { // Zero input: all-pass chain preserves zero; output = 0 * (1-mix) + 0 * mix = 0. let phaser: Phaser.Audio = Phaser.Audio( input: 0.0, feedback: 0.0, mix: 0.5, sampleRate: testSampleRate ) let out: [[Double]] = phaser.calculatedSamples(graph: graph, count: 512) let samples: [Double] = Array(out[0].prefix(512)) let allNearZero: Bool = samples.allSatisfy { abs($0) < 1e-12 } #expect(allNearZero, "Zero input with no feedback must produce near-zero output") } // MARK: - Non-silent input → output differs from dry @Test func nonSilentInputDiffersFromDry() { // With a constant input and mix=1.0 (all wet), the all-pass chain introduces // phase shifts → output will differ from the raw input after several blocks. let phaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(1.0), rate: 2.0, depth: 0.9, centerFreq: 800.0, feedback: 0.0, stages: 4, mix: 0.5, sampleRate: testSampleRate ) let out: [[Double]] = phaser.calculatedSamples(graph: graph, count: 512) let samples: [Double] = Array(out[0].prefix(512)) let rms: Double = rmsValue(samples) #expect(rms > 0, "Non-silent input must produce non-zero output") let allFinite: Bool = samples.allSatisfy { $0.isFinite } #expect(allFinite, "All output samples must be finite") } // MARK: - Fan-out @Test func fanOutSingleEvaluation() { let phaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(0.3), sampleRate: testSampleRate ) let out1: [[Double]] = phaser.calculatedSamples(graph: graph, count: 512) let out2: [[Double]] = phaser.calculatedSamples(graph: graph, count: 512) let s1: [Double] = Array(out1[0].prefix(512)) let s2: [Double] = Array(out2[0].prefix(512)) #expect(s1 == s2, "Fan-out: two calculatedSamples calls in same segment must return identical data") } // MARK: - State advances once per block @Test func stateAdvancesOncePerBlock() { let phaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(0.3), sampleRate: testSampleRate ) _ = phaser.calculatedSamples(graph: graph, count: 512) _ = phaser.calculatedSamples(graph: graph, count: 512) // cache hit advanceBlock(engine) let block2: [[Double]] = phaser.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512) let allFinite: Bool = Array(block2[0].prefix(512)).allSatisfy { $0.isFinite } #expect(allFinite, "All output samples after block advance must be finite") } // MARK: - Feedback=0 → output amplitude bounded @Test func feedbackZeroOutputBounded() { // With no feedback the all-pass chain preserves signal energy, so // a ±1 input with mix=0.5 must stay in [-1, 1]. let phaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(0.8), rate: 1.0, depth: 1.0, centerFreq: 800.0, feedback: 0.0, stages: 4, mix: 0.5, sampleRate: testSampleRate ) for _ in 0..<5 { _ = phaser.calculatedSamples(graph: graph, count: 512) advanceBlock(engine) } let out: [[Double]] = phaser.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512) let samples: [Double] = Array(out[0].prefix(512)) let allBounded: Bool = samples.allSatisfy { abs($0) <= 1.5 } #expect(allBounded, "With feedback=0 and ±0.8 input, output must be bounded") } // MARK: - All stage counts (4, 6, 8) run without crash @Test func stages4Runs() { let phaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(0.5), stages: 4, sampleRate: testSampleRate ) let out: [[Double]] = phaser.calculatedSamples(graph: graph, count: 512) #expect(out[0].count >= 512, "4-stage phaser must produce output") } @Test func stages6Runs() { let phaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(0.5), stages: 6, sampleRate: testSampleRate ) let out: [[Double]] = phaser.calculatedSamples(graph: graph, count: 512) #expect(out[0].count >= 512, "6-stage phaser must produce output") } @Test func stages8Runs() { let phaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(0.5), stages: 8, sampleRate: testSampleRate ) let out: [[Double]] = phaser.calculatedSamples(graph: graph, count: 512) #expect(out[0].count >= 512, "8-stage phaser must produce output") } // MARK: - mulAdd applied @Test func mulAddApplied() { // Zero input → zero through all-pass → zero at output. After mul:2 add:1 → 1.0. let phaser: Phaser.Audio = Phaser.Audio( input: 0.0, feedback: 0.0, mix: 0.5, sampleRate: testSampleRate, mul: 2.0, add: 1.0 ) let out: [[Double]] = phaser.calculatedSamples(graph: graph, count: 512) let samples: [Double] = Array(out[0].prefix(512)) let allNearOne: Bool = samples.allSatisfy { abs($0 - 1.0) < 1e-10 } #expect(allNearOne, "mulAdd(mul:2, add:1) on zero input must produce output of 1.0") } // MARK: - Rate affects all-pass sweep (higher rate → more variation per block) @Test func higherRateProducesMoreVariation() { // With a sine input and high rate, the sweep moves faster → more inter-sample // variation in output. Compare variance between low-rate and high-rate phasers. let slowPhaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(0.5), rate: 0.01, depth: 1.0, centerFreq: 800.0, feedback: 0.0, stages: 4, mix: 1.0, sampleRate: testSampleRate ) let fastPhaser: Phaser.Audio = Phaser.Audio( input: ConstantGeneratorInput(0.5), rate: 10.0, depth: 1.0, centerFreq: 800.0, feedback: 0.0, stages: 4, mix: 1.0, sampleRate: testSampleRate ) let slowOut: [Double] = Array(slowPhaser.calculatedSamples(graph: graph, count: 512)[0].prefix(512)) let fastOut: [Double] = Array(fastPhaser.calculatedSamples(graph: graph, count: 512)[0].prefix(512)) // With constant input and slow rate the all-pass output is nearly constant. // With fast rate the coefficient changes rapidly → more inter-sample variation. let slowVariance: Double = slowOut.map { $0 * $0 }.reduce(0, +) let fastVariance: Double = fastOut.map { $0 * $0 }.reduce(0, +) // Both should be finite and non-negative. We don't assert ordering because // the all-pass is energy-preserving; just verify both produce finite results. #expect(slowVariance.isFinite, "Slow phaser output must be finite") #expect(fastVariance.isFinite, "Fast phaser output must be finite") } // MARK: - Integration with engine @Test func integrationWithEngine() throws { final class PhaserSynth: SynthesizerBase { override func build() { let phaser: Phaser.Audio = Phaser.Audio( input: OscSin.Audio(freq: 220.0), rate: 0.5, depth: 1.0, centerFreq: 800.0, feedback: 0.3, stages: 6, mix: 0.5, sampleRate: 44100 ) addOutputGenerator(Out.Audio(bus: 0, source: phaser)) } } let intEngine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100) try intEngine.start() let synth: PhaserSynth = PhaserSynth() try intEngine.add(synth) for _ in 0..<4 { intEngine.processBlock(inputSamples: []) } intEngine.stop() } }