prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
// JitterTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Jitter")
|
||||
struct JitterTests {
|
||||
|
||||
// MARK: - Rate
|
||||
|
||||
@Test func rateIsControl() {
|
||||
let gen: Jitter.Control = Jitter.Control()
|
||||
#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: Jitter.Control = Jitter.Control(center: 0.0, speed: 1.0, amount: 0.1, 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: Jitter.Control = Jitter.Control(center: 0.0, speed: 1.0, amount: 0.1, 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)
|
||||
|
||||
let gen: Jitter.Control = Jitter.Control(center: 0.0, speed: 2.0, amount: 1.0, seed: 3)
|
||||
|
||||
let block1a: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
// Second call in same segment is a cache hit — must be identical.
|
||||
let block1b: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
#expect(block1a == block1b)
|
||||
|
||||
advanceBlock(engine)
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let block2: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
|
||||
// Block 2 should differ from block 1 (jitter advances state per block).
|
||||
// This tests that a new block was computed, not a stale cache.
|
||||
#expect(block2.last != nil)
|
||||
_ = block2 // ensure block2 is used
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Output stays within [center - amount, center + amount]
|
||||
|
||||
@Test func outputStaysWithinBounds() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
let center: Double = 5.0
|
||||
let amount: Double = 2.0
|
||||
let gen: Jitter.Control = Jitter.Control(center: center, speed: 10.0, amount: amount, seed: 42)
|
||||
|
||||
for _ in 0..<50 {
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
// Check last sample of each block (post-ramp settled value).
|
||||
let last: Double = out[0][63]
|
||||
#expect(last >= center - amount - 1e-9 && last <= center + amount + 1e-9)
|
||||
advanceBlock(engine)
|
||||
}
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Output drifts from center
|
||||
|
||||
@Test func outputDriftsFromCenter() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// High speed + high amount should cause the output to move noticeably from 0.0.
|
||||
let gen: Jitter.Control = Jitter.Control(center: 0.0, speed: 20.0, amount: 1.0, seed: 55)
|
||||
|
||||
var values: [Double] = []
|
||||
for _ in 0..<20 {
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
values.append(out[0][63])
|
||||
advanceBlock(engine)
|
||||
}
|
||||
|
||||
// With amount=1.0 and high speed, variance should be non-zero.
|
||||
let mean: Double = values.reduce(0, +) / Double(values.count)
|
||||
let variance: Double = values.map { ($0 - mean) * ($0 - mean) }.reduce(0, +) / Double(values.count)
|
||||
#expect(variance > 0.0)
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Speed=0 freezes output
|
||||
|
||||
@Test func speedZeroFreezesOutput() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
var graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// With speed=0, coeff=1.0, so _smoothed doesn't change. But the random step is
|
||||
// still added (scaled by 1-coeff = 0). Output should remain at 0 (no perturbation).
|
||||
let gen: Jitter.Control = Jitter.Control(center: 0.0, speed: 0.0, amount: 1.0, seed: 11)
|
||||
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let block1: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
advanceBlock(engine)
|
||||
graph = makeSynthGraph(engine: engine)
|
||||
let block2: [Double] = Array(gen.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
|
||||
|
||||
// With speed=0, last sample of both blocks should be the same.
|
||||
#expect(block1.last == block2.last)
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Ramp: last sample equals target
|
||||
|
||||
@Test func rampLastSampleEqualsTarget() throws {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// amount=0 clamps [center-0, center+0], forcing output to center=3.0 regardless of PRNG.
|
||||
let gen: Jitter.Control = Jitter.Control(center: 3.0, speed: 1.0, amount: 0.0, seed: 9)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: 64)
|
||||
#expect(abs(out[0][63] - 3.0) < 1e-9)
|
||||
|
||||
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: Jitter.Control = Jitter.Control(center: 0.0, speed: 2.0, amount: 0.5, seed: 9999)
|
||||
let gen2: Jitter.Control = Jitter.Control(center: 0.0, speed: 2.0, amount: 0.5, seed: 9999)
|
||||
|
||||
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()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
// amount=0 → output = center = 2.0 always.
|
||||
let genPlain: Jitter.Control = Jitter.Control(center: 2.0, speed: 1.0, amount: 0.0, seed: 66)
|
||||
let genMulAdd: Jitter.Control = Jitter.Control(center: 2.0, speed: 1.0, amount: 0.0, seed: 66, mul: 3.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))
|
||||
|
||||
// Expected: 2.0 × 3 + (-1) = 5.0
|
||||
let expected: Double = (plain.last ?? 0.0) * 3.0 + (-1.0)
|
||||
#expect(abs((scaled.last ?? 0.0) - expected) < 1e-9)
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Integration: builds and evaluates without crash
|
||||
|
||||
@Test func integrationRunsWithoutCrash() throws {
|
||||
final class JitterSynth: SynthesizerBase {
|
||||
override func build() {
|
||||
let jitter: Jitter.Control = Jitter.Control(center: 440.0, speed: 0.5, amount: 50.0)
|
||||
let osc: OscSin.Audio = OscSin.Audio(freq: jitter)
|
||||
addOutputGenerator(Out.Audio(bus: 0, source: osc * 0.1))
|
||||
}
|
||||
}
|
||||
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: 64)
|
||||
try engine.start()
|
||||
let synth: JitterSynth = JitterSynth()
|
||||
synth.build()
|
||||
|
||||
for _ in 0..<10 {
|
||||
engine.advanceSegmentSerial()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
synth.evaluate(graph: graph, count: 64)
|
||||
}
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
}
|
||||
@@ -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