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

339 lines
15 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.
// DemandGeneratorTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
// MARK: - DemandSequence
@Suite("DemandSequence")
struct DSequenceTests {
@Test func returnsValuesInOrder() {
let seq: DemandSequence = DemandSequence(values: [1.0, 2.0, 3.0])
#expect(seq.nextValue() == 1.0)
#expect(seq.nextValue() == 2.0)
#expect(seq.nextValue() == 3.0)
}
@Test func wrapsAfterFullCycle() {
let seq: DemandSequence = DemandSequence(values: [10.0, 20.0])
_ = seq.nextValue(); _ = seq.nextValue() // complete cycle 1
#expect(seq.nextValue() == 10.0) // cycle 2 starts
}
@Test func infiniteRepeatsNeverExhausts() {
let seq: DemandSequence = DemandSequence(values: [1.0, 2.0], repeats: -1)
for _ in 0..<100 { _ = seq.nextValue() }
#expect(!seq.isExhausted)
#expect(seq.nextValue() != nil)
}
@Test func exhaustsAfterNRepeats() {
let seq: DemandSequence = DemandSequence(values: [1.0, 2.0, 3.0], repeats: 2)
var count: Int = 0
while let _ = seq.nextValue() { count += 1 }
#expect(count == 6) // 3 values × 2 repeats
#expect(seq.isExhausted)
#expect(seq.nextValue() == nil)
}
@Test func zeroRepeatsExhaustedImmediately() {
let seq: DemandSequence = DemandSequence(values: [1.0, 2.0], repeats: 0)
#expect(seq.isExhausted)
#expect(seq.nextValue() == nil)
}
@Test func resetRestartsFromBeginning() {
let seq: DemandSequence = DemandSequence(values: [1.0, 2.0], repeats: 1)
_ = seq.nextValue(); _ = seq.nextValue() // exhaust
#expect(seq.isExhausted)
seq.reset()
#expect(!seq.isExhausted)
#expect(seq.nextValue() == 1.0)
}
@Test func emptyValuesAlwaysNil() {
let seq: DemandSequence = DemandSequence(values: [])
#expect(seq.nextValue() == nil)
#expect(!seq.isExhausted) // empty ≠ exhausted
}
}
// MARK: - DemandRandom
@Suite("DemandRandom")
struct DRandomTests {
@Test func returnsValuesFromPool() {
let pool: [Double] = [10.0, 20.0, 30.0, 40.0]
let rand: DemandRandom = DemandRandom(values: pool, repeats: 20)
for _ in 0..<20 {
guard let v: Double = rand.nextValue() else { Issue.record("unexpectedly nil"); return }
#expect(pool.contains(v))
}
}
@Test func infiniteRepeatsNeverExhausts() {
let rand: DemandRandom = DemandRandom(values: [1.0, 2.0, 3.0], repeats: -1)
for _ in 0..<100 { _ = rand.nextValue() }
#expect(!rand.isExhausted)
#expect(rand.nextValue() != nil)
}
@Test func exhaustsAfterNResponses() {
let rand: DemandRandom = DemandRandom(values: [1.0, 2.0, 3.0], repeats: 5)
var count: Int = 0
while let _ = rand.nextValue() { count += 1 }
#expect(count == 5)
#expect(rand.isExhausted)
}
@Test func resetRestartsResponseCount() {
let rand: DemandRandom = DemandRandom(values: [1.0, 2.0], repeats: 3)
for _ in 0..<3 { _ = rand.nextValue() }
#expect(rand.isExhausted)
rand.reset()
#expect(!rand.isExhausted)
var count: Int = 0
while let _ = rand.nextValue() { count += 1 }
#expect(count == 3)
}
@Test func deterministicWithSameSeed() {
let seed: UInt64 = 12345
let r1: DemandRandom = DemandRandom(values: [1.0, 2.0, 3.0, 4.0, 5.0], repeats: 10, seed: seed)
let r2: DemandRandom = DemandRandom(values: [1.0, 2.0, 3.0, 4.0, 5.0], repeats: 10, seed: seed)
for _ in 0..<10 {
#expect(r1.nextValue() == r2.nextValue())
}
}
}
// MARK: - Demand.Audio
@Suite("Demand.Audio")
struct DemandAudioTests {
@Test func rateIsAudio() {
let d: Demand.Audio = Demand.Audio(trig: 0.0, demand: DemandSequence(values: [1.0]))
#expect(d.rate == .audio)
}
@Test func holdsZeroBeforeFirstTrigger() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
let d: Demand.Audio = Demand.Audio(trig: 0.0, demand: DemandSequence(values: [10.0, 20.0]))
let graph: SynthGraph = makeSynthGraph(engine: engine)
let samples: [Double] = d.calculatedSamples(graph: graph, count: 16)[0]
for s: Double in samples { #expect(s == 0.0) }
}
@Test func advancesOnRisingEdge() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
// Trigger goes high at sample 4, stays high.
let trig: Closure = Closure { _, i in i >= 4 ? 1.0 : 0.0 }
let d: Demand.Audio = Demand.Audio(trig: trig, demand: DemandSequence(values: [99.0, 200.0]))
let graph: SynthGraph = makeSynthGraph(engine: engine)
let samples: [Double] = d.calculatedSamples(graph: graph, count: 16)[0]
// Samples 0-3: 0 (no trigger), sample 4+: 99.0
for i: Int in 0..<4 { #expect(samples[i] == 0.0) }
for i: Int in 4..<16 { #expect(samples[i] == 99.0) }
}
@Test func multipleTriggersInOneBlock() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
// Two rising edges: at sample 2 (2-3 high) and sample 8 (8-9 high).
let trig: Closure = Closure { _, i in
(i == 2 || i == 3 || i == 8 || i == 9) ? 1.0 : 0.0
}
let d: Demand.Audio = Demand.Audio(trig: trig, demand: DemandSequence(values: [10.0, 20.0, 30.0]))
let graph: SynthGraph = makeSynthGraph(engine: engine)
let samples: [Double] = d.calculatedSamples(graph: graph, count: 16)[0]
#expect(samples[0] == 0.0) // before first trigger
#expect(samples[2] == 10.0) // first trigger → 10
#expect(samples[7] == 10.0) // still holding 10
#expect(samples[8] == 20.0) // second trigger → 20
}
@Test func holdsLastValueAfterExhaustion() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
// Trigger rises at sample 0 (prevTrig = 0 initially), stays high.
let trig: Closure = Closure { _, _ in 1.0 }
let d: Demand.Audio = Demand.Audio(trig: trig, demand: DemandSequence(values: [42.0], repeats: 1))
let graph: SynthGraph = makeSynthGraph(engine: engine)
let samples: [Double] = d.calculatedSamples(graph: graph, count: 16)[0]
// Rising edge at sample 0 → 42.0. DemandSequence exhausted. No further rising edges (stays high).
for s: Double in samples.prefix(16) { #expect(s == 42.0) }
// Advance to next block; trig stays high → no new rising edge → still 42.0.
advanceBlock(engine)
let graph2: SynthGraph = makeSynthGraph(engine: engine)
let samples2: [Double] = d.calculatedSamples(graph: graph2, count: 16)[0]
for s: Double in samples2.prefix(16) { #expect(s == 42.0) }
}
@Test func resetInputClearsHeldValue() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
var block: Int = 0
// Block 0: trigger at sample 0 → held = 77.0.
// Block 1: reset at sample 0, no trigger → held clears to 0.
let trig: Closure = Closure { _, i in block == 0 && i == 0 ? 1.0 : 0.0 }
let reset: Closure = Closure { _, i in block == 1 && i == 0 ? 1.0 : 0.0 }
let d: Demand.Audio = Demand.Audio(trig: trig, reset: reset, demand: DemandSequence(values: [77.0, 88.0]))
let g1: SynthGraph = makeSynthGraph(engine: engine)
let s1: [Double] = d.calculatedSamples(graph: g1, count: 8)[0]
#expect(s1[0] == 77.0)
block = 1
advanceBlock(engine)
let g2: SynthGraph = makeSynthGraph(engine: engine)
let s2: [Double] = d.calculatedSamples(graph: g2, count: 8)[0]
// reset fired at sample 0 → _heldValue = 0; seq also reset to index 0.
for s: Double in s2 { #expect(s == 0.0) }
}
@Test func resetThenTriggerSameSample() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
let seq: DemandSequence = DemandSequence(values: [10.0, 20.0, 30.0])
// Consume first value so reset effect is observable.
_ = seq.nextValue() // 10.0 consumed; seq at index 1
// Reset and trigger both cross at sample 0 simultaneously.
let combined: Closure = Closure { _, i in i == 0 ? 1.0 : 0.0 }
let d: Demand.Audio = Demand.Audio(trig: combined, reset: combined, demand: seq)
let graph: SynthGraph = makeSynthGraph(engine: engine)
let samples: [Double] = d.calculatedSamples(graph: graph, count: 16)[0]
// reset fires first → seq.reset() → index back to 0
// trigger then pulls → 10.0 (first value again, not 20.0)
#expect(samples[0] == 10.0)
}
@Test func blockBoundaryTrigger() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
var trigHigh: Bool = false
let trig: Closure = Closure { _, _ in trigHigh ? 1.0 : 0.0 }
let d: Demand.Audio = Demand.Audio(trig: trig, demand: DemandSequence(values: [55.0]))
// Block 1: trig = 0 → _prevTrig persists as 0 on d after this block.
let g1: SynthGraph = makeSynthGraph(engine: engine)
let s1: [Double] = d.calculatedSamples(graph: g1, count: 8)[0]
for s: Double in s1.prefix(8) { #expect(s == 0.0) }
// Block 2: trig goes high from sample 0. Same instance d — _prevTrig = 0 → rising edge.
trigHigh = true
advanceBlock(engine)
let g2: SynthGraph = makeSynthGraph(engine: engine)
let s2: [Double] = d.calculatedSamples(graph: g2, count: 8)[0]
// _prevTrig = 0 (carried from block 1), trig[0] = 1 → edge detected on same instance.
#expect(s2[0] == 55.0)
}
}
// MARK: - Demand.Control
@Suite("Demand.Control")
struct DemandControlTests {
@Test func rateIsControl() {
let d: Demand.Control = Demand.Control(trig: 0.0, demand: DemandSequence(values: [1.0]))
#expect(d.rate == .control)
}
@Test func holdsZeroBeforeFirstTrigger() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
let d: Demand.Control = Demand.Control(trig: 0.0, demand: DemandSequence(values: [10.0]))
let graph: SynthGraph = makeSynthGraph(engine: engine)
_ = d.calculatedSamples(graph: graph, count: 16)
#expect(d.previousOutputValue == 0.0)
}
@Test func advancesOnRisingEdgeMidBlock() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
// Trigger crosses at sample 8.
let trig: Closure = Closure { _, i in i >= 8 ? 1.0 : 0.0 }
let d: Demand.Control = Demand.Control(trig: trig, demand: DemandSequence(values: [100.0]))
let graph: SynthGraph = makeSynthGraph(engine: engine)
_ = d.calculatedSamples(graph: graph, count: 16)
// Rising edge mid-block → heldValue = 100.0 → previousOutputValue = 100.0
#expect(d.previousOutputValue == 100.0)
}
@Test func outputIsRampToNewValue() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 4)
// trig always high → rising edge at sample 0 (prevTrig = 0 initially).
// _rampTarget = 0, _heldValue becomes 40.0. Ramp 0 → 40 over 4 samples.
let trig: Closure = Closure { _, _ in 1.0 }
let d: Demand.Control = Demand.Control(trig: trig, demand: DemandSequence(values: [40.0]))
let graph: SynthGraph = makeSynthGraph(engine: engine)
let samples: [Double] = d.calculatedSamples(graph: graph, count: 4)[0]
#expect(samples[0] == 10.0)
#expect(samples[1] == 20.0)
#expect(samples[2] == 30.0)
#expect(samples[3] == 40.0)
}
@Test func holdsAfterExhaustion() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
// Rising edge at sample 0 → 7.0. DemandSequence exhausted. Trig stays high → no new edge.
let trig: Closure = Closure { _, _ in 1.0 }
let d: Demand.Control = Demand.Control(trig: trig, demand: DemandSequence(values: [7.0], repeats: 1))
let g1: SynthGraph = makeSynthGraph(engine: engine)
_ = d.calculatedSamples(graph: g1, count: 8)
#expect(d.previousOutputValue == 7.0)
advanceBlock(engine)
let g2: SynthGraph = makeSynthGraph(engine: engine)
_ = d.calculatedSamples(graph: g2, count: 8)
#expect(d.previousOutputValue == 7.0)
}
@Test func resetClearsHeldValueAndRamps() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 4)
// Reset at sample 0 (prevReset = 0 initially) → demand.reset(), heldValue = 0.
// Ramp: _rampTarget = 0, _heldValue = 0 → flat at 0.
let resetSig: Closure = Closure { _, _ in 1.0 }
let d: Demand.Control = Demand.Control(trig: 0.0, reset: resetSig, demand: DemandSequence(values: [50.0]))
let graph: SynthGraph = makeSynthGraph(engine: engine)
_ = d.calculatedSamples(graph: graph, count: 4)
#expect(d.previousOutputValue == 0.0)
}
@Test func floatValueDoesNotAdvanceSequence() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 8)
// Single puller owns the sequence. trig always-high → rising edge at sample 0.
let d: Demand.Control = Demand.Control(
trig: Closure { _, _ in 1.0 },
demand: DemandSequence(values: [11.0, 22.0, 33.0])
)
let graph: SynthGraph = makeSynthGraph(engine: engine)
// floatValue returns previousOutputValue (0) without calling nextValue on seq.
let fv: Double = d.floatValue(graph: graph)
#expect(fv == 0.0)
// calculatedSamples triggers on the same block → must pull 11.0, not 22.0.
_ = d.calculatedSamples(graph: graph, count: 8)
#expect(d.previousOutputValue == 11.0)
}
@Test func resetThenTriggerSameBlock() {
let engine: SynthesisEngine = makeTestEngine(blockSize: 16)
let seq: DemandSequence = DemandSequence(values: [10.0, 20.0, 30.0])
// Advance seq past first value so reset effect is observable.
_ = seq.nextValue() // 10.0 consumed; seq at index 1
// Reset fires at sample 2, trigger at sample 6 (both are rising edges from 0).
let reset: Closure = Closure { _, i in i == 2 ? 1.0 : 0.0 }
let trig: Closure = Closure { _, i in i == 6 ? 1.0 : 0.0 }
let d: Demand.Control = Demand.Control(trig: trig, reset: reset, demand: seq)
let graph: SynthGraph = makeSynthGraph(engine: engine)
_ = d.calculatedSamples(graph: graph, count: 16)
// reset fires first (sample 2) → seq.reset(), heldValue = 0, didTrigger cleared.
// trigger fires after (sample 6) → pulls 10.0 (first value of fresh sequence).
#expect(d.previousOutputValue == 10.0)
}
}