110 lines
5.0 KiB
Swift
110 lines
5.0 KiB
Swift
// AmbiEncoderStereoTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
import Foundation
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@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")
|
||
}
|
||
}
|