// SlopeControlTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Slope.Control") struct SlopeControlTests { 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: Slope.Control = Slope.Control(input: 0.0) #expect(gen.rate == .control) } /// Output count matches request. @Test func outputCountMatchesRequest() { let gen: Slope.Control = Slope.Control(input: 0.5) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) #expect(Array(out[0].prefix(blockSize)).count == blockSize) } /// First block outputs 0.0 (lazy-init — no startup spike). @Test func firstBlockOutputsZero() { let gen: Slope.Control = Slope.Control(input: 5.0) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 0.0) < 1e-12, "first block should output 0.0 (lazy-init), got \(gen.previousOutputValue)") } /// Constant input produces slope 0 after initialization. @Test func constantInputSlopeIsZero() { let gen: Slope.Control = Slope.Control(input: 3.0) // Block 1: lazy-init → output 0. _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 0.0) < 1e-12, "first block of constant input: slope = 0") // Block 2: x stays at 3.0, prev = 3.0 → slope = 0. advanceBlock(engine) _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 0.0) < 1e-12, "second block of constant input: slope should still be 0") } /// Control-rate slope uses controlRate = sampleRate/blockSize. /// /// If the input steps from 0 to 1 between blocks, the slope is /// (1.0 - 0.0) * controlRate = sampleRate / blockSize. @Test func stepInputProducesControlRateSlope() { let controlRate: Double = sampleRate / Double(blockSize) let firstSerial: UInt64 = engine.segmentSerial let inputSignal: Closure = Closure { graph, _ in graph.engine.segmentSerial == firstSerial ? 0.0 : 1.0 } let gen: Slope.Control = Slope.Control(input: inputSignal) // Block 1: lazy-init → output 0, prevSample becomes 0.0. _ = gen.calculatedSamples(graph: graph, count: blockSize) #expect(abs(gen.previousOutputValue - 0.0) < 1e-12) // Block 2: x = 1.0, prev = 0.0 → slope = (1.0 - 0.0) * controlRate. advanceBlock(engine) _ = gen.calculatedSamples(graph: graph, count: blockSize) let expectedSlope: Double = controlRate #expect(abs(gen.previousOutputValue - expectedSlope) < 1e-9, "step slope: expected \(expectedSlope), got \(gen.previousOutputValue)") } /// Output block is flat (vDSP_vfillD): all samples equal the computed slope. @Test func outputBlockIsFlat() { let gen: Slope.Control = Slope.Control(input: 0.5) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) let expected: Double = out[0][blockSize - 1] for i: Int in 0..