prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
// AmbiAFormatConversionTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// AmbiDecoderMonoTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
// AmbiDecoderStereoTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("AmbiDecoderStereo")
|
||||
struct AmbiDecoderStereoTests {
|
||||
|
||||
private let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
private var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
/// Convenience: build a static B-format source from scalar W/X/Y/Z values.
|
||||
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 outputCountIsTwoChannels() {
|
||||
let dec: AmbiDecoderStereo = AmbiDecoderStereo(
|
||||
input: staticBFormat(w: 0.707, x: 1, y: 0, z: 0)
|
||||
)
|
||||
#expect(dec.outputChannelCount == 2)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 2)
|
||||
}
|
||||
|
||||
@Test func frontSourceProducesBalancedLR() {
|
||||
// Encoder at az=0, el=0 → W=1/√2, X=1, Y=0, Z=0. With Y=0, L=R.
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
let dec: AmbiDecoderStereo = AmbiDecoderStereo(
|
||||
input: staticBFormat(w: wScale, x: 1, y: 0, z: 0)
|
||||
)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(abs(out[0][32] - out[1][32]) < 1e-12,
|
||||
"Y=0 source must produce L == R")
|
||||
}
|
||||
|
||||
@Test func hardLeftSourceFavoursLeft() {
|
||||
// Source at az=π/2 → Y=1, X=0. L = W + 0.61, R = W − 0.61.
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
let dec: AmbiDecoderStereo = AmbiDecoderStereo(
|
||||
input: staticBFormat(w: wScale, x: 0, y: 1, z: 0)
|
||||
)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
let l: Double = out[0][32]
|
||||
let r: Double = out[1][32]
|
||||
#expect(l > r, "Left source must give L > R; got L=\(l) R=\(r)")
|
||||
#expect(abs(l - (wScale + 0.61)) < 1e-12)
|
||||
#expect(abs(r - (wScale - 0.61)) < 1e-12)
|
||||
}
|
||||
|
||||
@Test func zSilentlyDropped() {
|
||||
// Pure Z (elevation = π/2) source: W=1/√2, Z=1, X=Y=0. Decoder uses no Z.
|
||||
// L = R = W. Increasing |Z| arbitrarily changes nothing.
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
let decLowZ: AmbiDecoderStereo = AmbiDecoderStereo(
|
||||
input: staticBFormat(w: wScale, x: 0, y: 0, z: 0)
|
||||
)
|
||||
let decHighZ: AmbiDecoderStereo = AmbiDecoderStereo(
|
||||
input: staticBFormat(w: wScale, x: 0, y: 0, z: 1.0)
|
||||
)
|
||||
let outLow: [[Double]] = decLowZ.calculatedSamples(graph: graph, count: 64)
|
||||
let outHigh: [[Double]] = decHighZ.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(outLow[0][32] == outHigh[0][32])
|
||||
#expect(outLow[1][32] == outHigh[1][32])
|
||||
}
|
||||
|
||||
@Test func mulAddApplied() {
|
||||
let dec: AmbiDecoderStereo = AmbiDecoderStereo(
|
||||
input: staticBFormat(w: 1, x: 0, y: 0, z: 0),
|
||||
mul: 2.0,
|
||||
add: 0.1
|
||||
)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
// L = R = W·1 + 0.35·0 + 0.61·0 = 1. After mul·add: 1·2 + 0.1 = 2.1.
|
||||
#expect(abs(out[0][32] - 2.1) < 1e-12)
|
||||
#expect(abs(out[1][32] - 2.1) < 1e-12)
|
||||
}
|
||||
}
|
||||
@@ -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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// AmbiEncoder1ParameterRampingTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("AmbiEncoder1 parameter ramping")
|
||||
struct AmbiEncoder1ParameterRampingTests {
|
||||
|
||||
/// Audio-rate Generator drives azimuth — a constant 1.0 input source encoded
|
||||
/// with `azimuth = ConstantGeneratorInput(.pi / 2)` should yield X≈0, Y≈1
|
||||
/// throughout the block (no ramping, no parameter settle window — every
|
||||
/// sample sees the same azimuth).
|
||||
@Test func audioRateGeneratorDrivesAzimuth() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let enc: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: ConstantGeneratorInput(.pi / 2),
|
||||
elevation: 0.0
|
||||
)
|
||||
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: g, count: 64)
|
||||
// Sample 0 should already be at π/2 (no ramp — Generator delivers
|
||||
// the constant per-sample directly).
|
||||
#expect(abs(out[1][0]) < 1e-12, "X at audio-rate az=π/2 should be 0 at sample 0")
|
||||
#expect(abs(out[2][0] - 1.0) < 1e-12, "Y at audio-rate az=π/2 should be 1 at sample 0")
|
||||
}
|
||||
|
||||
@Test func azimuthRampIsApplied() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let lastSampleIdx: Int = engine.blockSize - 1
|
||||
|
||||
// ParameterNode now drives azimuth via the new `any ValidGeneratorInput`
|
||||
// init parameter — same ramping semantics as the prior @LiveParam-backed
|
||||
// version, but the caller owns the node.
|
||||
let azNode: ParameterNode = ParameterNode(0.0)
|
||||
let enc: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: azNode,
|
||||
elevation: 0.0
|
||||
)
|
||||
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = enc.calculatedSamples(graph: g1, count: 64)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 2: ramp azimuth to π/2 → X=0, Y=1 at last sample.
|
||||
azNode.value = .pi / 2
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = enc.calculatedSamples(graph: g2, count: 64)
|
||||
// First sample: az still near 0 → X≈1, Y≈0.
|
||||
// Last sample: az≈π/2 → X≈0, Y≈1.
|
||||
let xLast: Double = out2[1][lastSampleIdx] // X channel
|
||||
let yLast: Double = out2[2][lastSampleIdx] // Y channel
|
||||
#expect(abs(xLast) < 1e-4, "After azimuth=π/2 ramp: X should be ~0 at last sample")
|
||||
#expect(abs(yLast - 1.0) < 1e-4, "After azimuth=π/2 ramp: Y should be ~1 at last sample")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// AmbiEncoderStereoTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("AmbiEncoderStereo")
|
||||
struct AmbiEncoderStereoTests {
|
||||
|
||||
private let engine: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
private var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
@Test func outputChannelCountIsFour() {
|
||||
let enc: AmbiEncoderStereo = AmbiEncoderStereo(left: 1.0, right: 1.0)
|
||||
#expect(enc.outputChannelCount == 4)
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 4)
|
||||
}
|
||||
|
||||
@Test func zRowIsAlwaysSilent() {
|
||||
let enc: AmbiEncoderStereo = AmbiEncoderStereo(left: 0.7, right: -0.3, spread: .pi / 4)
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: 64)
|
||||
for s: Double in out[3].prefix(64) { #expect(s == 0) }
|
||||
}
|
||||
|
||||
@Test func spreadZeroMonoCentre() {
|
||||
// spread = 0 → cos = 1, sin = 0. Y row should be 0; W/X both ∝ (L + R).
|
||||
let enc: AmbiEncoderStereo = AmbiEncoderStereo(left: 0.6, right: 0.4, spread: 0)
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: 64)
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
let sum: Double = 0.6 + 0.4
|
||||
#expect(abs(out[0][32] - wScale * sum) < 1e-12) // W
|
||||
#expect(abs(out[1][32] - sum) < 1e-12) // X = cos(0)·sum = sum
|
||||
#expect(abs(out[2][32]) < 1e-12) // Y = sin(0)·diff = 0
|
||||
}
|
||||
|
||||
@Test func spreadHalfPiHardLeftRight() {
|
||||
// spread = π/2 → cos = 0 (X silent), sin = 1. Y = L − R.
|
||||
let enc: AmbiEncoderStereo = AmbiEncoderStereo(left: 1.0, right: 0.0, spread: .pi / 2)
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: 64)
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
#expect(abs(out[0][32] - wScale * 1.0) < 1e-12) // W = (1/√2) × (1 + 0)
|
||||
#expect(abs(out[1][32]) < 1e-12) // X = 0
|
||||
#expect(abs(out[2][32] - 1.0) < 1e-12) // Y = 1 × (1 − 0)
|
||||
}
|
||||
|
||||
@Test func matchesTwoEncoder1ReferenceAtPiOver4() {
|
||||
// spread = π/4 with left=1, right=0 should match an AmbiEncoder1 at az=+π/4.
|
||||
let spread: Double = .pi / 4
|
||||
let enc: AmbiEncoderStereo = AmbiEncoderStereo(left: 1.0, right: 0.0, spread: spread)
|
||||
let ref: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: spread,
|
||||
elevation: 0
|
||||
)
|
||||
// Settle parameter ramps in both via several blocks.
|
||||
for _: Int in 0..<8 {
|
||||
_ = enc.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 64)
|
||||
_ = ref.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 64)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
let encOut: [[Double]] = enc.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 64)
|
||||
let refOut: [[Double]] = ref.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 64)
|
||||
// Stereo encoder with right=0 should equal AmbiEncoder1 driven by left source at +spread.
|
||||
for row: Int in 0..<4 {
|
||||
#expect(abs(encOut[row][32] - refOut[row][32]) < 1e-10,
|
||||
"Row \(row) mismatch: \(encOut[row][32]) vs \(refOut[row][32])")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func mulAddApplied() {
|
||||
let enc: AmbiEncoderStereo = AmbiEncoderStereo(
|
||||
left: 0.5, right: 0.5, spread: 0, mul: 2.0, add: 0.1
|
||||
)
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: 64)
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
// W = wScale · 1.0 = 0.707, after mul·add = 0.707·2 + 0.1 ≈ 1.514.
|
||||
#expect(abs(out[0][32] - (wScale * 1.0 * 2.0 + 0.1)) < 1e-12)
|
||||
// Z = 0 → mul·add = 0 + 0.1 = 0.1.
|
||||
#expect(abs(out[3][32] - 0.1) < 1e-12)
|
||||
}
|
||||
|
||||
/// Audio-rate Generator drives `spread`. With `spread = π/2` (constant
|
||||
/// signal), X row coefficient cos(π/2) ≈ 0 → X channel silent; Y row
|
||||
/// coefficient sin(π/2) = 1 → Y = (L − R). Pins the new
|
||||
/// `_spreadIsGenerator == true` per-sample code path.
|
||||
@Test func audioRateGeneratorDrivesSpread() {
|
||||
let enc: AmbiEncoderStereo = AmbiEncoderStereo(
|
||||
left: 1.0,
|
||||
right: 0.0,
|
||||
spread: ConstantGeneratorInput(.pi / 2)
|
||||
)
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: 64)
|
||||
// Sample 0 should already be at π/2 (Generator delivers constant
|
||||
// per-sample directly — no ramp window).
|
||||
#expect(abs(out[1][0]) < 1e-12, "X = cos(π/2)·(L+R) should be 0")
|
||||
#expect(abs(out[2][0] - 1.0) < 1e-12, "Y = sin(π/2)·(L−R) should be 1.0")
|
||||
// Z row stays silent regardless of spread path.
|
||||
#expect(abs(out[3][0]) < 1e-12, "Z row always silent")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
// AmbiEncoderUHJTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
// AmbiSpeakerLayoutTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("AmbiSpeakerLayout")
|
||||
struct AmbiSpeakerLayoutTests {
|
||||
|
||||
private let azTolerance: Double = 1e-12
|
||||
private let elTolerance: Double = 1e-12
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private func makeEngine(blockSize: Int = 64) throws -> SynthesisEngine {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
return engine
|
||||
}
|
||||
|
||||
private func ringStepRadians(_ n: Int) -> Double { 2.0 * .pi / Double(n) }
|
||||
|
||||
// MARK: - Ring layouts
|
||||
|
||||
@Test func quadDefaultHasFourHorizontalSpeakers() {
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.quad()
|
||||
#expect(speakers.count == 4)
|
||||
for sp: (az: Double, el: Double) in speakers {
|
||||
#expect(abs(sp.el) < elTolerance)
|
||||
}
|
||||
let step: Double = ringStepRadians(4)
|
||||
for (i, sp) in speakers.enumerated() {
|
||||
#expect(abs(sp.az - Double(i) * step) < azTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func quadWithOffsetRotatesEveryAzimuth() {
|
||||
let offset: Double = .pi / 4
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.quad(azimuthOffset: offset)
|
||||
let step: Double = ringStepRadians(4)
|
||||
for (i, sp) in speakers.enumerated() {
|
||||
#expect(abs(sp.az - (offset + Double(i) * step)) < azTolerance)
|
||||
#expect(abs(sp.el) < elTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func hexagonHasSixHorizontalSpeakersEvenlySpaced() {
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.hexagon()
|
||||
#expect(speakers.count == 6)
|
||||
let step: Double = ringStepRadians(6)
|
||||
for i: Int in 1..<speakers.count {
|
||||
#expect(abs((speakers[i].az - speakers[i - 1].az) - step) < azTolerance)
|
||||
#expect(abs(speakers[i].el) < elTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func octagonHasEightHorizontalSpeakersEvenlySpaced() {
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.octagon()
|
||||
#expect(speakers.count == 8)
|
||||
let step: Double = ringStepRadians(8)
|
||||
for i: Int in 1..<speakers.count {
|
||||
#expect(abs((speakers[i].az - speakers[i - 1].az) - step) < azTolerance)
|
||||
#expect(abs(speakers[i].el) < elTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Double-ring layouts
|
||||
|
||||
@Test func cubeStacksTwoFourSpeakerRingsWithMirroredElevation() {
|
||||
let elevation: Double = .pi / 6
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.cube(elevation: elevation)
|
||||
#expect(speakers.count == 8)
|
||||
|
||||
for k: Int in 0..<4 {
|
||||
#expect(abs(speakers[k].el - elevation) < elTolerance)
|
||||
}
|
||||
for k: Int in 4..<8 {
|
||||
#expect(abs(speakers[k].el - (-elevation)) < elTolerance)
|
||||
}
|
||||
// Upper and lower rings share azimuths element-wise.
|
||||
for k: Int in 0..<4 {
|
||||
#expect(abs(speakers[k].az - speakers[k + 4].az) < azTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func cubeAzimuthOffsetAppliesToBothRings() {
|
||||
let offset: Double = .pi / 8
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.cube(
|
||||
elevation: .pi / 6, azimuthOffset: offset
|
||||
)
|
||||
let step: Double = ringStepRadians(4)
|
||||
for k: Int in 0..<4 {
|
||||
#expect(abs(speakers[k].az - (offset + Double(k) * step)) < azTolerance)
|
||||
#expect(abs(speakers[k + 4].az - (offset + Double(k) * step)) < azTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func doubleHexagonStacksTwoSixSpeakerRings() {
|
||||
let elevation: Double = .pi / 6
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.doubleHexagon(
|
||||
elevation: elevation
|
||||
)
|
||||
#expect(speakers.count == 12)
|
||||
for k: Int in 0..<6 {
|
||||
#expect(abs(speakers[k].el - elevation) < elTolerance)
|
||||
}
|
||||
for k: Int in 6..<12 {
|
||||
#expect(abs(speakers[k].el - (-elevation)) < elTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func doubleOctagonStacksTwoEightSpeakerRings() {
|
||||
let elevation: Double = .pi / 6
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.doubleOctagon(
|
||||
elevation: elevation
|
||||
)
|
||||
#expect(speakers.count == 16)
|
||||
for k: Int in 0..<8 {
|
||||
#expect(abs(speakers[k].el - elevation) < elTolerance)
|
||||
}
|
||||
for k: Int in 8..<16 {
|
||||
#expect(abs(speakers[k].el - (-elevation)) < elTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - stackedRings — general form
|
||||
|
||||
@Test func stackedRingsSingleLayerIgnoresElevation() {
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.stackedRings(
|
||||
vertices: 5, layers: 1, elevation: 0.3
|
||||
)
|
||||
#expect(speakers.count == 5)
|
||||
let step: Double = ringStepRadians(5)
|
||||
for (i, sp) in speakers.enumerated() {
|
||||
#expect(abs(sp.el) < elTolerance)
|
||||
#expect(abs(sp.az - Double(i) * step) < azTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func stackedRingsTwoLayersSplitsUpperAndLower() {
|
||||
let elevation: Double = 0.4
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.stackedRings(
|
||||
vertices: 7, layers: 2, elevation: elevation
|
||||
)
|
||||
#expect(speakers.count == 14)
|
||||
for k: Int in 0..<7 {
|
||||
#expect(abs(speakers[k].el - elevation) < elTolerance)
|
||||
#expect(abs(speakers[k + 7].el - (-elevation)) < elTolerance)
|
||||
#expect(abs(speakers[k].az - speakers[k + 7].az) < azTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func stackedRingsThreeLayersDistributesElevationEvenly() {
|
||||
let elevation: Double = 0.5
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.stackedRings(
|
||||
vertices: 4, layers: 3, elevation: elevation
|
||||
)
|
||||
#expect(speakers.count == 12)
|
||||
for k: Int in 0..<4 {
|
||||
#expect(abs(speakers[k].el - elevation) < elTolerance)
|
||||
}
|
||||
for k: Int in 4..<8 {
|
||||
#expect(abs(speakers[k].el - 0) < elTolerance)
|
||||
}
|
||||
for k: Int in 8..<12 {
|
||||
#expect(abs(speakers[k].el - (-elevation)) < elTolerance)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func stackedRingsFourLayersStepsEvenlyFromPlusToMinus() {
|
||||
let elevation: Double = 0.6
|
||||
let speakers: [(az: Double, el: Double)] = AmbiSpeakerLayout.stackedRings(
|
||||
vertices: 4, layers: 4, elevation: elevation
|
||||
)
|
||||
#expect(speakers.count == 16)
|
||||
let stepEl: Double = 2.0 * elevation / 3.0
|
||||
for layer: Int in 0..<4 {
|
||||
let expectedEl: Double = elevation - stepEl * Double(layer)
|
||||
for k: Int in 0..<4 {
|
||||
#expect(abs(speakers[layer * 4 + k].el - expectedEl) < elTolerance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AmbiDecoder1 integration smoke test
|
||||
|
||||
@Test func decoderAcceptsLayoutOutputForQuad() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let enc: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: 0.0,
|
||||
elevation: 0.0
|
||||
)
|
||||
|
||||
let dec: AmbiDecoder1 = AmbiDecoder1(
|
||||
input: enc,
|
||||
speakers: AmbiSpeakerLayout.quad(azimuthOffset: .pi / 4)
|
||||
)
|
||||
#expect(dec.outputChannelCount == 4)
|
||||
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 4)
|
||||
for row: [Double] in out {
|
||||
for s: Double in row.prefix(64) {
|
||||
#expect(s.isFinite)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// Pan2ParameterRampingTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Pan2 parameter ramping")
|
||||
struct Pan2ParameterRampingTests {
|
||||
|
||||
@Test func generatorPanInputAffectsOutput() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let lastSampleIdx: Int = engine.blockSize - 1
|
||||
|
||||
// Wire a ParameterNode as the pan input — it will ramp if changed.
|
||||
let panNode: ParameterNode = ParameterNode(0.0)
|
||||
let src: OscSin.Audio = OscSin.Audio(freq: 440.0)
|
||||
let pan: Pan2 = Pan2(input: src, pan: panNode)
|
||||
|
||||
// Block 1: pan = 0 (center) — both channels equal.
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out1: [[Double]] = pan.calculatedSamples(graph: g1, count: 64)
|
||||
let leftFirst: Double = out1[0][0]
|
||||
let rightFirst: Double = out1[1][0]
|
||||
#expect(abs(leftFirst - rightFirst) < 1e-4,
|
||||
"Center pan: L and R should be equal at sample 0")
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 2: pan = 1.0 (hard right) — ramp plays out over the block.
|
||||
panNode.value = 1.0
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = pan.calculatedSamples(graph: g2, count: 64)
|
||||
// At the last sample the ramp has reached the target (hard right).
|
||||
// Right channel should be much larger than left.
|
||||
let leftLast: Double = abs(out2[0][lastSampleIdx])
|
||||
let rightLast: Double = abs(out2[1][lastSampleIdx])
|
||||
#expect(rightLast > leftLast + 0.1,
|
||||
"After pan=1.0 ramp: right channel should exceed left at last sample")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
// PanAzDistanceTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("PanAzDistance")
|
||||
struct PanAzDistanceTests {
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
|
||||
let testSampleRate: Double = 44100
|
||||
let blockSize: Int = 512
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
// MARK: - Basic shape
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: PanAzDistance = PanAzDistance(input: 0.0, sampleRate: testSampleRate)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
@Test func outputHasFourChannels() {
|
||||
let gen: PanAzDistance = PanAzDistance(input: ConstantGeneratorInput(1.0), sampleRate: testSampleRate)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(out.count == 4)
|
||||
#expect(gen.outputChannelCount == 4)
|
||||
}
|
||||
|
||||
@Test func silentInputProducesSilentOutput() {
|
||||
let gen: PanAzDistance = PanAzDistance(input: 0.0, azimuth: .pi / 4, distance: 2.0,
|
||||
sampleRate: testSampleRate)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for ch: Int in 0..<4 {
|
||||
let samples: [Double] = Array(out[ch].prefix(blockSize))
|
||||
#expect(samples.allSatisfy { abs($0) < 1e-10 },
|
||||
"ch\(ch): zero input must produce near-zero output")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func firstBlockIsFinite() {
|
||||
let gen: PanAzDistance = PanAzDistance(
|
||||
input: ConstantGeneratorInput(0.5),
|
||||
azimuth: 0.3, distance: 0.7, velocity: 5.0,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for ch: Int in 0..<4 {
|
||||
let samples: [Double] = Array(out[ch].prefix(blockSize))
|
||||
#expect(samples.allSatisfy { $0.isFinite },
|
||||
"ch\(ch): all samples must be finite")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Inside the boundary
|
||||
|
||||
/// At `distance = 1`, az = π/4 should hard-pan to the RF corner — equivalent
|
||||
/// to `PanAz` at the same angle with the LP near Nyquist + velocity=0.
|
||||
@Test func boundaryCornerMatchesPanAz() {
|
||||
let cutoff: Double = 22000.0
|
||||
let gen: PanAzDistance = PanAzDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: .pi / 4, distance: 1.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let last: Int = blockSize - 1
|
||||
#expect(abs(out[0][last]) < 1e-3, "LF at az=π/4 boundary should be ~0")
|
||||
#expect(abs(out[1][last] - 1.0) < 1e-3, "RF at az=π/4 boundary should be ~1")
|
||||
#expect(abs(out[2][last]) < 1e-3, "RR at az=π/4 boundary should be ~0")
|
||||
#expect(abs(out[3][last]) < 1e-3, "LR at az=π/4 boundary should be ~0")
|
||||
}
|
||||
|
||||
/// At `distance = 0` (listener position) all four speakers should receive
|
||||
/// equal energy = 0.5 × input.
|
||||
@Test func distanceZeroCollapsesToEqualEnergy() {
|
||||
let cutoff: Double = 22000.0
|
||||
let gen: PanAzDistance = PanAzDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: 1.234, distance: 0.0, velocity: 0.0, // azimuth irrelevant at r=0
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let last: Int = blockSize - 1
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(out[ch][last] - 0.5) < 1e-3,
|
||||
"distance=0: ch\(ch) should be ~0.5, got \(out[ch][last])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Distance attenuation outside the boundary
|
||||
|
||||
/// At `distance = 2` along az = π/4 (RF corner direction), the RF channel
|
||||
/// should be ~half the gain it had at `distance = 1` (1/d attenuation).
|
||||
@Test func outsideBoundaryHalvedAtDoubleDistance() {
|
||||
let cutoff: Double = 22000.0
|
||||
|
||||
let near: PanAzDistance = PanAzDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: .pi / 4, distance: 1.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = near.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let nearOut: [[Double]] = near.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let lastNear: Double = nearOut[1][blockSize - 1]
|
||||
|
||||
let far: PanAzDistance = PanAzDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: .pi / 4, distance: 2.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = far.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let farOut: [[Double]] = far.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let lastFar: Double = farOut[1][blockSize - 1]
|
||||
|
||||
// amp(far)/amp(near) = (1/2) / 1 = 0.5.
|
||||
let ratio: Double = lastFar / lastNear
|
||||
#expect(abs(ratio - 0.5) < 0.02,
|
||||
"distance=2/distance=1 amplitude ratio should be ~0.5, got \(ratio)")
|
||||
}
|
||||
|
||||
/// Negative distance is treated as `|distance|` — direction comes from
|
||||
/// azimuth alone.
|
||||
@Test func negativeDistanceTreatedAsAbsoluteValue() {
|
||||
let cutoff: Double = 22000.0
|
||||
|
||||
let positive: PanAzDistance = PanAzDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: .pi / 4, distance: 2.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = positive.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let posOut: [[Double]] = positive.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let last: Int = blockSize - 1
|
||||
let posRF: Double = posOut[1][last]
|
||||
|
||||
let negative: PanAzDistance = PanAzDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: .pi / 4, distance: -2.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = negative.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let negOut: [[Double]] = negative.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let negRF: Double = negOut[1][last]
|
||||
|
||||
#expect(abs(posRF - negRF) < 1e-3,
|
||||
"negative distance must match |distance|: posRF=\(posRF) vs negRF=\(negRF)")
|
||||
}
|
||||
|
||||
// MARK: - Doppler
|
||||
|
||||
/// Positive velocity → read rate < 1 → output sine at lower frequency than input.
|
||||
@Test func dopplerLowersPitchForPositiveVelocity() {
|
||||
let velocity: Double = 50.0
|
||||
let speedOfSound: Double = 343.0
|
||||
let expectedRate: Double = 1.0 - velocity / speedOfSound
|
||||
|
||||
let inputFreq: Double = 440.0
|
||||
let osc: OscSin.Audio = OscSin.Audio(freq: ConstantGeneratorInput(inputFreq))
|
||||
|
||||
let gen: PanAzDistance = PanAzDistance(
|
||||
input: osc,
|
||||
azimuth: 0.0, distance: 0.0, // collapse to centre; no atten or LP
|
||||
velocity: ConstantGeneratorInput(velocity),
|
||||
airAbsorptionCutoff: 22000.0,
|
||||
speedOfSound: speedOfSound,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
|
||||
// Skip first few blocks for the delay line / LP to fill + settle.
|
||||
for _ in 0..<4 {
|
||||
_ = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let ch: [Double] = Array(out[0].prefix(blockSize))
|
||||
|
||||
var zeroCrossings: Int = 0
|
||||
for i: Int in 1..<ch.count {
|
||||
if (ch[i - 1] < 0 && ch[i] >= 0) || (ch[i - 1] > 0 && ch[i] <= 0) {
|
||||
zeroCrossings += 1
|
||||
}
|
||||
}
|
||||
let expectedFreq: Double = inputFreq * expectedRate
|
||||
let measuredFreq: Double = Double(zeroCrossings) * testSampleRate / (2.0 * Double(blockSize))
|
||||
let err: Double = abs(measuredFreq - expectedFreq) / expectedFreq
|
||||
#expect(err < 0.05,
|
||||
"Doppler: expected ~\(expectedFreq) Hz, measured \(measuredFreq) Hz (err \(err))")
|
||||
}
|
||||
|
||||
// MARK: - mul / add
|
||||
|
||||
@Test func mulIsAppliedPerChannel() {
|
||||
let gen: PanAzDistance = PanAzDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: 0.0, distance: 0.0, velocity: 0.0, // collapse to centre
|
||||
airAbsorptionCutoff: 22000.0,
|
||||
sampleRate: testSampleRate,
|
||||
mul: 2.0
|
||||
)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let last: Int = blockSize - 1
|
||||
// distance=0 → equal-energy 0.5 per channel, × 2 = 1.0.
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(out[ch][last] - 1.0) < 1e-2,
|
||||
"mul=2 at distance=0: ch\(ch) should be ~1.0, got \(out[ch][last])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Fan-out cache
|
||||
|
||||
@Test func fanOutCacheProducesIdenticalOutput() {
|
||||
let gen: PanAzDistance = PanAzDistance(
|
||||
input: ConstantGeneratorInput(0.7),
|
||||
azimuth: 0.3, distance: 0.5, velocity: 0.0,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(out1[ch] == out2[ch], "Fan-out cache mismatch on ch\(ch)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// PanAzParameterRampingTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("PanAz parameter ramping")
|
||||
struct PanAzParameterRampingTests {
|
||||
|
||||
@Test func azimuthRampIsApplied() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let lastSampleIdx: Int = engine.blockSize - 1
|
||||
|
||||
let pan: PanAz = PanAz(input: ConstantGeneratorInput(1.0))
|
||||
pan.azimuth = 0.0 // front centre
|
||||
|
||||
// Block 1: settle az=0.
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = pan.calculatedSamples(graph: g1, count: 64)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 2: ramp azimuth to π/4 (RF corner).
|
||||
pan.azimuth = .pi / 4
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = pan.calculatedSamples(graph: g2, count: 64)
|
||||
// At sample lastSampleIdx, az ≈ π/4 → hard-RF. ch0 (LF) ≈ 0, ch1 (RF) ≈ 1.
|
||||
let ch0Last: Double = abs(out2[0][lastSampleIdx])
|
||||
let ch1Last: Double = abs(out2[1][lastSampleIdx])
|
||||
#expect(ch0Last < 1e-4, "After azimuth=π/4 ramp: LF (ch0) should be near 0")
|
||||
#expect(ch1Last > 0.9, "After azimuth=π/4 ramp: RF (ch1) should dominate (>0.9), got \(ch1Last)")
|
||||
}
|
||||
|
||||
@Test func azimuthRampSweepsFrontToRear() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let lastSampleIdx: Int = engine.blockSize - 1
|
||||
|
||||
let pan: PanAz = PanAz(input: ConstantGeneratorInput(1.0))
|
||||
pan.azimuth = 0.0 // front centre
|
||||
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = pan.calculatedSamples(graph: g1, count: 64)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 2: ramp azimuth to π (rear centre).
|
||||
pan.azimuth = .pi
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = pan.calculatedSamples(graph: g2, count: 64)
|
||||
// At last sample az=π → (0, -1) → RR + LR even split, LF + RF ≈ 0.
|
||||
let ch0Last: Double = abs(out2[0][lastSampleIdx])
|
||||
let ch1Last: Double = abs(out2[1][lastSampleIdx])
|
||||
let ch2Last: Double = abs(out2[2][lastSampleIdx])
|
||||
let ch3Last: Double = abs(out2[3][lastSampleIdx])
|
||||
#expect(ch0Last < 1e-4, "After az=π ramp: LF should be near 0")
|
||||
#expect(ch1Last < 1e-4, "After az=π ramp: RF should be near 0")
|
||||
#expect(ch2Last > 0.5, "After az=π ramp: RR should carry rear energy")
|
||||
#expect(ch3Last > 0.5, "After az=π ramp: LR should carry rear energy")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// PanNParameterRampingTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("PanN parameter ramping")
|
||||
struct PanNParameterRampingTests {
|
||||
|
||||
@Test func generatorPositionRampIsApplied() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let lastSampleIdx: Int = engine.blockSize - 1
|
||||
|
||||
let posNode: ParameterNode = ParameterNode(0.0)
|
||||
let pan: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: 4, position: posNode)
|
||||
|
||||
// Block 1: position = 0 (all energy on ch0).
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out1: [[Double]] = pan.calculatedSamples(graph: g1, count: 64)
|
||||
#expect(abs(out1[0][0] - 1.0) < 1e-6, "Position=0: ch0 should be 1.0 at sample 0")
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 2: position ramps 0→1 (energy migrates toward ch1).
|
||||
posNode.value = 1.0
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = pan.calculatedSamples(graph: g2, count: 64)
|
||||
// Last sample: position=1 → full gain on ch1.
|
||||
#expect(abs(out2[1][lastSampleIdx] - 1.0) < 1e-5,
|
||||
"After position=1 ramp: ch1 last sample should be ~1.0")
|
||||
#expect(abs(out2[0][lastSampleIdx]) < 1e-5,
|
||||
"After position=1 ramp: ch0 last sample should be ~0.0")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
// PanNTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("PanN")
|
||||
struct PanNTests {
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
func makeEngine(blockSize: Int = 512) throws -> SynthesisEngine {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
return engine
|
||||
}
|
||||
|
||||
/// Returns per-channel peak gains for a unit-amplitude constant input.
|
||||
func gains(channels: Int, position: Double, blockSize: Int = 64) throws -> [Double] {
|
||||
let engine: SynthesisEngine = try makeEngine(blockSize: blockSize)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: channels, position: position)
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: blockSize)
|
||||
return out.map { $0[0] } // first sample of each channel = block-constant gain × 1.0
|
||||
}
|
||||
|
||||
// MARK: - Channel count
|
||||
|
||||
@Test func outputChannelCountMatchesChannelParam() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
for n: Int in [2, 3, 4, 6, 8, 12] {
|
||||
let pan: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: n)
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == n, "PanN(channels:\(n)) should produce \(n) output channels")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Position = 0: full gain on channel 0
|
||||
|
||||
@Test func positionZeroGivesFullGainOnChannel0() throws {
|
||||
let g: [Double] = try gains(channels: 4, position: 0.0)
|
||||
#expect(abs(g[0] - 1.0) < 1e-6, "position=0: ch0 gain should be 1.0, got \(g[0])")
|
||||
#expect(abs(g[1]) < 1e-6, "position=0: ch1 gain should be 0.0, got \(g[1])")
|
||||
#expect(abs(g[2]) < 1e-6, "position=0: ch2 gain should be 0.0, got \(g[2])")
|
||||
#expect(abs(g[3]) < 1e-6, "position=0: ch3 gain should be 0.0, got \(g[3])")
|
||||
}
|
||||
|
||||
// MARK: - Position = 0.5: equal power between channels 0 and 1
|
||||
|
||||
@Test func positionHalfGivesEqualPowerBetween0And1() throws {
|
||||
let g: [Double] = try gains(channels: 4, position: 0.5)
|
||||
let expected: Double = Double(2.0).squareRoot() / 2.0 // √2/2 ≈ 0.7071
|
||||
#expect(abs(g[0] - expected) < 1e-5, "position=0.5: ch0 gain should be √2/2, got \(g[0])")
|
||||
#expect(abs(g[1] - expected) < 1e-5, "position=0.5: ch1 gain should be √2/2, got \(g[1])")
|
||||
#expect(abs(g[2]) < 1e-6, "position=0.5: ch2 gain should be 0, got \(g[2])")
|
||||
#expect(abs(g[3]) < 1e-6, "position=0.5: ch3 gain should be 0, got \(g[3])")
|
||||
}
|
||||
|
||||
// MARK: - Position = 1: full gain on channel 1
|
||||
|
||||
@Test func positionOneGivesFullGainOnChannel1() throws {
|
||||
let g: [Double] = try gains(channels: 4, position: 1.0)
|
||||
#expect(abs(g[0]) < 1e-6, "position=1: ch0 should be 0")
|
||||
#expect(abs(g[1] - 1.0) < 1e-6, "position=1: ch1 should be 1.0, got \(g[1])")
|
||||
#expect(abs(g[2]) < 1e-6, "position=1: ch2 should be 0")
|
||||
#expect(abs(g[3]) < 1e-6, "position=1: ch3 should be 0")
|
||||
}
|
||||
|
||||
// MARK: - Wrapping: position = N equivalent to position = 0
|
||||
|
||||
@Test func positionAtNWrapsToChannel0() throws {
|
||||
let n: Int = 4
|
||||
let g0: [Double] = try gains(channels: n, position: 0.0)
|
||||
let gN: [Double] = try gains(channels: n, position: Double(n))
|
||||
for ch: Int in 0..<n {
|
||||
#expect(abs(gN[ch] - g0[ch]) < 1e-6,
|
||||
"position=N should equal position=0 on ch\(ch): \(gN[ch]) vs \(g0[ch])")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func positiveWrapEquivalence() throws {
|
||||
let n: Int = 8
|
||||
let ref: [Double] = try gains(channels: n, position: 0.5)
|
||||
let wrp: [Double] = try gains(channels: n, position: Double(n) + 0.5)
|
||||
for ch: Int in 0..<n {
|
||||
#expect(abs(wrp[ch] - ref[ch]) < 1e-6,
|
||||
"position=N+0.5 should equal position=0.5 on ch\(ch)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Negative position wraps
|
||||
|
||||
@Test func negativePositionWraps() throws {
|
||||
let n: Int = 4
|
||||
let nF: Double = Double(n)
|
||||
let refPos: Double = nF - 0.5 // between ch3 and ch0
|
||||
let ref: [Double] = try gains(channels: n, position: refPos)
|
||||
let neg: [Double] = try gains(channels: n, position: -0.5)
|
||||
for ch: Int in 0..<n {
|
||||
#expect(abs(neg[ch] - ref[ch]) < 1e-6,
|
||||
"position=-0.5 should equal position=N-0.5 on ch\(ch)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Equal-power invariant
|
||||
|
||||
@Test func equalPowerInvariant() throws {
|
||||
let n: Int = 8
|
||||
// Test many positions around the ring.
|
||||
let positions: [Double] = [0.0, 0.1, 0.5, 0.9, 1.0, 2.3, 3.7, 7.5]
|
||||
for pos: Double in positions {
|
||||
let g: [Double] = try gains(channels: n, position: pos)
|
||||
let sumSq: Double = g.reduce(0.0, { $0 + $1 * $1 })
|
||||
#expect(abs(sumSq - 1.0) < 1e-5,
|
||||
"Equal-power invariant failed at position=\(pos): sum of squares = \(sumSq)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Smoke: non-silent, finite output
|
||||
|
||||
@Test func outputIsNonSilentWithNonZeroInput() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanN = PanN(input: ConstantGeneratorInput(0.5), channels: 8, position: 2.5)
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 512)
|
||||
let sum: Double = out.flatMap { $0 }.reduce(0.0, { $0 + abs($1) })
|
||||
#expect(sum > 0, "PanN with non-zero input must produce non-silent output")
|
||||
}
|
||||
|
||||
@Test func outputIsFinite() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: 12, position: 5.7)
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 512)
|
||||
for (ch, channel) in out.enumerated() {
|
||||
for (i, sample) in channel.prefix(512).enumerated() {
|
||||
#expect(sample.isFinite, "PanN 12ch: ch\(ch) sample \(i) is not finite")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Fan-out cache
|
||||
|
||||
@Test func fanOutCacheProducesIdenticalOutput() throws {
|
||||
let engine: SynthesisEngine = try makeEngine(blockSize: 128)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanN = PanN(input: ConstantGeneratorInput(0.8), channels: 6, position: 1.75)
|
||||
let out1: [[Double]] = pan.calculatedSamples(graph: graph, count: 128)
|
||||
let out2: [[Double]] = pan.calculatedSamples(graph: graph, count: 128) // cache hit
|
||||
|
||||
for ch: Int in 0..<6 {
|
||||
#expect(out1[ch] == out2[ch], "Fan-out cache: ch\(ch) differs between two calls")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Generator position input
|
||||
|
||||
@Test func generatorPositionInput() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let posGen: ConstantGeneratorInput = ConstantGeneratorInput(2.5)
|
||||
let pan: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: 8, position: posGen)
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 512)
|
||||
let sum: Double = out.flatMap { $0 }.reduce(0.0, { $0 + abs($1) })
|
||||
#expect(sum > 0, "Generator position input must produce non-silent output")
|
||||
}
|
||||
|
||||
// MARK: - 3-channel equal spacing
|
||||
|
||||
@Test func threeChannelEqualSpacing() throws {
|
||||
// Each integer position should give full gain on exactly that channel.
|
||||
for ch: Int in 0..<3 {
|
||||
let g: [Double] = try gains(channels: 3, position: Double(ch))
|
||||
for c: Int in 0..<3 {
|
||||
let expected: Double = c == ch ? 1.0 : 0.0
|
||||
#expect(abs(g[c] - expected) < 1e-6,
|
||||
"3ch position=\(ch): ch\(c) gain should be \(expected), got \(g[c])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Wrap between last and first speaker
|
||||
|
||||
@Test func wrapBetweenLastAndFirstSpeaker() throws {
|
||||
// position = N - 0.5: equal power between channel N-1 and channel 0.
|
||||
let n: Int = 4
|
||||
let g: [Double] = try gains(channels: n, position: Double(n) - 0.5)
|
||||
let expected: Double = Double(2.0).squareRoot() / 2.0
|
||||
#expect(abs(g[n - 1] - expected) < 1e-5,
|
||||
"Wrap: ch\(n-1) gain should be √2/2, got \(g[n-1])")
|
||||
#expect(abs(g[0] - expected) < 1e-5,
|
||||
"Wrap: ch0 gain should be √2/2 (wraps from ch\(n-1)), got \(g[0])")
|
||||
#expect(abs(g[1]) < 1e-6, "Wrap: ch1 should be silent, got \(g[1])")
|
||||
#expect(abs(g[2]) < 1e-6, "Wrap: ch2 should be silent, got \(g[2])")
|
||||
}
|
||||
|
||||
// MARK: - Amplitude scaling
|
||||
|
||||
@Test func inputAmplitudeScalesOutputProportionally() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan1: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: 4, position: 0.5)
|
||||
let pan2: PanN = PanN(input: ConstantGeneratorInput(0.5), channels: 4, position: 0.5)
|
||||
|
||||
let out1: [[Double]] = pan1.calculatedSamples(graph: graph1, count: 64)
|
||||
let out2: [[Double]] = pan2.calculatedSamples(graph: graph2, count: 64)
|
||||
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(out2[ch][0] - out1[ch][0] * 0.5) < 1e-6,
|
||||
"ch\(ch): half-amplitude input should halve output")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
// PanXYDistanceTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("PanXYDistance")
|
||||
struct PanXYDistanceTests {
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100)
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
|
||||
let testSampleRate: Double = 44100
|
||||
let blockSize: Int = 512
|
||||
|
||||
init() throws {
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
// MARK: - Basic shape
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: PanXYDistance = PanXYDistance(input: 0.0, sampleRate: testSampleRate)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
@Test func outputHasFourChannels() {
|
||||
let gen: PanXYDistance = PanXYDistance(input: ConstantGeneratorInput(1.0), sampleRate: testSampleRate)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(out.count == 4)
|
||||
#expect(gen.outputChannelCount == 4)
|
||||
}
|
||||
|
||||
@Test func silentInputProducesSilentOutput() {
|
||||
let gen: PanXYDistance = PanXYDistance(input: 0.0, x: 0.5, y: 0.0, sampleRate: testSampleRate)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for ch: Int in 0..<4 {
|
||||
let samples: [Double] = Array(out[ch].prefix(blockSize))
|
||||
#expect(samples.allSatisfy { abs($0) < 1e-10 },
|
||||
"ch\(ch): zero input must produce near-zero output")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func firstBlockIsFinite() {
|
||||
let gen: PanXYDistance = PanXYDistance(
|
||||
input: ConstantGeneratorInput(0.5),
|
||||
x: 0.3, y: -0.4, velocity: 5.0,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for ch: Int in 0..<4 {
|
||||
let samples: [Double] = Array(out[ch].prefix(blockSize))
|
||||
#expect(samples.allSatisfy { $0.isFinite },
|
||||
"ch\(ch): all samples must be finite")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Inside square: unit gain, no distance shaping
|
||||
|
||||
/// With airAbsorptionCutoff near Nyquist + velocity=0 + position inside the unit
|
||||
/// square, after the LP settles the per-channel gain equals the `PanXY` law:
|
||||
/// at `(x=0, y=0)` all four channels = 0.5 × input.
|
||||
@Test func insideSquareCenterMatchesPanXYGains() {
|
||||
let cutoff: Double = 22000.0 // close to Nyquist at 44100
|
||||
let gen: PanXYDistance = PanXYDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
x: 0.0, y: 0.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
// Two blocks: first lets the LP settle on a DC input.
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let last: Int = blockSize - 1
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(out[ch][last] - 0.5) < 1e-3,
|
||||
"Center inside square: ch\(ch) should be ~0.5, got \(out[ch][last])")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func insideSquareCornerMatchesPanXYHardPan() {
|
||||
let cutoff: Double = 22000.0
|
||||
let gen: PanXYDistance = PanXYDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
x: 1.0, y: 1.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let last: Int = blockSize - 1
|
||||
#expect(abs(out[0][last]) < 1e-3, "LF at hard RF should be ~0")
|
||||
#expect(abs(out[1][last] - 1.0) < 1e-3, "RF at hard RF should be ~1")
|
||||
#expect(abs(out[2][last]) < 1e-3, "RR at hard RF should be ~0")
|
||||
#expect(abs(out[3][last]) < 1e-3, "LR at hard RF should be ~0")
|
||||
}
|
||||
|
||||
// MARK: - Distance attenuation outside the unit square
|
||||
|
||||
/// RMS energy at `(x=2, y=0)` should be ~half of `(x=1, y=0)` (amp = 1/2).
|
||||
/// Use velocity=0 + near-Nyquist cutoff so amplitude scaling dominates.
|
||||
@Test func outsideSquareHalvedAtDoubleDistance() {
|
||||
let cutoff: Double = 22000.0
|
||||
|
||||
let near: PanXYDistance = PanXYDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
x: 1.0, y: 0.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
// Settle LP.
|
||||
_ = near.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let nearOut: [[Double]] = near.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let lastNear: Double = nearOut[1][blockSize - 1] // RF dominant at x=1,y=0
|
||||
|
||||
let far: PanXYDistance = PanXYDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
x: 2.0, y: 0.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = far.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let farOut: [[Double]] = far.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let lastFar: Double = farOut[1][blockSize - 1]
|
||||
|
||||
// amp(far)/amp(near) = (1/2)/1 = 0.5.
|
||||
let ratio: Double = lastFar / lastNear
|
||||
#expect(abs(ratio - 0.5) < 0.02,
|
||||
"x=2/x=1 amplitude ratio should be ~0.5, got \(ratio)")
|
||||
}
|
||||
|
||||
/// Chebyshev metric: `(x=2, y=2)` has the same distance (max=2) as `(x=2, y=0)`.
|
||||
/// Total energy across the four channels (panned differently but with identical mono
|
||||
/// distance-shaped source) should match.
|
||||
@Test func chebyshevMetricMaxGovernsDistance() {
|
||||
let cutoff: Double = 22000.0
|
||||
|
||||
let axis: PanXYDistance = PanXYDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
x: 2.0, y: 0.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = axis.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let axisOut: [[Double]] = axis.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let last: Int = blockSize - 1
|
||||
let axisEnergy: Double = (0..<4).map { axisOut[$0][last] }.reduce(0.0) { $0 + $1 * $1 }
|
||||
|
||||
let diag: PanXYDistance = PanXYDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
x: 2.0, y: 2.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: cutoff,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
_ = diag.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let diagOut: [[Double]] = diag.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let diagEnergy: Double = (0..<4).map { diagOut[$0][last] }.reduce(0.0) { $0 + $1 * $1 }
|
||||
|
||||
// Both at Chebyshev distance 2 → amp=0.5 → energy = 0.5² × 1.0 (power invariant
|
||||
// of the panner) = 0.25.
|
||||
#expect(abs(axisEnergy - 0.25) < 0.01,
|
||||
"axis Chebyshev energy at d=2 should be ~0.25, got \(axisEnergy)")
|
||||
#expect(abs(diagEnergy - 0.25) < 0.01,
|
||||
"diag Chebyshev energy at d=2 should be ~0.25, got \(diagEnergy)")
|
||||
#expect(abs(axisEnergy - diagEnergy) < 0.01,
|
||||
"Chebyshev: axis vs diag energy must match (got \(axisEnergy) vs \(diagEnergy))")
|
||||
}
|
||||
|
||||
// MARK: - Doppler
|
||||
|
||||
/// Positive velocity → read rate < 1 → output sine at lower frequency than input.
|
||||
/// Test by counting zero crossings.
|
||||
@Test func dopplerLowersPitchForPositiveVelocity() {
|
||||
let velocity: Double = 50.0 // m/s
|
||||
let speedOfSound: Double = 343.0
|
||||
let expectedRate: Double = 1.0 - velocity / speedOfSound // ~0.854
|
||||
|
||||
let inputFreq: Double = 440.0
|
||||
let osc: OscSin.Audio = OscSin.Audio(freq: ConstantGeneratorInput(inputFreq))
|
||||
|
||||
let gen: PanXYDistance = PanXYDistance(
|
||||
input: osc,
|
||||
x: 0.0, y: 0.0, // inside square; no atten or LP
|
||||
velocity: ConstantGeneratorInput(velocity),
|
||||
airAbsorptionCutoff: 22000.0,
|
||||
speedOfSound: speedOfSound,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
|
||||
// Skip first few blocks for the delay line / LP to fill + settle.
|
||||
for _ in 0..<4 {
|
||||
_ = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
// Use LF channel (ch0). At (0,0) all four channels carry the same wet × 0.5 signal.
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let ch: [Double] = Array(out[0].prefix(blockSize))
|
||||
|
||||
// Count zero crossings (sign changes).
|
||||
var zeroCrossings: Int = 0
|
||||
for i: Int in 1..<ch.count {
|
||||
if (ch[i - 1] < 0 && ch[i] >= 0) || (ch[i - 1] > 0 && ch[i] <= 0) {
|
||||
zeroCrossings += 1
|
||||
}
|
||||
}
|
||||
// Expected: 2 zero crossings per cycle. Pitch ≈ inputFreq × readRate.
|
||||
let expectedFreq: Double = inputFreq * expectedRate
|
||||
let measuredFreq: Double = Double(zeroCrossings) * testSampleRate / (2.0 * Double(blockSize))
|
||||
// 5% tolerance — delay-line transient + LP rolloff blur the boundary slightly.
|
||||
let err: Double = abs(measuredFreq - expectedFreq) / expectedFreq
|
||||
#expect(err < 0.05,
|
||||
"Doppler: expected ~\(expectedFreq) Hz, measured \(measuredFreq) Hz (err \(err))")
|
||||
}
|
||||
|
||||
// MARK: - Lowpass engagement at distance
|
||||
|
||||
/// At a large distance the cutoff drops far below the input frequency, attenuating it.
|
||||
@Test func lowpassReducesHighFreqAtLargeDistance() throws {
|
||||
let highFreq: Double = 8000.0
|
||||
let osc: OscSin.Audio = OscSin.Audio(freq: ConstantGeneratorInput(highFreq))
|
||||
|
||||
let inside: PanXYDistance = PanXYDistance(
|
||||
input: osc,
|
||||
x: 0.0, y: 0.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: 22000.0,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
// Settle.
|
||||
for _ in 0..<4 {
|
||||
_ = inside.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
let insideOut: [[Double]] = inside.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
var insideSumSq: Double = 0.0
|
||||
for i: Int in 0..<blockSize { insideSumSq += insideOut[0][i] * insideOut[0][i] }
|
||||
let insideRMS: Double = sqrt(insideSumSq / Double(blockSize))
|
||||
|
||||
let outsideEng: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: testSampleRate)
|
||||
try outsideEng.start()
|
||||
defer { outsideEng.stop() }
|
||||
let osc2: OscSin.Audio = OscSin.Audio(freq: ConstantGeneratorInput(highFreq))
|
||||
let outside: PanXYDistance = PanXYDistance(
|
||||
input: osc2,
|
||||
x: 8.0, y: 0.0, velocity: 0.0, // far — cutoff = 22000 × (1/8)² = ~344 Hz
|
||||
airAbsorptionCutoff: 22000.0,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
for _ in 0..<4 {
|
||||
_ = outside.calculatedSamples(graph: makeSynthGraph(engine: outsideEng), count: blockSize)
|
||||
advanceBlock(outsideEng)
|
||||
}
|
||||
let outsideOut: [[Double]] = outside.calculatedSamples(graph: makeSynthGraph(engine: outsideEng), count: blockSize)
|
||||
var outsideSumSq: Double = 0.0
|
||||
for i: Int in 0..<blockSize { outsideSumSq += outsideOut[0][i] * outsideOut[0][i] }
|
||||
let outsideRMS: Double = sqrt(outsideSumSq / Double(blockSize))
|
||||
|
||||
// amp ratio alone = 1/8 = 0.125. With LP rolloff at 8 kHz vs 344 Hz cutoff,
|
||||
// additional attenuation drops outsideRMS far below inside × 0.125. Confirm
|
||||
// LP engaged meaningfully — total dropoff must exceed amp.
|
||||
let ratio: Double = outsideRMS / insideRMS
|
||||
let msg: String = "Far-distance high-freq RMS ratio should be < amp ratio 0.125 (LP engaged), got \(ratio)"
|
||||
#expect(ratio < 0.125, Comment(rawValue: msg))
|
||||
}
|
||||
|
||||
// MARK: - mul / add
|
||||
|
||||
@Test func mulIsAppliedPerChannel() {
|
||||
let gen: PanXYDistance = PanXYDistance(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
x: 0.0, y: 0.0, velocity: 0.0,
|
||||
airAbsorptionCutoff: 22000.0,
|
||||
sampleRate: testSampleRate,
|
||||
mul: 2.0
|
||||
)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||||
let last: Int = blockSize - 1
|
||||
// Center inside square: per-channel gain = 0.5; with mul=2 → ~1.0.
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(out[ch][last] - 1.0) < 1e-2,
|
||||
"mul=2 at center: ch\(ch) should be ~1.0, got \(out[ch][last])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Fan-out cache
|
||||
|
||||
@Test func fanOutCacheProducesIdenticalOutput() {
|
||||
let gen: PanXYDistance = PanXYDistance(
|
||||
input: ConstantGeneratorInput(0.7),
|
||||
x: 0.3, y: -0.4, velocity: 0.0,
|
||||
sampleRate: testSampleRate
|
||||
)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: g, count: blockSize)
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(out1[ch] == out2[ch], "Fan-out cache mismatch on ch\(ch)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Precondition tests intentionally omitted — mirrors SpatialDistance test suite.
|
||||
// Preconditions are validated by inspection; tripping them aborts the test process.
|
||||
@@ -0,0 +1,64 @@
|
||||
// PanXYParameterRampingTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("PanXY parameter ramping")
|
||||
struct PanXYParameterRampingTests {
|
||||
|
||||
@Test func xRampIsApplied() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let lastSampleIdx: Int = engine.blockSize - 1
|
||||
|
||||
let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0))
|
||||
pan.x = 0.0
|
||||
pan.y = 1.0
|
||||
|
||||
// Block 1: settle x=0, y=+1.
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = pan.calculatedSamples(graph: g1, count: 64)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 2: change x to +1; should ramp within block.
|
||||
pan.x = 1.0
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = pan.calculatedSamples(graph: g2, count: 64)
|
||||
let ch0Last: Double = abs(out2[0][lastSampleIdx])
|
||||
let ch1Last: Double = abs(out2[1][lastSampleIdx])
|
||||
#expect(ch0Last < 1e-4, "After x=1 ramp: LF (ch0) should be near 0 at last sample")
|
||||
#expect(ch1Last > 0.5, "After x=1 ramp: RF (ch1) should dominate")
|
||||
}
|
||||
|
||||
@Test func yRampIsApplied() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
defer { engine.stop() }
|
||||
|
||||
let lastSampleIdx: Int = engine.blockSize - 1
|
||||
|
||||
let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0))
|
||||
pan.x = 0.0
|
||||
pan.y = 1.0
|
||||
|
||||
let g1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = pan.calculatedSamples(graph: g1, count: 64)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 2: ramp y to -1 (full rear).
|
||||
pan.y = -1.0
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = pan.calculatedSamples(graph: g2, count: 64)
|
||||
let ch0Last: Double = abs(out2[0][lastSampleIdx])
|
||||
let ch3Last: Double = abs(out2[3][lastSampleIdx])
|
||||
#expect(ch0Last < 1e-4, "After y=-1 ramp: LF (ch0) should be near 0")
|
||||
#expect(ch3Last > 0.5, "After y=-1 ramp: LR (ch3) should dominate")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
// PanXYTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("PanXY")
|
||||
struct PanXYTests {
|
||||
|
||||
func makeEngine(blockSize: Int = 64) throws -> SynthesisEngine {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
return engine
|
||||
}
|
||||
|
||||
/// Last-sample gains for each channel with unit input.
|
||||
///
|
||||
/// `x` / `y` are `@LiveParam` ParameterNodes — when set immediately before the
|
||||
/// first block they ramp from 0 → target. The last sample equals the target.
|
||||
func gains(x: Double, y: Double) throws -> [Double] {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0))
|
||||
pan.x = x
|
||||
pan.y = y
|
||||
|
||||
let blockSize: Int = engine.blockSize
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: blockSize)
|
||||
let lastIdx: Int = blockSize - 1
|
||||
return out.map { ch in lastIdx < ch.count ? ch[lastIdx] : 0.0 }
|
||||
}
|
||||
|
||||
// MARK: - Channel count
|
||||
|
||||
@Test func outputHasFourChannels() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0))
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 4, "PanXY must produce exactly 4 channels")
|
||||
}
|
||||
|
||||
@Test func outputChannelCountIsFour() {
|
||||
let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0))
|
||||
#expect(pan.outputChannelCount == 4)
|
||||
}
|
||||
|
||||
// MARK: - Hard corners
|
||||
|
||||
@Test func hardLFCorner() throws {
|
||||
let g: [Double] = try gains(x: -1.0, y: +1.0)
|
||||
#expect(abs(g[0] - 1.0) < 1e-6, "Hard LF: ch0 should be 1.0")
|
||||
#expect(abs(g[1]) < 1e-6, "Hard LF: ch1 should be 0")
|
||||
#expect(abs(g[2]) < 1e-6, "Hard LF: ch2 should be 0")
|
||||
#expect(abs(g[3]) < 1e-6, "Hard LF: ch3 should be 0")
|
||||
}
|
||||
|
||||
@Test func hardRFCorner() throws {
|
||||
let g: [Double] = try gains(x: +1.0, y: +1.0)
|
||||
#expect(abs(g[0]) < 1e-6)
|
||||
#expect(abs(g[1] - 1.0) < 1e-6)
|
||||
#expect(abs(g[2]) < 1e-6)
|
||||
#expect(abs(g[3]) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func hardRRCorner() throws {
|
||||
let g: [Double] = try gains(x: +1.0, y: -1.0)
|
||||
#expect(abs(g[0]) < 1e-6)
|
||||
#expect(abs(g[1]) < 1e-6)
|
||||
#expect(abs(g[2] - 1.0) < 1e-6)
|
||||
#expect(abs(g[3]) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func hardLRCorner() throws {
|
||||
let g: [Double] = try gains(x: -1.0, y: -1.0)
|
||||
#expect(abs(g[0]) < 1e-6)
|
||||
#expect(abs(g[1]) < 1e-6)
|
||||
#expect(abs(g[2]) < 1e-6)
|
||||
#expect(abs(g[3] - 1.0) < 1e-6)
|
||||
}
|
||||
|
||||
// MARK: - Center
|
||||
|
||||
@Test func centerPositionHasEqualGainAllChannels() throws {
|
||||
let g: [Double] = try gains(x: 0.0, y: 0.0)
|
||||
let expected: Double = 0.5 // cos(π/4)*sin(π/4) = (√2/2)² = 0.5
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(g[ch] - expected) < 1e-6,
|
||||
"Center: ch\(ch) should be \(expected), got \(g[ch])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Power invariant
|
||||
|
||||
@Test func powerInvariantAtVariousPositions() throws {
|
||||
let positions: [(Double, Double)] = [
|
||||
(0.0, 0.0), (-1.0, 1.0), (1.0, -1.0), (0.5, -0.3), (-0.7, 0.8),
|
||||
(-0.5, -0.5), (0.25, 0.75)
|
||||
]
|
||||
for (xv, yv) in positions {
|
||||
let g: [Double] = try gains(x: xv, y: yv)
|
||||
let sumSq: Double = g.reduce(0.0) { $0 + $1 * $1 }
|
||||
#expect(abs(sumSq - 1.0) < 1e-5,
|
||||
"Power invariant failed at x=\(xv) y=\(yv): Σ ch² = \(sumSq)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Axis edges
|
||||
|
||||
@Test func fullLeftGivesZeroRightChannels() throws {
|
||||
let g: [Double] = try gains(x: -1.0, y: 0.0)
|
||||
#expect(abs(g[1]) < 1e-6, "Full left: RF (ch1) should be 0")
|
||||
#expect(abs(g[2]) < 1e-6, "Full left: RR (ch2) should be 0")
|
||||
}
|
||||
|
||||
@Test func fullRightGivesZeroLeftChannels() throws {
|
||||
let g: [Double] = try gains(x: +1.0, y: 0.0)
|
||||
#expect(abs(g[0]) < 1e-6, "Full right: LF (ch0) should be 0")
|
||||
#expect(abs(g[3]) < 1e-6, "Full right: LR (ch3) should be 0")
|
||||
}
|
||||
|
||||
@Test func fullFrontGivesZeroRearChannels() throws {
|
||||
let g: [Double] = try gains(x: 0.0, y: +1.0)
|
||||
#expect(abs(g[2]) < 1e-6, "Full front: RR (ch2) should be 0")
|
||||
#expect(abs(g[3]) < 1e-6, "Full front: LR (ch3) should be 0")
|
||||
}
|
||||
|
||||
@Test func fullRearGivesZeroFrontChannels() throws {
|
||||
let g: [Double] = try gains(x: 0.0, y: -1.0)
|
||||
#expect(abs(g[0]) < 1e-6, "Full rear: LF (ch0) should be 0")
|
||||
#expect(abs(g[1]) < 1e-6, "Full rear: RF (ch1) should be 0")
|
||||
}
|
||||
|
||||
// MARK: - Clamping
|
||||
|
||||
@Test func outOfRangeXIsClamped() throws {
|
||||
let gClamped: [Double] = try gains(x: 2.0, y: 0.0) // clamps to 1.0
|
||||
let gRef: [Double] = try gains(x: 1.0, y: 0.0)
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(gClamped[ch] - gRef[ch]) < 1e-6,
|
||||
"x > 1 should clamp: ch\(ch) \(gClamped[ch]) vs \(gRef[ch])")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func outOfRangeYIsClamped() throws {
|
||||
let gClamped: [Double] = try gains(x: 0.0, y: -3.0) // clamps to -1.0
|
||||
let gRef: [Double] = try gains(x: 0.0, y: -1.0)
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(gClamped[ch] - gRef[ch]) < 1e-6,
|
||||
"y < -1 should clamp: ch\(ch)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - mul / add
|
||||
|
||||
@Test func mulIsAppliedPerChannel() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0), mul: 2.0)
|
||||
pan.x = 0.0
|
||||
pan.y = 0.0
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
let last: Int = engine.blockSize - 1
|
||||
// Center gains = 0.5, ×2 = 1.0 per channel at last sample (ramp settled).
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(out[ch][last] - 1.0) < 1e-6,
|
||||
"mul=2 at center: ch\(ch) should equal 1.0, got \(out[ch][last])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Channel aliases
|
||||
|
||||
@Test func channelAliasesRouteToCorrectRows() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanXY = PanXY(input: ConstantGeneratorInput(1.0))
|
||||
pan.x = -1.0
|
||||
pan.y = +1.0 // ramp toward hard LF; last sample = corner
|
||||
|
||||
let direct: [[Double]] = pan.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
let last: Int = engine.blockSize - 1
|
||||
|
||||
let fl: [[Double]] = pan.frontLeft.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
let fr: [[Double]] = pan.frontRight.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
let rr: [[Double]] = pan.rearRight.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
let rl: [[Double]] = pan.rearLeft.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
|
||||
#expect(abs(fl[0][last] - direct[0][last]) < 1e-9, "frontLeft must equal ch0")
|
||||
#expect(abs(fr[0][last] - direct[1][last]) < 1e-9, "frontRight must equal ch1")
|
||||
#expect(abs(rr[0][last] - direct[2][last]) < 1e-9, "rearRight must equal ch2")
|
||||
#expect(abs(rl[0][last] - direct[3][last]) < 1e-9, "rearLeft must equal ch3")
|
||||
}
|
||||
|
||||
// MARK: - Fan-out cache
|
||||
|
||||
@Test func fanOutCacheProducesIdenticalOutput() throws {
|
||||
let engine: SynthesisEngine = try makeEngine(blockSize: 128)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanXY = PanXY(input: ConstantGeneratorInput(0.7))
|
||||
pan.x = 0.3
|
||||
pan.y = -0.4
|
||||
|
||||
let out1: [[Double]] = pan.calculatedSamples(graph: graph, count: 128)
|
||||
let out2: [[Double]] = pan.calculatedSamples(graph: graph, count: 128)
|
||||
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(out1[ch] == out2[ch], "Fan-out cache mismatch on ch\(ch)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parameter ramping suite lives in `PanXYParameterRampingTests.swift`
|
||||
// to keep one `@Suite` per file.
|
||||
@@ -0,0 +1,612 @@
|
||||
// PanningTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - PanAz (polar equal-power 4-speaker square panner)
|
||||
|
||||
@MainActor
|
||||
@Suite("PanAz")
|
||||
struct PanAzTests {
|
||||
|
||||
func makeEngine(blockSize: Int = 64) throws -> SynthesisEngine {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
return engine
|
||||
}
|
||||
|
||||
/// Returns last-sample gains for each channel with unit input.
|
||||
///
|
||||
/// `azimuth` is a `@LiveParam` ParameterNode (ControlGenerator); when set
|
||||
/// immediately before the first block it ramps from its initial value
|
||||
/// (0.0) to the target over the block. The last sample equals the target.
|
||||
func gains(azimuth: Double) throws -> [Double] {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanAz = PanAz(input: ConstantGeneratorInput(1.0))
|
||||
pan.azimuth = azimuth
|
||||
|
||||
let blockSize: Int = engine.blockSize
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: blockSize)
|
||||
let lastIdx: Int = blockSize - 1
|
||||
return out.map { ch in lastIdx < ch.count ? ch[lastIdx] : 0.0 }
|
||||
}
|
||||
|
||||
// MARK: - Channel count
|
||||
|
||||
@Test func outputHasFourChannels() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanAz = PanAz(input: ConstantGeneratorInput(1.0))
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 4, "PanAz must produce exactly 4 channels")
|
||||
}
|
||||
|
||||
@Test func outputChannelCountIsFour() {
|
||||
let pan: PanAz = PanAz(input: ConstantGeneratorInput(1.0))
|
||||
#expect(pan.outputChannelCount == 4)
|
||||
}
|
||||
|
||||
// MARK: - Hard corners (45° azimuths land on speaker corners)
|
||||
|
||||
@Test func hardLFCornerAtNegativeQuarterPi() throws {
|
||||
// az = -π/4 → (sin = -√2/2, cos = √2/2) scaled to (-1, +1) = LF corner.
|
||||
let g: [Double] = try gains(azimuth: -.pi / 4)
|
||||
#expect(abs(g[0] - 1.0) < 1e-6, "Hard LF: ch0 should be 1.0")
|
||||
#expect(abs(g[1]) < 1e-6, "Hard LF: ch1 should be 0")
|
||||
#expect(abs(g[2]) < 1e-6, "Hard LF: ch2 should be 0")
|
||||
#expect(abs(g[3]) < 1e-6, "Hard LF: ch3 should be 0")
|
||||
}
|
||||
|
||||
@Test func hardRFCornerAtQuarterPi() throws {
|
||||
// az = π/4 → (1, 1) = RF corner.
|
||||
let g: [Double] = try gains(azimuth: .pi / 4)
|
||||
#expect(abs(g[0]) < 1e-6)
|
||||
#expect(abs(g[1] - 1.0) < 1e-6)
|
||||
#expect(abs(g[2]) < 1e-6)
|
||||
#expect(abs(g[3]) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func hardRRCornerAtThreeQuartersPi() throws {
|
||||
// az = 3π/4 → (sin = √2/2, cos = -√2/2) scaled to (1, -1) = RR.
|
||||
let g: [Double] = try gains(azimuth: 3.0 * .pi / 4.0)
|
||||
#expect(abs(g[0]) < 1e-6)
|
||||
#expect(abs(g[1]) < 1e-6)
|
||||
#expect(abs(g[2] - 1.0) < 1e-6)
|
||||
#expect(abs(g[3]) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func hardLRCornerAtNegativeThreeQuartersPi() throws {
|
||||
// az = -3π/4 → (-1, -1) = LR corner.
|
||||
let g: [Double] = try gains(azimuth: -3.0 * .pi / 4.0)
|
||||
#expect(abs(g[0]) < 1e-6)
|
||||
#expect(abs(g[1]) < 1e-6)
|
||||
#expect(abs(g[2]) < 1e-6)
|
||||
#expect(abs(g[3] - 1.0) < 1e-6)
|
||||
}
|
||||
|
||||
// MARK: - Cardinal edge midpoints (front / right / rear / left)
|
||||
|
||||
@Test func frontCentreSplitsLFandRFEvenly() throws {
|
||||
// az = 0 → (0, 1). LF = RF = √2/2; RR = LR = 0.
|
||||
let g: [Double] = try gains(azimuth: 0.0)
|
||||
let half: Double = Double(2).squareRoot() / 2.0
|
||||
#expect(abs(g[0] - half) < 1e-6, "front centre: LF should be √2/2")
|
||||
#expect(abs(g[1] - half) < 1e-6, "front centre: RF should be √2/2")
|
||||
#expect(abs(g[2]) < 1e-6, "front centre: RR should be 0")
|
||||
#expect(abs(g[3]) < 1e-6, "front centre: LR should be 0")
|
||||
}
|
||||
|
||||
@Test func rightCentreSplitsRFandRREvenly() throws {
|
||||
// az = π/2 → (1, 0). RF = RR = √2/2; LF = LR = 0.
|
||||
let g: [Double] = try gains(azimuth: .pi / 2)
|
||||
let half: Double = Double(2).squareRoot() / 2.0
|
||||
#expect(abs(g[0]) < 1e-6)
|
||||
#expect(abs(g[1] - half) < 1e-6)
|
||||
#expect(abs(g[2] - half) < 1e-6)
|
||||
#expect(abs(g[3]) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func rearCentreSplitsRRandLREvenly() throws {
|
||||
// az = π → (0, -1). RR = LR = √2/2; LF = RF = 0.
|
||||
let g: [Double] = try gains(azimuth: .pi)
|
||||
let half: Double = Double(2).squareRoot() / 2.0
|
||||
#expect(abs(g[0]) < 1e-6)
|
||||
#expect(abs(g[1]) < 1e-6)
|
||||
#expect(abs(g[2] - half) < 1e-6)
|
||||
#expect(abs(g[3] - half) < 1e-6)
|
||||
}
|
||||
|
||||
@Test func leftCentreSplitsLFandLREvenly() throws {
|
||||
// az = -π/2 → (-1, 0). LF = LR = √2/2; RF = RR = 0.
|
||||
let g: [Double] = try gains(azimuth: -.pi / 2)
|
||||
let half: Double = Double(2).squareRoot() / 2.0
|
||||
#expect(abs(g[0] - half) < 1e-6)
|
||||
#expect(abs(g[1]) < 1e-6)
|
||||
#expect(abs(g[2]) < 1e-6)
|
||||
#expect(abs(g[3] - half) < 1e-6)
|
||||
}
|
||||
|
||||
// MARK: - Power invariant
|
||||
|
||||
@Test func powerInvariantAcrossFullCircle() throws {
|
||||
// Sample azimuth at 16 evenly-spaced angles around the circle.
|
||||
let steps: Int = 16
|
||||
for i: Int in 0..<steps {
|
||||
let az: Double = Double(i) * (2.0 * .pi / Double(steps)) - .pi
|
||||
let g: [Double] = try gains(azimuth: az)
|
||||
let sumSq: Double = g.reduce(0.0) { $0 + $1 * $1 }
|
||||
#expect(abs(sumSq - 1.0) < 1e-5,
|
||||
"Power invariant failed at az=\(az) rad: Σ ch² = \(sumSq)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Wrap-around (azimuth modulo 2π via sin/cos)
|
||||
|
||||
@Test func azimuthWrapsAroundAtTwoPi() throws {
|
||||
let g0: [Double] = try gains(azimuth: 0.0)
|
||||
let gTwoPi: [Double] = try gains(azimuth: 2.0 * .pi)
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(g0[ch] - gTwoPi[ch]) < 1e-6,
|
||||
"az=2π must match az=0: ch\(ch) \(g0[ch]) vs \(gTwoPi[ch])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Fan-out cache
|
||||
|
||||
@Test func fanOutCacheProducesIdenticalOutput() throws {
|
||||
let engine: SynthesisEngine = try makeEngine(blockSize: 128)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanAz = PanAz(input: ConstantGeneratorInput(0.7))
|
||||
pan.azimuth = 0.3
|
||||
|
||||
let out1: [[Double]] = pan.calculatedSamples(graph: graph, count: 128)
|
||||
let out2: [[Double]] = pan.calculatedSamples(graph: graph, count: 128)
|
||||
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(out1[ch] == out2[ch], "Fan-out cache mismatch on ch\(ch)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - mul / add
|
||||
|
||||
@Test func mulIsAppliedPerChannel() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let pan: PanAz = PanAz(input: ConstantGeneratorInput(1.0), mul: 2.0)
|
||||
pan.azimuth = .pi / 4 // RF corner
|
||||
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: engine.blockSize)
|
||||
let last: Int = engine.blockSize - 1
|
||||
#expect(abs(out[1][last] - 2.0) < 1e-6,
|
||||
"mul=2 at RF corner: ch1 should be 2.0, got \(out[1][last])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AmbiEncoder1
|
||||
|
||||
@MainActor
|
||||
@Suite("AmbiEncoder1")
|
||||
struct AmbiEncoder1Tests {
|
||||
|
||||
func makeEngine(blockSize: Int = 64) throws -> SynthesisEngine {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
return engine
|
||||
}
|
||||
|
||||
// MARK: - Channel count
|
||||
|
||||
@Test func outputHasFourChannels() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let enc: AmbiEncoder1 = AmbiEncoder1(input: ConstantGeneratorInput(1.0))
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 4, "AmbiEncoder1 must produce 4 channels [W, X, Y, Z]")
|
||||
}
|
||||
|
||||
// MARK: - W channel
|
||||
|
||||
@Test func wChannelIsInputScaledByInverseSqrt2() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let enc: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: 0.3,
|
||||
elevation: 0.5
|
||||
) // W doesn't depend on az/el
|
||||
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: 64)
|
||||
let expected: Double = 1.0 / Double(2.0).squareRoot()
|
||||
for i: Int in 0..<64 {
|
||||
#expect(abs(out[0][i] - expected) < 1e-6,
|
||||
"W channel sample \(i): expected \(expected), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - X, Y, Z at canonical angles
|
||||
|
||||
@Test func frontAzimuthZeroElevationZero() throws {
|
||||
// az=0, el=0: X = cos(0)cos(0) = 1, Y = sin(0)cos(0) = 0, Z = sin(0) = 0
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let enc: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: 0.0,
|
||||
elevation: 0.0
|
||||
)
|
||||
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(abs(out[1][0] - 1.0) < 1e-6, "X at az=0,el=0 should be 1.0")
|
||||
#expect(abs(out[2][0]) < 1e-6, "Y at az=0,el=0 should be 0.0")
|
||||
#expect(abs(out[3][0]) < 1e-6, "Z at az=0,el=0 should be 0.0")
|
||||
}
|
||||
|
||||
@Test func leftAzimuthHalfPiElevationZero() throws {
|
||||
// az=π/2, el=0: X = cos(π/2)cos(0) = 0, Y = sin(π/2)cos(0) = 1, Z = 0
|
||||
// Scalar Double init parameters apply the target value every sample.
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let enc: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: .pi / 2,
|
||||
elevation: 0.0
|
||||
)
|
||||
|
||||
let blockSize: Int = 64
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: blockSize)
|
||||
let last: Int = blockSize - 1
|
||||
#expect(abs(out[1][last]) < 1e-6, "X at az=π/2,el=0 should be 0 (last sample)")
|
||||
#expect(abs(out[2][last] - 1.0) < 1e-6, "Y at az=π/2,el=0 should be 1.0 (last sample)")
|
||||
#expect(abs(out[3][last]) < 1e-6, "Z at az=π/2,el=0 should be 0 (last sample)")
|
||||
}
|
||||
|
||||
@Test func aboveElevationHalfPi() throws {
|
||||
// az=0, el=π/2: X=cos(0)cos(π/2)=0, Y=0, Z=sin(π/2)=1
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let enc: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: 0.0,
|
||||
elevation: .pi / 2
|
||||
)
|
||||
|
||||
let blockSize: Int = 64
|
||||
let out: [[Double]] = enc.calculatedSamples(graph: graph, count: blockSize)
|
||||
let last: Int = blockSize - 1
|
||||
#expect(abs(out[1][last]) < 1e-6, "X at el=π/2 should be 0 (last sample)")
|
||||
#expect(abs(out[2][last]) < 1e-6, "Y at el=π/2 should be 0 (last sample)")
|
||||
#expect(abs(out[3][last] - 1.0) < 1e-6, "Z at el=π/2 should be 1.0 (last sample)")
|
||||
}
|
||||
|
||||
// MARK: - Amplitude scaling
|
||||
|
||||
@Test func outputScalesWithInput() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let enc1: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(1.0),
|
||||
azimuth: 0.4,
|
||||
elevation: 0.2
|
||||
)
|
||||
let enc2: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(0.5),
|
||||
azimuth: 0.4,
|
||||
elevation: 0.2
|
||||
)
|
||||
|
||||
let out1: [[Double]] = enc1.calculatedSamples(graph: graph1, count: 64)
|
||||
let out2: [[Double]] = enc2.calculatedSamples(graph: graph2, count: 64)
|
||||
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(abs(out2[ch][0] - out1[ch][0] * 0.5) < 1e-6,
|
||||
"Half-amplitude input must halve ch\(ch) output")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Fan-out cache
|
||||
|
||||
@Test func fanOutCacheProducesIdenticalOutput() throws {
|
||||
let engine: SynthesisEngine = try makeEngine(blockSize: 128)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let enc: AmbiEncoder1 = AmbiEncoder1(
|
||||
input: ConstantGeneratorInput(0.8),
|
||||
azimuth: 1.0,
|
||||
elevation: 0.3
|
||||
)
|
||||
|
||||
let out1: [[Double]] = enc.calculatedSamples(graph: graph, count: 128)
|
||||
let out2: [[Double]] = enc.calculatedSamples(graph: graph, count: 128)
|
||||
|
||||
for ch: Int in 0..<4 {
|
||||
#expect(out1[ch] == out2[ch], "Fan-out cache mismatch on ch\(ch)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AmbiDecoder1
|
||||
|
||||
@MainActor
|
||||
@Suite("AmbiDecoder1")
|
||||
struct AmbiDecoder1Tests {
|
||||
|
||||
func makeEngine(blockSize: Int = 64) throws -> SynthesisEngine {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
return engine
|
||||
}
|
||||
|
||||
// MARK: - Channel count
|
||||
|
||||
@Test func outputChannelCountMatchesSpeakerCount() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
// Build B-format manually: W=1/√2, X=1, Y=0, Z=0 (encoder at az=0, el=0)
|
||||
let w: ConstantGeneratorInput = ConstantGeneratorInput(1.0 / Double(2.0).squareRoot())
|
||||
let x: ConstantGeneratorInput = ConstantGeneratorInput(1.0)
|
||||
let y: ConstantGeneratorInput = ConstantGeneratorInput(0.0)
|
||||
let z: ConstantGeneratorInput = ConstantGeneratorInput(0.0)
|
||||
let bformat: GeneratorArray = GeneratorArray([w, x, y, z])
|
||||
|
||||
let speakers: [(az: Double, el: Double)] = [
|
||||
(az: .pi / 2, el: 0), // L
|
||||
(az: -.pi / 2, el: 0) // R
|
||||
]
|
||||
let dec: AmbiDecoder1 = AmbiDecoder1(input: bformat, speakers: speakers)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 2, "2-speaker decoder must produce 2 channels")
|
||||
}
|
||||
|
||||
@Test func fourSpeakerDecoderHasFourChannels() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let bformat: GeneratorArray = GeneratorArray([
|
||||
ConstantGeneratorInput(0.707) as any Generator,
|
||||
ConstantGeneratorInput(1.0),
|
||||
ConstantGeneratorInput(0.0),
|
||||
ConstantGeneratorInput(0.0)
|
||||
])
|
||||
let speakers: [(az: Double, el: Double)] = [
|
||||
(az: 3 * .pi / 4, el: 0), (az: .pi / 4, el: 0),
|
||||
(az: -.pi / 4, el: 0), (az: -3 * .pi / 4, el: 0)
|
||||
]
|
||||
let dec: AmbiDecoder1 = AmbiDecoder1(input: bformat, speakers: speakers)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 4, "4-speaker decoder must produce 4 channels")
|
||||
}
|
||||
|
||||
// MARK: - Decode matrix correctness
|
||||
|
||||
@Test func symmetricStereoDecoderBalancedForFrontSource() throws {
|
||||
// Source at az=0 (front), el=0 with stereo speaker pair (L=+π/2, R=-π/2).
|
||||
// Both speakers should get identical output due to symmetry.
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
let bformat: GeneratorArray = GeneratorArray([
|
||||
ConstantGeneratorInput(wScale) as any Generator, // W
|
||||
ConstantGeneratorInput(1.0), // X (front = cos(0)cos(0) = 1)
|
||||
ConstantGeneratorInput(0.0), // Y
|
||||
ConstantGeneratorInput(0.0) // Z
|
||||
])
|
||||
let speakers: [(az: Double, el: Double)] = [
|
||||
(az: .pi / 2, el: 0), // L
|
||||
(az: -.pi / 2, el: 0) // R
|
||||
]
|
||||
let dec: AmbiDecoder1 = AmbiDecoder1(input: bformat, speakers: speakers)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
// Both L and R should have the same gain (symmetric decode for front source).
|
||||
#expect(abs(out[0][0] - out[1][0]) < 1e-6,
|
||||
"Front source: L and R should be equal, got L=\(out[0][0]) R=\(out[1][0])")
|
||||
}
|
||||
|
||||
@Test func decodedOutputMatchesManualFormula() throws {
|
||||
// Manual verification: speaker at az=π/2, el=0.
|
||||
// Decode = (1/√2)×W + cos(π/2)cos(0)×X + sin(π/2)cos(0)×Y + sin(0)×Z
|
||||
// = (1/√2)×W + 0×X + 1×Y + 0×Z = (1/√2)×W + Y
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let wVal: Double = 0.5
|
||||
let yVal: Double = 0.3
|
||||
let bformat: GeneratorArray = GeneratorArray([
|
||||
ConstantGeneratorInput(wVal) as any Generator,
|
||||
ConstantGeneratorInput(0.0),
|
||||
ConstantGeneratorInput(yVal),
|
||||
ConstantGeneratorInput(0.0)
|
||||
])
|
||||
let speakers: [(az: Double, el: Double)] = [(az: .pi / 2, el: 0)]
|
||||
let dec: AmbiDecoder1 = AmbiDecoder1(input: bformat, speakers: speakers)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
let expected: Double = wScale * wVal + yVal
|
||||
#expect(abs(out[0][0] - expected) < 1e-5,
|
||||
"Decoded output \(out[0][0]) doesn't match manual formula \(expected)")
|
||||
}
|
||||
|
||||
// MARK: - Non-silent output
|
||||
|
||||
@Test func outputIsNonSilent() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
let bformat: GeneratorArray = GeneratorArray([
|
||||
ConstantGeneratorInput(wScale) as any Generator,
|
||||
ConstantGeneratorInput(1.0),
|
||||
ConstantGeneratorInput(0.0),
|
||||
ConstantGeneratorInput(0.0)
|
||||
])
|
||||
let speakers: [(az: Double, el: Double)] = [
|
||||
(az: .pi / 2, el: 0), (az: -.pi / 2, el: 0)
|
||||
]
|
||||
let dec: AmbiDecoder1 = AmbiDecoder1(input: bformat, speakers: speakers)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
let sum: Double = out.flatMap { $0.prefix(64) }.reduce(0.0, { $0 + abs($1) })
|
||||
#expect(sum > 0, "Decoder output must be non-silent with non-zero B-format input")
|
||||
}
|
||||
|
||||
// MARK: - Encode → decode round-trip
|
||||
|
||||
@Test func encodeDecodeRoundTripMatchesManualFormula() throws {
|
||||
// Simulate AmbiEncoder1 at az=0 (front), el=0: W=1/√2, X=1, Y=0, Z=0.
|
||||
// Decode to symmetric L/R speakers (az=±π/2, el=0).
|
||||
// Expected: L = (1/√2)×(1/√2) + cos(π/2)·cos(0)·1 + sin(π/2)·cos(0)·0 + sin(0)·0 = 0.5 + 0 + 0 + 0 = 0.5
|
||||
// R = (1/√2)×(1/√2) + cos(-π/2)·cos(0)·1 + sin(-π/2)·cos(0)·0 + sin(0)·0 = 0.5 + 0 + 0 + 0 = 0.5
|
||||
let engine: SynthesisEngine = try makeEngine(blockSize: 64)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
let bformat: GeneratorArray = GeneratorArray([
|
||||
ConstantGeneratorInput(wScale) as any Generator, // W
|
||||
ConstantGeneratorInput(1.0), // X (cos(0)cos(0))
|
||||
ConstantGeneratorInput(0.0), // Y (sin(0)cos(0))
|
||||
ConstantGeneratorInput(0.0) // Z (sin(0))
|
||||
])
|
||||
let speakers: [(az: Double, el: Double)] = [
|
||||
(az: .pi / 2, el: 0), // L
|
||||
(az: -.pi / 2, el: 0) // R
|
||||
]
|
||||
let dec: AmbiDecoder1 = AmbiDecoder1(input: bformat, speakers: speakers)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
let expected: Double = 0.5
|
||||
#expect(abs(out[0][0] - expected) < 1e-5,
|
||||
"Round-trip L channel: expected \(expected), got \(out[0][0])")
|
||||
#expect(abs(out[1][0] - expected) < 1e-5,
|
||||
"Round-trip R channel: expected \(expected), got \(out[1][0])")
|
||||
#expect(abs(out[0][0] - out[1][0]) < 1e-10,
|
||||
"Front source must decode symmetrically: L=\(out[0][0]) R=\(out[1][0])")
|
||||
}
|
||||
|
||||
// MARK: - Fan-out cache
|
||||
|
||||
@Test func fanOutCacheProducesIdenticalOutput() throws {
|
||||
let engine: SynthesisEngine = try makeEngine(blockSize: 128)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let wScale: Double = 1.0 / Double(2.0).squareRoot()
|
||||
let bformat: GeneratorArray = GeneratorArray([
|
||||
ConstantGeneratorInput(wScale) as any Generator,
|
||||
ConstantGeneratorInput(0.6),
|
||||
ConstantGeneratorInput(0.3),
|
||||
ConstantGeneratorInput(0.1)
|
||||
])
|
||||
let speakers: [(az: Double, el: Double)] = [
|
||||
(az: .pi / 2, el: 0), (az: -.pi / 2, el: 0), (az: 0, el: 0)
|
||||
]
|
||||
let dec: AmbiDecoder1 = AmbiDecoder1(input: bformat, speakers: speakers)
|
||||
let out1: [[Double]] = dec.calculatedSamples(graph: graph, count: 128)
|
||||
let out2: [[Double]] = dec.calculatedSamples(graph: graph, count: 128)
|
||||
|
||||
for ch: Int in 0..<3 {
|
||||
#expect(out1[ch] == out2[ch], "Fan-out cache mismatch on ch\(ch)")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Direct encoder → decoder wiring (no GeneratorArray wrapping)
|
||||
|
||||
@Test func encoderFeedsDecoderDirectlyMatchesGeneratorArrayPath() throws {
|
||||
// After the AmbiDecoder1 init signature was relaxed from `input: GeneratorArray` to
|
||||
// `input: any Generator` with an `outputChannelCount == 4` precondition, the typical
|
||||
// path (encoder → decoder) no longer needs `GeneratorArray([enc[0], enc[1], enc[2], enc[3]])`
|
||||
// splitting+restacking. Verify the direct path produces identical output to the
|
||||
// historical GeneratorArray-wrapped path.
|
||||
let engine: SynthesisEngine = try makeEngine(blockSize: 64)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let speakers: [(az: Double, el: Double)] = [
|
||||
(az: 3 * .pi / 4, el: 0),
|
||||
(az: .pi / 4, el: 0),
|
||||
(az: -.pi / 4, el: 0),
|
||||
(az: -3 * .pi / 4, el: 0)
|
||||
]
|
||||
|
||||
// Direct: encoder feeds decoder.
|
||||
let direct: AmbiDecoder1 = AmbiDecoder1(
|
||||
input: AmbiEncoder1(input: ConstantGeneratorInput(0.5)),
|
||||
speakers: speakers
|
||||
)
|
||||
let directOut: [[Double]] = direct.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
// Wrapped: same encoder, projected through GeneratorArray of channel selectors.
|
||||
let enc: AmbiEncoder1 = AmbiEncoder1(input: ConstantGeneratorInput(0.5))
|
||||
let bformat: GeneratorArray = GeneratorArray([enc[0], enc[1], enc[2], enc[3]])
|
||||
let wrapped: AmbiDecoder1 = AmbiDecoder1(input: bformat, speakers: speakers)
|
||||
let wrappedOut: [[Double]] = wrapped.calculatedSamples(graph: graph, count: 64)
|
||||
|
||||
#expect(directOut.count == 4 && wrappedOut.count == 4)
|
||||
for ch: Int in 0..<4 {
|
||||
for i: Int in 0..<64 {
|
||||
#expect(abs(directOut[ch][i] - wrappedOut[ch][i]) < 1e-12,
|
||||
"Channel \(ch) sample \(i) differs: direct=\(directOut[ch][i]) wrapped=\(wrappedOut[ch][i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test func manualBFormatViaGeneratorArrayStillWorks() throws {
|
||||
// Confirm the manual-B-format path (GeneratorArray of 4 mono components) is still
|
||||
// valid input shape after the init signature change — GeneratorArray reports
|
||||
// `outputChannelCount == generators.count`, so a 4-element array passes the new
|
||||
// `outputChannelCount == 4` precondition.
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
defer { engine.stop() }
|
||||
|
||||
let bformat: GeneratorArray = GeneratorArray([
|
||||
ConstantGeneratorInput(1.0 / Double(2.0).squareRoot()) as any Generator,
|
||||
ConstantGeneratorInput(1.0),
|
||||
ConstantGeneratorInput(0.0),
|
||||
ConstantGeneratorInput(0.0)
|
||||
])
|
||||
let speakers: [(az: Double, el: Double)] = [
|
||||
(az: .pi / 2, el: 0),
|
||||
(az: -.pi / 2, el: 0)
|
||||
]
|
||||
let dec: AmbiDecoder1 = AmbiDecoder1(input: bformat, speakers: speakers)
|
||||
let out: [[Double]] = dec.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(out.count == 2)
|
||||
let energy: Double = out.flatMap { $0.prefix(64) }.reduce(0.0, { $0 + abs($1) })
|
||||
#expect(energy > 0, "Manual B-format decode must be non-silent")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user