Files
2026-06-21 11:45:58 -07:00

200 lines
8.4 KiB
Swift
Raw Permalink 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.
// RateRampTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("RateRamp")
struct RateRampTests {
private func makeEngine() -> SynthesisEngine {
makeTestEngine(blockSize: 64, sampleRate: 64)
}
// MARK: - Rate
@Test func rateIsControl() {
let gen: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 1.0)
#expect(gen.rate == .control)
}
// MARK: - First block ramps from start (not zero)
@Test func firstBlockStartsAtStart() throws {
// start=5.0, rate=1.0, no trigger. _acc seeds to 5 on first call.
// Block 1: ramp 5 → 6. Block 2: 6 → 7. Block 3: 7 → 8.
let engine: SynthesisEngine = makeEngine()
try engine.start()
let ramp: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 1.0, start: 5.0)
let samples: [Double] = collectBlocks(ramp, engine: engine, blocks: 3)
let endValues: [Double] = (0..<3).map { samples[$0 * 64 + 63] }
#expect(abs(endValues[0] - 6.0) < 1e-9, "got \(endValues[0])")
#expect(abs(endValues[1] - 7.0) < 1e-9)
#expect(abs(endValues[2] - 8.0) < 1e-9)
// First sample of block 1 should be near start (5 + step), well above 0.
#expect(samples[0] > 5.0)
#expect(samples[0] <= 6.0)
engine.stop()
}
// MARK: - Accumulation: rate × seconds
@Test func accumulatorAdvancesAtRate() throws {
// sampleRate = blockSize = 64 → 1 block = 1 second. rate=2.0 → +2 per block.
let engine: SynthesisEngine = makeEngine()
try engine.start()
let ramp: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 2.0)
let samples: [Double] = collectBlocks(ramp, engine: engine, blocks: 4)
// Block ends should be 2, 4, 6, 8 (accumulator value at end of each block).
let endValues: [Double] = (0..<4).map { samples[$0 * 64 + 63] }
#expect(abs(endValues[0] - 2.0) < 1e-9)
#expect(abs(endValues[1] - 4.0) < 1e-9)
#expect(abs(endValues[2] - 6.0) < 1e-9)
#expect(abs(endValues[3] - 8.0) < 1e-9)
engine.stop()
}
// MARK: - Trigger edge resets accumulator to start
@Test func triggerResetsToStart() throws {
let engine: SynthesisEngine = makeEngine()
try engine.start()
// Pre-accumulate for 3 blocks with no trigger.
let trigger: ParameterNode = ParameterNode(0.0)
let ramp: RateRamp.Control = RateRamp.Control(trigger: trigger, rate: 1.0, start: 0.0)
_ = collectBlocks(ramp, engine: engine, blocks: 3) // _acc ≈ 3.0
// Raise trigger HIGH. The rising edge is detected from _prevTrig=0 → t=1.
trigger.value = 1.0
let blockAfter: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
// After reset: _acc = start = 0, then _acc += rate * 1s = 1.0. Last sample == 1.0.
let lastSample: Double = blockAfter.last ?? 0.0
#expect(abs(lastSample - 1.0) < 1e-9, "expected ~1.0, got \(lastSample)")
engine.stop()
}
// MARK: - Modulating start between triggers has no effect on output
@Test func startModulationOnlyAtTriggerEdge() throws {
let engine: SynthesisEngine = makeEngine()
try engine.start()
let trigger: ParameterNode = ParameterNode(0.0)
let startParam: ParameterNode = ParameterNode(0.0)
let ramp: RateRamp.Control = RateRamp.Control(trigger: trigger, rate: 1.0, start: startParam)
// Block 1: no trigger, start=0; accumulator should be 1.0 at end.
let b1: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
#expect(abs((b1.last ?? 0.0) - 1.0) < 1e-9)
// Change start to 100 — should have no effect because no trigger fires.
startParam.value = 100.0
let b2: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
// Still accumulating from previous _acc=1.0 → end of block = 2.0.
#expect(abs((b2.last ?? 0.0) - 2.0) < 1e-9, "got \(b2.last ?? 0.0)")
// Now fire trigger: _acc resets to startParam.value=100, then +=1 → end=101.
trigger.value = 1.0
let b3: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
#expect(abs((b3.last ?? 0.0) - 101.0) < 1e-9, "got \(b3.last ?? 0.0)")
engine.stop()
}
// MARK: - Rate modulation changes growth speed
@Test func rateModulationChangesGrowth() throws {
let engine: SynthesisEngine = makeEngine()
try engine.start()
let rate: ParameterNode = ParameterNode(1.0)
let ramp: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: rate)
let b1: [Double] = collectBlocks(ramp, engine: engine, blocks: 1) // +1
rate.value = 5.0
let b2: [Double] = collectBlocks(ramp, engine: engine, blocks: 1) // +5
#expect(abs((b1.last ?? 0.0) - 1.0) < 1e-9)
#expect(abs((b2.last ?? 0.0) - 6.0) < 1e-9, "got \(b2.last ?? 0.0)")
engine.stop()
}
// MARK: - Cross-block edge detection (high-to-low to high)
@Test func crossBlockEdgeDetection() throws {
let engine: SynthesisEngine = makeEngine()
try engine.start()
let trigger: ParameterNode = ParameterNode(1.0)
let ramp: RateRamp.Control = RateRamp.Control(trigger: trigger, rate: 1.0, start: 0.0)
// Block 1: trigger goes 0 → 1 (initial edge from _prevTrig=0). Reset to 0. End=1.0.
let b1: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
#expect(abs((b1.last ?? 0.0) - 1.0) < 1e-9)
// Block 2: trigger stays 1. No new edge. _acc continues from 1.0 → 2.0.
let b2: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
#expect(abs((b2.last ?? 0.0) - 2.0) < 1e-9)
// Drop trigger low, then back high. Edge fires again.
trigger.value = 0.0
let b3: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
#expect(abs((b3.last ?? 0.0) - 3.0) < 1e-9) // no edge — accumulator continues
trigger.value = 1.0
let b4: [Double] = collectBlocks(ramp, engine: engine, blocks: 1)
// Edge fired → reset to start (0) + rate (1) = 1.0.
#expect(abs((b4.last ?? 0.0) - 1.0) < 1e-9, "got \(b4.last ?? 0.0)")
engine.stop()
}
// MARK: - mul / add
@Test func mulAddScalesOutput() throws {
let engine: SynthesisEngine = makeEngine()
try engine.start()
let plain: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 1.0)
let scaled: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 1.0, mul: 2.0, add: 1.0)
let p: [Double] = collectBlocks(plain, engine: engine, blocks: 3)
let s: [Double] = collectBlocks(scaled, engine: engine, blocks: 3)
for i: Int in 0..<p.count {
#expect(abs(s[i] - (p[i] * 2.0 + 1.0)) < 1e-9)
}
engine.stop()
}
// MARK: - Fan-out cache
@Test func fanOutReturnsCachedResult() throws {
let engine: SynthesisEngine = makeEngine()
try engine.start()
let graph: SynthGraph = makeSynthGraph(engine: engine)
let ramp: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 1.0)
let out1: [Double] = Array(ramp.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
let out2: [Double] = Array(ramp.calculatedSamples(graph: graph, count: 64)[0].prefix(64))
#expect(out1 == out2)
engine.stop()
}
// MARK: - Sub-block bypass: state advances exactly once per audio block
@Test func stateAdvancesOncePerAudioBlock() throws {
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 64,
controlBlockSize: 16,
sampleRate: 64,
numOutputChannels: 2,
numInputChannels: 2,
numAudioBuses: 16,
numControlBuses: 16,
eventQueueCapacity: lyrebirdDefaultEventQueueCapacity,
driver: OfflineDriver()
)
try engine.start()
let ramp: RateRamp.Control = RateRamp.Control(trigger: 0.0, rate: 2.0)
let samples: [Double] = collectBlocks(ramp, engine: engine, blocks: 4)
// Despite controlBlockSize=16, _acc must advance once per audio block of 64 samples.
// After 4 blocks: _acc = 4 * 2 = 8.
let lastSample: Double = samples.last ?? 0.0
#expect(abs(lastSample - 8.0) < 1e-9, "got \(lastSample)")
engine.stop()
}
}