prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
// LinearCrossFadeAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("LinearCrossFade.Audio")
|
||||
struct LinearCrossFadeAudioTests {
|
||||
|
||||
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: LinearCrossFade.Audio = LinearCrossFade.Audio(inA: 0.0, inB: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
// MARK: - Output count
|
||||
|
||||
@Test func outputCountMatchesRequest() {
|
||||
let gen: LinearCrossFade.Audio = LinearCrossFade.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 → gainA = 1, gainB = 0 → output equals inA.
|
||||
@Test func panMinusOneOutputsInA() {
|
||||
let gen: LinearCrossFade.Audio = LinearCrossFade.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 → gainA = 0, gainB = 1 → output equals inB.
|
||||
@Test func panPlusOneOutputsInB() {
|
||||
let gen: LinearCrossFade.Audio = LinearCrossFade.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 0.5 (linear, sum = 1).
|
||||
@Test func panZeroHalfEach() {
|
||||
let inAVal: Double = 1.0
|
||||
let inBVal: Double = 1.0
|
||||
let expected: Double = inAVal * 0.5 + inBVal * 0.5
|
||||
let gen: LinearCrossFade.Audio = LinearCrossFade.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])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Linear sum invariant: gainA + gainB == 1 at several pan values.
|
||||
@Test func linearSumInvariant() {
|
||||
let panValues: [Double] = [-1.0, -0.5, 0.0, 0.5, 1.0]
|
||||
for pan: Double in panValues {
|
||||
// Use inA=1/inB=0 and inA=0/inB=1 to isolate the gains.
|
||||
let genA: LinearCrossFade.Audio = LinearCrossFade.Audio(inA: 1.0, inB: 0.0, pan: pan)
|
||||
let genB: LinearCrossFade.Audio = LinearCrossFade.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 sumGains: Double = gainA + gainB
|
||||
#expect(abs(sumGains - 1.0) < 1e-12,
|
||||
"linear sum invariant at pan=\(pan): gainA+gainB=\(sumGains), 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.4
|
||||
let panGen: ConstantGeneratorInput = ConstantGeneratorInput(panValue)
|
||||
let genViaGenerator: LinearCrossFade.Audio = LinearCrossFade.Audio(
|
||||
inA: 1.0, inB: 1.0, pan: panGen)
|
||||
let genViaScalar: LinearCrossFade.Audio = LinearCrossFade.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 = 1.0 * 0.5 + 1.0 * 0.5 = 1.0
|
||||
// with mul=2, add=1 → expected = 1.0 * 2 + 1 = 3.0
|
||||
let gen: LinearCrossFade.Audio = LinearCrossFade.Audio(inA: 1.0, inB: 1.0, pan: 0.0)
|
||||
let genScaled: LinearCrossFade.Audio = LinearCrossFade.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: LinearCrossFade.Audio = LinearCrossFade.Audio(inA: 0.4, inB: 0.6, pan: 0.2)
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Integration
|
||||
|
||||
@Test func integrationWithEngine() throws {
|
||||
final class LinXFadeSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let gen: LinearCrossFade.Audio = LinearCrossFade.Audio(inA: 0.5, inB: 0.8, pan: 0.5)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: gen))
|
||||
}
|
||||
}
|
||||
let synth: LinXFadeSynth = LinXFadeSynth()
|
||||
do {
|
||||
try engine.add(synth)
|
||||
} catch {
|
||||
Issue.record("engine.add failed: \(error)")
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user