// CrossoverDistortionControlTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("CrossoverDistortion.Control") struct CrossoverDistortionControlTests { 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 rateIsControl() { let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.0) #expect(gen.rate == .control) } @Test func outputCountMatchesRequest() { let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.0) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) #expect(Array(out[0].prefix(blockSize)).count == blockSize) } /// amp=0, input=0.5: ramp target equals 0.5 (passthrough at control rate). @Test func ampZeroIsPassthrough() { let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.5, amp: 0.0, smooth: 0.0) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) let lastSample: Double = out[0][blockSize - 1] #expect(abs(lastSample - 0.5) < 1e-12, "passthrough ramp target: expected 0.5 got \(lastSample)") } /// amp=0.5, smooth=0, input=0.7: ramp target ≈ 0.2. @Test func hardKneeLinearRegionTarget() { let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.7, amp: 0.5, smooth: 0.0) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) let lastSample: Double = out[0][blockSize - 1] #expect(abs(lastSample - 0.2) < 1e-12, "hard-knee target: expected 0.2 got \(lastSample)") } /// amp=0.5, smooth=0, input=0.3: ramp target ≈ 0 (dead zone). @Test func deadZoneTargetIsZero() { let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.3, amp: 0.5, smooth: 0.0) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) let lastSample: Double = out[0][blockSize - 1] #expect(lastSample == 0.0, "dead zone target: expected 0 got \(lastSample)") } /// floatValue returns previousOutputValue. @Test func floatValueReturnsPreviousOutputValue() { let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.7, amp: 0.5, smooth: 0.0) let _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.floatValue(graph: graph) - 0.2) < 1e-12) } /// mul/add: target × mul + add is folded before ramping. @Test func mulAddFoldsIntoTarget() { // amp=0 (passthrough), input=0.3: base target = 0.3; ×2+1 = 1.6 let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.3, amp: 0.0, mul: 2.0, add: 1.0) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) let lastSample: Double = out[0][blockSize - 1] #expect(abs(lastSample - 1.6) < 1e-12, "mulAdd target: expected 1.6 got \(lastSample)") } /// Fan-out: two calculatedSamples calls on the same graph return identical first channel. @Test func fanOut() { let gen: CrossoverDistortion.Control = CrossoverDistortion.Control(input: 0.7, 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..