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

356 lines
14 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// UIParamNodeTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable import LyrebirdUI
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - Init
@Suite("UIParamNode init") struct UIParamNodeInitTests {
@Test func initialValueStoredAsObservable() {
let node: UIParamNode = UIParamNode(440.0)
#expect(node.value == 440.0)
}
@Test func initialValueReadableViaFloatValue() {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let node: UIParamNode = UIParamNode(0.5)
#expect(node.floatValue(graph: graph) == 0.5)
}
@Test func initialValueReadableViaIntValue() {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let node: UIParamNode = UIParamNode(3.7)
#expect(node.intValue(graph: graph) == 3)
}
@Test func intValueAfterUpdate() {
// intValue must reflect the updated value (direct atomic read, no ramp).
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let node: UIParamNode = UIParamNode(1.0)
node.update(5.7)
#expect(node.intValue(graph: graph) == 5)
}
@Test func intValueDoesNotPrimeFanOutCache() {
// Calling intValue then calculatedSamples in the same segment must produce a correct
// ramp — intValue must not consume the fan-out budget (no calculatedSamples side-effect).
let engine: SynthesisEngine = makeUITestEngine()
let blockCount: Int = 4
let node: UIParamNode = UIParamNode(0.0)
node.update(4.0)
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
// intValue call — must be a direct atomic read, no _computeSamples invocation.
_ = node.intValue(graph: graph)
// calculatedSamples in the same segment must still compute the correct ramp.
let result: [[Double]] = node.calculatedSamples(graph: graph, count: blockCount)
// Ramp 0→4 in 4 steps: [1.0, 2.0, 3.0, 4.0]
#expect(result[0][blockCount - 1] == 4.0,
"calculatedSamples after intValue must still produce the correct ramp target")
}
@Test func needsInterpolationIsTrue() {
let node: UIParamNode = UIParamNode(1.0)
#expect(node.needsInterpolation() == true)
}
}
// MARK: - Atomic update round-trip
@Suite("UIParamNode atomic update") struct UIParamNodeAtomicTests {
@Test func updateSetsObservableValue() {
let node: UIParamNode = UIParamNode(100.0)
node.update(200.0)
#expect(node.value == 200.0)
}
@Test func updateReflectsInFloatValue() {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let node: UIParamNode = UIParamNode(100.0)
node.update(880.0)
#expect(node.floatValue(graph: graph) == 880.0)
}
@Test func multipleUpdatesRetainLatestValue() {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let node: UIParamNode = UIParamNode(0.0)
node.update(1.0)
node.update(2.0)
node.update(3.0)
#expect(node.floatValue(graph: graph) == 3.0)
#expect(node.value == 3.0)
}
@Test func zeroRoundTrip() {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let node: UIParamNode = UIParamNode(1.0)
node.update(0.0)
#expect(node.floatValue(graph: graph) == 0.0)
}
@Test func negativeValueRoundTrip() {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let node: UIParamNode = UIParamNode(0.0)
node.update(-1.5)
#expect(node.floatValue(graph: graph) == -1.5)
}
}
// MARK: - calculatedSamples interpolation
@Suite("UIParamNode calculatedSamples") struct UIParamNodeCalcTests {
@Test func flatBlockWhenValueUnchanged() throws {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let blockCount: Int = 4
let node: UIParamNode = UIParamNode(1.0)
let result: [[Double]] = node.calculatedSamples(graph: graph, count: blockCount)
#expect(result.count == 1)
// Buffer is pre-allocated to lyrebirdDefaultResultBufferSize — never resized.
// Only the first blockCount elements are meaningful.
#expect(result[0].count >= blockCount)
// No change — step = 0 — the first blockCount samples should all equal 1.0
for sample: Double in result[0].prefix(blockCount) {
#expect(sample == 1.0)
}
}
@Test func rampReachesTargetAtLastSample() throws {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let blockCount: Int = 4
let node: UIParamNode = UIParamNode(0.0)
node.update(4.0)
let result: [[Double]] = node.calculatedSamples(graph: graph, count: blockCount)
// The meaningful range is [0..<blockCount]; index blockCount-1 is the last ramp sample.
#expect(result[0][blockCount - 1] == 4.0)
}
@Test func rampIsLinear() throws {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let node: UIParamNode = UIParamNode(0.0)
node.update(4.0)
let result: [[Double]] = node.calculatedSamples(graph: graph, count: 4)
// Ramp from 0→4 in 4 steps: expected [1.0, 2.0, 3.0, 4.0] at indices 0–3
#expect(result[0][0] == 1.0)
#expect(result[0][1] == 2.0)
#expect(result[0][2] == 3.0)
#expect(result[0][3] == 4.0)
}
@Test func subsequentBlockIsFlatAtTarget() throws {
let engine: SynthesisEngine = makeUITestEngine()
let blockCount: Int = 4
let node: UIParamNode = UIParamNode(0.0)
node.update(2.0)
// Block 1 — ramp from 0 to 2.0.
let graph1: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
_ = node.calculatedSamples(graph: graph1, count: blockCount)
// Advance to the next segment so the fan-out cache is invalidated.
advanceUIBlock(engine)
// Block 2 — value unchanged, should be flat at 2.0.
let graph2: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let second: [[Double]] = node.calculatedSamples(graph: graph2, count: blockCount)
// Only the first blockCount elements are meaningful after this call.
for sample: Double in second[0].prefix(blockCount) {
#expect(sample == 2.0)
}
}
@Test func sampleBlockMatchesFirstChannel() throws {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
// Use separate nodes with identical initial state so each call starts fresh.
let node2D: UIParamNode = UIParamNode(0.0)
node2D.update(4.0)
let via2D: [Double] = node2D.calculatedSamples(graph: graph, count: 4).first ?? []
let node1D: UIParamNode = UIParamNode(0.0)
node1D.update(4.0)
let via1D: [Double] = node1D.sampleBlock(graph: graph, count: 4, previousValue: 0.0)
#expect(via2D == via1D)
}
@Test func bufferStaysAtPreAllocatedSize() throws {
let engine: SynthesisEngine = makeUITestEngine()
let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let node: UIParamNode = UIParamNode(1.0)
let first: [[Double]] = node.calculatedSamples(graph: graph, count: 8)
let second: [[Double]] = node.calculatedSamples(graph: graph, count: 8)
// Buffer is fixed at lyrebirdDefaultResultBufferSize — never resized regardless of count.
#expect(first[0].count == lyrebirdDefaultResultBufferSize)
#expect(second[0].count == lyrebirdDefaultResultBufferSize)
}
}
// MARK: - ValidGeneratorInput wiring
@Suite("UIParamNode as ValidGeneratorInput") struct UIParamNodeWiringTests {
@Test func usableAsSourceParameter() throws {
let engine: SynthesisEngine = makeUITestEngine()
try engine.start()
let node: UIParamNode = UIParamNode(0.5)
class ParamSynth: SynthesizerBase {
let node: UIParamNode
init(node: UIParamNode) { self.node = node; super.init() }
override func build() {
addOutputGenerator(Out.Audio(bus: 0, source: node))
}
}
let synth: ParamSynth = ParamSynth(node: node)
try engine.add(synth)
engine.processBlock(inputSamples: [])
let bus0: [Float] = engine.audioBuses[0]
// Out accumulates, so bus 0 should be non-zero
let anyNonZero: Bool = bus0.contains { $0 != 0.0 }
#expect(anyNonZero)
}
@Test func updateReflectsInNextBlock() throws {
let engine: SynthesisEngine = makeUITestEngine()
try engine.start()
let node: UIParamNode = UIParamNode(0.0)
class ParamSynth: SynthesizerBase {
let node: UIParamNode
init(node: UIParamNode) { self.node = node; super.init() }
override func build() {
addOutputGenerator(Out.Audio(bus: 0, source: node))
}
}
let synth: ParamSynth = ParamSynth(node: node)
try engine.add(synth)
engine.processBlock(inputSamples: [])
// Bus 0 should be zero (node value was 0)
let allZero: Bool = engine.audioBuses[0].allSatisfy { $0 == 0.0 }
#expect(allZero)
// Update to 1.0 — next block ramps from 0 to 1.0; last sample reaches target.
node.update(1.0)
engine.processBlock(inputSamples: [])
let lastSample: Float = engine.audioBuses[0][7]
#expect(abs(lastSample - 1.0) < 1e-6)
}
}
// MARK: - Source-swap
@Suite("UIParamNode source swap") struct UIParamNodeSourceSwapTests {
@Test func sourceAssignmentRoutesThroughCalculatedSamples() {
// Verifies that UIParamNode.calculatedSamples forwards to
// _node.calculatedSamples — actually drains the source queue and
// renders the source's output, not the scalar ramp.
let engine: SynthesisEngine = makeUITestEngine()
let blockCount: Int = 8
let node: UIParamNode = UIParamNode(100.0)
node.update(100.0)
// Warm-up block to settle the scalar ramp at 100.
let graph0: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
_ = node.calculatedSamples(graph: graph0, count: blockCount)
advanceUIBlock(engine)
// Install a constant-value source generator that outputs 770.
node.source = ConstantTestGenerator(770.0)
// Next block should reflect the source's output, not the scalar 100.
let graph1: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let result: [[Double]] = node.calculatedSamples(graph: graph1, count: blockCount)
// The source is a control-rate constant — calculatedSamples returns
// a one-row buffer containing the constant value.
#expect(result[0][blockCount - 1] == 770.0,
"UIParamNode.source must drive output via _node.calculatedSamples")
}
@Test func sourceNilRevertsToScalarRamp() {
let engine: SynthesisEngine = makeUITestEngine()
let blockCount: Int = 8
let node: UIParamNode = UIParamNode(100.0)
node.update(100.0)
let graph0: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
_ = node.calculatedSamples(graph: graph0, count: blockCount)
advanceUIBlock(engine)
node.source = ConstantTestGenerator(770.0)
let graph1: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
_ = node.calculatedSamples(graph: graph1, count: blockCount)
advanceUIBlock(engine)
// Revert. After enough blocks the scalar ramp should resettle at 100.
node.source = nil
for _ in 0..<8 {
let g: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
_ = node.calculatedSamples(graph: g, count: blockCount)
advanceUIBlock(engine)
}
let gFinal: SynthGraph = SynthGraph(engine: engine, busOffset: 0)
let final: [[Double]] = node.calculatedSamples(graph: gFinal, count: blockCount)
#expect(abs(final[0][blockCount - 1] - 100.0) < 1e-9,
"UIParamNode.source = nil must revert to scalar")
}
}
/// Minimal ControlGenerator emitting a constant value, used to test that
/// `UIParamNode.source` actually routes through `_node.calculatedSamples`
/// and not the scalar ramp.
private final class ConstantTestGenerator: GeneratorBase, ControlGenerator, @unchecked Sendable {
private let _value: Double
private var _buf: [[Double]] = [[0.0]]
var previousOutputValue: Double
init(_ value: Double) {
_value = value
previousOutputValue = value
_buf = [[Double](repeating: value, count: lyrebirdDefaultResultBufferSize)]
}
func floatValue(graph: SynthGraph) -> Double { _value }
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
return _buf
}
}
// MARK: - Test helpers
/// Advances the fan-out cache serial by one, simulating a block boundary.
/// Mirror of `advanceBlock` in `LyrebirdTests/TestHelpers.swift` for use in UI tests.
private func advanceUIBlock(_ engine: SynthesisEngine) {
engine.advanceSegmentSerial()
}
private func makeUITestEngine() -> SynthesisEngine {
SynthesisEngine(
blockSize: 8,
sampleRate: 44100,
numOutputChannels: 2,
numInputChannels: 0,
numAudioBuses: 8,
numControlBuses: 8,
driver: OfflineDriver()
)
}