516 lines
22 KiB
Swift
516 lines
22 KiB
Swift
// 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..<count { _result[0][i] = outputValue }
|
|
return _result
|
|
}
|
|
|
|
func floatValue(graph: SynthGraph) -> 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..<count { _result[0][i] = outputValue }
|
|
return _result
|
|
}
|
|
|
|
func floatValue(graph: SynthGraph) -> 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
|
|
|
|
@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..<totalBlocks {
|
|
let samples: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
let nw: Double = expectedNewWeight(blockIndex: blockIndex, totalBlocks: totalBlocks)
|
|
let expected: Double = nw * 1000.0 // old is 0, new is 1000
|
|
for (i, s) in samples.enumerated() {
|
|
let msg: String = "Crossfade block \(blockIndex) sample \(i): expected \(expected) (nw=\(nw)), got \(s)"
|
|
#expect(abs(s - expected) < 1e-9, Comment(rawValue: msg))
|
|
}
|
|
}
|
|
|
|
// Block 4: purely new source.
|
|
let pureNewBlock: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
for (i, s) in pureNewBlock.enumerated() {
|
|
#expect(
|
|
abs(s - 1000.0) < 1e-10,
|
|
"Post-crossfade block sample \(i): expected 1000.0, got \(s)"
|
|
)
|
|
}
|
|
|
|
_ = engine.drainParameterSourceKillQueue()
|
|
}
|
|
|
|
// MARK: - 3. Old source released exactly after crossfade completes, not before
|
|
|
|
@Test func oldSourceReleasedExactlyAfterCrossfadeCompletes() {
|
|
let blockSize: Int = 8
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
let node: ParameterNode = ParameterNode(0.0)
|
|
let sentinel: CrossfadeDeinitSentinel = CrossfadeDeinitSentinel()
|
|
|
|
let totalCrossfadeBlocks: UInt16 = 4
|
|
|
|
do {
|
|
let oldSrc: SentinelCrossfadeSource = SentinelCrossfadeSource(10.0, sentinel: sentinel)
|
|
let newSrc: ConstantCrossfadeSource = ConstantCrossfadeSource(20.0)
|
|
|
|
// Install old source.
|
|
node.source = oldSrc
|
|
_ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
|
|
// Start crossfade — old source stays alive as _crossfadeOldSlot.
|
|
node.setSource(newSrc, crossfadeBlocks: totalCrossfadeBlocks)
|
|
|
|
// Render N-1 crossfade blocks — old source must NOT be killed yet.
|
|
for blockIndex: Int in 0..<(Int(totalCrossfadeBlocks) - 1) {
|
|
_ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
let drainedEarly: Int = engine.drainParameterSourceKillQueue()
|
|
let earlyMsg: String = "Block \(blockIndex): old source must not be on kill queue yet (crossfade in progress), drained \(drainedEarly)"
|
|
#expect(drainedEarly == 0, Comment(rawValue: earlyMsg))
|
|
#expect(
|
|
!sentinel.released,
|
|
"Block \(blockIndex): old source must not be deinit'd yet"
|
|
)
|
|
}
|
|
// `oldSrc` local reference drops here; sentinel still alive via _crossfadeOldSlot.
|
|
}
|
|
// Local `oldSrc` is gone; the crossfade still holds the reference.
|
|
#expect(!sentinel.released, "Sentinel must not fire before crossfade completes")
|
|
|
|
// Render the final (Nth) crossfade block — after this the old source is killed.
|
|
_ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
|
|
// Kill queue must have exactly 1 entry (the old source).
|
|
let drainedAfter: Int = engine.drainParameterSourceKillQueue()
|
|
#expect(
|
|
drainedAfter == 1,
|
|
"Kill queue must have exactly 1 item after crossfade completes, got \(drainedAfter)"
|
|
)
|
|
#expect(sentinel.released, "Old source must be deinit'd after kill-queue drain")
|
|
|
|
// Post-crossfade render — purely new source.
|
|
_ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
_ = engine.drainParameterSourceKillQueue()
|
|
}
|
|
|
|
// MARK: - 4. Hard swap (crossfadeBlocks=0) still hard-swaps
|
|
|
|
@Test func hardSwapCrossfadeZeroStillHardSwaps() {
|
|
let blockSize: Int = 8
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
let node: ParameterNode = ParameterNode(0.0)
|
|
|
|
let oldSrc: ConstantCrossfadeSource = ConstantCrossfadeSource(5.0)
|
|
let newSrc: ConstantCrossfadeSource = ConstantCrossfadeSource(99.0)
|
|
|
|
// Install old source.
|
|
node.source = oldSrc
|
|
_ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
|
|
// Hard swap (crossfadeBlocks=0).
|
|
node.setSource(newSrc, crossfadeBlocks: 0)
|
|
let swapBlock: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
|
|
// All samples must be purely 99.0 — no crossfade mix.
|
|
for (i, s) in swapBlock.enumerated() {
|
|
#expect(
|
|
abs(s - 99.0) < 1e-10,
|
|
"Hard-swap sample \(i): expected 99.0, got \(s)"
|
|
)
|
|
}
|
|
|
|
_ = engine.drainParameterSourceKillQueue()
|
|
}
|
|
|
|
// MARK: - 5. Mid-crossfade swap kills in-progress old slot and starts new crossfade
|
|
|
|
@Test func midCrossfadeSwapKillsInProgressOldSlot() {
|
|
// Start crossfade A→B with 8 blocks.
|
|
// After 3 blocks, issue B→C with 4 blocks.
|
|
// Verify: A was killed at block 3 (when B→C arrived), B-to-C runs 4 blocks, C is only slot after 7 total blocks.
|
|
|
|
let blockSize: Int = 8
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
let node: ParameterNode = ParameterNode(0.0)
|
|
|
|
let sentinelA: CrossfadeDeinitSentinel = CrossfadeDeinitSentinel()
|
|
let srcB: ConstantCrossfadeSource = ConstantCrossfadeSource(200.0)
|
|
let srcC: ConstantCrossfadeSource = ConstantCrossfadeSource(300.0)
|
|
|
|
do {
|
|
let srcA: SentinelCrossfadeSource = SentinelCrossfadeSource(100.0, sentinel: sentinelA)
|
|
|
|
// Install A (hard swap).
|
|
node.source = srcA
|
|
_ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
|
|
// Start A→B crossfade, 8 blocks.
|
|
node.setSource(srcB, crossfadeBlocks: 8)
|
|
|
|
// Render 3 crossfade blocks (indices 0, 1, 2).
|
|
for _ in 0..<3 {
|
|
_ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
}
|
|
}
|
|
// srcA local ref dropped — kill queue holds the final retain after B→C swap.
|
|
|
|
// A is still alive (crossfade in progress, not yet killed — still in _crossfadeOldSlot).
|
|
#expect(!sentinelA.released, "A must still be alive after 3 blocks into A→B crossfade")
|
|
_ = engine.drainParameterSourceKillQueue() // should be empty
|
|
|
|
// Issue B→C crossfade, 4 blocks. This kills A immediately (in-progress crossfade terminated).
|
|
node.setSource(srcC, crossfadeBlocks: 4)
|
|
_ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize) // block 3: drain + first B→C block
|
|
|
|
// A must be on kill queue now (killed when B→C arrived).
|
|
let drainedA: Int = engine.drainParameterSourceKillQueue()
|
|
#expect(drainedA == 1, "A must be killed when B→C crossfade starts, got \(drainedA)")
|
|
#expect(sentinelA.released, "A sentinel must fire after kill-queue drain")
|
|
|
|
// Render remaining 3 B→C blocks (blocks 1, 2, 3 of the 4-block fade).
|
|
for blockIndex: Int in 1..<4 {
|
|
let samples: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
let nw: Double = expectedNewWeight(blockIndex: blockIndex, totalBlocks: 4)
|
|
// old=B (200), new=C (300)
|
|
let expected: Double = (1.0 - nw) * 200.0 + nw * 300.0
|
|
for (i, s) in samples.enumerated() {
|
|
#expect(
|
|
abs(s - expected) < 1e-9,
|
|
"B→C block \(blockIndex) sample \(i): expected \(expected), got \(s)"
|
|
)
|
|
}
|
|
}
|
|
|
|
// After 4 B→C blocks, C should be the only slot.
|
|
_ = engine.drainParameterSourceKillQueue() // drains B
|
|
let pureC: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
for (i, s) in pureC.enumerated() {
|
|
#expect(abs(s - 300.0) < 1e-10, "Pure C block sample \(i): expected 300.0, got \(s)")
|
|
}
|
|
|
|
_ = engine.drainParameterSourceKillQueue()
|
|
}
|
|
|
|
// MARK: - 6. Crossfade from scalar → generator
|
|
|
|
@Test func crossfadeFromScalarToGenerator() {
|
|
// Scalar at 440.0, crossfade to generator outputting 880.0, 4 blocks.
|
|
// The "old" is the scalar ramp (previousOutputValue=440, target=440, step=0).
|
|
// Each block: old=440, new=880; mix = (1-nw)*440 + nw*880.
|
|
|
|
let blockSize: Int = 8
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
let node: ParameterNode = ParameterNode(440.0)
|
|
|
|
let newSrc: ConstantCrossfadeSource = ConstantCrossfadeSource(880.0)
|
|
let totalBlocks: Int = 4
|
|
|
|
// No source installed yet — scalar path is active.
|
|
// Issue crossfade from scalar → generator.
|
|
node.setSource(newSrc, crossfadeBlocks: UInt16(totalBlocks))
|
|
|
|
for blockIndex: Int in 0..<totalBlocks {
|
|
let samples: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
let nw: Double = expectedNewWeight(blockIndex: blockIndex, totalBlocks: totalBlocks)
|
|
// old = 440 (scalar), new = 880
|
|
let expected: Double = (1.0 - nw) * 440.0 + nw * 880.0
|
|
for (i, s) in samples.enumerated() {
|
|
let scalarGenMsg: String = "Scalar→gen crossfade block \(blockIndex) sample \(i): expected \(expected) (nw=\(nw)), got \(s)"
|
|
#expect(abs(s - expected) < 1e-9, Comment(rawValue: scalarGenMsg))
|
|
}
|
|
}
|
|
|
|
// After 4 blocks: purely 880.
|
|
let pureNew: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
for (i, s) in pureNew.enumerated() {
|
|
#expect(abs(s - 880.0) < 1e-10, "Post-crossfade sample \(i): expected 880.0, got \(s)")
|
|
}
|
|
|
|
_ = engine.drainParameterSourceKillQueue()
|
|
}
|
|
|
|
// MARK: - 7. Crossfade from generator → nil (revert to scalar)
|
|
|
|
@Test func crossfadeFromGeneratorToScalar() {
|
|
// Generator outputting 880. Scalar target = 440.
|
|
// Revert to scalar with 4-block crossfade.
|
|
// Old = generator (880), new = scalar ramp (880→440 over each block).
|
|
// However: previousOutputValue starts at 880 (last generator output).
|
|
// Scalar ramp per block: origin = 880 (updated each block), target = 440.
|
|
// block 0: ramp 880→440 over blockSize? No — previousOutputValue is updated
|
|
// from blended output each block. The scalar ramp per block is:
|
|
// prev = previousOutputValue (starts at 880, driven toward 440 each block),
|
|
// step = (440 - prev) / count.
|
|
// For simplicity, verify that the blended output starts near 880 (first block mostly old)
|
|
// and ends near 440 (last block mostly new). We verify approximate monotonic decrease.
|
|
|
|
let blockSize: Int = 8
|
|
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
|
let node: ParameterNode = ParameterNode(440.0) // scalar target = 440
|
|
|
|
let srcGen: ConstantCrossfadeSource = ConstantCrossfadeSource(880.0)
|
|
let totalBlocks: Int = 4
|
|
|
|
// Install generator first.
|
|
node.source = srcGen
|
|
_ = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
|
|
// Revert to scalar with crossfade.
|
|
node.setSource(nil, crossfadeBlocks: UInt16(totalBlocks))
|
|
|
|
var lastBlockMean: Double = 880.0
|
|
for blockIndex: Int in 0..<totalBlocks {
|
|
let samples: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
let mean: Double = samples.reduce(0.0, +) / Double(samples.count)
|
|
// Block 0: heavily weighted toward old (880). Block 3: heavily toward new (scalar → 440).
|
|
// Verify monotonic decrease.
|
|
let monoMsg: String = "Gen→scalar crossfade block \(blockIndex): mean (\(mean)) should not exceed previous (\(lastBlockMean))"
|
|
#expect(mean <= lastBlockMean + 1.0, Comment(rawValue: monoMsg))
|
|
lastBlockMean = mean
|
|
}
|
|
|
|
// After crossfade: scalar path. The ramp converges to 440 by the last sample
|
|
// of the block (scalar ramp: prev + step*(i+1), where step*(blockSize) = target - prev).
|
|
let scalarBlock: [Double] = renderCrossfadeBlock(node, engine: engine, blockSize: blockSize)
|
|
// The last sample of a scalar ramp block always equals the target (440.0).
|
|
#expect(
|
|
abs(scalarBlock[blockSize - 1] - 440.0) < 1e-10,
|
|
"Post-crossfade last sample must converge to scalar target 440.0, got \(scalarBlock[blockSize - 1])"
|
|
)
|
|
// All samples must be between 440 and the ramp origin (≤ previous block mean).
|
|
for (i, s) in scalarBlock.enumerated() {
|
|
#expect(
|
|
s >= 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)")
|
|
}
|
|
}
|
|
}
|