1451 lines
60 KiB
Swift
1451 lines
60 KiB
Swift
// ControlGeneratorTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Foundation
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
// MARK: - OscSin.Control Tests
|
||
|
||
@MainActor
|
||
@Suite("OscSin.Control")
|
||
struct OscSinControlTests {
|
||
let blockSize: Int = 64
|
||
let sampleRate: Double = 44100.0
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(OscSin.Control(freq: 1.0).rate == .control)
|
||
}
|
||
|
||
@Test func outputCountMatchesRequest() {
|
||
let osc: OscSin.Control = OscSin.Control(freq: 1.0)
|
||
let samples: [[Double]] = osc.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(samples.count == 1)
|
||
#expect(samples[0].count >= blockSize)
|
||
}
|
||
|
||
@Test func outputIsRampFromZeroToTarget() {
|
||
// First block: previousOutputValue == 0, target is the sine at the new phase.
|
||
let osc: OscSin.Control = OscSin.Control(freq: 1.0)
|
||
let samples: [Double] = osc.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
// Ramp reaches target at last sample; first sample must be closer to 0 than last.
|
||
let first: Double = samples[0]
|
||
let last: Double = samples[blockSize - 1]
|
||
// Both samples should be small (1 Hz LFO after one block at 44100/64 rate).
|
||
#expect(abs(first) < abs(last) || (first == 0.0 && last == 0.0))
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let osc: OscSin.Control = OscSin.Control(freq: 440.0)
|
||
let samples: [Double] = osc.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let lastSample: Double = samples[blockSize - 1]
|
||
#expect(osc.previousOutputValue == lastSample)
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let osc: OscSin.Control = OscSin.Control(freq: 1.0)
|
||
// Before any evaluation, previousOutputValue == 0.
|
||
#expect(osc.floatValue(graph: graph) == 0.0)
|
||
// Evaluate one block.
|
||
_ = osc.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = osc.previousOutputValue
|
||
// floatValue should return the same stored value without changing it.
|
||
#expect(osc.floatValue(graph: graph) == stored)
|
||
#expect(osc.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let osc: OscSin.Control = OscSin.Control(freq: 1.0)
|
||
let a: [[Double]] = osc.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = osc.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
// previousOutputValue must equal the last sample of the cached block.
|
||
#expect(osc.previousOutputValue == a[0][blockSize - 1])
|
||
}
|
||
|
||
@Test func phaseAccumulatesAcrossBlocks() {
|
||
// At 440 Hz, OscSin.Control should cover significantly different phase positions
|
||
// across successive blocks.
|
||
let osc: OscSin.Control = OscSin.Control(freq: 440.0)
|
||
let g: SynthGraph = graph
|
||
var prevLast: Double = 0.0
|
||
var allSame: Bool = true
|
||
for _ in 0..<10 {
|
||
let samples: [Double] = osc.calculatedSamples(graph: g, count: blockSize)[0]
|
||
let last: Double = samples[blockSize - 1]
|
||
if last != prevLast { allSame = false }
|
||
prevLast = last
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(!allSame, "Oscillator output should vary across blocks")
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
let osc: OscSin.Control = OscSin.Control(freq: 100.0)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = osc.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func deterministicOutput() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 44100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let oscA: OscSin.Control = OscSin.Control(freq: 100.0)
|
||
let oscB: OscSin.Control = OscSin.Control(freq: 100.0)
|
||
for _ in 0..<5 {
|
||
let a: [Double] = oscA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = oscB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(Array(a.prefix(blockSize)) == Array(b.prefix(blockSize)))
|
||
advanceBlock(engine)
|
||
advanceBlock(engine2)
|
||
}
|
||
}
|
||
|
||
@Test func interpolationModesProduceDifferentOutputForSmallTable() {
|
||
let smallTable: LyrebirdBuffer = LyrebirdBuffer(count: 64)
|
||
smallTable.fillSine()
|
||
let oscNone: OscSin.Control = OscSin.Control(freq: 100.0, interpolation: .none, table: smallTable)
|
||
let oscLinear: OscSin.Control = OscSin.Control(freq: 100.0, interpolation: .linear, table: smallTable)
|
||
let oscCubic: OscSin.Control = OscSin.Control(freq: 100.0, interpolation: .cubic, table: smallTable)
|
||
let sNone: [Double] = oscNone.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let sLinear: [Double] = oscLinear.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let sCubic: [Double] = oscCubic.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
// All modes should produce non-zero output.
|
||
#expect(sNone.prefix(blockSize).contains { $0 != 0.0 })
|
||
#expect(sLinear.prefix(blockSize).contains { $0 != 0.0 })
|
||
#expect(sCubic.prefix(blockSize).contains { $0 != 0.0 })
|
||
}
|
||
}
|
||
|
||
// MARK: - OscTableControl Tests
|
||
|
||
@MainActor
|
||
@Suite("OscTableControl")
|
||
struct OscTableControlTests {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
let table: LyrebirdBuffer = LyrebirdBuffer(count: 512)
|
||
table.fillSine()
|
||
#expect(OscTable.Control(table: table, freq: 1.0).rate == .control)
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let table: LyrebirdBuffer = LyrebirdBuffer(count: 512)
|
||
table.fillSine()
|
||
let osc: OscTable.Control = OscTable.Control(table: table, freq: 100.0)
|
||
let samples: [Double] = osc.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(osc.previousOutputValue == samples[blockSize - 1])
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let table: LyrebirdBuffer = LyrebirdBuffer(count: 512)
|
||
table.fillSine()
|
||
let osc: OscTable.Control = OscTable.Control(table: table, freq: 1.0)
|
||
#expect(osc.floatValue(graph: graph) == 0.0)
|
||
_ = osc.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = osc.previousOutputValue
|
||
#expect(osc.floatValue(graph: graph) == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let table: LyrebirdBuffer = LyrebirdBuffer(count: 512)
|
||
table.fillSine()
|
||
let osc: OscTable.Control = OscTable.Control(table: table, freq: 1.0)
|
||
let a: [[Double]] = osc.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = osc.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func invalidTableOutputsSilence() {
|
||
let table: LyrebirdBuffer = LyrebirdBuffer(count: 512)
|
||
table.fillSine()
|
||
let osc: OscTable.Control = OscTable.Control(table: table, freq: 100.0)
|
||
table.invalidate()
|
||
let samples: [Double] = osc.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
// Target becomes 0; ramp ends at 0.
|
||
#expect(samples[blockSize - 1] == 0.0)
|
||
}
|
||
|
||
@Test func deterministicOutput() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 44100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let table: LyrebirdBuffer = LyrebirdBuffer(count: 512)
|
||
table.fillSine()
|
||
let oscA: OscTable.Control = OscTable.Control(table: table, freq: 100.0)
|
||
let oscB: OscTable.Control = OscTable.Control(table: table, freq: 100.0)
|
||
for _ in 0..<5 {
|
||
let a: [Double] = oscA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = oscB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(Array(a.prefix(blockSize)) == Array(b.prefix(blockSize)))
|
||
advanceBlock(engine)
|
||
advanceBlock(engine2)
|
||
}
|
||
}
|
||
|
||
@Test func nonPowerOfTwoTableWorks() {
|
||
let table: LyrebirdBuffer = LyrebirdBuffer(count: 300)
|
||
table.fill { i, n in sin(2.0 * Double.pi * Double(i) / Double(n)) }
|
||
let osc: OscTable.Control = OscTable.Control(table: table, freq: 1.0)
|
||
let samples: [Double] = osc.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(samples.count >= blockSize)
|
||
}
|
||
}
|
||
|
||
// MARK: - ImpulseControl Tests
|
||
|
||
@MainActor
|
||
@Suite("ImpulseControl")
|
||
struct ImpulseControlTests {
|
||
// Use blockSize == sampleRate so 1 Hz fires exactly once per block.
|
||
let blockSize: Int = 100
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 100, sampleRate: 100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(Impulse.Control(freq: 1.0).rate == .control)
|
||
}
|
||
|
||
@Test func firesOnFirstBlock() {
|
||
// initPhase == 0 → fires on first block (phase starts at 1.0, crosses immediately).
|
||
let imp: Impulse.Control = Impulse.Control(freq: 1.0, initPhase: 0.0)
|
||
let samples: [Double] = imp.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
// previousOutputValue == 1.0 after first block means it fired.
|
||
#expect(imp.previousOutputValue == 1.0)
|
||
// Ramp ends at 1.0; last sample equals 1.0.
|
||
#expect(samples[blockSize - 1] == 1.0)
|
||
}
|
||
|
||
@Test func doesNotFireTwiceInOneBlock() {
|
||
// At 1 Hz with blockSize == sampleRate, exactly one fire per block.
|
||
let imp: Impulse.Control = Impulse.Control(freq: 1.0, initPhase: 0.0)
|
||
_ = imp.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let samples: [Double] = imp.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
// Second block: fires once, ramp from 1.0 → 1.0 (stays hot).
|
||
#expect(imp.previousOutputValue == 1.0)
|
||
#expect(samples[blockSize - 1] == 1.0)
|
||
}
|
||
|
||
@Test func doesNotFireWhenFreqIsZero() {
|
||
let imp: Impulse.Control = Impulse.Control(freq: 0.0)
|
||
_ = imp.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
_ = imp.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(imp.previousOutputValue == 0.0)
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let imp: Impulse.Control = Impulse.Control(freq: 1.0)
|
||
#expect(imp.floatValue(graph: graph) == 0.0)
|
||
_ = imp.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = imp.previousOutputValue
|
||
#expect(imp.floatValue(graph: graph) == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let imp: Impulse.Control = Impulse.Control(freq: 1.0)
|
||
let a: [[Double]] = imp.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = imp.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputCountMatchesRequest() {
|
||
let imp: Impulse.Control = Impulse.Control(freq: 1.0)
|
||
let samples: [[Double]] = imp.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(samples[0].count >= blockSize)
|
||
}
|
||
|
||
@Test func deterministicOutput() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let impA: Impulse.Control = Impulse.Control(freq: 1.0, initPhase: 0.0)
|
||
let impB: Impulse.Control = Impulse.Control(freq: 1.0, initPhase: 0.0)
|
||
for _ in 0..<5 {
|
||
let a: [Double] = impA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = impB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(Array(a.prefix(blockSize)) == Array(b.prefix(blockSize)))
|
||
advanceBlock(engine)
|
||
advanceBlock(engine2)
|
||
}
|
||
}
|
||
|
||
@Test func highFreqPhaseWrapIsStable() {
|
||
// freq=3.0 → phase increment = 3.0 per block (> 2.0); single subtraction would leave phase ≥ 1.0
|
||
let imp: Impulse.Control = Impulse.Control(freq: 3.0, initPhase: 0.0)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = imp.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(!s.isNaN)
|
||
#expect(s >= 0.0 && s <= 1.0)
|
||
}
|
||
#expect(imp.previousOutputValue == 0.0 || imp.previousOutputValue == 1.0)
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func firesOncePerBlockAtHighFreq() {
|
||
// Design contract: ImpulseControl fires at most once per block regardless of freq
|
||
// (single `if phase >= 1.0` check with truncatingRemainder, no while-loop).
|
||
// At freq=3.0 Hz with blockSize=sampleRate=100, increment=3.0 per block — always
|
||
// crosses 1.0, so every block fires exactly once.
|
||
let imp: Impulse.Control = Impulse.Control(freq: 3.0, initPhase: 0.0)
|
||
var fireCount: Int = 0
|
||
let blockCount: Int = 10
|
||
for _ in 0..<blockCount {
|
||
_ = imp.calculatedSamples(graph: graph, count: blockSize)
|
||
if imp.previousOutputValue == 1.0 { fireCount += 1 }
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(fireCount == blockCount, "Every block must fire exactly once at freq=3 Hz")
|
||
}
|
||
}
|
||
|
||
// MARK: - WhiteNoiseControl Tests
|
||
|
||
@MainActor
|
||
@Suite("WhiteNoiseControl")
|
||
struct WhiteNoiseControlTests {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(WhiteNoise.Control().rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
let noise: WhiteNoise.Control = WhiteNoise.Control(seed: 42)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let noise: WhiteNoise.Control = WhiteNoise.Control(seed: 1)
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(noise.previousOutputValue == samples[blockSize - 1])
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let noise: WhiteNoise.Control = WhiteNoise.Control(seed: 2)
|
||
#expect(noise.floatValue(graph: graph) == 0.0)
|
||
_ = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = noise.previousOutputValue
|
||
#expect(noise.floatValue(graph: graph) == stored)
|
||
#expect(noise.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let noise: WhiteNoise.Control = WhiteNoise.Control(seed: 3)
|
||
let a: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputVariesAcrossBlocks() {
|
||
let noise: WhiteNoise.Control = WhiteNoise.Control(seed: 4)
|
||
var values: Set<Double> = Set<Double>()
|
||
for _ in 0..<10 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
values.insert(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(values.count > 1, "PRNG must produce distinct values across blocks")
|
||
}
|
||
|
||
@Test func deterministicWithSameSeed() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let noiseA: WhiteNoise.Control = WhiteNoise.Control(seed: 99)
|
||
let noiseB: WhiteNoise.Control = WhiteNoise.Control(seed: 99)
|
||
let a: [Double] = noiseA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = noiseB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(a == b)
|
||
}
|
||
}
|
||
|
||
// MARK: - LFStepNoiseControl Tests
|
||
|
||
@MainActor
|
||
@Suite("LFStepNoiseControl")
|
||
struct LFStepNoiseControlTests {
|
||
// blockSize == sampleRate so 1 Hz fires exactly once per block.
|
||
let blockSize: Int = 100
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 100, sampleRate: 100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(LFStepNoise.Control(freq: 1.0).rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
let noise: LFStepNoise.Control = LFStepNoise.Control(freq: 2.0, seed: 10)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func stepsAtCorrectRate() {
|
||
// At 1 Hz with blockSize=sampleRate=100, value should change every block.
|
||
let noise: LFStepNoise.Control = LFStepNoise.Control(freq: 1.0, seed: 20)
|
||
var targets: [Double] = [Double]()
|
||
for _ in 0..<5 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
targets.append(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
// With 5 blocks at 1 Hz each, we expect distinct values (PRNG produces varied output).
|
||
let unique: Set<Double> = Set(targets)
|
||
#expect(unique.count > 1)
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let noise: LFStepNoise.Control = LFStepNoise.Control(freq: 2.0, seed: 30)
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
// previousOutputValue = target; samples[last] = rampStart + step*count — may differ by 1 ULP
|
||
#expect(abs(noise.previousOutputValue - samples[blockSize - 1]) < 1e-9)
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let noise: LFStepNoise.Control = LFStepNoise.Control(freq: 1.0, seed: 40)
|
||
_ = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = noise.previousOutputValue
|
||
#expect(noise.floatValue(graph: graph) == stored)
|
||
#expect(noise.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let noise: LFStepNoise.Control = LFStepNoise.Control(freq: 1.0, seed: 50)
|
||
let a: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func zeroFreqHoldsValue() {
|
||
let noise: LFStepNoise.Control = LFStepNoise.Control(freq: 0.0, seed: 60)
|
||
let s1: Double = noise.calculatedSamples(graph: graph, count: blockSize)[0][blockSize - 1]
|
||
advanceBlock(engine)
|
||
let s2: Double = noise.calculatedSamples(graph: graph, count: blockSize)[0][blockSize - 1]
|
||
#expect(s1 == s2, "Zero freq should hold value indefinitely")
|
||
}
|
||
|
||
@Test func deterministicWithSameSeed() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let noiseA: LFStepNoise.Control = LFStepNoise.Control(freq: 2.0, seed: 999)
|
||
let noiseB: LFStepNoise.Control = LFStepNoise.Control(freq: 2.0, seed: 999)
|
||
for _ in 0..<5 {
|
||
let a: [Double] = noiseA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = noiseB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(Array(a.prefix(blockSize)) == Array(b.prefix(blockSize)))
|
||
advanceBlock(engine)
|
||
advanceBlock(engine2)
|
||
}
|
||
}
|
||
|
||
@Test func highFreqPhaseWrapIsStable() {
|
||
// freq=3.0 → phase increment = 3.0 per block (> 2.0); if-not-while would skip PRNG steps
|
||
let noise: LFStepNoise.Control = LFStepNoise.Control(freq: 3.0, seed: 777)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(!s.isNaN)
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func negativeFreqHoldsValue() {
|
||
// Negative freq must not drift phase; previousOutputValue must freeze at the initial target.
|
||
let noise: LFStepNoise.Control = LFStepNoise.Control(freq: -1.0, seed: 555)
|
||
_ = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
let frozen: Double = noise.previousOutputValue // exact target, no ramp rounding
|
||
for _ in 0..<10 {
|
||
advanceBlock(engine)
|
||
_ = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(noise.previousOutputValue == frozen,
|
||
"Negative freq must hold the initial value indefinitely")
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - LFLineNoiseControl Tests
|
||
|
||
@MainActor
|
||
@Suite("LFLineNoiseControl")
|
||
struct LFLineNoiseControlTests {
|
||
let blockSize: Int = 100
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 100, sampleRate: 100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(LFLineNoise.Control(freq: 1.0).rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
let noise: LFLineNoise.Control = LFLineNoise.Control(freq: 2.0, seed: 70)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let noise: LFLineNoise.Control = LFLineNoise.Control(freq: 2.0, seed: 80)
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(noise.previousOutputValue == samples[blockSize - 1])
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let noise: LFLineNoise.Control = LFLineNoise.Control(freq: 1.0, seed: 90)
|
||
_ = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = noise.previousOutputValue
|
||
#expect(noise.floatValue(graph: graph) == stored)
|
||
#expect(noise.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let noise: LFLineNoise.Control = LFLineNoise.Control(freq: 1.0, seed: 100)
|
||
let a: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputVariesAcrossBlocks() {
|
||
let noise: LFLineNoise.Control = LFLineNoise.Control(freq: 2.0, seed: 110)
|
||
var targets: Set<Double> = Set<Double>()
|
||
for _ in 0..<10 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
targets.insert(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(targets.count > 1)
|
||
}
|
||
|
||
@Test func deterministicWithSameSeed() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let noiseA: LFLineNoise.Control = LFLineNoise.Control(freq: 2.0, seed: 888)
|
||
let noiseB: LFLineNoise.Control = LFLineNoise.Control(freq: 2.0, seed: 888)
|
||
for _ in 0..<5 {
|
||
let a: [Double] = noiseA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = noiseB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(Array(a.prefix(blockSize)) == Array(b.prefix(blockSize)))
|
||
advanceBlock(engine)
|
||
advanceBlock(engine2)
|
||
}
|
||
}
|
||
|
||
@Test func outputIsSmoothAcrossBlockBoundary() {
|
||
// Block B's ramp uses block A's last value as its anchor (previousOutputValue).
|
||
let noise: LFLineNoise.Control = LFLineNoise.Control(freq: 0.5, seed: 120)
|
||
let blockA: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let endOfA: Double = blockA[blockSize - 1]
|
||
#expect(noise.previousOutputValue == endOfA)
|
||
advanceBlock(engine)
|
||
let blockB: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
// Block B ramp: result[i] = endOfA + step * (i+1), so result[0] = endOfA + step.
|
||
let stepB: Double = (blockB[blockSize - 1] - endOfA) / Double(blockSize)
|
||
#expect(abs(blockB[0] - (endOfA + stepB)) < 1e-9)
|
||
}
|
||
|
||
@Test func negativeFreqOutputInRange() {
|
||
// Negative freq must not cause extrapolation outside [-1, 1].
|
||
let noise: LFLineNoise.Control = LFLineNoise.Control(freq: -1.0, seed: 666)
|
||
for _ in 0..<10 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(!s.isNaN)
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9,
|
||
"Negative freq must not extrapolate outside [-1, 1]; got \(s)")
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - Counting ControlGenerator for sub-block tests
|
||
|
||
/// Counts _computeSamples calls. Returns a constant 0.5 every call.
|
||
private final class CountingControl: GeneratorBase, ControlGenerator {
|
||
var previousOutputValue: Double = 0.0
|
||
private(set) var computeCallCount: Int = 0
|
||
|
||
init() {
|
||
super.init()
|
||
for i: Int in 0..<_result[0].count { _result[0][i] = 0.5 }
|
||
_setupSubBlockBuffer()
|
||
}
|
||
|
||
func floatValue(graph: SynthGraph) -> Double { 0.5 }
|
||
|
||
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
|
||
computeCallCount += 1
|
||
previousOutputValue = 0.5
|
||
return _result
|
||
}
|
||
}
|
||
|
||
// MARK: - Sub-block control tests
|
||
|
||
@MainActor
|
||
@Suite("ControlGenerator sub-block")
|
||
struct ControlGeneratorSubBlockTests {
|
||
|
||
/// In sub-block mode, _computeSamples is called blockSize/controlBlockSize times per block.
|
||
/// In full-block mode it is called once.
|
||
@Test func subBlockCallsComputeCorrectly() throws {
|
||
let blockSize: Int = 64
|
||
let cbs: Int = 8
|
||
|
||
let subEngine: SynthesisEngine = SynthesisEngine(
|
||
blockSize: blockSize, controlBlockSize: cbs,
|
||
sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0,
|
||
driver: OfflineDriver()
|
||
)
|
||
let fullEngine: SynthesisEngine = SynthesisEngine(
|
||
blockSize: blockSize,
|
||
sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0,
|
||
driver: OfflineDriver()
|
||
)
|
||
try subEngine.start()
|
||
try fullEngine.start()
|
||
|
||
let subGen: CountingControl = CountingControl()
|
||
let fullGen: CountingControl = CountingControl()
|
||
|
||
_ = subGen.calculatedSamples(graph: makeSynthGraph(engine: subEngine), count: blockSize)
|
||
_ = fullGen.calculatedSamples(graph: makeSynthGraph(engine: fullEngine), count: blockSize)
|
||
|
||
#expect(subGen.computeCallCount == blockSize / cbs, "_computeSamples should be called once per sub-block")
|
||
#expect(fullGen.computeCallCount == 1, "full-block mode calls _computeSamples once")
|
||
}
|
||
|
||
/// Sub-block assembly: output buffer must be filled with each sub-block's contribution
|
||
/// in the correct position. CountingControl returns 0.5 every call with previousOutputValue
|
||
/// already set to 0.5, so all sub-blocks produce all-0.5 samples — buffer should be 0.5 everywhere.
|
||
@Test func subBlockAssemblyFillsBuffer() throws {
|
||
let blockSize: Int = 64
|
||
let cbs: Int = 16
|
||
|
||
let engine: SynthesisEngine = SynthesisEngine(
|
||
blockSize: blockSize, controlBlockSize: cbs,
|
||
sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0,
|
||
driver: OfflineDriver()
|
||
)
|
||
try engine.start()
|
||
|
||
let gen: CountingControl = CountingControl()
|
||
// Prime previousOutputValue so ramp is flat (prev == target == 0.5).
|
||
gen.previousOutputValue = 0.5
|
||
|
||
let result: [[Double]] = gen.calculatedSamples(graph: makeSynthGraph(engine: engine), count: blockSize)
|
||
#expect(result.count == 1)
|
||
for i: Int in 0..<blockSize {
|
||
#expect(abs(result[0][i] - 0.5) < 1e-9,
|
||
"Sample \(i) should be 0.5, got \(result[0][i])")
|
||
}
|
||
}
|
||
|
||
/// Fan-out cache: second calculatedSamples call in same segment must be a cache hit,
|
||
/// so _computeSamples is called exactly once regardless of how many fans read the output.
|
||
@Test func fanOutCacheInSubBlockMode() throws {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine = SynthesisEngine(
|
||
blockSize: blockSize, controlBlockSize: 16,
|
||
sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0,
|
||
driver: OfflineDriver()
|
||
)
|
||
try engine.start()
|
||
let gen: CountingControl = CountingControl()
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
|
||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(gen.computeCallCount == blockSize / 16,
|
||
"second call in same segment must be a cache hit — _computeSamples called \(gen.computeCallCount) times, expected \(blockSize / 16)")
|
||
}
|
||
|
||
/// NRTDriver.suggestedControlBlockSize must be 1 (sample-accurate offline rendering).
|
||
@Test func nrtDriverSuggestsControlBlockSizeOne() throws {
|
||
let tmpURL: URL = URL(fileURLWithPath: NSTemporaryDirectory())
|
||
.appendingPathComponent("nrt_cbs_test.aiff")
|
||
let nrtDriver: NRTDriver = NRTDriver(outputURL: tmpURL, duration: 0.1)
|
||
let nrtEngine: SynthesisEngine = SynthesisEngine(
|
||
blockSize: 512, sampleRate: 44100,
|
||
numOutputChannels: 2, numInputChannels: 0,
|
||
driver: nrtDriver
|
||
)
|
||
#expect(nrtEngine.controlBlockSize == 1)
|
||
}
|
||
|
||
/// OfflineDriver.suggestedControlBlockSize is nil → engine uses full blockSize.
|
||
@Test func offlineDriverUsesFullBlock() throws {
|
||
let engine: SynthesisEngine = SynthesisEngine(
|
||
blockSize: 64, sampleRate: 44100,
|
||
numOutputChannels: 2, numInputChannels: 0,
|
||
driver: OfflineDriver()
|
||
)
|
||
try engine.start()
|
||
#expect(engine.controlBlockSize == 64)
|
||
}
|
||
|
||
/// Explicit controlBlockSize overrides driver suggestion.
|
||
@Test func explicitControlBlockSizeIsRespected() throws {
|
||
let engine: SynthesisEngine = SynthesisEngine(
|
||
blockSize: 512, controlBlockSize: 32,
|
||
sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0,
|
||
driver: OfflineDriver()
|
||
)
|
||
try engine.start()
|
||
#expect(engine.controlBlockSize == 32)
|
||
}
|
||
|
||
/// In.Control(channels: 2) must produce correct output for both channels in sub-block mode.
|
||
@Test func multiChannelInControlSubBlock() throws {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine = SynthesisEngine(
|
||
blockSize: blockSize, controlBlockSize: 16,
|
||
sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0,
|
||
driver: OfflineDriver()
|
||
)
|
||
try engine.start()
|
||
|
||
// Write distinct values to two consecutive control buses.
|
||
engine.controlBuses[0] = Float(0.25)
|
||
engine.controlBuses[1] = Float(0.75)
|
||
|
||
let gen: In.Control = In.Control(bus: 0, channels: 2)
|
||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||
let result: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||
|
||
#expect(result.count == 2, "In.Control(channels:2) must return 2 channels")
|
||
// Last sample of each ramp reaches the bus value (ramp convention).
|
||
#expect(abs(result[0][blockSize - 1] - 0.25) < 1e-6,
|
||
"Channel 0 last sample must reach bus value 0.25")
|
||
#expect(abs(result[1][blockSize - 1] - 0.75) < 1e-6,
|
||
"Channel 1 last sample must reach bus value 0.75; got \(result[1][blockSize - 1])")
|
||
}
|
||
|
||
// In sub-block mode OscSin.Control produces shorter ramps (more frequent updates) than
|
||
// full-block mode — the intermediate samples differ intentionally. The meaningful invariant
|
||
// is that both modes reach the same final target at the last sample of the block: total
|
||
// phase advance is blockSize samples worth regardless of sub-block partitioning, so the
|
||
// target sin value at block end is identical. previousOutputValue must also match.
|
||
@Test func oscSinControlSubBlockSameFinalTargetAsFullBlock() throws {
|
||
let blockSize: Int = 64
|
||
let cbs: Int = 16
|
||
let sampleRate: Double = 44100.0
|
||
|
||
let engineFull: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: sampleRate)
|
||
try engineFull.start()
|
||
let graphFull: SynthGraph = makeSynthGraph(engine: engineFull)
|
||
|
||
let engineSub: SynthesisEngine = SynthesisEngine(
|
||
blockSize: blockSize,
|
||
controlBlockSize: cbs,
|
||
sampleRate: sampleRate,
|
||
numOutputChannels: 2, numInputChannels: 0,
|
||
driver: OfflineDriver()
|
||
)
|
||
try engineSub.start()
|
||
let graphSub: SynthGraph = makeSynthGraph(engine: engineSub)
|
||
|
||
let freq: Double = 440.0
|
||
let oscFull: OscSin.Control = OscSin.Control(freq: freq)
|
||
let oscSub: OscSin.Control = OscSin.Control(freq: freq)
|
||
|
||
let full: [[Double]] = oscFull.calculatedSamples(graph: graphFull, count: blockSize)
|
||
let sub: [[Double]] = oscSub.calculatedSamples(graph: graphSub, count: blockSize)
|
||
|
||
// Last sample of both modes must match: same total phase advance → same target.
|
||
#expect(abs(full[0][blockSize - 1] - sub[0][blockSize - 1]) < 1e-9)
|
||
// previousOutputValue must also be identical after one block.
|
||
#expect(abs(oscFull.previousOutputValue - oscSub.previousOutputValue) < 1e-9)
|
||
}
|
||
}
|
||
|
||
// MARK: - PinkNoiseControl Tests
|
||
|
||
@MainActor
|
||
@Suite("PinkNoiseControl")
|
||
struct PinkNoiseControlTests {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(PinkNoise.Control().rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
let noise: PinkNoise.Control = PinkNoise.Control(seed: 42)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
// Pink noise can exceed ±1 slightly due to IIR ringing; allow ±2.
|
||
#expect(s >= -2.0 && s <= 2.0)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let noise: PinkNoise.Control = PinkNoise.Control(seed: 1)
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(noise.previousOutputValue == samples[blockSize - 1])
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let noise: PinkNoise.Control = PinkNoise.Control(seed: 2)
|
||
#expect(noise.floatValue(graph: graph) == 0.0)
|
||
_ = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = noise.previousOutputValue
|
||
#expect(noise.floatValue(graph: graph) == stored)
|
||
#expect(noise.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let noise: PinkNoise.Control = PinkNoise.Control(seed: 3)
|
||
let a: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputVariesAcrossBlocks() {
|
||
let noise: PinkNoise.Control = PinkNoise.Control(seed: 4)
|
||
var values: Set<Double> = Set<Double>()
|
||
for _ in 0..<10 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
values.insert(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(values.count > 1, "PinkNoise.Control must produce distinct values across blocks")
|
||
}
|
||
|
||
@Test func deterministicWithSameSeed() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let noiseA: PinkNoise.Control = PinkNoise.Control(seed: 99)
|
||
let noiseB: PinkNoise.Control = PinkNoise.Control(seed: 99)
|
||
let a: [Double] = noiseA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = noiseB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(a == b)
|
||
}
|
||
}
|
||
|
||
// MARK: - BrownNoiseControl Tests
|
||
|
||
@MainActor
|
||
@Suite("BrownNoiseControl")
|
||
struct BrownNoiseControlTests {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(BrownNoise.Control().rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
let noise: BrownNoise.Control = BrownNoise.Control(seed: 42)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let noise: BrownNoise.Control = BrownNoise.Control(seed: 1)
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(noise.previousOutputValue == samples[blockSize - 1])
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let noise: BrownNoise.Control = BrownNoise.Control(seed: 2)
|
||
#expect(noise.floatValue(graph: graph) == 0.0)
|
||
_ = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = noise.previousOutputValue
|
||
#expect(noise.floatValue(graph: graph) == stored)
|
||
#expect(noise.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let noise: BrownNoise.Control = BrownNoise.Control(seed: 3)
|
||
let a: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputVariesAcrossBlocks() {
|
||
let noise: BrownNoise.Control = BrownNoise.Control(seed: 4)
|
||
var values: Set<Double> = Set<Double>()
|
||
for _ in 0..<10 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
values.insert(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(values.count > 1, "BrownNoise.Control must produce distinct values across blocks")
|
||
}
|
||
|
||
@Test func deterministicWithSameSeed() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let noiseA: BrownNoise.Control = BrownNoise.Control(seed: 99)
|
||
let noiseB: BrownNoise.Control = BrownNoise.Control(seed: 99)
|
||
let a: [Double] = noiseA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = noiseB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(a == b)
|
||
}
|
||
}
|
||
|
||
// MARK: - GrayNoiseControl Tests
|
||
|
||
@MainActor
|
||
@Suite("GrayNoiseControl")
|
||
struct GrayNoiseControlTests {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(GrayNoise.Control().rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
let noise: GrayNoise.Control = GrayNoise.Control(seed: 42)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let noise: GrayNoise.Control = GrayNoise.Control(seed: 1)
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(noise.previousOutputValue == samples[blockSize - 1])
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let noise: GrayNoise.Control = GrayNoise.Control(seed: 2)
|
||
#expect(noise.floatValue(graph: graph) == 0.0)
|
||
_ = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = noise.previousOutputValue
|
||
#expect(noise.floatValue(graph: graph) == stored)
|
||
#expect(noise.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let noise: GrayNoise.Control = GrayNoise.Control(seed: 3)
|
||
let a: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputVariesAcrossBlocks() {
|
||
let noise: GrayNoise.Control = GrayNoise.Control(seed: 4)
|
||
var values: Set<Double> = Set<Double>()
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
values.insert(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(values.count > 1, "GrayNoise.Control must produce distinct values across blocks")
|
||
}
|
||
|
||
@Test func deterministicWithSameSeed() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let noiseA: GrayNoise.Control = GrayNoise.Control(seed: 99)
|
||
let noiseB: GrayNoise.Control = GrayNoise.Control(seed: 99)
|
||
let a: [Double] = noiseA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = noiseB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(a == b)
|
||
}
|
||
}
|
||
|
||
// MARK: - ClipNoiseControl Tests
|
||
|
||
@MainActor
|
||
@Suite("ClipNoiseControl")
|
||
struct ClipNoiseControlTests {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(ClipNoise.Control().rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
// Each sample in the ramp must be within [-1, 1] — interpolating between -1 and +1.
|
||
let noise: ClipNoise.Control = ClipNoise.Control(seed: 42)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let noise: ClipNoise.Control = ClipNoise.Control(seed: 1)
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(noise.previousOutputValue == samples[blockSize - 1])
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let noise: ClipNoise.Control = ClipNoise.Control(seed: 2)
|
||
#expect(noise.floatValue(graph: graph) == 0.0)
|
||
_ = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = noise.previousOutputValue
|
||
#expect(noise.floatValue(graph: graph) == stored)
|
||
#expect(noise.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let noise: ClipNoise.Control = ClipNoise.Control(seed: 3)
|
||
let a: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputVariesAcrossBlocks() {
|
||
let noise: ClipNoise.Control = ClipNoise.Control(seed: 4)
|
||
var values: Set<Double> = Set<Double>()
|
||
for _ in 0..<10 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
values.insert(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(values.count > 1, "ClipNoise.Control must produce distinct values across blocks")
|
||
}
|
||
|
||
@Test func deterministicWithSameSeed() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let noiseA: ClipNoise.Control = ClipNoise.Control(seed: 99)
|
||
let noiseB: ClipNoise.Control = ClipNoise.Control(seed: 99)
|
||
let a: [Double] = noiseA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = noiseB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(a == b)
|
||
}
|
||
}
|
||
|
||
// MARK: - DustControl Tests
|
||
|
||
@MainActor
|
||
@Suite("DustControl")
|
||
struct DustControlTests {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(Dust.Control(density: 10.0).rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
// Ramp interpolates between 0.0 (no fire) or a value in (0,1) (fire).
|
||
let dust: Dust.Control = Dust.Control(density: 100.0, seed: 42)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = dust.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(s >= 0.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let dust: Dust.Control = Dust.Control(density: 1000.0, seed: 1)
|
||
let samples: [Double] = dust.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(dust.previousOutputValue == samples[blockSize - 1])
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let dust: Dust.Control = Dust.Control(density: 100.0, seed: 2)
|
||
#expect(dust.floatValue(graph: graph) == 0.0)
|
||
_ = dust.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = dust.previousOutputValue
|
||
#expect(dust.floatValue(graph: graph) == stored)
|
||
#expect(dust.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let dust: Dust.Control = Dust.Control(density: 100.0, seed: 3)
|
||
let a: [[Double]] = dust.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = dust.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputVariesAcrossBlocks() {
|
||
let dust: Dust.Control = Dust.Control(density: 2000.0, seed: 4)
|
||
var values: Set<Double> = Set<Double>()
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = dust.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
values.insert(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
// With high density, we expect both zero blocks and firing blocks.
|
||
#expect(values.count > 1, "Dust.Control must produce varying values across blocks")
|
||
}
|
||
|
||
@Test func deterministicWithSameSeed() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let dustA: Dust.Control = Dust.Control(density: 500.0, seed: 99)
|
||
let dustB: Dust.Control = Dust.Control(density: 500.0, seed: 99)
|
||
let a: [Double] = dustA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = dustB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(a == b)
|
||
}
|
||
}
|
||
|
||
// MARK: - Dust2Control Tests
|
||
|
||
@MainActor
|
||
@Suite("Dust2Control")
|
||
struct Dust2ControlTests {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(Dust2.Control(density: 10.0).rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
// Ramp interpolates toward 0 or a value in (-1, 1).
|
||
let dust2: Dust2.Control = Dust2.Control(density: 100.0, seed: 42)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = dust2.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let dust2: Dust2.Control = Dust2.Control(density: 1000.0, seed: 1)
|
||
let samples: [Double] = dust2.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(dust2.previousOutputValue == samples[blockSize - 1])
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let dust2: Dust2.Control = Dust2.Control(density: 100.0, seed: 2)
|
||
#expect(dust2.floatValue(graph: graph) == 0.0)
|
||
_ = dust2.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = dust2.previousOutputValue
|
||
#expect(dust2.floatValue(graph: graph) == stored)
|
||
#expect(dust2.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let dust2: Dust2.Control = Dust2.Control(density: 100.0, seed: 3)
|
||
let a: [[Double]] = dust2.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = dust2.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputVariesAcrossBlocks() {
|
||
let dust2: Dust2.Control = Dust2.Control(density: 2000.0, seed: 4)
|
||
var values: Set<Double> = Set<Double>()
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = dust2.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
values.insert(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(values.count > 1, "Dust2.Control must produce varying values across blocks")
|
||
}
|
||
|
||
@Test func deterministicWithSameSeed() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let dustA: Dust2.Control = Dust2.Control(density: 500.0, seed: 99)
|
||
let dustB: Dust2.Control = Dust2.Control(density: 500.0, seed: 99)
|
||
let a: [Double] = dustA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = dustB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(a == b)
|
||
}
|
||
}
|
||
|
||
|
||
// MARK: - CrackleControl Tests
|
||
|
||
@MainActor
|
||
@Suite("CrackleControl")
|
||
struct CrackleControlTests {
|
||
let blockSize: Int = 64
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(Crackle.Control().rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
// Crackle output is bounded in [-1, 1]; ramp stays in range.
|
||
let crackle: Crackle.Control = Crackle.Control(chaos: 1.5)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = crackle.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(s >= -1.0 - 1e-9 && s <= 1.0 + 1e-9)
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let crackle: Crackle.Control = Crackle.Control(chaos: 1.5)
|
||
let samples: [Double] = crackle.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(abs(crackle.previousOutputValue - samples[blockSize - 1]) < 1e-9)
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let crackle: Crackle.Control = Crackle.Control(chaos: 1.5)
|
||
// Before first block, previousOutputValue == 0.
|
||
#expect(crackle.floatValue(graph: graph) == 0.0)
|
||
_ = crackle.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = crackle.previousOutputValue
|
||
#expect(crackle.floatValue(graph: graph) == stored)
|
||
// Calling floatValue does not advance state.
|
||
#expect(crackle.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let crackle: Crackle.Control = Crackle.Control(chaos: 1.5)
|
||
let a: [[Double]] = crackle.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = crackle.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputVariesAcrossBlocks() {
|
||
// The chaotic map produces different outputs on successive blocks.
|
||
let crackle: Crackle.Control = Crackle.Control(chaos: 1.5)
|
||
var targets: Set<Double> = Set<Double>()
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = crackle.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
targets.insert(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(targets.count > 1, "Crackle.Control must produce distinct values across blocks")
|
||
}
|
||
|
||
@Test func deterministicWithSameInitialState() {
|
||
// Two Crackle.Control instances share the same deterministic initial state,
|
||
// so they produce identical output.
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||
do { try engine2.start() } catch { return }
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let crackleA: Crackle.Control = Crackle.Control(chaos: 1.5)
|
||
let crackleB: Crackle.Control = Crackle.Control(chaos: 1.5)
|
||
let a: [Double] = crackleA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = crackleB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(Array(a.prefix(blockSize)) == Array(b.prefix(blockSize)))
|
||
}
|
||
}
|
||
|
||
// MARK: - LFCurveNoiseControl Tests
|
||
|
||
@MainActor
|
||
@Suite("LFCurveNoiseControl")
|
||
struct LFCurveNoiseControlTests {
|
||
let blockSize: Int = 100
|
||
let engine: SynthesisEngine
|
||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||
|
||
init() throws {
|
||
engine = makeTestEngine(blockSize: 100, sampleRate: 100)
|
||
try engine.start()
|
||
}
|
||
|
||
@Test func rateIsControl() {
|
||
#expect(LFCurveNoise.Control(freq: 1.0).rate == .control)
|
||
}
|
||
|
||
@Test func outputInValidRange() {
|
||
// Parabolic interpolation can overshoot by ~1.25×; ramp stays within that.
|
||
let overshootTolerance: Double = 1.25
|
||
let noise: LFCurveNoise.Control = LFCurveNoise.Control(freq: 2.0, seed: 70)
|
||
for _ in 0..<20 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
for s: Double in samples.prefix(blockSize) {
|
||
#expect(abs(s) <= overshootTolerance + 1e-9,
|
||
"LFCurveNoise.Control sample \(s) exceeds overshoot tolerance ±\(overshootTolerance)")
|
||
}
|
||
advanceBlock(engine)
|
||
}
|
||
}
|
||
|
||
@Test func previousOutputValueUpdatedAfterBlock() {
|
||
let noise: LFCurveNoise.Control = LFCurveNoise.Control(freq: 2.0, seed: 80)
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
#expect(noise.previousOutputValue == samples[blockSize - 1])
|
||
}
|
||
|
||
@Test func floatValueReturnsPreviousOutputWithoutAdvancing() {
|
||
let noise: LFCurveNoise.Control = LFCurveNoise.Control(freq: 1.0, seed: 90)
|
||
_ = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
advanceBlock(engine)
|
||
let stored: Double = noise.previousOutputValue
|
||
#expect(noise.floatValue(graph: graph) == stored)
|
||
#expect(noise.previousOutputValue == stored)
|
||
}
|
||
|
||
@Test func fanOutCacheSingleEvaluationPerSegment() {
|
||
let noise: LFCurveNoise.Control = LFCurveNoise.Control(freq: 1.0, seed: 100)
|
||
let a: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
let b: [[Double]] = noise.calculatedSamples(graph: graph, count: blockSize)
|
||
#expect(a[0] == b[0])
|
||
}
|
||
|
||
@Test func outputVariesAcrossBlocks() {
|
||
let noise: LFCurveNoise.Control = LFCurveNoise.Control(freq: 2.0, seed: 110)
|
||
var targets: Set<Double> = Set<Double>()
|
||
for _ in 0..<10 {
|
||
let samples: [Double] = noise.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
targets.insert(samples[blockSize - 1])
|
||
advanceBlock(engine)
|
||
}
|
||
#expect(targets.count > 1)
|
||
}
|
||
|
||
@Test func deterministicWithSameSeed() throws {
|
||
let engine2: SynthesisEngine = makeTestEngine(blockSize: blockSize, sampleRate: 100)
|
||
try engine2.start()
|
||
let g2: SynthGraph = makeSynthGraph(engine: engine2)
|
||
let noiseA: LFCurveNoise.Control = LFCurveNoise.Control(freq: 2.0, seed: 888)
|
||
let noiseB: LFCurveNoise.Control = LFCurveNoise.Control(freq: 2.0, seed: 888)
|
||
for _ in 0..<5 {
|
||
let a: [Double] = noiseA.calculatedSamples(graph: graph, count: blockSize)[0]
|
||
let b: [Double] = noiseB.calculatedSamples(graph: g2, count: blockSize)[0]
|
||
#expect(Array(a.prefix(blockSize)) == Array(b.prefix(blockSize)))
|
||
advanceBlock(engine)
|
||
advanceBlock(engine2)
|
||
}
|
||
}
|
||
}
|