Files
Lyrebird/Tests/LyrebirdTests/Generators/Panning/AmbiEncoder1ParameterRampingTests.swift
T
2026-06-21 11:45:58 -07:00

70 lines
2.8 KiB
Swift

// 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")
}
}