prep for 1.2.0 release updates

This commit is contained in:
2026-06-21 11:45:58 -07:00
parent 73191c958f
commit 032b7c39d3
286 changed files with 21116 additions and 121 deletions
@@ -0,0 +1,142 @@
// AmbiDecoderUHJTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@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..<settleBlocks {
_ = dec.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
advanceBlock(engine)
}
let out: [[Double]] = dec.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
let expectedW: Double = uhjDecodeWRe // = 0.982
let expectedX: Double = uhjDecodeXRe // = 0.419
let expectedY: Double = uhjDecodeYIm // = 0.385 (quadrature-only contribution survives)
#expect(abs(out[0][500] - expectedW) < 1e-3,
"W steady-state for L=R=1 DC: expected ≈ \(expectedW), got \(out[0][500])")
#expect(abs(out[1][500] - expectedX) < 1e-3,
"X steady-state: expected ≈ \(expectedX), got \(out[1][500])")
#expect(abs(out[2][500] - expectedY) < 1e-3,
"Y steady-state: expected ≈ \(expectedY), got \(out[2][500])")
#expect(out[3][500] == 0, "Z must be exactly 0")
}
@Test func mulAddApplied() {
let dec: AmbiDecoderUHJ = AmbiDecoderUHJ(input: staticStereo(l: 1, r: 1), mul: 2.0, add: 0.1)
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)
// Z row should be 0·2 + 0.1 = 0.1 after mul/add.
for s: Double in out[3].prefix(512) {
#expect(abs(s - 0.1) < 1e-12, "Z (silent + mul/add): expected 0.1, got \(s)")
}
}
}