Files
Lyrebird/Tests/LyrebirdTests/AmbiAFormatConversionTests.swift
T
2026-06-01 18:27:40 -07:00

111 lines
4.2 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// AmbiAFormatConversionTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("AmbiAFormat conversion (B↔A)")
struct AmbiAFormatConversionTests {
private let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
private var graph: SynthGraph { makeSynthGraph(engine: engine) }
init() throws {
try engine.start()
}
private func static4Row(_ a: Double, _ b: Double, _ c: Double, _ d: Double) -> GeneratorArray {
return GeneratorArray([
ConstantGeneratorInput(a) as any Generator,
ConstantGeneratorInput(b),
ConstantGeneratorInput(c),
ConstantGeneratorInput(d)
])
}
// MARK: - Output topology
@Test func bToAOutputCountIsFour() {
let bToA: AmbiBToA = AmbiBToA(input: static4Row(1, 0, 0, 0))
#expect(bToA.outputChannelCount == 4)
let out: [[Double]] = bToA.calculatedSamples(graph: graph, count: 64)
#expect(out.count == 4)
}
@Test func aToBOutputCountIsFour() {
let aToB: AmbiAToB = AmbiAToB(input: static4Row(1, 0, 0, 0))
#expect(aToB.outputChannelCount == 4)
let out: [[Double]] = aToB.calculatedSamples(graph: graph, count: 64)
#expect(out.count == 4)
}
// MARK: - Matrix correctness
@Test func bToAExpandsWOnlyEqually() {
// W only → all 4 capsules get W·0.5.
let bToA: AmbiBToA = AmbiBToA(input: static4Row(1.0, 0, 0, 0))
let out: [[Double]] = bToA.calculatedSamples(graph: graph, count: 64)
for row: Int in 0..<4 {
#expect(abs(out[row][32] - 0.5) < 1e-12)
}
}
@Test func bToAExpandsXAsFrontMinusBack() {
// X only → LFU = +0.5, RFD = +0.5, LBD = −0.5, RBU = −0.5.
let bToA: AmbiBToA = AmbiBToA(input: static4Row(0, 1.0, 0, 0))
let out: [[Double]] = bToA.calculatedSamples(graph: graph, count: 64)
#expect(abs(out[0][32] - 0.5) < 1e-12)
#expect(abs(out[1][32] - 0.5) < 1e-12)
#expect(abs(out[2][32] - (-0.5)) < 1e-12)
#expect(abs(out[3][32] - (-0.5)) < 1e-12)
}
@Test func aToBExtractsWFromEqualCapsules() {
// All 4 capsules = 1 → W = 0.5·4 = 2, X = Y = Z = 0.
let aToB: AmbiAToB = AmbiAToB(input: static4Row(1, 1, 1, 1))
let out: [[Double]] = aToB.calculatedSamples(graph: graph, count: 64)
#expect(abs(out[0][32] - 2.0) < 1e-12)
#expect(abs(out[1][32]) < 1e-12)
#expect(abs(out[2][32]) < 1e-12)
#expect(abs(out[3][32]) < 1e-12)
}
// MARK: - Round trip
@Test func bToAToBIsIdentity() {
// Round trip B → A → B should be identity (up to FP noise).
// Use distinct W/X/Y/Z so any cross-talk surfaces.
let original: GeneratorArray = static4Row(0.123, -0.456, 0.789, -0.234)
let bToA: AmbiBToA = AmbiBToA(input: original)
let aToB: AmbiAToB = AmbiAToB(input: bToA)
let out: [[Double]] = aToB.calculatedSamples(graph: graph, count: 64)
#expect(abs(out[0][32] - 0.123) < 1e-12)
#expect(abs(out[1][32] - (-0.456)) < 1e-12)
#expect(abs(out[2][32] - 0.789) < 1e-12)
#expect(abs(out[3][32] - (-0.234)) < 1e-12)
}
@Test func aToBToAIsIdentity() {
// Round trip A → B → A on distinct capsule values.
let original: GeneratorArray = static4Row(0.21, -0.34, 0.55, -0.42)
let aToB: AmbiAToB = AmbiAToB(input: original)
let bToA: AmbiBToA = AmbiBToA(input: aToB)
let out: [[Double]] = bToA.calculatedSamples(graph: graph, count: 64)
#expect(abs(out[0][32] - 0.21) < 1e-12)
#expect(abs(out[1][32] - (-0.34)) < 1e-12)
#expect(abs(out[2][32] - 0.55) < 1e-12)
#expect(abs(out[3][32] - (-0.42)) < 1e-12)
}
@Test func mulAddApplied() {
let aToB: AmbiAToB = AmbiAToB(input: static4Row(1, 1, 1, 1), mul: 2.0, add: 0.1)
let out: [[Double]] = aToB.calculatedSamples(graph: graph, count: 64)
// W = 2.0 baseline. After mul·add: 2·2 + 0.1 = 4.1.
#expect(abs(out[0][32] - 4.1) < 1e-12)
}
}