// PanAzDistanceTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("PanAzDistance") struct PanAzDistanceTests { 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: PanAzDistance = PanAzDistance(input: 0.0, sampleRate: testSampleRate) #expect(gen.rate == .audio) } @Test func outputHasFourChannels() { let gen: PanAzDistance = PanAzDistance(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: PanAzDistance = PanAzDistance(input: 0.0, azimuth: .pi / 4, distance: 2.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: PanAzDistance = PanAzDistance( input: ConstantGeneratorInput(0.5), azimuth: 0.3, distance: 0.7, 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 the boundary /// At `distance = 1`, az = π/4 should hard-pan to the RF corner — equivalent /// to `PanAz` at the same angle with the LP near Nyquist + velocity=0. @Test func boundaryCornerMatchesPanAz() { let cutoff: Double = 22000.0 let gen: PanAzDistance = PanAzDistance( input: ConstantGeneratorInput(1.0), azimuth: .pi / 4, distance: 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 az=π/4 boundary should be ~0") #expect(abs(out[1][last] - 1.0) < 1e-3, "RF at az=π/4 boundary should be ~1") #expect(abs(out[2][last]) < 1e-3, "RR at az=π/4 boundary should be ~0") #expect(abs(out[3][last]) < 1e-3, "LR at az=π/4 boundary should be ~0") } /// At `distance = 0` (listener position) all four speakers should receive /// equal energy = 0.5 × input. @Test func distanceZeroCollapsesToEqualEnergy() { let cutoff: Double = 22000.0 let gen: PanAzDistance = PanAzDistance( input: ConstantGeneratorInput(1.0), azimuth: 1.234, distance: 0.0, velocity: 0.0, // azimuth irrelevant at r=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 for ch: Int in 0..<4 { #expect(abs(out[ch][last] - 0.5) < 1e-3, "distance=0: ch\(ch) should be ~0.5, got \(out[ch][last])") } } // MARK: - Distance attenuation outside the boundary /// At `distance = 2` along az = π/4 (RF corner direction), the RF channel /// should be ~half the gain it had at `distance = 1` (1/d attenuation). @Test func outsideBoundaryHalvedAtDoubleDistance() { let cutoff: Double = 22000.0 let near: PanAzDistance = PanAzDistance( input: ConstantGeneratorInput(1.0), azimuth: .pi / 4, distance: 1.0, velocity: 0.0, airAbsorptionCutoff: cutoff, sampleRate: testSampleRate ) _ = 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] let far: PanAzDistance = PanAzDistance( input: ConstantGeneratorInput(1.0), azimuth: .pi / 4, distance: 2.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, "distance=2/distance=1 amplitude ratio should be ~0.5, got \(ratio)") } /// Negative distance is treated as `|distance|` — direction comes from /// azimuth alone. @Test func negativeDistanceTreatedAsAbsoluteValue() { let cutoff: Double = 22000.0 let positive: PanAzDistance = PanAzDistance( input: ConstantGeneratorInput(1.0), azimuth: .pi / 4, distance: 2.0, velocity: 0.0, airAbsorptionCutoff: cutoff, sampleRate: testSampleRate ) _ = positive.calculatedSamples(graph: graph, count: blockSize) advanceBlock(engine) let posOut: [[Double]] = positive.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let last: Int = blockSize - 1 let posRF: Double = posOut[1][last] let negative: PanAzDistance = PanAzDistance( input: ConstantGeneratorInput(1.0), azimuth: .pi / 4, distance: -2.0, velocity: 0.0, airAbsorptionCutoff: cutoff, sampleRate: testSampleRate ) _ = negative.calculatedSamples(graph: graph, count: blockSize) advanceBlock(engine) let negOut: [[Double]] = negative.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let negRF: Double = negOut[1][last] #expect(abs(posRF - negRF) < 1e-3, "negative distance must match |distance|: posRF=\(posRF) vs negRF=\(negRF)") } // MARK: - Doppler /// Positive velocity → read rate < 1 → output sine at lower frequency than input. @Test func dopplerLowersPitchForPositiveVelocity() { let velocity: Double = 50.0 let speedOfSound: Double = 343.0 let expectedRate: Double = 1.0 - velocity / speedOfSound let inputFreq: Double = 440.0 let osc: OscSin.Audio = OscSin.Audio(freq: ConstantGeneratorInput(inputFreq)) let gen: PanAzDistance = PanAzDistance( input: osc, azimuth: 0.0, distance: 0.0, // collapse to centre; 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) } let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let ch: [Double] = Array(out[0].prefix(blockSize)) var zeroCrossings: Int = 0 for i: Int in 1..= 0) || (ch[i - 1] > 0 && ch[i] <= 0) { zeroCrossings += 1 } } let expectedFreq: Double = inputFreq * expectedRate let measuredFreq: Double = Double(zeroCrossings) * testSampleRate / (2.0 * Double(blockSize)) let err: Double = abs(measuredFreq - expectedFreq) / expectedFreq #expect(err < 0.05, "Doppler: expected ~\(expectedFreq) Hz, measured \(measuredFreq) Hz (err \(err))") } // MARK: - mul / add @Test func mulIsAppliedPerChannel() { let gen: PanAzDistance = PanAzDistance( input: ConstantGeneratorInput(1.0), azimuth: 0.0, distance: 0.0, velocity: 0.0, // collapse to centre airAbsorptionCutoff: 22000.0, sampleRate: testSampleRate, mul: 2.0 ) _ = gen.calculatedSamples(graph: graph, count: blockSize) advanceBlock(engine) let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize) let last: Int = blockSize - 1 // distance=0 → equal-energy 0.5 per channel, × 2 = 1.0. for ch: Int in 0..<4 { #expect(abs(out[ch][last] - 1.0) < 1e-2, "mul=2 at distance=0: ch\(ch) should be ~1.0, got \(out[ch][last])") } } // MARK: - Fan-out cache @Test func fanOutCacheProducesIdenticalOutput() { let gen: PanAzDistance = PanAzDistance( input: ConstantGeneratorInput(0.7), azimuth: 0.3, distance: 0.5, velocity: 0.0, sampleRate: testSampleRate ) let g: SynthGraph = makeSynthGraph(engine: engine) let out1: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize) let out2: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize) for ch: Int in 0..<4 { #expect(out1[ch] == out2[ch], "Fan-out cache mismatch on ch\(ch)") } } }