// ParameterNodeCrossfadeTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // MARK: - Private test helpers /// Constant-output `ControlGenerator` for crossfade tests. private final class ConstantCrossfadeSource: GeneratorBase, ControlGenerator, @unchecked Sendable { let outputValue: Double var previousOutputValue: Double init(_ value: Double) { outputValue = value previousOutputValue = value super.init() _setupSubBlockBuffer() } func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] { _checkCount(count) for i: Int in 0.. Double { outputValue } } /// Deinit sentinel for ARC tracking in crossfade tests. private final class CrossfadeDeinitSentinel { var released: Bool = false } /// Source generator that sets `sentinel.released = true` on deinit. private final class SentinelCrossfadeSource: GeneratorBase, ControlGenerator, @unchecked Sendable { let outputValue: Double var previousOutputValue: Double private let _sentinel: CrossfadeDeinitSentinel init(_ value: Double, sentinel: CrossfadeDeinitSentinel) { outputValue = value previousOutputValue = value _sentinel = sentinel super.init() _setupSubBlockBuffer() } deinit { _sentinel.released = true } func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] { _checkCount(count) for i: Int in 0.. Double { outputValue } } // MARK: - Helpers /// Render one block and return the first-channel samples (prefix count). private func renderCrossfadeBlock( _ node: ParameterNode, engine: SynthesisEngine, blockSize: Int ) -> [Double] { advanceBlock(engine) let graph: SynthGraph = makeSynthGraph(engine: engine) let result: [[Double]] = node.calculatedSamples(graph: graph, count: blockSize) return Array(result[0].prefix(blockSize)) } /// Mid-block-snapshot mix fraction for block `blockIndex` (0-based) in a crossfade of /// `totalBlocks` blocks. Returns the NEW weight. /// /// Convention: newWeight = (total - remaining + 0.5) / total /// where remaining = total - blockIndex (counts down from total at block 0). /// Therefore: newWeight = (blockIndex + 0.5) / total private func expectedNewWeight(blockIndex: Int, totalBlocks: Int) -> Double { (Double(blockIndex) + 0.5) / Double(totalBlocks) } // MARK: - Test suite @MainActor @Suite("ParameterNode crossfade") struct ParameterNodeCrossfadeTests { // MARK: - 1. crossfadeBlocks=1 produces a 50% mix @Test func crossfadeOneBlockProduces50PercentMix() { // Old source: constant 100.0. New source: constant 200.0. // crossfadeBlocks=1 → single block at 50% new weight. // Expected: all samples = 0.5*100 + 0.5*200 = 150.0 let blockSize: Int = 8 let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) let node: ParameterNode = ParameterNode(0.0) let oldSrc: ConstantCrossfadeSource = ConstantCrossfadeSource(100.0) let newSrc: ConstantCrossfadeSource = ConstantCrossfadeSource(200.0) // Install old source (hard swap — no crossfade). node.source = oldSrc _ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize) // Now crossfade to new source over 1 block. node.setSource(newSrc, crossfadeBlocks: 1) let crossfadeBlock: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize) let expectedMix: Double = 0.5 * 100.0 + 0.5 * 200.0 // 150.0 for (i, s) in crossfadeBlock.enumerated() { #expect( abs(s - expectedMix) < 1e-10, "Block 0 (crossfadeBlocks=1) sample \(i): expected \(expectedMix), got \(s)" ) } // Block after crossfade: purely new source (200.0). let afterBlock: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize) for (i, s) in afterBlock.enumerated() { #expect( abs(s - 200.0) < 1e-10, "Post-crossfade sample \(i): expected 200.0, got \(s)" ) } _ = engine.drainParameterSourceKillQueue() } // MARK: - 2. crossfadeBlocks=4 produces correctly ramped mix across 4 blocks @Test func crossfadeFourBlocksProducesRampedMix() { // Old: constant 0.0. New: constant 1000.0. // crossfadeBlocks=4 → newWeights at blocks 0..3: // block 0: (0 + 0.5) / 4 = 0.125 → 0.125 * 1000 = 125.0 // block 1: (1 + 0.5) / 4 = 0.375 → 0.375 * 1000 = 375.0 // block 2: (2 + 0.5) / 4 = 0.625 → 0.625 * 1000 = 625.0 // block 3: (3 + 0.5) / 4 = 0.875 → 0.875 * 1000 = 875.0 // Block 4 (after crossfade): 1000.0 let blockSize: Int = 8 let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) let node: ParameterNode = ParameterNode(0.0) let oldSrc: ConstantCrossfadeSource = ConstantCrossfadeSource(0.0) let newSrc: ConstantCrossfadeSource = ConstantCrossfadeSource(1000.0) // Install old source first. node.source = oldSrc _ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize) // Start crossfade. node.setSource(newSrc, crossfadeBlocks: 4) let totalBlocks: Int = 4 for blockIndex: Int in 0..= 440.0 - 1e-10, "Post-crossfade revert sample \(i): must be ≥ scalar target 440.0, got \(s)" ) #expect( s <= lastBlockMean + 1e-10, "Post-crossfade revert sample \(i): must be ≤ last crossfade mean (\(lastBlockMean)), got \(s)" ) } // Kill queue: srcGen should be killed when crossfade completes. let drained: Int = engine.drainParameterSourceKillQueue() #expect(drained >= 1, "srcGen must be killed after revert crossfade, got \(drained)") } // MARK: - 8. parameterNodeMaxCrossfadeBlocks constant value @Test func maxCrossfadeBlocksConstantIsCorrect() { // Verify the constant is 1024. #expect(parameterNodeMaxCrossfadeBlocks == 1024) } // MARK: - 9. Crossfade of 1024 blocks is accepted (at the boundary) @Test func maxCrossfadeBlocksIsAccepted() { // setSource with crossfadeBlocks = 1024 must not trap. let node: ParameterNode = ParameterNode(0.0) let src: ConstantCrossfadeSource = ConstantCrossfadeSource(1.0) // Should not trap — 1024 == parameterNodeMaxCrossfadeBlocks. node.setSource(src, crossfadeBlocks: 1024) // No further assertion needed — not trapping is the assertion. } // MARK: - 10. Crossfade source-slot path fan-out cache still honoured during crossfade @Test func fanOutCacheHonouredDuringCrossfade() { // Two consumers evaluate the same ParameterNode during a crossfade block. // Both must get the cached (blended) result. let blockSize: Int = 8 let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) let node: ParameterNode = ParameterNode(0.0) let oldSrc: ConstantCrossfadeSource = ConstantCrossfadeSource(100.0) let newSrc: ConstantCrossfadeSource = ConstantCrossfadeSource(200.0) node.source = oldSrc _ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize) // Start crossfade. node.setSource(newSrc, crossfadeBlocks: 4) // Advance block serial but use the same graph for two consumer calls. advanceBlock(engine) let graph: SynthGraph = makeSynthGraph(engine: engine) let result1: [[Double]] = node.calculatedSamples(graph: graph, count: blockSize) let result2: [[Double]] = node.calculatedSamples(graph: graph, count: blockSize) // Both must return the same blended value (cache hit for second). let expectedMix: Double = expectedNewWeight(blockIndex: 0, totalBlocks: 4) * 200.0 + (1.0 - expectedNewWeight(blockIndex: 0, totalBlocks: 4)) * 100.0 #expect( abs(result1[0][0] - expectedMix) < 1e-9, "First consumer: expected \(expectedMix) (blended), got \(result1[0][0])" ) #expect( abs(result2[0][0] - result1[0][0]) < 1e-12, "Fan-out cache: second consumer must match first, got \(result2[0][0])" ) _ = engine.drainParameterSourceKillQueue() } // MARK: - 11. Scalar path still works after failed crossfade (both nil, hard swap) @Test func scalarPathUnchangedWithNoCrossfade() { // Scalar path should be unaffected when no crossfade is configured. let blockSize: Int = 8 let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize) let node: ParameterNode = ParameterNode(0.0) node.value = 1.0 advanceBlock(engine) let graph: SynthGraph = makeSynthGraph(engine: engine) let samples: [Double] = node.calculatedSamples(graph: graph, count: blockSize)[0] // Expected: ramp 0→1 over 8 samples: [0.125, 0.25, ..., 1.0] let expected: [Double] = (1...8).map { Double($0) / 8.0 } for (i, (s, e)) in zip(samples.prefix(8), expected).enumerated() { #expect(abs(s - e) < 1e-12, "Scalar ramp sample \(i): expected \(e), got \(s)") } } }