prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
// CrossFadeAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("CrossFade.Audio")
|
||||
struct CrossFadeAudioTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
let sampleRate: Double = 44100
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test func rateIsAudio() {
|
||||
let gen: CrossFade.Audio = CrossFade.Audio(inA: 0.0, inB: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
// MARK: - Output count
|
||||
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: CrossFade.Audio = CrossFade.Audio(inA: 1.0, inB: 2.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
}
|
||||
|
||||
// MARK: - Pan extremes
|
||||
|
||||
/// pan = -1 → output equals inA exactly (gainA = cos(0) = 1, gainB = sin(0) = 0).
|
||||
@Test func panMinusOneOutputsInA() {
|
||||
let gen: CrossFade.Audio = CrossFade.Audio(inA: 0.7, inB: 0.3, pan: -1.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - 0.7) < 1e-12,
|
||||
"pan=-1 sample \(i): expected 0.7, got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// pan = +1 → output equals inB exactly (gainA = cos(pi/2) = 0, gainB = sin(pi/2) = 1).
|
||||
@Test func panPlusOneOutputsInB() {
|
||||
let gen: CrossFade.Audio = CrossFade.Audio(inA: 0.7, inB: 0.3, pan: 1.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - 0.3) < 1e-12,
|
||||
"pan=+1 sample \(i): expected 0.3, got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// pan = 0 → both at cos(pi/4) = sqrt(2)/2 gain.
|
||||
@Test func panZeroEqualPowerMix() {
|
||||
let inAVal: Double = 1.0
|
||||
let inBVal: Double = 1.0
|
||||
let expectedGain: Double = cos(.pi / 4)
|
||||
let expected: Double = inAVal * expectedGain + inBVal * expectedGain
|
||||
let gen: CrossFade.Audio = CrossFade.Audio(inA: inAVal, inB: inBVal, pan: 0.0)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[0][i] - expected) < 1e-12,
|
||||
"pan=0 sample \(i): expected \(expected), got \(out[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Equal-power invariant: gainA^2 + gainB^2 == 1 at several pan values.
|
||||
@Test func equalPowerInvariant() {
|
||||
// Use inA = 1, inB = 0 to isolate gainA; then inA = 0, inB = 1 for gainB.
|
||||
let panValues: [Double] = [-1.0, -0.5, 0.0, 0.5, 1.0]
|
||||
for pan: Double in panValues {
|
||||
let genA: CrossFade.Audio = CrossFade.Audio(inA: 1.0, inB: 0.0, pan: pan)
|
||||
let genB: CrossFade.Audio = CrossFade.Audio(inA: 0.0, inB: 1.0, pan: pan)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let outA: [[Double]] = genA.calculatedSamples(graph: g, count: blockSize)
|
||||
let outB: [[Double]] = genB.calculatedSamples(graph: g, count: blockSize)
|
||||
let gainA: Double = outA[0][0]
|
||||
let gainB: Double = outB[0][0]
|
||||
let sum: Double = gainA * gainA + gainB * gainB
|
||||
#expect(abs(sum - 1.0) < 1e-12,
|
||||
"equal-power invariant at pan=\(pan): gainA^2+gainB^2=\(sum), expected 1.0")
|
||||
}
|
||||
}
|
||||
|
||||
/// Generator-driven pan: `ConstantGeneratorInput` (an AudioGenerator) takes the
|
||||
/// generator path and produces the same output as the scalar path with the same value.
|
||||
@Test func generatorPanMatchesScalarPan() {
|
||||
let panValue: Double = 0.3
|
||||
let panGen: ConstantGeneratorInput = ConstantGeneratorInput(panValue)
|
||||
let genViaGenerator: CrossFade.Audio = CrossFade.Audio(inA: 1.0, inB: 1.0, pan: panGen)
|
||||
let genViaScalar: CrossFade.Audio = CrossFade.Audio(inA: 1.0, inB: 1.0, pan: panValue)
|
||||
let g: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let outGen: [[Double]] = genViaGenerator.calculatedSamples(graph: g, count: blockSize)
|
||||
let outScalar: [[Double]] = genViaScalar.calculatedSamples(graph: g, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(outGen[0][i] - outScalar[0][i]) < 1e-12,
|
||||
"generator pan vs scalar pan: sample \(i) differs")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - mul/add
|
||||
|
||||
@Test func mulAddApplied() {
|
||||
// pan = 0, inA = inB = 1 → output ≈ cos(pi/4) + cos(pi/4) ≈ 1.4142
|
||||
// with mul=2, add=1 → expected = original * 2 + 1
|
||||
let gen: CrossFade.Audio = CrossFade.Audio(inA: 1.0, inB: 1.0, pan: 0.0)
|
||||
let genScaled: CrossFade.Audio = CrossFade.Audio(inA: 1.0, inB: 1.0, pan: 0.0, mul: 2.0, add: 1.0)
|
||||
let base: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: g2, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
let expected: Double = base[0][i] * 2.0 + 1.0
|
||||
#expect(abs(scaled[0][i] - expected) < 1e-12,
|
||||
"mulAdd sample \(i): expected \(expected), got \(scaled[0][i])")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Fan-out
|
||||
|
||||
@Test func fanOutSameSegment() {
|
||||
let gen: CrossFade.Audio = CrossFade.Audio(inA: 0.5, inB: 0.8, pan: 0.3)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[0][i] == out2[0][i],
|
||||
"fan-out: sample \(i) differs between calls on same segment")
|
||||
}
|
||||
}
|
||||
|
||||
/// State advances exactly once per block (second call in the same segment is a cache hit).
|
||||
@Test func stateAdvancesOncePerBlock() {
|
||||
let gen: CrossFade.Audio = CrossFade.Audio(inA: 0.5, inB: 0.5, pan: 0.0)
|
||||
let out1a: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out1b: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) // cache hit
|
||||
// Both calls in the same segment must return identical data.
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1a[0][i] == out1b[0][i])
|
||||
}
|
||||
advanceBlock(engine)
|
||||
let g2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: g2, count: blockSize)
|
||||
// CrossFade.Audio is stateless — same constant inputs produce the same output.
|
||||
let halfPi: Double = .pi / 2
|
||||
let p: Double = (0.0 + 1.0) * 0.5 // pan=0 → p=0.5
|
||||
let expected: Double = 0.5 * cos(p * halfPi) + 0.5 * sin(p * halfPi)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out2[0][i] - expected) < 1e-12)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Integration
|
||||
|
||||
@Test func integrationWithEngine() throws {
|
||||
final class XFadeSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let gen: CrossFade.Audio = CrossFade.Audio(inA: 0.5, inB: 0.8, pan: -0.5)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: gen))
|
||||
}
|
||||
}
|
||||
let synth: XFadeSynth = XFadeSynth()
|
||||
do {
|
||||
try engine.add(synth)
|
||||
} catch {
|
||||
Issue.record("engine.add failed: \(error)")
|
||||
}
|
||||
// Advance one block to confirm no crash.
|
||||
advanceBlock(engine)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user