prep for 1.2.0 release updates

This commit is contained in:
2026-06-21 11:45:58 -07:00
parent 73191c958f
commit 032b7c39d3
286 changed files with 21116 additions and 121 deletions
@@ -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.