94 lines
3.5 KiB
Swift
94 lines
3.5 KiB
Swift
// 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)
|
||
}
|
||
}
|