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

149 lines
6.0 KiB
Swift

// SampleAndHoldTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("SampleAndHold.Audio")
struct SampleAndHoldAudioTests {
let engine: SynthesisEngine
var graph: SynthGraph { makeSynthGraph(engine: engine) }
let blockSize: Int = 64
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
@Test func rateIsAudio() {
let gen: SampleAndHold.Audio = SampleAndHold.Audio(input: 0.0, trig: 0.0)
#expect(gen.rate == .audio)
}
/// Without any rising edge, output stays at the initial held value (0.0).
@Test func noTriggerHoldsZero() {
let gen: SampleAndHold.Audio = SampleAndHold.Audio(input: 0.7, trig: 0.0)
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
for i: Int in 0..<blockSize {
#expect(out[i] == 0.0)
}
}
/// Single rising edge at sample 32 captures the input at that frame.
@Test func risingEdgeCapturesInputAtThatFrame() {
let input: Closure = Closure { _, i in Double(i) * 0.01 }
let trig: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
let gen: SampleAndHold.Audio = SampleAndHold.Audio(input: input, trig: trig)
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
// Samples 0..31: held = 0 (no edge yet).
for i: Int in 0..<32 {
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
}
// Samples 32..63: held = input at sample 32 = 0.32.
for i: Int in 32..<blockSize {
#expect(abs(out[i] - 0.32) < 1e-12,
"sample \(i) expected 0.32 got \(out[i])")
}
}
/// Sustained `trig > 0` produces only one capture (no re-fire without falling edge).
@Test func sustainedTriggerProducesOneCapture() {
// Ramp input throughout. Trigger goes high at sample 16 and stays high.
let input: Closure = Closure { _, i in Double(i) * 0.01 }
let trig: Closure = Closure { _, i in i < 16 ? 0.0 : 1.0 }
let gen: SampleAndHold.Audio = SampleAndHold.Audio(input: input, trig: trig)
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
for i: Int in 0..<16 {
#expect(out[i] == 0.0)
}
// Captured 0.16; never updated since trig stays high.
for i: Int in 16..<blockSize {
#expect(abs(out[i] - 0.16) < 1e-12)
}
}
/// Multiple edges within a block re-capture on each rising edge.
@Test func multipleEdgesRecaptureEachTime() {
let input: Closure = Closure { _, i in Double(i) * 0.01 }
// Pulses: low samples 0..15, high 16..23, low 24..39, high 40..55, low 56..63.
let trig: Closure = Closure { _, i in
((16..<24).contains(i) || (40..<56).contains(i)) ? 1.0 : 0.0
}
let gen: SampleAndHold.Audio = SampleAndHold.Audio(input: input, trig: trig)
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
// First edge at 16 → captures 0.16.
for i: Int in 16..<40 { #expect(abs(out[i] - 0.16) < 1e-12) }
// Second edge at 40 → captures 0.40.
for i: Int in 40..<blockSize { #expect(abs(out[i] - 0.40) < 1e-12) }
}
@Test func heldValuePersistsAcrossBlocks() {
let firstSerial: UInt64 = engine.segmentSerial
let input: Closure = Closure { _, i in Double(i) * 0.01 }
let trig: Closure = Closure { graph, i in
graph.engine.segmentSerial == firstSerial && i == 16 ? 1.0 : 0.0
}
let gen: SampleAndHold.Audio = SampleAndHold.Audio(input: input, trig: trig)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
advanceBlock(engine)
// Block 2: trig stays at 0 (segmentSerial differs).
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
for i: Int in 0..<blockSize {
#expect(abs(out[i] - 0.16) < 1e-12,
"block 2 sample \(i) expected 0.16 got \(out[i])")
}
}
}
@Suite("SampleAndHold.Control")
struct SampleAndHoldControlTests {
let engine: SynthesisEngine
var graph: SynthGraph { makeSynthGraph(engine: engine) }
let blockSize: Int = 64
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
@Test func rateIsControl() {
let gen: SampleAndHold.Control = SampleAndHold.Control(input: 0.0, trig: 0.0)
#expect(gen.rate == .control)
}
@Test func noTriggerHoldsZero() {
let gen: SampleAndHold.Control = SampleAndHold.Control(input: 0.7, trig: 0.0)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.previousOutputValue == 0.0)
}
/// Block-rate at-most-one capture: a single rising edge anywhere in the
/// block captures the input at that frame and the output ramps to it.
@Test func atMostOneCapturePerBlock() {
let input: Closure = Closure { _, i in Double(i) * 0.01 }
// Two rising edges in one block: at sample 16 and at sample 40. The
// first wins under Control-rate "didCapture" gating.
let trig: Closure = Closure { _, i in
(i == 16 || i == 40) ? 1.0 : 0.0
}
let gen: SampleAndHold.Control = SampleAndHold.Control(input: input, trig: trig)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
// Captured 0.16. Output ramped from 0.0 to 0.16.
#expect(abs(gen.previousOutputValue - 0.16) < 1e-12)
}
@Test func floatValueReturnsPreviousOutputValue() {
let gen: SampleAndHold.Control = SampleAndHold.Control(input: 0.5, trig: 1.0)
#expect(gen.floatValue(graph: graph) == 0.0)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
}
}