// WaveshaperControlTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Waveshaper.Control") struct WaveshaperControlTests { 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() } func makeIdentityTable(n: Int = 1024) -> LyrebirdBuffer { let table: LyrebirdBuffer = LyrebirdBuffer(count: n) table.fill { i, count in Double(i) / Double(count - 1) * 2.0 - 1.0 } return table } @Test func rateIsControl() { let table: LyrebirdBuffer = makeIdentityTable() let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.0) #expect(gen.rate == .control) } @Test func outputCountMatchesRequest() { let table: LyrebirdBuffer = makeIdentityTable() let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.0) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) #expect(Array(out[0].prefix(blockSize)).count == blockSize) } /// Identity table with constant input 0.5: ramp reaches ~0.5 by last sample. @Test func identityTableConstantInput() { let table: LyrebirdBuffer = makeIdentityTable() let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.5) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) // Control ramp: last sample of first block equals target ≈ 0.5. let lastSample: Double = out[0][blockSize - 1] #expect(abs(lastSample - 0.5) < 1e-10, "ramp target: expected ~0.5 got \(lastSample)") } /// floatValue returns previousOutputValue (the control-rate target). @Test func floatValueReturnsPreviousOutputValue() { let table: LyrebirdBuffer = makeIdentityTable() let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.7) // After first block, previousOutputValue should be ~0.7. let _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.floatValue(graph: graph) - 0.7) < 1e-10) } /// Invalidated table produces silence (target = 0 → ramp toward 0). @Test func invalidTableProduceSilence() { let table: LyrebirdBuffer = makeIdentityTable() let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.5) table.invalidate() let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) // With invalid table target = 0; initial previousOutputValue = 0, so output is flat zero. #expect(out[0][blockSize - 1] == 0.0) } /// mul/add: with mul=2.0 add=1.0, ramp target is approximately original×2+1. @Test func mulAddFoldsIntoTarget() { let tableSize: Int = 1024 let table: LyrebirdBuffer = makeIdentityTable(n: tableSize) // identity table at 0.25 → waveshaper output ≈ 0.25; ×2 +1 = 1.5 let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.25, 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.5) < 1e-10, "mulAdd target: expected ~1.5 got \(lastSample)") } /// Fan-out: two calculatedSamples calls on the same graph return identical first channel. @Test func fanOut() { let table: LyrebirdBuffer = makeIdentityTable() let gen: Waveshaper.Control = Waveshaper.Control(table: table, input: 0.3) let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) for i: Int in 0..