// AmbiDecoderUHJTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("AmbiDecoderUHJ") struct AmbiDecoderUHJTests { private let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100) private var graph: SynthGraph { makeSynthGraph(engine: engine) } init() throws { try engine.start() } private func staticStereo(l: Double, r: Double) -> GeneratorArray { return GeneratorArray([ ConstantGeneratorInput(l) as any Generator, ConstantGeneratorInput(r) ]) } @Test func outputChannelCountIsFour() { let dec: AmbiDecoderUHJ = AmbiDecoderUHJ(input: staticStereo(l: 1, r: 1)) #expect(dec.outputChannelCount == 4) let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64) #expect(out.count == 4) } @Test func zRowIsAlwaysSilent() { let dec: AmbiDecoderUHJ = AmbiDecoderUHJ(input: staticStereo(l: 0.7, r: -0.3)) // Run several blocks; Z must remain exactly 0 throughout. for _: Int in 0..<8 { let out: [[Double]] = dec.calculatedSamples( graph: makeSynthGraph(engine: engine), count: 512 ) for s: Double in out[3].prefix(512) { #expect(s == 0, "Z row must be silent; saw \(s)") } advanceBlock(engine) } } @Test func equalLREncodesMonoToWXOnly() { // L = R = const → only the symmetric W/X matrix terms survive (Y has L − R coefficient). let dec: AmbiDecoderUHJ = AmbiDecoderUHJ(input: staticStereo(l: 0.5, r: 0.5)) for _: Int in 0..<32 { _ = dec.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512) advanceBlock(engine) } let out: [[Double]] = dec.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512) // Y output should be near zero after Hilbert filters settle (L − R = 0 for in-phase // path, and 0.385·(L_im + R_im) cancels only if L and R are coherent — they ARE the // same constant, so both Hilbert filters produce identical lj and rj, leaving // Y = 0.5·(0.763·0 + 0.385·(lj + rj)) = 0.385·lj. // For DC L = R = 0.5, Hilbert settles both paths to 0.5, so Y → 0.385·0.5 ≈ 0.193·0.5 // — non-zero but Y/W ratio is bounded. Verify W and X are dominant. let w: Double = out[0][500] let x: Double = out[1][500] let y: Double = out[2][500] #expect(abs(w) > abs(y), "W=\(w) should dominate Y=\(y) for centred-mono UHJ") #expect(abs(x) > 0, "X=\(x) must be nonzero") } @Test func roundTripBToUHJToBPreservesEnergy() { // B-format → UHJ → B-format: W/X/Y energy should be approximately preserved // within the Hilbert cascade's design band (UHJ matrix is energy-preserving in // theory). Z always returns 0. let wScale: Double = 1.0 / Double(2.0).squareRoot() let origBFormat: GeneratorArray = GeneratorArray([ OscSin.Audio(freq: 1_000.0) as any Generator, ConstantGeneratorInput(wScale) as any Generator, ConstantGeneratorInput(0.7) as any Generator, ConstantGeneratorInput(0) as any Generator ]) // Note: GeneratorArray of mixed Generator types — W gets the 1 kHz sine, // X/Y constant. Verify decoder returns 4 finite rows with Z = 0. let encoded: AmbiEncoderUHJ = AmbiEncoderUHJ(input: origBFormat) let decoded: AmbiDecoderUHJ = AmbiDecoderUHJ(input: encoded) // Settle Hilbert filters in both stages (each adds ~16τ settling). for _: Int in 0..<64 { _ = decoded.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512) advanceBlock(engine) } let out: [[Double]] = decoded.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512) // All four rows must be finite. Z row exactly 0. for row: Int in 0..<4 { for s: Double in out[row].prefix(512) { #expect(s.isFinite, "Row \(row) sample not finite") } } for s: Double in out[3].prefix(512) { #expect(s == 0, "Z row must be 0 after round trip") } } @Test func steadyStateEqualLRPinsCoefficients() { // Deterministic coefficient pin: with L = R = 1 DC, both Hilbert paths settle to // 1.0 on both channels. The decode matrix collapses to: // W = 0.5·(kWRe·(l + r) + kWIm·(lj − rj)) = 0.5·(kWRe·2 + 0) = kWRe // X = 0.5·(kXRe·(l + r) + kXIm·(rj − lj)) = 0.5·(kXRe·2 + 0) = kXRe // Y = 0.5·(kYRe·(l − r) + kYIm·(lj + rj)) = 0.5·(0 + kYIm·2) = kYIm // Z = 0 // Any sign flip on the decode coefficients in AmbiUHJCoeffs.swift trips this. let dec: AmbiDecoderUHJ = AmbiDecoderUHJ(input: staticStereo(l: 1.0, r: 1.0)) let settleBlocks: Int = 48 for _: Int in 0..