1.0.0 release

This commit is contained in:
2026-06-01 18:27:40 -07:00
parent b1ab0f102c
commit 1f20fcab20
1151 changed files with 230703 additions and 14 deletions
@@ -0,0 +1,152 @@
// AmbiEncoderUHJTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("AmbiEncoderUHJ")
struct AmbiEncoderUHJTests {
private let engine: SynthesisEngine = makeTestEngine(blockSize: 512, 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)
])
}
@Test func outputChannelCountIsTwo() {
let enc: AmbiEncoderUHJ = AmbiEncoderUHJ(input: staticBFormat(w: 1, x: 0, y: 0, z: 0))
#expect(enc.outputChannelCount == 2)
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: 64)
#expect(out.count == 2)
}
@Test func zChannelDroppedSilently() {
// Vary Z heavily — output L/R must be unchanged.
let lowZ: AmbiEncoderUHJ = AmbiEncoderUHJ(input: staticBFormat(w: 0.5, x: 0.5, y: 0.5, z: 0))
let highZ: AmbiEncoderUHJ = AmbiEncoderUHJ(input: staticBFormat(w: 0.5, x: 0.5, y: 0.5, z: 0.9))
// Settle Hilbert filters.
for _: Int in 0..<32 {
_ = lowZ.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
_ = highZ.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
advanceBlock(engine)
}
let outLow: [[Double]] = lowZ.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
let outHigh: [[Double]] = highZ.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
// Compare a late-block sample. Z shouldn't enter the UHJ output at all.
#expect(abs(outLow[0][500] - outHigh[0][500]) < 1e-10,
"Z must not affect L output")
#expect(abs(outLow[1][500] - outHigh[1][500]) < 1e-10,
"Z must not affect R output")
}
@Test func frontSourceBalancesLAndREnergy() {
// Encoded front-centre source (az=0, el=0) on a 1 kHz tone:
// W = (1/√2)·sin(ωt), X = sin(ωt), Y = 0, Z = 0.
// Y is the only L/R-asymmetric matrix term; with Y = 0 the energy should be
// balanced (RMS(L) ≈ RMS(R)) once the Hilbert filters settle. Per-sample
// equality does NOT hold — the wj/xj imaginary components have fixed signs
// between L and R, so the instantaneous difference is non-zero, but RMS over
// many cycles is the same.
let wScale: Double = 1.0 / Double(2.0).squareRoot()
let osc: OscSin.Audio = OscSin.Audio(freq: 1_000.0)
// Build W = (1/√2)·source, X = 1·source using mul.
let bformat: GeneratorArray = GeneratorArray([
(osc * wScale) as any Generator,
osc as any Generator,
ConstantGeneratorInput(0) as any Generator,
ConstantGeneratorInput(0) as any Generator
])
let enc: AmbiEncoderUHJ = AmbiEncoderUHJ(input: bformat)
// Settle Hilbert filters.
for _: Int in 0..<32 {
_ = enc.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
advanceBlock(engine)
}
let out: [[Double]] = enc.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
var rmsL: Double = 0
var rmsR: Double = 0
for i: Int in 0..<512 {
rmsL += out[0][i] * out[0][i]
rmsR += out[1][i] * out[1][i]
}
let ratio: Double = rmsL / rmsR
// Energies should match to within ±10% (Hilbert phase error introduces ~7% mismatch
// at 1 kHz under the Niemitalo cascade; hard-left pan would push the ratio well
// outside this band).
#expect(abs(ratio - 1.0) < 0.10,
"Front source: RMS(L) / RMS(R) = \(ratio), expected ~1.0")
}
@Test func panHardLeftFavoursLChannel() {
// Encoded source at az=π/2: X=0, Y=1, W=1/√2. UHJ matrix L gets +Y, R gets −Y.
// After settling, RMS(L) should exceed RMS(R).
let wScale: Double = 1.0 / Double(2.0).squareRoot()
let bformat: GeneratorArray = staticBFormat(w: wScale, x: 0, y: 1.0, z: 0)
let enc: AmbiEncoderUHJ = AmbiEncoderUHJ(input: bformat)
for _: Int in 0..<32 {
_ = enc.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
advanceBlock(engine)
}
let out: [[Double]] = enc.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
var rmsL: Double = 0
var rmsR: Double = 0
for i: Int in 0..<512 {
rmsL += out[0][i] * out[0][i]
rmsR += out[1][i] * out[1][i]
}
#expect(rmsL > rmsR, "Pan-hard-left: RMS(L)=\(rmsL) must exceed RMS(R)=\(rmsR)")
}
@Test func steadyStateDCWWithZeroXYPinsCoefficients() {
// Deterministic coefficient pin: with W = 1 DC and X = Y = Z = 0, both Hilbert
// paths on W settle to 1.0 (each IIR all-pass section has unity DC magnitude;
// 4 cascaded sections × π phase shift each = 4π = 0, so DC passes through with
// gain +1). The UHJ matrix collapses to:
// L = 0.5 · (uhjEncodeWRe · 1 − uhjEncodeWIm · 1) = 0.5 · (0.9397 − 0.342)
// R = 0.5 · (uhjEncodeWRe · 1 + uhjEncodeWIm · 1) = 0.5 · (0.9397 + 0.342)
// Any sign flip on `imagPart` or wrong constant in AmbiUHJCoeffs.swift will trip this.
let bformat: GeneratorArray = staticBFormat(w: 1.0, x: 0, y: 0, z: 0)
let enc: AmbiEncoderUHJ = AmbiEncoderUHJ(input: bformat)
// Long settle for the slow IIR pole — Hilbert constants document ≥ 16τ for 1e-4.
let settleBlocks: Int = 48
for _: Int in 0..<settleBlocks {
_ = enc.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
advanceBlock(engine)
}
let out: [[Double]] = enc.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
let expectedL: Double = 0.5 * (uhjEncodeWRe - uhjEncodeWIm)
let expectedR: Double = 0.5 * (uhjEncodeWRe + uhjEncodeWIm)
#expect(abs(out[0][500] - expectedL) < 1e-3,
"L steady-state for W=1 DC: expected ≈ \(expectedL), got \(out[0][500])")
#expect(abs(out[1][500] - expectedR) < 1e-3,
"R steady-state for W=1 DC: expected ≈ \(expectedR), got \(out[1][500])")
}
@Test func mulAddApplied() {
let bformat: GeneratorArray = staticBFormat(w: 1, x: 0, y: 0, z: 0)
let enc: AmbiEncoderUHJ = AmbiEncoderUHJ(input: bformat, mul: 2.0, add: 0.1)
// Settle Hilbert filters (long pole).
for _: Int in 0..<32 {
_ = enc.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
advanceBlock(engine)
}
let out: [[Double]] = enc.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512)
// Both channels: finite + offset by mul·add baseline (Hilbert settling around DC W input).
#expect(out[0][500].isFinite)
#expect(out[1][500].isFinite)
}
}