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

233 lines
9.8 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.
// PanNTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("PanN")
struct PanNTests {
// MARK: - Helpers
func makeEngine(blockSize: Int = 512) throws -> SynthesisEngine {
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
try engine.start()
return engine
}
/// Returns per-channel peak gains for a unit-amplitude constant input.
func gains(channels: Int, position: Double, blockSize: Int = 64) throws -> [Double] {
let engine: SynthesisEngine = try makeEngine(blockSize: blockSize)
let graph: SynthGraph = makeSynthGraph(engine: engine)
defer { engine.stop() }
let pan: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: channels, position: position)
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: blockSize)
return out.map { $0[0] } // first sample of each channel = block-constant gain × 1.0
}
// MARK: - Channel count
@Test func outputChannelCountMatchesChannelParam() throws {
let engine: SynthesisEngine = try makeEngine()
let graph: SynthGraph = makeSynthGraph(engine: engine)
defer { engine.stop() }
for n: Int in [2, 3, 4, 6, 8, 12] {
let pan: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: n)
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 64)
#expect(out.count == n, "PanN(channels:\(n)) should produce \(n) output channels")
}
}
// MARK: - Position = 0: full gain on channel 0
@Test func positionZeroGivesFullGainOnChannel0() throws {
let g: [Double] = try gains(channels: 4, position: 0.0)
#expect(abs(g[0] - 1.0) < 1e-6, "position=0: ch0 gain should be 1.0, got \(g[0])")
#expect(abs(g[1]) < 1e-6, "position=0: ch1 gain should be 0.0, got \(g[1])")
#expect(abs(g[2]) < 1e-6, "position=0: ch2 gain should be 0.0, got \(g[2])")
#expect(abs(g[3]) < 1e-6, "position=0: ch3 gain should be 0.0, got \(g[3])")
}
// MARK: - Position = 0.5: equal power between channels 0 and 1
@Test func positionHalfGivesEqualPowerBetween0And1() throws {
let g: [Double] = try gains(channels: 4, position: 0.5)
let expected: Double = Double(2.0).squareRoot() / 2.0 // √2/2 ≈ 0.7071
#expect(abs(g[0] - expected) < 1e-5, "position=0.5: ch0 gain should be √2/2, got \(g[0])")
#expect(abs(g[1] - expected) < 1e-5, "position=0.5: ch1 gain should be √2/2, got \(g[1])")
#expect(abs(g[2]) < 1e-6, "position=0.5: ch2 gain should be 0, got \(g[2])")
#expect(abs(g[3]) < 1e-6, "position=0.5: ch3 gain should be 0, got \(g[3])")
}
// MARK: - Position = 1: full gain on channel 1
@Test func positionOneGivesFullGainOnChannel1() throws {
let g: [Double] = try gains(channels: 4, position: 1.0)
#expect(abs(g[0]) < 1e-6, "position=1: ch0 should be 0")
#expect(abs(g[1] - 1.0) < 1e-6, "position=1: ch1 should be 1.0, got \(g[1])")
#expect(abs(g[2]) < 1e-6, "position=1: ch2 should be 0")
#expect(abs(g[3]) < 1e-6, "position=1: ch3 should be 0")
}
// MARK: - Wrapping: position = N equivalent to position = 0
@Test func positionAtNWrapsToChannel0() throws {
let n: Int = 4
let g0: [Double] = try gains(channels: n, position: 0.0)
let gN: [Double] = try gains(channels: n, position: Double(n))
for ch: Int in 0..<n {
#expect(abs(gN[ch] - g0[ch]) < 1e-6,
"position=N should equal position=0 on ch\(ch): \(gN[ch]) vs \(g0[ch])")
}
}
@Test func positiveWrapEquivalence() throws {
let n: Int = 8
let ref: [Double] = try gains(channels: n, position: 0.5)
let wrp: [Double] = try gains(channels: n, position: Double(n) + 0.5)
for ch: Int in 0..<n {
#expect(abs(wrp[ch] - ref[ch]) < 1e-6,
"position=N+0.5 should equal position=0.5 on ch\(ch)")
}
}
// MARK: - Negative position wraps
@Test func negativePositionWraps() throws {
let n: Int = 4
let nF: Double = Double(n)
let refPos: Double = nF - 0.5 // between ch3 and ch0
let ref: [Double] = try gains(channels: n, position: refPos)
let neg: [Double] = try gains(channels: n, position: -0.5)
for ch: Int in 0..<n {
#expect(abs(neg[ch] - ref[ch]) < 1e-6,
"position=-0.5 should equal position=N-0.5 on ch\(ch)")
}
}
// MARK: - Equal-power invariant
@Test func equalPowerInvariant() throws {
let n: Int = 8
// Test many positions around the ring.
let positions: [Double] = [0.0, 0.1, 0.5, 0.9, 1.0, 2.3, 3.7, 7.5]
for pos: Double in positions {
let g: [Double] = try gains(channels: n, position: pos)
let sumSq: Double = g.reduce(0.0, { $0 + $1 * $1 })
#expect(abs(sumSq - 1.0) < 1e-5,
"Equal-power invariant failed at position=\(pos): sum of squares = \(sumSq)")
}
}
// MARK: - Smoke: non-silent, finite output
@Test func outputIsNonSilentWithNonZeroInput() throws {
let engine: SynthesisEngine = try makeEngine()
let graph: SynthGraph = makeSynthGraph(engine: engine)
defer { engine.stop() }
let pan: PanN = PanN(input: ConstantGeneratorInput(0.5), channels: 8, position: 2.5)
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 512)
let sum: Double = out.flatMap { $0 }.reduce(0.0, { $0 + abs($1) })
#expect(sum > 0, "PanN with non-zero input must produce non-silent output")
}
@Test func outputIsFinite() throws {
let engine: SynthesisEngine = try makeEngine()
let graph: SynthGraph = makeSynthGraph(engine: engine)
defer { engine.stop() }
let pan: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: 12, position: 5.7)
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 512)
for (ch, channel) in out.enumerated() {
for (i, sample) in channel.prefix(512).enumerated() {
#expect(sample.isFinite, "PanN 12ch: ch\(ch) sample \(i) is not finite")
}
}
}
// 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: PanN = PanN(input: ConstantGeneratorInput(0.8), channels: 6, position: 1.75)
let out1: [[Double]] = pan.calculatedSamples(graph: graph, count: 128)
let out2: [[Double]] = pan.calculatedSamples(graph: graph, count: 128) // cache hit
for ch: Int in 0..<6 {
#expect(out1[ch] == out2[ch], "Fan-out cache: ch\(ch) differs between two calls")
}
}
// MARK: - Generator position input
@Test func generatorPositionInput() throws {
let engine: SynthesisEngine = try makeEngine()
let graph: SynthGraph = makeSynthGraph(engine: engine)
defer { engine.stop() }
let posGen: ConstantGeneratorInput = ConstantGeneratorInput(2.5)
let pan: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: 8, position: posGen)
let out: [[Double]] = pan.calculatedSamples(graph: graph, count: 512)
let sum: Double = out.flatMap { $0 }.reduce(0.0, { $0 + abs($1) })
#expect(sum > 0, "Generator position input must produce non-silent output")
}
// MARK: - 3-channel equal spacing
@Test func threeChannelEqualSpacing() throws {
// Each integer position should give full gain on exactly that channel.
for ch: Int in 0..<3 {
let g: [Double] = try gains(channels: 3, position: Double(ch))
for c: Int in 0..<3 {
let expected: Double = c == ch ? 1.0 : 0.0
#expect(abs(g[c] - expected) < 1e-6,
"3ch position=\(ch): ch\(c) gain should be \(expected), got \(g[c])")
}
}
}
// MARK: - Wrap between last and first speaker
@Test func wrapBetweenLastAndFirstSpeaker() throws {
// position = N - 0.5: equal power between channel N-1 and channel 0.
let n: Int = 4
let g: [Double] = try gains(channels: n, position: Double(n) - 0.5)
let expected: Double = Double(2.0).squareRoot() / 2.0
#expect(abs(g[n - 1] - expected) < 1e-5,
"Wrap: ch\(n-1) gain should be √2/2, got \(g[n-1])")
#expect(abs(g[0] - expected) < 1e-5,
"Wrap: ch0 gain should be √2/2 (wraps from ch\(n-1)), got \(g[0])")
#expect(abs(g[1]) < 1e-6, "Wrap: ch1 should be silent, got \(g[1])")
#expect(abs(g[2]) < 1e-6, "Wrap: ch2 should be silent, got \(g[2])")
}
// MARK: - Amplitude scaling
@Test func inputAmplitudeScalesOutputProportionally() throws {
let engine: SynthesisEngine = try makeEngine()
let graph1: SynthGraph = makeSynthGraph(engine: engine)
let graph2: SynthGraph = makeSynthGraph(engine: engine)
defer { engine.stop() }
let pan1: PanN = PanN(input: ConstantGeneratorInput(1.0), channels: 4, position: 0.5)
let pan2: PanN = PanN(input: ConstantGeneratorInput(0.5), channels: 4, position: 0.5)
let out1: [[Double]] = pan1.calculatedSamples(graph: graph1, count: 64)
let out2: [[Double]] = pan2.calculatedSamples(graph: graph2, count: 64)
for ch: Int in 0..<4 {
#expect(abs(out2[ch][0] - out1[ch][0] * 0.5) < 1e-6,
"ch\(ch): half-amplitude input should halve output")
}
}
}