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

610 lines
25 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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)
@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
@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
@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")
}
}