Files
2026-06-21 11:45:58 -07:00

135 lines
5.1 KiB
Swift

// CrossFadeControlTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("CrossFade.Control")
struct CrossFadeControlTests {
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 rateIsControl() {
let gen: CrossFade.Control = CrossFade.Control(inA: 0.0, inB: 0.0)
#expect(gen.rate == .control)
}
// MARK: - Output count
@Test func outputCountMatchesRequest() {
let gen: CrossFade.Control = CrossFade.Control(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 → target equals inA.
@Test func panMinusOneTargetIsInA() {
let gen: CrossFade.Control = CrossFade.Control(inA: 0.7, inB: 0.3, pan: -1.0)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
// previousOutputValue converges to target = 0.7 after ramp.
#expect(abs(gen.previousOutputValue - 0.7) < 1e-12,
"pan=-1 target should be 0.7, got \(gen.previousOutputValue)")
}
/// pan = +1 → target equals inB.
@Test func panPlusOneTargetIsInB() {
let gen: CrossFade.Control = CrossFade.Control(inA: 0.7, inB: 0.3, pan: 1.0)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.3) < 1e-12,
"pan=+1 target should be 0.3, got \(gen.previousOutputValue)")
}
/// pan = 0 → target is (inA + inB) * cos(pi/4).
@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.Control = CrossFade.Control(inA: inAVal, inB: inBVal, pan: 0.0)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - expected) < 1e-12,
"pan=0 target: expected \(expected), got \(gen.previousOutputValue)")
}
// MARK: - Ramp
/// Last sample of first block equals target.
@Test func rampsToTarget() {
let gen: CrossFade.Control = CrossFade.Control(inA: 0.0, inB: 1.0, pan: 1.0)
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
// Target at pan=1 is inB = 1.0; ramp converges to it.
let lastSample: Double = out[0][blockSize - 1]
#expect(abs(lastSample - 1.0) < 1e-12,
"last sample of ramp should equal target 1.0, got \(lastSample)")
}
// MARK: - mul/add
@Test func mulAddApplied() {
// pan=0, inA=inB=1 → target = cos(pi/4) + sin(pi/4)
// With mul=2, add=1: target_scaled = original*2+1
let expectedGain: Double = cos(.pi / 4)
let baseTarget: Double = 1.0 * expectedGain + 1.0 * expectedGain
let expectedScaled: Double = baseTarget * 2.0 + 1.0
let gen: CrossFade.Control = CrossFade.Control(inA: 1.0, inB: 1.0, pan: 0.0, mul: 2.0, add: 1.0)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - expectedScaled) < 1e-12,
"mulAdd: expected \(expectedScaled), got \(gen.previousOutputValue)")
}
// MARK: - Fan-out
@Test func fanOutSameSegment() {
let gen: CrossFade.Control = CrossFade.Control(inA: 0.3, inB: 0.7, pan: 0.0)
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")
}
}
/// floatValue returns previousOutputValue.
@Test func floatValueReturnsPreviousOutputValue() {
let gen: CrossFade.Control = CrossFade.Control(inA: 0.5, inB: 0.5, pan: 0.0)
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
}
// MARK: - Integration
@Test func integrationWithEngine() throws {
final class XFadeControlSynth: SynthesizerBase {
override func build() {
let gen: CrossFade.Control = CrossFade.Control(inA: 0.5, inB: 0.8, pan: 0.0)
addOutputGenerator(Out.Audio(bus: 0, source: gen))
}
}
let synth: XFadeControlSynth = XFadeControlSynth()
do {
try engine.add(synth)
} catch {
Issue.record("engine.add failed: \(error)")
}
advanceBlock(engine)
}
}