Files
Lyrebird/Tests/LyrebirdTests/ParameterNodeTests.swift
T
2026-06-01 18:27:40 -07:00

338 lines
14 KiB
Swift

// 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 {
let expected: Double = 440.0 + step * Double(i + 1)
#expect(abs(block2[i] - expected) < 1e-9,
"Block 2 sample \(i): expected \(expected), got \(block2[i])")
}
}
@Test func noRampWhenValueUnchanged() {
// When value is not changed between blocks, each block should be flat at the current value.
let blockSize: Int = 8
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
let node: ParameterNode = ParameterNode(440.0)
let graph1: SynthGraph = makeSynthGraph(engine: engine)
let block1: [Double] = node.calculatedSamples(graph: graph1, count: blockSize)[0]
advanceBlock(engine)
let graph2: SynthGraph = makeSynthGraph(engine: engine)
let block2: [Double] = node.calculatedSamples(graph: graph2, count: blockSize)[0]
for sample: Double in block1.prefix(blockSize) {
#expect(abs(sample - 440.0) < 1e-12, "Block 1 must be flat when value unchanged")
}
for sample: Double in block2.prefix(blockSize) {
#expect(abs(sample - 440.0) < 1e-12, "Block 2 must be flat when value unchanged")
}
}
// MARK: - Fan-out caching
@Test func fanOutTwoConsumersSameSegment() {
// Two consumers calling calculatedSamples in the same segment must see identical buffers.
let blockSize: Int = 8
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let node: ParameterNode = ParameterNode(0.0)
node.value = 100.0
let result1: [[Double]] = node.calculatedSamples(graph: graph, count: blockSize)
let result2: [[Double]] = node.calculatedSamples(graph: graph, count: blockSize)
for i: Int in 0..<blockSize {
#expect(result1[0][i] == result2[0][i],
"Fan-out: both consumers must see the same sample at index \(i)")
}
}
@Test func fanOutDoesNotDoubleAdvancePreviousValue() {
// After a segment with two consumers, _previousValue must not be double-advanced.
// We verify this by checking the ramp in block 2 starts from the correct origin (target of block 1).
let blockSize: Int = 4
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
let node: ParameterNode = ParameterNode(0.0)
node.value = 4.0
let graph1: SynthGraph = makeSynthGraph(engine: engine)
// Two consumers in block 1
_ = node.calculatedSamples(graph: graph1, count: blockSize)
_ = node.calculatedSamples(graph: graph1, count: blockSize)
advanceBlock(engine)
// Block 2: new target 8.0 — ramp from 4.0 to 8.0 (step=1.0 over 4 samples).
node.value = 8.0
let graph2: SynthGraph = makeSynthGraph(engine: engine)
let block2: [Double] = node.calculatedSamples(graph: graph2, count: blockSize)[0]
// Ramp from 4.0 to 8.0 (4 samples → step = 1.0)
let expected: [Double] = [5.0, 6.0, 7.0, 8.0]
for (i, (s, e)) in zip(block2.prefix(blockSize), expected).enumerated() {
#expect(abs(s - e) < 1e-12,
"Fan-out double-advance check: sample \(i) expected \(e), got \(s)")
}
}
// MARK: - Atomic update visibility
@Test func atomicUpdateVisibleInNextBlock() {
// A value written before the block boundary must be visible during that block.
let blockSize: Int = 8
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
let node: ParameterNode = ParameterNode(0.0)
// Write a new value
node.value = 500.0
let graph: SynthGraph = makeSynthGraph(engine: engine)
let samples: [Double] = node.calculatedSamples(graph: graph, count: blockSize)[0]
// The ramp target is 500.0 — the last sample must equal 500.0
#expect(abs(samples[blockSize - 1] - 500.0) < 1e-12,
"Atomic update must be visible in the same block's calculatedSamples call")
}
@Test func valueGetterReadsAtomicBits() {
// value getter uses acquiring load — must reflect the last store.
let node: ParameterNode = ParameterNode(1.0)
node.value = 2.5
#expect(node.value == 2.5)
node.value = -0.5
#expect(node.value == -0.5)
}
// MARK: - floatValue is a direct atomic read
@Test func floatValueReturnsCurrentAtomicTarget() {
// floatValue is a block-constant direct atomic read — returns the current target value,
// not a ramp sample. Generators that detect ParameterNode as a Generator always call
// calculatedSamples directly, so floatValue is only reached by explicit scalar consumers.
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let node: ParameterNode = ParameterNode(0.0)
node.value = 100.0
let fv: Double = node.floatValue(graph: graph)
#expect(fv == 100.0, "floatValue must return the current atomic target value")
}
@Test func floatValueDoesNotConsumeComputeBudget() {
// floatValue must not advance _previousValue or consume the fan-out budget.
// After a floatValue call, calculatedSamples in the same segment must compute
// the correct ramp from scratch.
let blockSize: Int = 8
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let node: ParameterNode = ParameterNode(0.0)
node.value = 80.0
_ = node.floatValue(graph: graph)
let samples: [Double] = node.calculatedSamples(graph: graph, count: blockSize)[0]
// Full ramp 0→80 over 8 samples — last sample must be exactly 80.0
#expect(samples[blockSize - 1] == 80.0,
"calculatedSamples after floatValue must still compute the complete ramp")
}
// MARK: - Pre-allocated buffer reuse
@Test func bufferIdentityPreservedAcrossBlocks() {
// The underlying buffer object should be reused, not replaced, between blocks.
// We verify by checking _result[0] pointer identity indirectly: if the buffer were
// reallocated, subsequent blocks would reflect new storage. Instead we verify
// correctness across many blocks, which would catch RT-allocation violations in
// sanitized builds.
let blockSize: Int = 8
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
let node: ParameterNode = ParameterNode(0.0)
for block: Int in 0..<50 {
let graph: SynthGraph = makeSynthGraph(engine: engine)
node.value = Double(block)
let samples: [[Double]] = node.calculatedSamples(graph: graph, count: blockSize)
#expect(samples.count == 1, "Single-channel output expected at block \(block)")
#expect(samples[0].count >= 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..<partialCount {
let expected: Double = Double(i + 1)
#expect(abs(samples[i] - expected) < 1e-12,
"Partial count: sample \(i) expected \(expected), got \(samples[i])")
}
}
}
// MARK: - @LiveParam Tests
@Suite("@LiveParam")
struct LiveParamTests {
@Test func wrappedValueReadsAndWrites() {
// Setting wrappedValue must update the underlying ParameterNode's atomic.
final class Holder: SynthesizerBase {
@LiveParam var freq: Double = 440.0
override func build() {}
}
let holder: Holder = Holder()
#expect(holder.freq == 440.0)
holder.freq = 880.0
#expect(holder.freq == 880.0)
holder.freq = 220.0
#expect(holder.freq == 220.0)
}
@Test func projectedValueIsParameterNode() {
// $name must expose the ParameterNode so it can be wired into a signal graph.
final class Holder: SynthesizerBase {
@LiveParam var amp: Double = 0.5
override func build() {}
}
let holder: Holder = Holder()
let node: ParameterNode = holder.$amp
// ParameterNode is an AudioGenerator — verify it can produce samples
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let samples: [Double] = node.calculatedSamples(graph: graph, count: 8)[0]
for sample: Double in samples.prefix(8) {
#expect(abs(sample - 0.5) < 1e-12, "Initial flat block at 0.5")
}
}
@Test func projectedValueAndWrappedValueShareSameNode() {
// Writing wrappedValue must be immediately visible through the projected node's value getter.
final class Holder: SynthesizerBase {
@LiveParam var freq: Double = 440.0
override func build() {}
}
let holder: Holder = Holder()
holder.freq = 1000.0
#expect(holder.$freq.value == 1000.0,
"ParameterNode.value must reflect the wrappedValue write")
}
@Test func projectedNodeIdentityStable() {
// The same ParameterNode instance must be returned on every $name access.
final class Holder: SynthesizerBase {
@LiveParam var freq: Double = 440.0
override func build() {}
}
let holder: Holder = Holder()
let node1: ParameterNode = holder.$freq
let node2: ParameterNode = holder.$freq
#expect(node1 === node2, "Projected value must be the same ParameterNode instance")
}
@Test func liveParamWiresAsGenerator() {
// When @LiveParam is wired into OscSin, OscSin detects it as a Generator and calls
// calculatedSamples (per-sample interpolation path), not floatValue (block-constant path).
final class Tone: SynthesizerBase {
@LiveParam var freq: Double = 440.0
override func build() {}
}
let tone: Tone = Tone()
// ParameterNode conforms to Generator (AudioGenerator)
let node: ParameterNode = tone.$freq
let isGenerator: Bool = node is any Generator
#expect(isGenerator, "$LiveParam projected value must conform to Generator for per-sample interpolation")
}
}