165 lines
8.0 KiB
Swift
165 lines
8.0 KiB
Swift
// CrossoverDistortionAudioTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
import Foundation
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("CrossoverDistortion.Audio")
|
||
struct CrossoverDistortionAudioTests {
|
||
|
||
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()
|
||
}
|
||
|
||
@Test func rateIsAudio() {
|
||
let gen: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.0)
|
||
#expect(gen.rate == .audio)
|
||
}
|
||
|
||
@Test func outputCountMatchesRequest() {
|
||
let gen: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.0)
|
||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||
}
|
||
|
||
/// amp=0 means no dead zone → output equals input (passthrough).
|
||
/// Tests three constant inputs to verify the passthrough at amp=0.
|
||
@Test func ampZeroIsPassthrough() {
|
||
// Positive input
|
||
let genPos: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.5, amp: 0.0, smooth: 0.0)
|
||
let outPos: [[Double]] = genPos.calculatedSamples(graph: graph, count: blockSize)
|
||
for i: Int in 0..<blockSize {
|
||
#expect(abs(outPos[0][i] - 0.5) < 1e-12,
|
||
"passthrough +0.5 sample \(i): expected 0.5 got \(outPos[0][i])")
|
||
}
|
||
// Negative input
|
||
advanceBlock(engine)
|
||
let genNeg: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: -0.5, amp: 0.0, smooth: 0.0)
|
||
let outNeg: [[Double]] = genNeg.calculatedSamples(graph: graph, count: blockSize)
|
||
for i: Int in 0..<blockSize {
|
||
#expect(abs(outNeg[0][i] - (-0.5)) < 1e-12,
|
||
"passthrough -0.5 sample \(i): expected -0.5 got \(outNeg[0][i])")
|
||
}
|
||
// Zero input
|
||
advanceBlock(engine)
|
||
let genZero: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.0, amp: 0.0, smooth: 0.0)
|
||
let outZero: [[Double]] = genZero.calculatedSamples(graph: graph, count: blockSize)
|
||
for i: Int in 0..<blockSize {
|
||
#expect(outZero[0][i] == 0.0, "passthrough 0 sample \(i): expected 0 got \(outZero[0][i])")
|
||
}
|
||
}
|
||
|
||
/// amp=0.5, smooth=0: hard knee dead zone [−0.5, 0.5] → zero; x=0.3→0; x=0.7→0.2; x=-0.7→-0.2.
|
||
@Test func hardKneeDeadZoneAndLinearRegion() {
|
||
// Within dead zone (x=0.3, amp=0.5): output = 0
|
||
let genDeadZone: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.3, amp: 0.5, smooth: 0.0)
|
||
let outDeadZone: [[Double]] = genDeadZone.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(outDeadZone[0][0] == 0.0, "dead zone: expected 0 got \(outDeadZone[0][0])")
|
||
|
||
// Positive linear region (x=0.7, amp=0.5): over=0.2, out = 0.2
|
||
advanceBlock(engine)
|
||
let genPos: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.7, amp: 0.5, smooth: 0.0)
|
||
let outPos: [[Double]] = genPos.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(abs(outPos[0][0] - 0.2) < 1e-12, "positive linear: expected 0.2 got \(outPos[0][0])")
|
||
|
||
// Negative linear region (x=-0.7, amp=0.5): out = -0.2
|
||
advanceBlock(engine)
|
||
let genNeg: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: -0.7, amp: 0.5, smooth: 0.0)
|
||
let outNeg: [[Double]] = genNeg.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(abs(outNeg[0][0] - (-0.2)) < 1e-12, "negative linear: expected -0.2 got \(outNeg[0][0])")
|
||
|
||
// Dead zone at threshold (x=0.5, amp=0.5): exactly at boundary, out = 0
|
||
advanceBlock(engine)
|
||
let genEdge: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.5, amp: 0.5, smooth: 0.0)
|
||
let outEdge: [[Double]] = genEdge.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(outEdge[0][0] == 0.0, "at threshold: expected 0 got \(outEdge[0][0])")
|
||
}
|
||
|
||
/// smooth > 0: output just above the threshold is smaller than the hard-knee value (C1 ease-in).
|
||
@Test func smoothKneeIsLessThanHardKneeNearThreshold() {
|
||
// amp=0.5, smooth=1.0, x=0.55: over=0.05, kneeWidth=0.5.
|
||
// hard knee: out = 0.05
|
||
// smooth ease-in: out = (0.05^2)/(2*0.5) = 0.0025/1.0 = 0.0025
|
||
advanceBlock(engine)
|
||
let genHard: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.55, amp: 0.5, smooth: 0.0)
|
||
let genSmooth: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.55, amp: 0.5, smooth: 1.0)
|
||
let outHard: [[Double]] = genHard.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let outSmooth: [[Double]] = genSmooth.calculatedSamples(graph: graph, count: blockSize)
|
||
// Smooth output should be smaller than hard-knee output (ease-in starts slower).
|
||
#expect(outSmooth[0][0] < outHard[0][0],
|
||
"smooth should be less than hard at knee entry: smooth=\(outSmooth[0][0]) hard=\(outHard[0][0])")
|
||
// Both should be positive.
|
||
#expect(outSmooth[0][0] > 0.0, "smooth output should be positive above threshold")
|
||
// Verify exact value: over^2/(2*kneeWidth) = 0.05^2 / (2*0.5) = 0.0025
|
||
#expect(abs(outSmooth[0][0] - 0.0025) < 1e-12, "smooth ease-in: expected 0.0025 got \(outSmooth[0][0])")
|
||
}
|
||
|
||
/// mul/add: with mul=2.0 add=1.0, output is base×2+1.
|
||
@Test func mulAddScalesOutput() {
|
||
// Use amp=0 (passthrough) so base output = input.
|
||
let inputVal: Double = 0.4
|
||
let genBase: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: inputVal, amp: 0.0)
|
||
let genScaled: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: inputVal, amp: 0.0, mul: 2.0, add: 1.0)
|
||
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
|
||
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, 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])")
|
||
}
|
||
}
|
||
|
||
/// Fan-out: two calculatedSamples calls on the same graph return identical first channel.
|
||
@Test func fanOut() {
|
||
let gen: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.3, amp: 0.5)
|
||
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])
|
||
}
|
||
}
|
||
|
||
/// Second call in same segment is a cache hit — state does not double-advance.
|
||
@Test func cacheHitDoesNotDoubleAdvance() {
|
||
let gen: CrossoverDistortion.Audio = CrossoverDistortion.Audio(input: 0.7, amp: 0.5)
|
||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
let _ = gen.calculatedSamples(graph: graph, count: blockSize) // cache hit
|
||
for i: Int in 0..<blockSize {
|
||
// amp=0.5, x=0.7: out = 0.2 for all samples.
|
||
#expect(abs(out1[0][i] - 0.2) < 1e-12)
|
||
}
|
||
}
|
||
|
||
/// Integration: runs inside a Synthesizer without crash.
|
||
@Test func integrationWithEngine() throws {
|
||
class CrossoverSynth: SynthesizerBase {
|
||
override func build() {
|
||
let dist: CrossoverDistortion.Audio = CrossoverDistortion.Audio(
|
||
input: OscSin.Audio(freq: 440.0),
|
||
amp: 0.3,
|
||
smooth: 0.5
|
||
)
|
||
addOutputGenerator(Out.Audio(bus: 0, source: dist))
|
||
}
|
||
}
|
||
let synth: CrossoverSynth = CrossoverSynth()
|
||
try engine.add(synth)
|
||
for _: Int in 0..<4 {
|
||
engine.processBlock(inputSamples: [])
|
||
}
|
||
engine.remove(synth)
|
||
}
|
||
}
|