// PanXYDistanceTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("PanXYDistance") struct PanXYDistanceTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100) var graph: SynthGraph { makeSynthGraph(engine: engine) } let testSampleRate: Double = 44100 let blockSize: Int = 512 init() throws { try engine.start() } // MARK: - Basic shape @Test func rateIsAudio() { let gen: PanXYDistance = PanXYDistance(input: 0.0, sampleRate: testSampleRate) #expect(gen.rate == .audio) } @Test func outputHasFourChannels() { let gen: PanXYDistance = PanXYDistance(input: ConstantGeneratorInput(1.0), sampleRate: testSampleRate) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) #expect(out.count == 4) #expect(gen.outputChannelCount == 4) } @Test func silentInputProducesSilentOutput() { let gen: PanXYDistance = PanXYDistance(input: 0.0, x: 0.5, y: 0.0, sampleRate: testSampleRate) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) for ch: Int in 0..<4 { let samples: [Double] = Array(out[ch].prefix(blockSize)) #expect(samples.allSatisfy { abs($0) < 1e-10 }, "ch\(ch): zero input must produce near-zero output") } } @Test func firstBlockIsFinite() { let gen: PanXYDistance = PanXYDistance( input: ConstantGeneratorInput(0.5), x: 0.3, y: -0.4, velocity: 5.0, sampleRate: testSampleRate ) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) for ch: Int in 0..<4 { let samples: [Double] = Array(out[ch].prefix(blockSize)) #expect(samples.allSatisfy { $0.isFinite }, "ch\(ch): all samples must be finite") } } // MARK: - Inside square: unit gain, no distance shaping /// With airAbsorptionCutoff near Nyquist + velocity=0 + position inside the unit /// square, after the LP settles the per-channel gain equals the `PanXY` law: /// at `(x=0, y=0)` all four channels = 0.5 × input. @Test func insideSquareCenterMatchesPanXYGains() { let cutoff: Double = 22000.0 // close to Nyquist at 44100 let gen: PanXYDistance = PanXYDistance( input: ConstantGeneratorInput(1.0), x: 0.0, y: 0.0, velocity: 0.0, airAbsorptionCutoff: cutoff, sampleRate: testSampleRate ) // Two blocks: first lets the LP settle on a DC input. _ = gen.calculatedSamples(graph: graph, count: blockSize) advanceBlock(engine) let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let last: Int = blockSize - 1 for ch: Int in 0..<4 { #expect(abs(out[ch][last] - 0.5) < 1e-3, "Center inside square: ch\(ch) should be ~0.5, got \(out[ch][last])") } } @Test func insideSquareCornerMatchesPanXYHardPan() { let cutoff: Double = 22000.0 let gen: PanXYDistance = PanXYDistance( input: ConstantGeneratorInput(1.0), x: 1.0, y: 1.0, velocity: 0.0, airAbsorptionCutoff: cutoff, sampleRate: testSampleRate ) _ = gen.calculatedSamples(graph: graph, count: blockSize) advanceBlock(engine) let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let last: Int = blockSize - 1 #expect(abs(out[0][last]) < 1e-3, "LF at hard RF should be ~0") #expect(abs(out[1][last] - 1.0) < 1e-3, "RF at hard RF should be ~1") #expect(abs(out[2][last]) < 1e-3, "RR at hard RF should be ~0") #expect(abs(out[3][last]) < 1e-3, "LR at hard RF should be ~0") } // MARK: - Distance attenuation outside the unit square /// RMS energy at `(x=2, y=0)` should be ~half of `(x=1, y=0)` (amp = 1/2). /// Use velocity=0 + near-Nyquist cutoff so amplitude scaling dominates. @Test func outsideSquareHalvedAtDoubleDistance() { let cutoff: Double = 22000.0 let near: PanXYDistance = PanXYDistance( input: ConstantGeneratorInput(1.0), x: 1.0, y: 0.0, velocity: 0.0, airAbsorptionCutoff: cutoff, sampleRate: testSampleRate ) // Settle LP. _ = near.calculatedSamples(graph: graph, count: blockSize) advanceBlock(engine) let nearOut: [[Double]] = near.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let lastNear: Double = nearOut[1][blockSize - 1] // RF dominant at x=1,y=0 let far: PanXYDistance = PanXYDistance( input: ConstantGeneratorInput(1.0), x: 2.0, y: 0.0, velocity: 0.0, airAbsorptionCutoff: cutoff, sampleRate: testSampleRate ) _ = far.calculatedSamples(graph: graph, count: blockSize) advanceBlock(engine) let farOut: [[Double]] = far.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let lastFar: Double = farOut[1][blockSize - 1] // amp(far)/amp(near) = (1/2)/1 = 0.5. let ratio: Double = lastFar / lastNear #expect(abs(ratio - 0.5) < 0.02, "x=2/x=1 amplitude ratio should be ~0.5, got \(ratio)") } /// Chebyshev metric: `(x=2, y=2)` has the same distance (max=2) as `(x=2, y=0)`. /// Total energy across the four channels (panned differently but with identical mono /// distance-shaped source) should match. @Test func chebyshevMetricMaxGovernsDistance() { let cutoff: Double = 22000.0 let axis: PanXYDistance = PanXYDistance( input: ConstantGeneratorInput(1.0), x: 2.0, y: 0.0, velocity: 0.0, airAbsorptionCutoff: cutoff, sampleRate: testSampleRate ) _ = axis.calculatedSamples(graph: graph, count: blockSize) advanceBlock(engine) let axisOut: [[Double]] = axis.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let last: Int = blockSize - 1 let axisEnergy: Double = (0..<4).map { axisOut[$0][last] }.reduce(0.0) { $0 + $1 * $1 } let diag: PanXYDistance = PanXYDistance( input: ConstantGeneratorInput(1.0), x: 2.0, y: 2.0, velocity: 0.0, airAbsorptionCutoff: cutoff, sampleRate: testSampleRate ) _ = diag.calculatedSamples(graph: graph, count: blockSize) advanceBlock(engine) let diagOut: [[Double]] = diag.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let diagEnergy: Double = (0..<4).map { diagOut[$0][last] }.reduce(0.0) { $0 + $1 * $1 } // Both at Chebyshev distance 2 → amp=0.5 → energy = 0.5² × 1.0 (power invariant // of the panner) = 0.25. #expect(abs(axisEnergy - 0.25) < 0.01, "axis Chebyshev energy at d=2 should be ~0.25, got \(axisEnergy)") #expect(abs(diagEnergy - 0.25) < 0.01, "diag Chebyshev energy at d=2 should be ~0.25, got \(diagEnergy)") #expect(abs(axisEnergy - diagEnergy) < 0.01, "Chebyshev: axis vs diag energy must match (got \(axisEnergy) vs \(diagEnergy))") } // MARK: - Doppler /// Positive velocity → read rate < 1 → output sine at lower frequency than input. /// Test by counting zero crossings. @Test func dopplerLowersPitchForPositiveVelocity() { let velocity: Double = 50.0 // m/s let speedOfSound: Double = 343.0 let expectedRate: Double = 1.0 - velocity / speedOfSound // ~0.854 let inputFreq: Double = 440.0 let osc: OscSin.Audio = OscSin.Audio(freq: ConstantGeneratorInput(inputFreq)) let gen: PanXYDistance = PanXYDistance( input: osc, x: 0.0, y: 0.0, // inside square; no atten or LP velocity: ConstantGeneratorInput(velocity), airAbsorptionCutoff: 22000.0, speedOfSound: speedOfSound, sampleRate: testSampleRate ) // Skip first few blocks for the delay line / LP to fill + settle. for _ in 0..<4 { _ = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) advanceBlock(engine) } // Use LF channel (ch0). At (0,0) all four channels carry the same wet × 0.5 signal. let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let ch: [Double] = Array(out[0].prefix(blockSize)) // Count zero crossings (sign changes). var zeroCrossings: Int = 0 for i: Int in 1..= 0) || (ch[i - 1] > 0 && ch[i] <= 0) { zeroCrossings += 1 } } // Expected: 2 zero crossings per cycle. Pitch ≈ inputFreq × readRate. let expectedFreq: Double = inputFreq * expectedRate let measuredFreq: Double = Double(zeroCrossings) * testSampleRate / (2.0 * Double(blockSize)) // 5% tolerance — delay-line transient + LP rolloff blur the boundary slightly. let err: Double = abs(measuredFreq - expectedFreq) / expectedFreq #expect(err < 0.05, "Doppler: expected ~\(expectedFreq) Hz, measured \(measuredFreq) Hz (err \(err))") } // MARK: - Lowpass engagement at distance /// At a large distance the cutoff drops far below the input frequency, attenuating it. @Test func lowpassReducesHighFreqAtLargeDistance() throws { let highFreq: Double = 8000.0 let osc: OscSin.Audio = OscSin.Audio(freq: ConstantGeneratorInput(highFreq)) let inside: PanXYDistance = PanXYDistance( input: osc, x: 0.0, y: 0.0, velocity: 0.0, airAbsorptionCutoff: 22000.0, sampleRate: testSampleRate ) // Settle. for _ in 0..<4 { _ = inside.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) advanceBlock(engine) } let insideOut: [[Double]] = inside.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) var insideSumSq: Double = 0.0 for i: Int in 0..