// 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.. 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..