// ParameterNodeTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // MARK: - ParameterNode Tests @Suite("ParameterNode") struct ParameterNodeTests { // MARK: - Initial state @Test func initialValueFlat() { // First block should be entirely flat at the initial value — no ramp artefact. let engine: SynthesisEngine = makeTestEngine(blockSize: 8) let graph: SynthGraph = makeSynthGraph(engine: engine) let node: ParameterNode = ParameterNode(1.0) let samples: [Double] = node.calculatedSamples(graph: graph, count: 8)[0] for sample: Double in samples.prefix(8) { #expect(abs(sample - 1.0) < 1e-12, "Initial block must be flat at initialValue") } } @Test func initialValueReturnedByFloatValue() { let engine: SynthesisEngine = makeTestEngine(blockSize: 8) let graph: SynthGraph = makeSynthGraph(engine: engine) let node: ParameterNode = ParameterNode(440.0) #expect(node.value == 440.0) #expect(node.floatValue(graph: graph) == 440.0) } // MARK: - Linear ramp interpolation @Test func rampsLinearlyToNewValue() { // Block 1: value=0.0, target=1.0 over 4 samples → [0.25, 0.50, 0.75, 1.00] let blockSize: Int = 4 let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) let graph: SynthGraph = makeSynthGraph(engine: engine) let node: ParameterNode = ParameterNode(0.0) // Set a new target before block 1 node.value = 1.0 let samples: [Double] = node.calculatedSamples(graph: graph, count: blockSize)[0] let expected: [Double] = [0.25, 0.50, 0.75, 1.00] for (i, (s, e)) in zip(samples.prefix(blockSize), expected).enumerated() { #expect(abs(s - e) < 1e-12, "Sample \(i): expected \(e), got \(s)") } } @Test func lastSampleEqualsTarget() { // The last sample in a ramp block must exactly equal the target. let blockSize: Int = 16 let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) let graph: SynthGraph = makeSynthGraph(engine: engine) let node: ParameterNode = ParameterNode(0.0) node.value = 880.0 let samples: [Double] = node.calculatedSamples(graph: graph, count: blockSize)[0] #expect(samples[blockSize - 1] == 880.0, "Last sample must equal the target value") } @Test func consecutiveBlocksRampContinuously() { // Block 1: ramp 0→440. Block 2: ramp 440→880. let blockSize: Int = 8 let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) let node: ParameterNode = ParameterNode(0.0) node.value = 440.0 let graph1: SynthGraph = makeSynthGraph(engine: engine) let block1: [Double] = node.calculatedSamples(graph: graph1, count: blockSize)[0] advanceBlock(engine) node.value = 880.0 let graph2: SynthGraph = makeSynthGraph(engine: engine) let block2: [Double] = node.calculatedSamples(graph: graph2, count: blockSize)[0] // Block 1 ends at 440.0 #expect(abs(block1[blockSize - 1] - 440.0) < 1e-12) // Block 2 starts from 440.0 and ramps to 880.0 let step: Double = (880.0 - 440.0) / Double(blockSize) for i: Int in 0..= blockSize, "Pre-allocated buffer must cover blockSize at block \(block)") advanceBlock(engine) } } // MARK: - Partial count (count < blockSize) @Test func partialCountRampsCorrectly() { // calculatedSamples with count < blockSize must still produce a valid ramp. let blockSize: Int = 512 let partialCount: Int = 16 let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) let graph: SynthGraph = makeSynthGraph(engine: engine) let node: ParameterNode = ParameterNode(0.0) node.value = 16.0 let samples: [Double] = node.calculatedSamples(graph: graph, count: partialCount)[0] // step = 16.0 / 16 = 1.0 → samples[i] = i + 1 for i: Int in 0..