// AmbiDecoderMonoTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("AmbiDecoderMono") struct AmbiDecoderMonoTests { private let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100) private var graph: SynthGraph { makeSynthGraph(engine: engine) } init() throws { try engine.start() } private func staticBFormat(w: Double, x: Double, y: Double, z: Double) -> GeneratorArray { return GeneratorArray([ ConstantGeneratorInput(w) as any Generator, ConstantGeneratorInput(x), ConstantGeneratorInput(y), ConstantGeneratorInput(z) ]) } /// Encoder math: for unit-amplitude source at (az, el), W = 1/√2, X = cos(az)·cos(el), /// Y = sin(az)·cos(el), Z = sin(el). Use this to drive virtual-mic tests. private func encodedSource(amplitude: Double, az: Double, el: Double) -> GeneratorArray { let wScale: Double = 1.0 / Double(2.0).squareRoot() let cosEl: Double = cos(el) return staticBFormat( w: wScale * amplitude, x: cos(az) * cosEl * amplitude, y: sin(az) * cosEl * amplitude, z: sin(el) * amplitude ) } @Test func outputChannelCountIsOne() { let dec: AmbiDecoderMono = AmbiDecoderMono( input: encodedSource(amplitude: 1, az: 0, el: 0), pattern: 1.0 ) #expect(dec.outputChannelCount == 1) let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64) #expect(out.count == 1) } @Test func omniReturnsUnitGainForAnyDirection() { // pattern = 0 → pure omni; should return the source amplitude regardless of (az, el). for (az, el): (Double, Double) in [(0, 0), (.pi / 2, 0), (.pi, 0), (0, .pi / 2)] { let dec: AmbiDecoderMono = AmbiDecoderMono( input: encodedSource(amplitude: 0.6, az: az, el: el), azimuth: 0, elevation: 0, pattern: 0 ) let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64) #expect(abs(out[0][32] - 0.6) < 1e-12, "Omni at source (az=\(az), el=\(el)) should return 0.6; got \(out[0][32])") } } @Test func cardioidOnAxisFullGain() { // pattern = 1, mic at (0, 0), source at (0, 0): out = (1-0.5)·W·√2 + 0.5·(1·1·1 + 0 + 0) // = 0.5 · 1 + 0.5 · 1 = 1.0 ✓ let dec: AmbiDecoderMono = AmbiDecoderMono( input: encodedSource(amplitude: 1, az: 0, el: 0), azimuth: 0, elevation: 0, pattern: 1.0 ) let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64) #expect(abs(out[0][32] - 1.0) < 1e-12) } @Test func cardioidBehindIsNullToFP() { // Cardioid pointing forward, source directly behind (az=π): cos(π) = −1 → // directional term = 0.5·(−1) = −0.5. omni = 0.5·W·√2 = 0.5. Sum = 0. let dec: AmbiDecoderMono = AmbiDecoderMono( input: encodedSource(amplitude: 1, az: .pi, el: 0), azimuth: 0, elevation: 0, pattern: 1.0 ) let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64) #expect(abs(out[0][32]) < 1e-12, "Cardioid null behind should be 0; got \(out[0][32])") } @Test func figureEightFrontBackOppositePolarity() { // pattern = 2 → pure figure-8. omni gain = 0. Front (az=0): +1; back (az=π): −1. let front: AmbiDecoderMono = AmbiDecoderMono( input: encodedSource(amplitude: 1, az: 0, el: 0), azimuth: 0, elevation: 0, pattern: 2.0 ) let back: AmbiDecoderMono = AmbiDecoderMono( input: encodedSource(amplitude: 1, az: .pi, el: 0), azimuth: 0, elevation: 0, pattern: 2.0 ) let frontOut: [[Double]] = front.calculatedSamples(graph: graph, count: 64) let backOut: [[Double]] = back.calculatedSamples(graph: graph, count: 64) #expect(abs(frontOut[0][32] - 1.0) < 1e-12) #expect(abs(backOut[0][32] - (-1.0)) < 1e-12) } @Test func figureEightSideNull() { // Figure-8 mic pointing forward, source at side (az=π/2): cos(π/2) = 0. Null. let dec: AmbiDecoderMono = AmbiDecoderMono( input: encodedSource(amplitude: 1, az: .pi / 2, el: 0), azimuth: 0, elevation: 0, pattern: 2.0 ) let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64) #expect(abs(out[0][32]) < 1e-12) } @Test func mulAddApplied() { let dec: AmbiDecoderMono = AmbiDecoderMono( input: encodedSource(amplitude: 1, az: 0, el: 0), azimuth: 0, elevation: 0, pattern: 1.0, mul: 2.0, add: 0.1 ) let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64) // On-axis cardioid = 1.0; mul·add → 1·2 + 0.1 = 2.1. #expect(abs(out[0][32] - 2.1) < 1e-12) } }