prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,238 @@
|
||||
// TriggeredRandomTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("TriggeredRandom")
|
||||
struct TriggeredRandomTests {
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
// MARK: - Output count
|
||||
|
||||
@Test func outputCountMatchesRequest() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let gen: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 0.0, lo: 0.0, hi: 1.0, seed: 1)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out[0].prefix(64)).count == 64)
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Fan-out
|
||||
|
||||
@Test func fanOutReturnsCachedResult() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let gen: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 0.0, lo: 0.0, hi: 1.0, seed: 2)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(Array(out1[0].prefix(64)) == Array(out2[0].prefix(64)))
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - State advances once per block (bypass)
|
||||
|
||||
@Test func stateAdvancesOncePerBlock() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Trigger high on block 1 to fire a new random value.
|
||||
let gen: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 1.0, lo: 0.0, hi: 1.0, seed: 3)
|
||||
let block1a: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
// Second call in same segment must be a cache hit — identical output.
|
||||
let block1b: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
#expect(block1a == block1b)
|
||||
|
||||
// Advance to next block — trigger still high (no rising edge, so value stays).
|
||||
advanceBlock(engine)
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let block2: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
|
||||
// After the first block (rising edge fired), subsequent blocks with no new edge
|
||||
// should hold the same target — last sample should match last sample of block1.
|
||||
#expect(block2.last == block1a.last)
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Value stays constant between triggers
|
||||
|
||||
@Test func valueStaysConstantBetweenTriggers() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// No trigger (trigger = 0.0 constantly), so held value never changes.
|
||||
let gen: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 0.0, lo: 0.0, hi: 1.0, seed: 42)
|
||||
|
||||
// Collect the end-of-ramp target over several blocks.
|
||||
var lastSamples: [Double] = []
|
||||
for _ in 0..<5 {
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
lastSamples.append(out[0][63])
|
||||
advanceBlock(engine)
|
||||
}
|
||||
|
||||
// All blocks should converge to the same held value (0.0, the initial value).
|
||||
for sample: Double in lastSamples {
|
||||
#expect(sample == 0.0)
|
||||
}
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Value changes on trigger edge
|
||||
|
||||
@Test func valueChangesOnTriggerEdge() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Use a constant-high trigger to fire on the very first block (rising edge from
|
||||
// initial _prevTrig = 0.0 → trigger = 1.0).
|
||||
let gen: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 1.0, lo: 0.0, hi: 1.0, seed: 99)
|
||||
|
||||
// Block 1: trigger edge fires → new random value sampled.
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let block1: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
let target1: Double = block1.last ?? 0.0
|
||||
|
||||
// Since seed is deterministic, we can predict a non-zero value was sampled.
|
||||
// The exact value depends on the PRNG — just verify it was sampled from [lo, hi].
|
||||
#expect(target1 >= 0.0 && target1 <= 1.0)
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Output stays in [lo, hi]
|
||||
|
||||
@Test func outputStaysInRange() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let lo: Double = 200.0
|
||||
let hi: Double = 800.0
|
||||
|
||||
// Use an Impulse-like trigger: alternate high/low blocks by constructing a
|
||||
// generator that fires on the first sample of each block.
|
||||
// Simpler: use a ConstantGeneratorInput that is always 1.0 to fire on block 1,
|
||||
// then 0.0 for subsequent blocks.
|
||||
let gen: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 1.0, lo: lo, hi: hi, seed: 7)
|
||||
|
||||
// Skip block 1: ramp starts from initial _rampTarget=0.0, so early samples are
|
||||
// outside [lo,hi] until the ramp reaches the first held value.
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 64)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Blocks 2+: both ramp endpoints are in [lo,hi], so all samples stay in range.
|
||||
for _ in 0..<19 {
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
for i: Int in 0..<64 {
|
||||
#expect(out[0][i] >= lo && out[0][i] <= hi, "sample \(i) out of range")
|
||||
}
|
||||
advanceBlock(engine)
|
||||
}
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Deterministic output with fixed seed
|
||||
|
||||
@Test func deterministicOutputWithSeed() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let gen1: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 1.0, lo: 0.0, hi: 1.0, seed: 12345)
|
||||
let gen2: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 1.0, lo: 0.0, hi: 1.0, seed: 12345)
|
||||
|
||||
let out1: [Double] = Array(gen1.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
let out2: [Double] = Array(gen2.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
#expect(out1 == out2)
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - mulAdd
|
||||
|
||||
@Test func mulAddScalesOutput() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let genPlain: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 1.0, lo: 0.5, hi: 0.5, seed: 77)
|
||||
let genMulAdd: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 1.0, lo: 0.5, hi: 0.5, seed: 77, mul: 2.0, add: 1.0)
|
||||
|
||||
let plain: [Double] = Array(genPlain.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
let scaled: [Double] = Array(genMulAdd.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
|
||||
// Since lo == hi == 0.5, every value = 0.5 after ramp settles.
|
||||
// With mul:2 add:1, the last sample should be ≈ 0.5 × 2 + 1 = 2.0.
|
||||
let expected: Double = plain.last.map { $0 * 2.0 + 1.0 } ?? 0.0
|
||||
#expect(abs((scaled.last ?? 0.0) - expected) < 1e-9)
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Ramp: last sample equals target after settling
|
||||
|
||||
@Test func rampLastSampleEqualsTarget() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// Trigger fires on block 1 (rising edge from 0 → 1).
|
||||
// lo == hi == 0.3 so target is deterministic regardless of PRNG.
|
||||
let gen: TriggeredRandom.Control = TriggeredRandom.Control(trigger: 1.0, lo: 0.3, hi: 0.3, seed: 5)
|
||||
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
// After the ramp from 0.0 → 0.3, the last sample should be 0.3.
|
||||
#expect(abs(out[0][63] - 0.3) < 1e-9)
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Integration: builds and evaluates without crash
|
||||
|
||||
@Test func integrationRunsWithoutCrash() throws {
|
||||
final class TrigRandSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let trig: Impulse.Control = Impulse.Control(freq: 4.0)
|
||||
let rand: TriggeredRandom.Control = TriggeredRandom.Control(trigger: trig, lo: 200.0, hi: 800.0)
|
||||
addOutputGenerator(Out.Control(bus: 0, source: rand))
|
||||
}
|
||||
}
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
let synth: TrigRandSynth = TrigRandSynth()
|
||||
synth.build()
|
||||
|
||||
for _ in 0..<10 {
|
||||
engine.advanceSegmentSerial()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
synth.evaluate(graph: graph, count: 64)
|
||||
}
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user