prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
// GateTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Gate.Audio")
|
||||
struct GateAudioTests {
|
||||
|
||||
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: Gate.Audio = Gate.Audio(input: 0.0, trig: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Constant trig=1 → pure passthrough.
|
||||
@Test func openGatePassesInput() {
|
||||
let input: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
let gen: Gate.Audio = Gate.Audio(input: input, trig: 1.0)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[i] - Double(i) * 0.01) < 1e-12,
|
||||
"sample \(i): expected \(Double(i) * 0.01), got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Constant trig=0 → output stays at initial 0.0 regardless of input.
|
||||
@Test func closedGateHoldsZero() {
|
||||
let gen: Gate.Audio = Gate.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, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Gate falling-edge holds the last input seen during the high phase.
|
||||
@Test func fallingEdgeHoldsLastInput() {
|
||||
// Input ramps from 0 to 0.63 across samples 0..63 (step 0.01).
|
||||
let input: Closure = Closure { _, i in Double(i) * 0.01 }
|
||||
// Trigger high for samples 0..31, low for 32..63.
|
||||
let trig: Closure = Closure { _, i in i < 32 ? 1.0 : 0.0 }
|
||||
let gen: Gate.Audio = Gate.Audio(input: input, trig: trig)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// Samples 0..31 — passthrough.
|
||||
for i: Int in 0..<32 {
|
||||
#expect(abs(out[i] - Double(i) * 0.01) < 1e-12)
|
||||
}
|
||||
// Samples 32..63 — held at last passed sample (i=31, value=0.31).
|
||||
for i: Int in 32..<blockSize {
|
||||
#expect(abs(out[i] - 0.31) < 1e-12,
|
||||
"sample \(i) expected held=0.31, got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Held value persists across block boundaries.
|
||||
@Test func heldValuePersistsAcrossBlocks() {
|
||||
// Block 1: input ramps, gate high then low; held = 0.31 at end.
|
||||
// Block 2 (segmentSerial != block 1's): gate stays low.
|
||||
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 < 32 ? 1.0 : 0.0)
|
||||
: 0.0
|
||||
}
|
||||
let gen: Gate.Audio = Gate.Audio(input: input, trig: trig)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
advanceBlock(engine)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[i] - 0.31) < 1e-12,
|
||||
"block 2 sample \(i) expected held=0.31, got \(out[i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("Gate.Control")
|
||||
struct GateControlTests {
|
||||
|
||||
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: Gate.Control = Gate.Control(input: 0.0, trig: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Open gate, constant input → control output ramps from 0 to input value.
|
||||
@Test func openGateRampsToInput() {
|
||||
let gen: Gate.Control = Gate.Control(input: 0.5, trig: 1.0)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// First block — ramp from rampStart=0.0 to 0.5; last sample should be 0.5.
|
||||
#expect(abs(out[blockSize - 1] - 0.5) < 1e-12)
|
||||
// Step linear: each sample = step * (i+1), step = 0.5/64.
|
||||
let step: Double = 0.5 / Double(blockSize)
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[i] - step * Double(i + 1)) < 1e-12)
|
||||
}
|
||||
#expect(abs(gen.previousOutputValue - 0.5) < 1e-12)
|
||||
}
|
||||
|
||||
@Test func closedGateHoldsZero() {
|
||||
let gen: Gate.Control = Gate.Control(input: 0.5, trig: 0.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: Gate.Control = Gate.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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
// PulseCountAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("PulseCount.Audio")
|
||||
struct PulseCountAudioTests {
|
||||
|
||||
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: PulseCount.Audio = PulseCount.Audio(trig: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Without any trigger, count stays at 0.
|
||||
@Test func noTriggerCountStaysZero() {
|
||||
let gen: PulseCount.Audio = PulseCount.Audio(trig: 0.0)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Three rising edges at known samples increment count by 1 each.
|
||||
@Test func countIncrementsOnEachEdge() {
|
||||
// Edges at samples 10, 20, 30 (single-sample pulses between 0 zones).
|
||||
let trig: Closure = Closure { _, i in
|
||||
(i == 10 || i == 20 || i == 30) ? 1.0 : 0.0
|
||||
}
|
||||
let gen: PulseCount.Audio = PulseCount.Audio(trig: trig)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// Count increments at the edge sample itself.
|
||||
for i: Int in 0..<10 {
|
||||
#expect(out[i] == 0.0)
|
||||
}
|
||||
for i: Int in 10..<20 {
|
||||
#expect(out[i] == 1.0, "sample \(i) expected 1.0 got \(out[i])")
|
||||
}
|
||||
for i: Int in 20..<30 {
|
||||
#expect(out[i] == 2.0, "sample \(i) expected 2.0 got \(out[i])")
|
||||
}
|
||||
for i: Int in 30..<blockSize {
|
||||
#expect(out[i] == 3.0, "sample \(i) expected 3.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Sustained trig > 0 fires only once (edge-only detection).
|
||||
@Test func sustainedTrigFiresOnce() {
|
||||
// Trig goes high at 10 and stays high.
|
||||
let trig: Closure = Closure { _, i in i >= 10 ? 1.0 : 0.0 }
|
||||
let gen: PulseCount.Audio = PulseCount.Audio(trig: trig)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<10 { #expect(out[i] == 0.0) }
|
||||
for i: Int in 10..<blockSize { #expect(out[i] == 1.0) }
|
||||
}
|
||||
|
||||
/// Reset rising edge clears count to 0.
|
||||
@Test func resetClearsCount() {
|
||||
// Three trig edges then a reset at sample 40.
|
||||
let trig: Closure = Closure { _, i in (i == 5 || i == 15 || i == 25) ? 1.0 : 0.0 }
|
||||
let reset: Closure = Closure { _, i in i == 40 ? 1.0 : 0.0 }
|
||||
let gen: PulseCount.Audio = PulseCount.Audio(trig: trig, reset: reset)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// Count = 3 at sample 25; reset at 40 brings it back to 0.
|
||||
#expect(abs(out[39] - 3.0) < 1e-12)
|
||||
for i: Int in 40..<blockSize {
|
||||
#expect(out[i] == 0.0, "post-reset sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset happens BEFORE trig increment when both occur at the same sample.
|
||||
@Test func resetBeforeTrig() {
|
||||
// Both reset and trig fire at sample 10.
|
||||
let trig: Closure = Closure { _, i in (i == 5 || i == 10) ? 1.0 : 0.0 }
|
||||
let reset: Closure = Closure { _, i in i == 10 ? 1.0 : 0.0 }
|
||||
let gen: PulseCount.Audio = PulseCount.Audio(trig: trig, reset: reset)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// After sample 5: count = 1.
|
||||
#expect(abs(out[9] - 1.0) < 1e-12)
|
||||
// Sample 10: reset → count=0, then trig → count=1.
|
||||
for i: Int in 10..<blockSize {
|
||||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||||
"post-reset+trig sample \(i) expected 1.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Count persists across block boundaries.
|
||||
@Test func countPersistsAcrossBlocks() {
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
// Three edges in block 1.
|
||||
let trig: Closure = Closure { graph, i in
|
||||
graph.engine.segmentSerial == firstSerial && (i == 5 || i == 15 || i == 25)
|
||||
? 1.0 : 0.0
|
||||
}
|
||||
let gen: PulseCount.Audio = PulseCount.Audio(trig: trig)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 2: no triggers, count stays at 3.
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out2[i] - 3.0) < 1e-12,
|
||||
"block-2 sample \(i) expected 3.0 got \(out2[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in same segment return identical first channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: PulseCount.Audio = PulseCount.Audio(trig: 0.0)
|
||||
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
|
||||
/// mulAdd: count × 2 + 1 when mul=2, add=1.
|
||||
@Test func mulAddApplied() {
|
||||
let trig: Closure = Closure { _, i in (i == 10 || i == 20) ? 1.0 : 0.0 }
|
||||
let genNoMul: PulseCount.Audio = PulseCount.Audio(trig: trig, mul: 1.0, add: 0.0)
|
||||
let genMul: PulseCount.Audio = PulseCount.Audio(trig: trig, mul: 2.0, add: 1.0)
|
||||
let outNone: [Double] = genNoMul.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
advanceBlock(engine)
|
||||
let outMul: [Double] = genMul.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(outMul[i] - (outNone[i] * 2.0 + 1.0)) < 1e-12,
|
||||
"mul/add mismatch at sample \(i)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// PulseCountControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("PulseCount.Control")
|
||||
struct PulseCountControlTests {
|
||||
|
||||
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: PulseCount.Control = PulseCount.Control(trig: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Without any trigger, previousOutputValue = 0.
|
||||
@Test func noTriggerCountStaysZero() {
|
||||
let gen: PulseCount.Control = PulseCount.Control(trig: 0.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
/// Rising edges increment the count; block-final count is previousOutputValue.
|
||||
@Test func edgesIncrementCount() {
|
||||
let trig: Closure = Closure { _, i in
|
||||
(i == 10 || i == 20 || i == 30) ? 1.0 : 0.0
|
||||
}
|
||||
let gen: PulseCount.Control = PulseCount.Control(trig: trig)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 3.0) < 1e-12,
|
||||
"expected count 3.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Reset clears count to 0.
|
||||
@Test func resetClearsCount() {
|
||||
let trig: Closure = Closure { _, i in (i == 5 || i == 15 || i == 25) ? 1.0 : 0.0 }
|
||||
let reset: Closure = Closure { _, i in i == 40 ? 1.0 : 0.0 }
|
||||
let gen: PulseCount.Control = PulseCount.Control(trig: trig, reset: reset)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Reset fires after trig edges; block-final count = 0.
|
||||
#expect(gen.previousOutputValue == 0.0,
|
||||
"after reset expected 0.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Output block is flat (discrete).
|
||||
@Test func outputBlockIsFlat() {
|
||||
let trig: Closure = Closure { _, i in i == 10 ? 1.0 : 0.0 }
|
||||
let gen: PulseCount.Control = PulseCount.Control(trig: trig)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let block: [Double] = Array(out[0].prefix(blockSize))
|
||||
let finalValue: Double = gen.previousOutputValue
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(block[i] == finalValue,
|
||||
"sample \(i) expected \(finalValue) got \(block[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: PulseCount.Control = PulseCount.Control(trig: 0.0)
|
||||
#expect(gen.floatValue(graph: graph) == 0.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Count persists across blocks.
|
||||
@Test func countPersistsAcrossBlocks() {
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
let trig: Closure = Closure { graph, i in
|
||||
graph.engine.segmentSerial == firstSerial && (i == 5 || i == 15) ? 1.0 : 0.0
|
||||
}
|
||||
let gen: PulseCount.Control = PulseCount.Control(trig: trig)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 2.0) < 1e-12)
|
||||
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 2.0) < 1e-12,
|
||||
"block-2 count expected 2.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in same segment return identical channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: PulseCount.Control = PulseCount.Control(trig: 0.0)
|
||||
let out1: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
let out2: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
// SampleAndHoldTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@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])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
// SchmidtAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Schmidt.Audio")
|
||||
struct SchmidtAudioTests {
|
||||
|
||||
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: Schmidt.Audio = Schmidt.Audio(input: 0.0, lo: 0.2, hi: 0.8)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Input starts below lo (0.2); state starts at 0 and never crosses hi (0.8).
|
||||
@Test func staysBelowLoRemainsOff() {
|
||||
// Constant input of 0.1 — below lo=0.2.
|
||||
let gen: Schmidt.Audio = Schmidt.Audio(input: 0.1, lo: 0.2, hi: 0.8)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Input rises above hi → state = 1.0, then drops to within band → stays 1.0 (hysteresis).
|
||||
@Test func hysteresisHoldsHighInBand() {
|
||||
// Input: 0.0 for samples 0..15; 0.9 for 16..31 (crosses hi=0.8);
|
||||
// 0.5 for 32..63 (within band [0.2, 0.8] — should stay high).
|
||||
let input: Closure = Closure { _, i in
|
||||
if i < 16 { return 0.0 }
|
||||
if i < 32 { return 0.9 }
|
||||
return 0.5
|
||||
}
|
||||
let gen: Schmidt.Audio = Schmidt.Audio(input: input, lo: 0.2, hi: 0.8)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
for i: Int in 0..<16 {
|
||||
#expect(out[i] == 0.0, "pre-hi sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
for i: Int in 16..<blockSize {
|
||||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||||
"post-hi sample \(i) expected 1.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Input drops below lo → state = 0.0, then rises into band → stays 0.0 (hysteresis).
|
||||
@Test func hysteresisHoldsLowInBand() {
|
||||
// State starts 0; force it high first, then let it drop.
|
||||
let input: Closure = Closure { _, i in
|
||||
if i < 10 { return 0.9 } // hi-cross → state=1
|
||||
if i < 20 { return 0.1 } // lo-cross → state=0
|
||||
return 0.5 // in-band → stay 0
|
||||
}
|
||||
let gen: Schmidt.Audio = Schmidt.Audio(input: input, lo: 0.2, hi: 0.8)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
for i: Int in 10..<20 {
|
||||
// State should drop to 0 as soon as input < lo.
|
||||
if i >= 10 {
|
||||
// At sample 10 input = 0.1 < lo=0.2, state → 0.
|
||||
if i == 10 {
|
||||
#expect(out[i] == 0.0,
|
||||
"sample \(i) after lo-cross expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
for i: Int in 20..<blockSize {
|
||||
#expect(out[i] == 0.0,
|
||||
"in-band-low sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// State persists across block boundaries.
|
||||
@Test func statePersistsAcrossBlocks() {
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
// Cross hi in block 1 and stay there.
|
||||
let input: Closure = Closure { graph, _ in
|
||||
graph.engine.segmentSerial == firstSerial ? 0.9 : 0.5
|
||||
}
|
||||
let gen: Schmidt.Audio = Schmidt.Audio(input: input, lo: 0.2, hi: 0.8)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 2: input = 0.5 (in band), state should still be 1.
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out2[i] - 1.0) < 1e-12,
|
||||
"block-2 sample \(i) expected 1.0 got \(out2[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in same segment return identical first channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: Schmidt.Audio = Schmidt.Audio(input: 0.5, lo: 0.2, hi: 0.8)
|
||||
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
|
||||
/// mulAdd: output × 2 + 1 when mul=2, add=1.
|
||||
@Test func mulAddApplied() {
|
||||
// Constant high input → output = 1.0 → mul/add = 1.0*2+1 = 3.0.
|
||||
let gen: Schmidt.Audio = Schmidt.Audio(input: 0.9, lo: 0.2, hi: 0.8, mul: 2.0, add: 1.0)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// First sample: state starts 0, then x=0.9 > hi=0.8 → state=1. At sample 0 state already 1.
|
||||
// (state update then output, so sample 0 = mulAdd(1.0) = 3.0).
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out[i] - 3.0) < 1e-12,
|
||||
"mul/add sample \(i) expected 3.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Scalar lo/hi thresholds: input oscillates above hi then drops below lo.
|
||||
@Test func scalarThresholdsHoldHysteresis() {
|
||||
// High then low input with scalar lo=0.2, hi=0.8.
|
||||
// Input: 0 for 0..9, 0.9 for 10..19 (crosses hi → state=1),
|
||||
// 0.5 for 20..39 (in band → stays 1),
|
||||
// 0.1 for 40..63 (crosses lo → state=0).
|
||||
let input: Closure = Closure { _, i in
|
||||
if i < 10 { return 0.0 }
|
||||
if i < 20 { return 0.9 }
|
||||
if i < 40 { return 0.5 }
|
||||
return 0.1
|
||||
}
|
||||
let gen: Schmidt.Audio = Schmidt.Audio(input: input, lo: 0.2, hi: 0.8)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// 0..9: state=0 (x < hi=0.8).
|
||||
for i: Int in 0..<10 {
|
||||
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
// 10..39: state=1 (hi crossed at 10; in-band 20..39 stays 1).
|
||||
for i: Int in 10..<40 {
|
||||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||||
"sample \(i) expected 1.0 got \(out[i])")
|
||||
}
|
||||
// 40..63: state=0 (x=0.1 < lo=0.2).
|
||||
for i: Int in 40..<blockSize {
|
||||
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// SchmidtControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Schmidt.Control")
|
||||
struct SchmidtControlTests {
|
||||
|
||||
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: Schmidt.Control = Schmidt.Control(input: 0.0, lo: 0.2, hi: 0.8)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Input never crosses hi; state stays 0.
|
||||
@Test func staysBelowHiRemainsOff() {
|
||||
let gen: Schmidt.Control = Schmidt.Control(input: 0.1, lo: 0.2, hi: 0.8)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
/// Input crosses hi; state becomes 1.0.
|
||||
@Test func crossingHiSetsState() {
|
||||
// Constant input above hi.
|
||||
let gen: Schmidt.Control = Schmidt.Control(input: 0.9, lo: 0.2, hi: 0.8)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 1.0) < 1e-12,
|
||||
"expected 1.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Hysteresis: after state=1, input in band does not drop to 0.
|
||||
@Test func hysteresisRetainsHighState() {
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
// Block 1: high input → state=1.
|
||||
let input: Closure = Closure { graph, _ in
|
||||
graph.engine.segmentSerial == firstSerial ? 0.9 : 0.5
|
||||
}
|
||||
let gen: Schmidt.Control = Schmidt.Control(input: input, lo: 0.2, hi: 0.8)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 1.0) < 1e-12)
|
||||
|
||||
advanceBlock(engine)
|
||||
// Block 2: input=0.5 (in band) → state stays 1.
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 1.0) < 1e-12,
|
||||
"hysteresis expected 1.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Output block is flat (discrete, not ramped).
|
||||
@Test func outputBlockIsFlat() {
|
||||
let gen: Schmidt.Control = Schmidt.Control(input: 0.9, lo: 0.2, hi: 0.8)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let block: [Double] = Array(out[0].prefix(blockSize))
|
||||
let finalValue: Double = gen.previousOutputValue
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(block[i] == finalValue,
|
||||
"sample \(i) expected \(finalValue) got \(block[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: Schmidt.Control = Schmidt.Control(input: 0.0, lo: 0.2, hi: 0.8)
|
||||
#expect(gen.floatValue(graph: graph) == 0.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in same segment return identical channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: Schmidt.Control = Schmidt.Control(input: 0.0, lo: 0.2, hi: 0.8)
|
||||
let out1: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
let out2: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,326 @@
|
||||
// SendTriggerTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - SendTrigger.Audio
|
||||
|
||||
@MainActor
|
||||
@Suite("SendTrigger.Audio")
|
||||
struct SendTriggerAudioTests {
|
||||
|
||||
func makeSetup() -> (SynthesisEngine, TriggerListener) {
|
||||
let engine: SynthesisEngine = makeTestEngine()
|
||||
let listener: TriggerListener = TriggerListener(engine: engine)
|
||||
return (engine, listener)
|
||||
}
|
||||
|
||||
// MARK: - Edge detection
|
||||
|
||||
@Test func singleRisingEdgeFiresOnce() {
|
||||
let (engine, listener) = makeSetup()
|
||||
nonisolated(unsafe) var received: [TriggerEvent] = []
|
||||
listener.addHandler { event, _ in received.append(event) }
|
||||
|
||||
// Rising edge at sample 256: 0…255 = 0.0, 256…511 = 1.0
|
||||
let trig: Closure = Closure { _, i in i < 256 ? 0.0 : 1.0 }
|
||||
let gen: SendTrigger.Audio = SendTrigger.Audio(trig: trig, id: 1)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 512)
|
||||
listener._drainSync()
|
||||
|
||||
#expect(received.count == 1)
|
||||
#expect(received[0].id == 1)
|
||||
}
|
||||
|
||||
@Test func sustainedHighProducesNoRepeatEvent() {
|
||||
let (engine, listener) = makeSetup()
|
||||
nonisolated(unsafe) var count: Int = 0
|
||||
listener.addHandler(id: 1) { _, _ in count += 1 }
|
||||
|
||||
let gen: SendTrigger.Audio = SendTrigger.Audio(trig: 1.0, id: 1) // already high at start
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 512)
|
||||
listener._drainSync()
|
||||
// _prevTrig starts at 0 so first sample IS a rising edge — fire once
|
||||
#expect(count == 1)
|
||||
|
||||
// Second block: still high, prevTrig now high — no new edge
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph2, count: 512)
|
||||
listener._drainSync()
|
||||
#expect(count == 1)
|
||||
}
|
||||
|
||||
@Test func multipleEdgesInOneBlockAllDelivered() {
|
||||
let (engine, listener) = makeSetup()
|
||||
nonisolated(unsafe) var count: Int = 0
|
||||
listener.addHandler(id: 2) { _, _ in count += 1 }
|
||||
|
||||
// Alternating 0, 1 pattern → 256 rising edges in 512 samples
|
||||
let trig: Closure = Closure { _, i in i % 2 == 0 ? 0.0 : 1.0 }
|
||||
let gen: SendTrigger.Audio = SendTrigger.Audio(trig: trig, id: 2)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 512)
|
||||
listener._drainSync()
|
||||
|
||||
#expect(count == 256)
|
||||
}
|
||||
|
||||
@Test func noEdgeWhenSignalStaysLow() {
|
||||
let (engine, listener) = makeSetup()
|
||||
nonisolated(unsafe) var count: Int = 0
|
||||
listener.addHandler(id: 3) { _, _ in count += 1 }
|
||||
|
||||
let gen: SendTrigger.Audio = SendTrigger.Audio(trig: 0.0, id: 3)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 512)
|
||||
listener._drainSync()
|
||||
|
||||
#expect(count == 0)
|
||||
}
|
||||
|
||||
// MARK: - Pass-through
|
||||
|
||||
@Test func outputEqualsTrigInput() {
|
||||
let engine: SynthesisEngine = makeTestEngine()
|
||||
let trig: Closure = Closure { _, i in i % 2 == 0 ? 0.0 : 1.0 }
|
||||
let gen: SendTrigger.Audio = SendTrigger.Audio(trig: trig, id: 9)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: 512)[0]
|
||||
|
||||
// Each sample should match the trig pattern
|
||||
for i: Int in 0..<512 {
|
||||
let expected: Double = i % 2 == 0 ? 0.0 : 1.0
|
||||
#expect(out[i] == expected)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Value payload
|
||||
|
||||
@Test func valuePayloadMatchesCrossingFrame() {
|
||||
let (engine, listener) = makeSetup()
|
||||
nonisolated(unsafe) var received: [TriggerEvent] = []
|
||||
listener.addHandler(id: 4) { event, _ in received.append(event) }
|
||||
|
||||
// Trig rises at sample 100; value = sample index * 0.001
|
||||
let trig: Closure = Closure { _, i in i < 100 ? 0.0 : 1.0 }
|
||||
let value: Closure = Closure { _, i in Double(i) * 0.001 }
|
||||
let gen: SendTrigger.Audio = SendTrigger.Audio(trig: trig, id: 4, value: value)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 512)
|
||||
listener._drainSync()
|
||||
|
||||
#expect(received.count == 1)
|
||||
// value at sample 100
|
||||
#expect(abs(received[0].value - 0.1) < 0.0001)
|
||||
}
|
||||
|
||||
// MARK: - ID routing
|
||||
|
||||
@Test func onlyMatchingIDHandlerFires() {
|
||||
let (engine, listener) = makeSetup()
|
||||
nonisolated(unsafe) var countA: Int = 0
|
||||
nonisolated(unsafe) var countB: Int = 0
|
||||
listener.addHandler(id: 10) { _, _ in countA += 1 }
|
||||
listener.addHandler(id: 20) { _, _ in countB += 1 }
|
||||
|
||||
let gen: SendTrigger.Audio = SendTrigger.Audio(trig: 1.0, id: 10)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 512)
|
||||
listener._drainSync()
|
||||
|
||||
#expect(countA == 1)
|
||||
#expect(countB == 0)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SendTrigger.Control
|
||||
|
||||
@MainActor
|
||||
@Suite("SendTrigger.Control")
|
||||
struct SendTriggerControlTests {
|
||||
|
||||
func makeSetup() -> (SynthesisEngine, TriggerListener) {
|
||||
let engine: SynthesisEngine = makeTestEngine()
|
||||
let listener: TriggerListener = TriggerListener(engine: engine)
|
||||
return (engine, listener)
|
||||
}
|
||||
|
||||
@Test func atMostOneEventPerBlock() {
|
||||
let (engine, listener) = makeSetup()
|
||||
nonisolated(unsafe) var count: Int = 0
|
||||
listener.addHandler(id: 5) { _, _ in count += 1 }
|
||||
|
||||
// Dense alternating signal — many potential edges, but control gate allows only one
|
||||
let trig: Closure = Closure { _, i in i % 2 == 0 ? 0.0 : 1.0 }
|
||||
let gen: SendTrigger.Control = SendTrigger.Control(trig: trig, id: 5)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 512)
|
||||
listener._drainSync()
|
||||
|
||||
#expect(count == 1)
|
||||
}
|
||||
|
||||
@Test func noEdgeLowSignalNoEvent() {
|
||||
let (engine, listener) = makeSetup()
|
||||
nonisolated(unsafe) var count: Int = 0
|
||||
listener.addHandler(id: 6) { _, _ in count += 1 }
|
||||
|
||||
let gen: SendTrigger.Control = SendTrigger.Control(trig: 0.0, id: 6)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 512)
|
||||
listener._drainSync()
|
||||
|
||||
#expect(count == 0)
|
||||
}
|
||||
|
||||
@Test func secondBlockNoEdgeWhenAlreadyHigh() {
|
||||
let (engine, listener) = makeSetup()
|
||||
nonisolated(unsafe) var count: Int = 0
|
||||
listener.addHandler(id: 7) { _, _ in count += 1 }
|
||||
|
||||
let gen: SendTrigger.Control = SendTrigger.Control(trig: 1.0, id: 7)
|
||||
|
||||
let graph1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph1, count: 512)
|
||||
listener._drainSync()
|
||||
#expect(count == 1) // first block: prevTrig=0 → edge fires
|
||||
|
||||
advanceBlock(engine)
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph2, count: 512)
|
||||
listener._drainSync()
|
||||
#expect(count == 1) // second block: prevTrig=1 → no new edge
|
||||
}
|
||||
|
||||
@Test func passThroughReturnsTrigSamples() {
|
||||
let engine: SynthesisEngine = makeTestEngine()
|
||||
let pattern: Double = 0.75
|
||||
let gen: SendTrigger.Control = SendTrigger.Control(trig: pattern, id: 8)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: 512)[0]
|
||||
|
||||
// All samples should be the trig value (0.75 constant input)
|
||||
for i: Int in 0..<512 {
|
||||
#expect(out[i] == 0.75)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func controlValueIsFloatValueAtCrossingBlock() {
|
||||
let (engine, listener) = makeSetup()
|
||||
nonisolated(unsafe) var received: [TriggerEvent] = []
|
||||
listener.addHandler(id: 9) { event, _ in received.append(event) }
|
||||
|
||||
let gen: SendTrigger.Control = SendTrigger.Control(trig: 1.0, id: 9, value: 0.42)
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: 512)
|
||||
listener._drainSync()
|
||||
|
||||
#expect(received.count == 1)
|
||||
#expect(abs(received[0].value - 0.42) < 0.0001)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - NRT integration
|
||||
|
||||
@MainActor
|
||||
@Suite("SendTrigger NRT")
|
||||
struct SendTriggerNRTTests {
|
||||
|
||||
func makeTempURL() -> URL {
|
||||
FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(UUID().uuidString + ".caf")
|
||||
}
|
||||
|
||||
@Test func nrtTriggerHandlerFiresAndCountsBlocks() throws {
|
||||
let url: URL = makeTempURL()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
let driver: NRTDriver = NRTDriver(outputURL: url, duration: 0.1) // ~8-9 blocks at 512
|
||||
let engine: SynthesisEngine = SynthesisEngine(
|
||||
blockSize: 512,
|
||||
sampleRate: 44100,
|
||||
numOutputChannels: 2,
|
||||
numInputChannels: 0,
|
||||
numAudioBuses: 128,
|
||||
numControlBuses: 4096,
|
||||
driver: driver
|
||||
)
|
||||
|
||||
let listener: TriggerListener = TriggerListener(engine: engine)
|
||||
driver.triggerListener = listener
|
||||
|
||||
// Synthesizer that fires a trigger every block via an Impulse-like constant
|
||||
final class TriggerEveryBlock: SynthesizerBase {
|
||||
override func build() {
|
||||
// 1.0 constant: prevTrig starts at 0 so first block fires, then stays high
|
||||
addOutputGenerator(SendTrigger.Control(trig: 1.0, id: 99))
|
||||
}
|
||||
}
|
||||
|
||||
nonisolated(unsafe) var fireCount: Int = 0
|
||||
listener.addHandler(id: 99) { _, _ in fireCount += 1 }
|
||||
|
||||
try engine.add(TriggerEveryBlock())
|
||||
try engine.start()
|
||||
|
||||
// One trigger fires on the first block only (constant high → no subsequent edges)
|
||||
#expect(fireCount == 1)
|
||||
}
|
||||
|
||||
@Test func nrtCtxAddSynthRendersInSubsequentBlocks() throws {
|
||||
let url: URL = makeTempURL()
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
// 9+ blocks: first block fires trigger, subsequent blocks should render added synth
|
||||
let driver: NRTDriver = NRTDriver(outputURL: url, duration: 0.12)
|
||||
let engine: SynthesisEngine = SynthesisEngine(
|
||||
blockSize: 512,
|
||||
sampleRate: 44100,
|
||||
numOutputChannels: 2,
|
||||
numInputChannels: 0,
|
||||
numAudioBuses: 128,
|
||||
numControlBuses: 4096,
|
||||
driver: driver
|
||||
)
|
||||
|
||||
let listener: TriggerListener = TriggerListener(engine: engine)
|
||||
driver.triggerListener = listener
|
||||
|
||||
// Synth that fires one trigger on first block (constant 1.0 → edge on first block only)
|
||||
final class OneShotTrigger: SynthesizerBase {
|
||||
override func build() {
|
||||
addOutputGenerator(SendTrigger.Control(trig: 1.0, id: 55))
|
||||
}
|
||||
}
|
||||
|
||||
// Synth added dynamically by the trigger handler; counts evaluate calls
|
||||
nonisolated(unsafe) var synthEvalCount: Int = 0
|
||||
|
||||
final class CountingSynth: Synthesizer {
|
||||
let counter: () -> Void
|
||||
init(counter: @escaping () -> Void) { self.counter = counter }
|
||||
func build() {}
|
||||
func evaluate(graph: SynthGraph, count: Int) { counter() }
|
||||
}
|
||||
|
||||
listener.addHandler(id: 55) { _, ctx in
|
||||
ctx.add(CountingSynth(counter: { synthEvalCount += 1 }))
|
||||
}
|
||||
|
||||
try engine.add(OneShotTrigger())
|
||||
try engine.start()
|
||||
|
||||
// CountingSynth was added via ctx.add in the first-block handler.
|
||||
// NRT inline executor: add is synchronous, so synth is in pendingQueue before next processBlock.
|
||||
// It should have been evaluated for all remaining blocks after the trigger.
|
||||
#expect(synthEvalCount > 0)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
// SignalChangedTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("SignalChanged.Audio")
|
||||
struct SignalChangedAudioTests {
|
||||
|
||||
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: SignalChanged.Audio = SignalChanged.Audio(input: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Constant input → no per-sample change → output all zeros.
|
||||
@Test func constantInputProducesZeros() {
|
||||
let gen: SignalChanged.Audio = SignalChanged.Audio(input: 0.5)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// First sample: prev=0, current=0.5 → diff=0.5 > 0 → 1.0 (initial transition)
|
||||
#expect(out[0] == 1.0)
|
||||
// Subsequent samples: 0.5 == 0.5 → 0.0
|
||||
for i: Int in 1..<blockSize {
|
||||
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Step input fires `1.0` only on the change sample.
|
||||
@Test func stepInputFiresOnChangeSample() {
|
||||
// 0.0 for samples 0..<32, then 1.0 for samples 32..<64.
|
||||
let stepInput: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
||||
let gen: SignalChanged.Audio = SignalChanged.Audio(input: stepInput)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
if i == 32 {
|
||||
#expect(out[i] == 1.0, "expected change fire at sample 32")
|
||||
} else {
|
||||
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Threshold gates small changes.
|
||||
@Test func thresholdSuppressesSubthresholdChanges() {
|
||||
// Tiny step inside the threshold band.
|
||||
let smallStep: Closure = Closure { _, i in i < 32 ? 0.0 : 0.05 }
|
||||
let gen: SignalChanged.Audio = SignalChanged.Audio(input: smallStep, threshold: 0.1)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[i] == 0.0, "sample \(i) should not fire below threshold")
|
||||
}
|
||||
}
|
||||
|
||||
@Test func thresholdAllowsAboveThresholdChanges() {
|
||||
let bigStep: Closure = Closure { _, i in i < 32 ? 0.0 : 0.5 }
|
||||
let gen: SignalChanged.Audio = SignalChanged.Audio(input: bigStep, threshold: 0.1)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
#expect(out[32] == 1.0)
|
||||
for i: Int in 0..<blockSize where i != 32 {
|
||||
#expect(out[i] == 0.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Negative-going changes are detected via abs().
|
||||
@Test func negativeChangeFires() {
|
||||
let drop: Closure = Closure { _, i in i < 32 ? 1.0 : 0.0 }
|
||||
let gen: SignalChanged.Audio = SignalChanged.Audio(input: drop)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// First sample: prev=0, current=1.0 → fires.
|
||||
#expect(out[0] == 1.0)
|
||||
// Sample 32: 1.0 → 0.0, change=1.0 > 0 → fires.
|
||||
#expect(out[32] == 1.0)
|
||||
}
|
||||
|
||||
/// State persists across blocks: a sample equal to the previous block's
|
||||
/// last sample produces no change at the start of the next block.
|
||||
@Test func prevSampleCarriesAcrossBlocks() {
|
||||
// Per-block-discriminated closure: block 1 = 1.0, block 2 = 1.0
|
||||
// (continuous), so no change anywhere in block 2.
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
let constInput: Closure = Closure { graph, i in
|
||||
graph.engine.segmentSerial == firstSerial && i == 0 ? 1.0 : 1.0
|
||||
}
|
||||
let gen: SignalChanged.Audio = SignalChanged.Audio(input: constInput)
|
||||
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// First sample of block 1: prev=0, current=1.0 → fires.
|
||||
#expect(out1[0] == 1.0)
|
||||
for i: Int in 1..<blockSize { #expect(out1[i] == 0.0) }
|
||||
|
||||
advanceBlock(engine)
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out2[i] == 0.0, "block 2 sample \(i) should be 0.0 (no change)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("SignalChanged.Control")
|
||||
struct SignalChangedControlTests {
|
||||
|
||||
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: SignalChanged.Control = SignalChanged.Control(input: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
@Test func constantInputProducesZeroAfterFirstBlock() {
|
||||
let gen: SignalChanged.Control = SignalChanged.Control(input: 0.5)
|
||||
// First block: 0 → 0.5 transition → fires.
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == 1.0)
|
||||
|
||||
// Second block: 0.5 → 0.5 → no change.
|
||||
advanceBlock(engine)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[i] == 0.0)
|
||||
}
|
||||
#expect(gen.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
@Test func stepInputFiresOnceForBlock() {
|
||||
let step: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
||||
let gen: SignalChanged.Control = SignalChanged.Control(input: step)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// Block-constant 1.0: any change in the block fires for the whole block.
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[i] == 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: SignalChanged.Control = SignalChanged.Control(input: 0.5)
|
||||
#expect(gen.floatValue(graph: graph) == 0.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
@Suite("SignalChanged generator-driven threshold")
|
||||
struct SignalChangedGeneratorThresholdTests {
|
||||
|
||||
let engine: SynthesisEngine
|
||||
var graph: SynthGraph { makeSynthGraph(engine: engine) }
|
||||
let blockSize: Int = 64
|
||||
|
||||
init() throws {
|
||||
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
|
||||
try engine.start()
|
||||
}
|
||||
|
||||
/// A live ParameterNode threshold is read per-sample.
|
||||
@Test func parameterNodeThresholdIsHonoured() {
|
||||
let thresh: ParameterNode = ParameterNode(0.5)
|
||||
let step: Closure = Closure { _, i in i < 32 ? 0.0 : 0.3 }
|
||||
let gen: SignalChanged.Audio = SignalChanged.Audio(input: step, threshold: thresh)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// With threshold 0.5, a 0.3 step does NOT exceed.
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[i] == 0.0, "sample \(i) should be 0.0 with threshold 0.5")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
// StepperAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Stepper.Audio")
|
||||
struct StepperAudioTests {
|
||||
|
||||
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: Stepper.Audio = Stepper.Audio(trig: 0.0)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Without any trigger, value stays at resetval (default 0).
|
||||
@Test func noTriggerStaysAtResetval() {
|
||||
let gen: Stepper.Audio = Stepper.Audio(trig: 0.0, min: 0, max: 7)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Stepping from 0 with min=0 max=7 step=1 wraps at 8 (0,1,2,...,7,0,1,...).
|
||||
@Test func stepsAndWraps() {
|
||||
// Eight edges: values 1,2,3,4,5,6,7,0.
|
||||
let trig: Closure = Closure { _, i in
|
||||
(i == 0 || i == 4 || i == 8 || i == 12 || i == 16 ||
|
||||
i == 20 || i == 24 || i == 28) ? 1.0 : 0.0
|
||||
}
|
||||
let gen: Stepper.Audio = Stepper.Audio(trig: trig, min: 0, max: 7, step: 1, resetval: 0)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// Sample 0: trig edge → value becomes 1.
|
||||
for i: Int in 0..<4 { #expect(out[i] == 1.0, "sample \(i): \(out[i])") }
|
||||
for i: Int in 4..<8 { #expect(out[i] == 2.0, "sample \(i): \(out[i])") }
|
||||
for i: Int in 8..<12 { #expect(out[i] == 3.0, "sample \(i): \(out[i])") }
|
||||
for i: Int in 12..<16 { #expect(out[i] == 4.0, "sample \(i): \(out[i])") }
|
||||
for i: Int in 16..<20 { #expect(out[i] == 5.0, "sample \(i): \(out[i])") }
|
||||
for i: Int in 20..<24 { #expect(out[i] == 6.0, "sample \(i): \(out[i])") }
|
||||
for i: Int in 24..<28 { #expect(out[i] == 7.0, "sample \(i): \(out[i])") }
|
||||
// 8th edge wraps: 0..28 edges all fired, value wraps to 0.
|
||||
for i: Int in 28..<blockSize { #expect(out[i] == 0.0, "sample \(i): \(out[i])") }
|
||||
}
|
||||
|
||||
/// Reset edge returns value to resetval.
|
||||
@Test func resetReturnsToResetval() {
|
||||
// Use resetval=0 (initial value). Three edges bring value to 3.
|
||||
// Reset at sample 40 sets value back to resetval=2 (distinct from post-trig value).
|
||||
let trig: Closure = Closure { _, i in
|
||||
(i == 5 || i == 10 || i == 15) ? 1.0 : 0.0
|
||||
}
|
||||
let reset: Closure = Closure { _, i in i == 40 ? 1.0 : 0.0 }
|
||||
let gen: Stepper.Audio = Stepper.Audio(
|
||||
trig: trig,
|
||||
reset: reset,
|
||||
min: 0,
|
||||
max: 7,
|
||||
step: 1,
|
||||
resetval: 2
|
||||
)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// Initial value = 2 (resetval). After 3 edges at 5,10,15: value = 5.
|
||||
#expect(abs(out[39] - 5.0) < 1e-12,
|
||||
"pre-reset expected 5.0 got \(out[39])")
|
||||
// After reset at 40: value = 2.
|
||||
for i: Int in 40..<blockSize {
|
||||
#expect(abs(out[i] - 2.0) < 1e-12,
|
||||
"post-reset sample \(i) expected 2.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Negative step steps down and wraps.
|
||||
@Test func negativeStepWraps() {
|
||||
// Start at resetval=7, step=-1, range [0..7].
|
||||
let trig: Closure = Closure { _, i in
|
||||
(i == 0 || i == 4 || i == 8) ? 1.0 : 0.0
|
||||
}
|
||||
let gen: Stepper.Audio = Stepper.Audio(
|
||||
trig: trig,
|
||||
min: 0,
|
||||
max: 7,
|
||||
step: -1,
|
||||
resetval: 7
|
||||
)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// Edge 0: 7 + (-1) = 6.
|
||||
for i: Int in 0..<4 { #expect(out[i] == 6.0, "sample \(i): \(out[i])") }
|
||||
// Edge 4: 6 + (-1) = 5.
|
||||
for i: Int in 4..<8 { #expect(out[i] == 5.0, "sample \(i): \(out[i])") }
|
||||
// Edge 8: 5 + (-1) = 4.
|
||||
for i: Int in 8..<blockSize { #expect(out[i] == 4.0, "sample \(i): \(out[i])") }
|
||||
}
|
||||
|
||||
/// State persists across block boundaries.
|
||||
@Test func statePersistsAcrossBlocks() {
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
let trig: Closure = Closure { graph, i in
|
||||
graph.engine.segmentSerial == firstSerial && (i == 5 || i == 15 || i == 25)
|
||||
? 1.0 : 0.0
|
||||
}
|
||||
let gen: Stepper.Audio = Stepper.Audio(trig: trig, min: 0, max: 7)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(out2[i] - 3.0) < 1e-12,
|
||||
"block-2 sample \(i) expected 3.0 got \(out2[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in same segment return identical first channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: Stepper.Audio = Stepper.Audio(trig: 0.0)
|
||||
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
|
||||
/// mulAdd: step output × 2 + 1 when mul=2, add=1.
|
||||
@Test func mulAddApplied() {
|
||||
let trig: Closure = Closure { _, i in i == 0 ? 1.0 : 0.0 }
|
||||
let genNoMul: Stepper.Audio = Stepper.Audio(trig: trig, min: 0, max: 7, mul: 1.0, add: 0.0)
|
||||
let genMul: Stepper.Audio = Stepper.Audio(trig: trig, min: 0, max: 7, mul: 2.0, add: 1.0)
|
||||
let outNone: [Double] = genNoMul.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
advanceBlock(engine)
|
||||
let outMul: [Double] = genMul.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(outMul[i] - (outNone[i] * 2.0 + 1.0)) < 1e-12,
|
||||
"mul/add mismatch at sample \(i)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// StepperControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Stepper.Control")
|
||||
struct StepperControlTests {
|
||||
|
||||
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: Stepper.Control = Stepper.Control(trig: 0.0)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Without any trigger, previousOutputValue = resetval (default 0).
|
||||
@Test func noTriggerStaysAtResetval() {
|
||||
let gen: Stepper.Control = Stepper.Control(trig: 0.0, min: 0, max: 7, resetval: 0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
/// Block-final step value after three triggers.
|
||||
@Test func stepsAccumulate() {
|
||||
let trig: Closure = Closure { _, i in
|
||||
(i == 10 || i == 20 || i == 30) ? 1.0 : 0.0
|
||||
}
|
||||
let gen: Stepper.Control = Stepper.Control(trig: trig, min: 0, max: 7, step: 1, resetval: 0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 3.0) < 1e-12,
|
||||
"expected 3.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Wrap: 8 steps from 0 wraps back to 0.
|
||||
@Test func wrapsAroundRange() {
|
||||
let trig: Closure = Closure { _, i in
|
||||
(i == 0 || i == 4 || i == 8 || i == 12 || i == 16 ||
|
||||
i == 20 || i == 24 || i == 28) ? 1.0 : 0.0
|
||||
}
|
||||
let gen: Stepper.Control = Stepper.Control(trig: trig, min: 0, max: 7, step: 1, resetval: 0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// 8 steps from 0 in range 0..7 (span=8) wraps to 0.
|
||||
#expect(gen.previousOutputValue == 0.0,
|
||||
"after 8 steps expected 0.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Reset edge returns value to resetval.
|
||||
@Test func resetReturnsToResetval() {
|
||||
let trig: Closure = Closure { _, i in (i == 5 || i == 10) ? 1.0 : 0.0 }
|
||||
let reset: Closure = Closure { _, i in i == 40 ? 1.0 : 0.0 }
|
||||
let gen: Stepper.Control = Stepper.Control(
|
||||
trig: trig,
|
||||
reset: reset,
|
||||
min: 0,
|
||||
max: 7,
|
||||
resetval: 4
|
||||
)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Reset fires at 40, after two triggers, so block-final value = 4.
|
||||
#expect(abs(gen.previousOutputValue - 4.0) < 1e-12,
|
||||
"after reset expected 4.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Output block is flat (discrete, not ramped).
|
||||
@Test func outputBlockIsFlat() {
|
||||
let trig: Closure = Closure { _, i in i == 10 ? 1.0 : 0.0 }
|
||||
let gen: Stepper.Control = Stepper.Control(trig: trig, min: 0, max: 7)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let block: [Double] = Array(out[0].prefix(blockSize))
|
||||
let finalValue: Double = gen.previousOutputValue
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(block[i] == finalValue,
|
||||
"sample \(i) expected \(finalValue) got \(block[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: Stepper.Control = Stepper.Control(trig: 0.0)
|
||||
#expect(gen.floatValue(graph: graph) == 0.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in same segment return identical channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: Stepper.Control = Stepper.Control(trig: 0.0)
|
||||
let out1: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
let out2: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
// Trig1AudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Trig1.Audio")
|
||||
struct Trig1AudioTests {
|
||||
|
||||
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: Trig1.Audio = Trig1.Audio(input: 0.0, dur: 0.1)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Constant-zero input produces no rising edge; output stays at 0.
|
||||
@Test func noTriggerOutputsZero() {
|
||||
let gen: Trig1.Audio = Trig1.Audio(input: 0.0, dur: 0.1)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Rising edge at sample 16 outputs 1.0 (NOT the input level) for dur samples.
|
||||
/// Trig1 always outputs unity regardless of the input amplitude.
|
||||
@Test func risingEdgeOutputsUnity() {
|
||||
let durSamples: Int = 10
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Input = 0 until sample 16, then 0.99 (non-unity) to confirm Trig1 outputs 1.0 not 0.99.
|
||||
let input: Closure = Closure { _, i in i < 16 ? 0.0 : 0.99 }
|
||||
let gen: Trig1.Audio = Trig1.Audio(input: input, dur: dur)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// Before edge: 0.
|
||||
for i: Int in 0..<16 {
|
||||
#expect(out[i] == 0.0, "pre-edge sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
// Pulse: exactly 1.0 regardless of input level.
|
||||
for i: Int in 16..<(16 + durSamples) {
|
||||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||||
"pulse sample \(i) expected 1.0 got \(out[i])")
|
||||
}
|
||||
// After pulse: 0.
|
||||
for i: Int in (16 + durSamples)..<blockSize {
|
||||
#expect(out[i] == 0.0,
|
||||
"post-pulse sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Sustained input > 0 (no falling edge) produces only one pulse.
|
||||
@Test func sustainedTrigProducesOnePulse() {
|
||||
let durSamples: Int = 8
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Input stays at 0.5 from sample 10 onwards (no falling edge → no second pulse).
|
||||
let input: Closure = Closure { _, i in i >= 10 ? 0.5 : 0.0 }
|
||||
let gen: Trig1.Audio = Trig1.Audio(input: input, dur: dur)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// Pulse samples: 10..(10+durSamples-1) = 1.0.
|
||||
for i: Int in 10..<(10 + durSamples) {
|
||||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||||
"pulse sample \(i) expected 1.0 got \(out[i])")
|
||||
}
|
||||
// After pulse: 0 (trig still high but no new edge — no re-fire).
|
||||
for i: Int in (10 + durSamples)..<blockSize {
|
||||
#expect(out[i] == 0.0,
|
||||
"post-pulse sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Input drops back to 0 then rises again — new rising edge restarts timer at 1.0.
|
||||
@Test func newEdgeRestartsTimer() {
|
||||
let durSamples: Int = 20
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// First edge at 5, drops to 0 at 10, second edge at 15.
|
||||
let input: Closure = Closure { _, i in
|
||||
if i < 5 { return 0.0 }
|
||||
if i < 10 { return 0.5 }
|
||||
if i < 15 { return 0.0 }
|
||||
return 0.5
|
||||
}
|
||||
let gen: Trig1.Audio = Trig1.Audio(input: input, dur: dur)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// From 15 onwards (re-triggered) all samples in the 20-sample window = 1.0.
|
||||
for i: Int in 15..<(15 + durSamples) {
|
||||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||||
"re-trigger sample \(i) expected 1.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in same segment return identical first channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: Trig1.Audio = Trig1.Audio(input: 0.0, dur: 0.01)
|
||||
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
|
||||
/// mulAdd: pulse output × 2 + 1 = 3.0 during hold.
|
||||
@Test func mulAddApplied() {
|
||||
let durSamples: Int = 20
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Input goes from 0 to 0.5 at sample 0 (fires immediately).
|
||||
let input: Closure = Closure { _, _ in 0.5 }
|
||||
let genMulAdd: Trig1.Audio = Trig1.Audio(input: input, dur: dur, mul: 2.0, add: 1.0)
|
||||
let out: [Double] = genMulAdd.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// 1.0 * 2 + 1 = 3.0 during pulse.
|
||||
for i: Int in 0..<durSamples {
|
||||
#expect(abs(out[i] - 3.0) < 1e-12,
|
||||
"mul/add sample \(i) expected 3.0 got \(out[i])")
|
||||
}
|
||||
// After pulse: 0.0 * 2 + 1 = 1.0.
|
||||
for i: Int in durSamples..<blockSize {
|
||||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||||
"post-pulse mul/add sample \(i) expected 1.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
// Trig1ControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Trig1.Control")
|
||||
struct Trig1ControlTests {
|
||||
|
||||
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: Trig1.Control = Trig1.Control(input: 0.0, dur: 0.1)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Constant-zero input produces no rising edge; output holds 0.
|
||||
@Test func noTriggerOutputsZero() {
|
||||
let gen: Trig1.Control = Trig1.Control(input: 0.0, dur: 0.1)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
/// Rising edge in the block causes previousOutputValue = 1.0 (unity, not input level).
|
||||
@Test func risingEdgeOutputsUnity() {
|
||||
let durSamples: Int = blockSize * 2
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Input = 0 for samples 0..15, then 0.99 (non-unity to confirm Trig1 outputs 1.0).
|
||||
let input: Closure = Closure { _, i in i < 16 ? 0.0 : 0.99 }
|
||||
let gen: Trig1.Control = Trig1.Control(input: input, dur: dur)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 1.0) < 1e-12,
|
||||
"previousOutputValue expected 1.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Boundary: a pulse whose duration equals exactly one block stays high
|
||||
/// across every sample, so the block-final value is 1.0 — not 0
|
||||
/// (regression for inferring output from post-loop `remaining`).
|
||||
@Test func pulseSpanningExactlyOneBlockOutputsUnity() {
|
||||
let durSamples: Int = blockSize // pulse == one block exactly
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
let gen: Trig1.Control = Trig1.Control(input: 0.3, dur: dur)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 1.0) < 1e-12,
|
||||
"full-block pulse expected 1.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Output block is flat (discrete, not ramped).
|
||||
@Test func outputBlockIsFlat() {
|
||||
let durSamples: Int = blockSize * 2
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Constant 0.3 input — fires at sample 0 (prevTrig=0, x=0.3 > 0).
|
||||
let gen: Trig1.Control = Trig1.Control(input: 0.3, dur: dur)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let block: [Double] = Array(out[0].prefix(blockSize))
|
||||
// After trigger at sample 0, output is unity = 1.0 flat.
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(block[i] - 1.0) < 1e-12,
|
||||
"sample \(i) expected 1.0 got \(block[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Countdown expires: after dur samples output returns to 0.
|
||||
@Test func countdownExpires() {
|
||||
let durSamples: Int = blockSize + 10
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
let input: Closure = Closure { graph, i in
|
||||
graph.engine.segmentSerial == firstSerial && i == 0 ? 1.0 : 0.0
|
||||
}
|
||||
let gen: Trig1.Control = Trig1.Control(input: input, dur: dur)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 1.0) < 1e-12)
|
||||
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == 0.0,
|
||||
"after countdown expired expected 0.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: Trig1.Control = Trig1.Control(input: 0.0, dur: 0.1)
|
||||
#expect(gen.floatValue(graph: graph) == 0.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in same segment return identical channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: Trig1.Control = Trig1.Control(input: 0.0, dur: 0.1)
|
||||
let out1: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
let out2: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
// TrigAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Trig.Audio")
|
||||
struct TrigAudioTests {
|
||||
|
||||
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: Trig.Audio = Trig.Audio(input: 0.0, dur: 0.1)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Constant-zero input produces no rising edge; output stays at 0.
|
||||
@Test func noTriggerOutputsZero() {
|
||||
let gen: Trig.Audio = Trig.Audio(input: 0.0, dur: 0.1)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Rising edge at sample 16 outputs the input value at that frame for `dur` samples.
|
||||
/// The `input` signal is both the trigger source and the captured level.
|
||||
@Test func risingEdgeOutputsInputLevel() {
|
||||
let durSamples: Int = 10
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Input is 0 for samples 0..15, then 0.16 for samples 16+.
|
||||
// Rising edge fires at sample 16; level captured = 0.16.
|
||||
let input: Closure = Closure { _, i in i < 16 ? 0.0 : 0.16 }
|
||||
let gen: Trig.Audio = Trig.Audio(input: input, dur: dur)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// Before edge: 0.
|
||||
for i: Int in 0..<16 {
|
||||
#expect(out[i] == 0.0, "pre-edge sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
// Edge and dur-1 more samples: level = 0.16.
|
||||
for i: Int in 16..<(16 + durSamples) {
|
||||
#expect(abs(out[i] - 0.16) < 1e-12,
|
||||
"pulse sample \(i) expected 0.16 got \(out[i])")
|
||||
}
|
||||
// After pulse: 0.
|
||||
for i: Int in (16 + durSamples)..<blockSize {
|
||||
#expect(out[i] == 0.0, "post-pulse sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// New rising edge during hold (input drops to 0 then goes positive again)
|
||||
/// restarts timer and re-captures level.
|
||||
@Test func newEdgeRestartsTimer() {
|
||||
let durSamples: Int = 20
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// First edge at sample 5 (input goes from 0 to 0.05).
|
||||
// Second edge at sample 15 (input drops to 0 then goes to 0.15).
|
||||
let input: Closure = Closure { _, i in
|
||||
if i < 5 { return 0.0 }
|
||||
if i < 10 { return 0.05 }
|
||||
if i < 15 { return 0.0 }
|
||||
return 0.15
|
||||
}
|
||||
let gen: Trig.Audio = Trig.Audio(input: input, dur: dur)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// After second edge at 15 the level should be re-captured as 0.15.
|
||||
// Check samples 15..34 (20 samples) for level 0.15.
|
||||
for i: Int in 15..<(15 + durSamples) {
|
||||
#expect(abs(out[i] - 0.15) < 1e-12,
|
||||
"re-capture sample \(i) expected 0.15 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Sustained input > 0 (no falling edge) produces only one capture.
|
||||
@Test func sustainedTrigProducesOneCapture() {
|
||||
let durSamples: Int = 8
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Input goes from 0 to 0.10 at sample 10 and stays.
|
||||
let input: Closure = Closure { _, i in i >= 10 ? 0.10 : 0.0 }
|
||||
let gen: Trig.Audio = Trig.Audio(input: input, dur: dur)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// Captured at i=10: level 0.10.
|
||||
for i: Int in 10..<(10 + durSamples) {
|
||||
#expect(abs(out[i] - 0.10) < 1e-12,
|
||||
"sustained sample \(i) expected 0.10 got \(out[i])")
|
||||
}
|
||||
// After pulse ends, input still high but no new edge → output = 0.
|
||||
for i: Int in (10 + durSamples)..<blockSize {
|
||||
#expect(out[i] == 0.0,
|
||||
"post-pulse sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// State (remaining countdown) persists across block boundaries.
|
||||
@Test func heldCountdownPersistsAcrossBlocks() {
|
||||
// dur > 1 block: fire in block 1, still counting in block 2.
|
||||
let durSamples: Int = 100
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
// Input = 0.5 for sample 0 of block 1 only, 0 elsewhere.
|
||||
let input: Closure = Closure { graph, i in
|
||||
graph.engine.segmentSerial == firstSerial && i == 0 ? 0.5 : 0.0
|
||||
}
|
||||
let gen: Trig.Audio = Trig.Audio(input: input, dur: dur)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Block 2: 64 more samples consumed from the 100-sample countdown.
|
||||
// We fired at sample 0 in block 1, consuming 64 of the 100 samples.
|
||||
// In block 2 the remaining = 100 - 64 = 36 samples should still be high.
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<36 {
|
||||
#expect(abs(out2[i] - 0.5) < 1e-12,
|
||||
"block-2 pulse sample \(i) expected 0.5 got \(out2[i])")
|
||||
}
|
||||
for i: Int in 36..<blockSize {
|
||||
#expect(out2[i] == 0.0,
|
||||
"block-2 post-pulse sample \(i) expected 0.0 got \(out2[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in the same segment return identical first channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let input: Closure = Closure { _, i in i == 16 ? 0.5 : 0.0 }
|
||||
let gen: Trig.Audio = Trig.Audio(input: input, dur: 0.01)
|
||||
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
|
||||
/// mulAdd: output is original × 2 + 1 when mul=2, add=1.
|
||||
@Test func mulAddApplied() {
|
||||
let durSamples: Int = 20
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
let input: Closure = Closure { _, i in i == 0 ? 0.5 : 0.0 }
|
||||
let genNoMulAdd: Trig.Audio = Trig.Audio(input: input, dur: dur, mul: 1.0, add: 0.0)
|
||||
let genMulAdd: Trig.Audio = Trig.Audio(input: input, dur: dur, mul: 2.0, add: 1.0)
|
||||
let outNone: [Double] = genNoMulAdd.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
advanceBlock(engine)
|
||||
let outMul: [Double] = genMulAdd.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// During the pulse period, outNone[i] = 0.5, outMul[i] should be 0.5*2+1 = 2.0.
|
||||
for i: Int in 0..<durSamples {
|
||||
#expect(abs(outMul[i] - (outNone[i] * 2.0 + 1.0)) < 1e-12,
|
||||
"mul/add mismatch at sample \(i)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
// TrigControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("Trig.Control")
|
||||
struct TrigControlTests {
|
||||
|
||||
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: Trig.Control = Trig.Control(input: 0.0, dur: 0.1)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Constant-zero input produces no rising edge; output holds 0.
|
||||
@Test func noTriggerOutputsZero() {
|
||||
let gen: Trig.Control = Trig.Control(input: 0.0, dur: 0.1)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
/// Rising edge at sample 16 causes output to hold the captured level for the block.
|
||||
@Test func risingEdgeCapturesLevel() {
|
||||
let durSamples: Int = blockSize * 2 // multi-block hold
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Input is 0 for samples 0..15, then 0.16 for the rest.
|
||||
let input: Closure = Closure { _, i in i < 16 ? 0.0 : 0.16 }
|
||||
let gen: Trig.Control = Trig.Control(input: input, dur: dur)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// The block contains an edge at sample 16 — level 0.16 should be captured.
|
||||
#expect(abs(gen.previousOutputValue - 0.16) < 1e-12,
|
||||
"previousOutputValue expected 0.16 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Countdown advances once per block via bypass; expires after dur.
|
||||
@Test func subBlockBypassCallsOncePerBlock() {
|
||||
// dur slightly longer than one block so we can test 2-block expiry.
|
||||
let durSamples: Int = blockSize + 10
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
// Input = 1.0 at sample 0 of block 1 only.
|
||||
let input: Closure = Closure { graph, i in
|
||||
graph.engine.segmentSerial == firstSerial && i == 0 ? 1.0 : 0.0
|
||||
}
|
||||
let gen: Trig.Control = Trig.Control(input: input, dur: dur)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// After block 1: still within dur, level = 1.0.
|
||||
#expect(abs(gen.previousOutputValue - 1.0) < 1e-12)
|
||||
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// After block 2: dur has expired, level = 0.
|
||||
#expect(gen.previousOutputValue == 0.0,
|
||||
"after dur expired expected 0.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Boundary: a hold whose duration equals exactly one block stays active
|
||||
/// across every sample, so the block-final value is the captured level —
|
||||
/// not 0 (regression for inferring output from post-loop `remaining`).
|
||||
@Test func holdSpanningExactlyOneBlockOutputsLevel() {
|
||||
let durSamples: Int = blockSize // hold == one block exactly
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Edge at sample 0 (prevTrig defaults to 0, x = 0.7).
|
||||
let input: Closure = Closure { _, _ in 0.7 }
|
||||
let gen: Trig.Control = Trig.Control(input: input, dur: dur)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(abs(gen.previousOutputValue - 0.7) < 1e-12,
|
||||
"full-block hold expected 0.7 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: Trig.Control = Trig.Control(input: 0.0, dur: 0.1)
|
||||
#expect(gen.floatValue(graph: graph) == 0.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Output block is flat (discrete, not ramped).
|
||||
@Test func outputBlockIsFlat() {
|
||||
let durSamples: Int = blockSize * 2
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Input goes from 0 to 0.7 at sample 0.
|
||||
let input: Closure = Closure { _, i in i == 0 ? 0.7 : 0.0 }
|
||||
// Actually input needs to STAY > 0 for the full dur. Use constant 0.7 after edge.
|
||||
let input2: Closure = Closure { _, _ in 0.7 }
|
||||
let gen: Trig.Control = Trig.Control(input: input2, dur: dur)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let block: [Double] = Array(out[0].prefix(blockSize))
|
||||
// After trigger fires at sample 0 (prevTrig=0, x=0.7), captured level = 0.7.
|
||||
// All block samples should be 0.7.
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(abs(block[i] - 0.7) < 1e-12,
|
||||
"sample \(i) expected 0.7 got \(block[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan-out: two calculatedSamples calls in same segment return identical channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: Trig.Control = Trig.Control(input: 0.0, dur: 0.1)
|
||||
let out1: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
let out2: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
// TriggeredDelayAudioTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("TriggeredDelay.Audio")
|
||||
struct TriggeredDelayAudioTests {
|
||||
|
||||
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: TriggeredDelay.Audio = TriggeredDelay.Audio(input: 0.0, dur: 0.1)
|
||||
#expect(gen.rate == .audio)
|
||||
}
|
||||
|
||||
/// Without any rising edge, output stays at 0.
|
||||
@Test func noTriggerOutputsZero() {
|
||||
let gen: TriggeredDelay.Audio = TriggeredDelay.Audio(input: 0.0, dur: 0.01)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out[i] == 0.0, "sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Rising edge at sample 10 fires a single-sample 1.0 after `dur` samples.
|
||||
@Test func risingEdgeFiresAfterDelay() {
|
||||
let durSamples: Int = 10
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
let trig: Closure = Closure { _, i in i == 10 ? 1.0 : 0.0 }
|
||||
let gen: TriggeredDelay.Audio = TriggeredDelay.Audio(input: trig, dur: dur)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
let fireAt: Int = 10 + durSamples // sample 20
|
||||
for i: Int in 0..<blockSize {
|
||||
if i == fireAt {
|
||||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||||
"delayed pulse at sample \(i) expected 1.0 got \(out[i])")
|
||||
} else {
|
||||
#expect(out[i] == 0.0,
|
||||
"sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Edges during countdown are IGNORED — only the first is honoured.
|
||||
@Test func interleavedEdgesIgnored() {
|
||||
let durSamples: Int = 20
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// First edge at 5, second edge at 15 (within delay period — should be ignored).
|
||||
let trig: Closure = Closure { _, i in (i == 5 || i == 15) ? 1.0 : 0.0 }
|
||||
let gen: TriggeredDelay.Audio = TriggeredDelay.Audio(input: trig, dur: dur)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
|
||||
// Pulse fires at 5 + 20 = 25.
|
||||
for i: Int in 0..<blockSize {
|
||||
if i == 25 {
|
||||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||||
"delayed pulse at sample \(i) expected 1.0 got \(out[i])")
|
||||
} else {
|
||||
#expect(out[i] == 0.0,
|
||||
"sample \(i) expected 0.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// After firing, generator returns to idle and can accept a new trigger.
|
||||
@Test func returnsToIdleAfterFiring() {
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
let durSamples: Int = 5
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
// Fire one trigger at sample 0 of block 1.
|
||||
let trig: Closure = Closure { graph, i in
|
||||
graph.engine.segmentSerial == firstSerial && i == 0 ? 1.0 : 0.0
|
||||
}
|
||||
let gen: TriggeredDelay.Audio = TriggeredDelay.Audio(input: trig, dur: dur)
|
||||
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// Pulse should fire at sample 5 (durSamples) in block 1.
|
||||
#expect(abs(out1[5] - 1.0) < 1e-12)
|
||||
// All other samples = 0 in block 1.
|
||||
for i: Int in 0..<blockSize where i != 5 {
|
||||
#expect(out1[i] == 0.0)
|
||||
}
|
||||
|
||||
advanceBlock(engine)
|
||||
// Block 2: no trigger, all zeros (idle).
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out2[i] == 0.0,
|
||||
"block-2 sample \(i) expected 0.0 got \(out2[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in same segment return identical first channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: TriggeredDelay.Audio = TriggeredDelay.Audio(input: 0.0, dur: 0.01)
|
||||
let out1: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
let out2: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
|
||||
/// mulAdd: pulse 1.0 * 2 + 1 = 3.0.
|
||||
@Test func mulAddApplied() {
|
||||
let durSamples: Int = 5
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
let trig: Closure = Closure { _, i in i == 0 ? 1.0 : 0.0 }
|
||||
let gen: TriggeredDelay.Audio = TriggeredDelay.Audio(input: trig, dur: dur, mul: 2.0, add: 1.0)
|
||||
let out: [Double] = gen.calculatedSamples(graph: graph, count: blockSize)[0]
|
||||
// At sample durSamples=5: 1.0 * 2 + 1 = 3.0.
|
||||
#expect(abs(out[durSamples] - 3.0) < 1e-12,
|
||||
"mul/add at pulse sample expected 3.0 got \(out[durSamples])")
|
||||
// Other samples: 0.0 * 2 + 1 = 1.0.
|
||||
for i: Int in 0..<blockSize where i != durSamples {
|
||||
#expect(abs(out[i] - 1.0) < 1e-12,
|
||||
"mul/add at non-pulse sample \(i) expected 1.0 got \(out[i])")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
// TriggeredDelayControlTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("TriggeredDelay.Control")
|
||||
struct TriggeredDelayControlTests {
|
||||
|
||||
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: TriggeredDelay.Control = TriggeredDelay.Control(input: 0.0, dur: 0.1)
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
/// Without any rising edge, output holds 0.
|
||||
@Test func noTriggerOutputsZero() {
|
||||
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: 0.0, dur: 0.1)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.previousOutputValue == 0.0)
|
||||
}
|
||||
|
||||
/// Rising edge triggers countdown; after dur samples the block that contains the
|
||||
/// firing position will have previousOutputValue = 1.0.
|
||||
@Test func delayedPulseFires() {
|
||||
// dur = exactly blockSize samples so the pulse fires in block 2.
|
||||
let durSamples: Int = blockSize
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
let firstSerial: UInt64 = engine.segmentSerial
|
||||
let trig: Closure = Closure { graph, i in
|
||||
graph.engine.segmentSerial == firstSerial && i == 0 ? 1.0 : 0.0
|
||||
}
|
||||
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: trig, dur: dur)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Block 1: counting down, output = 0.
|
||||
#expect(gen.previousOutputValue == 0.0)
|
||||
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Block 2: countdown reached 0, pulse fired = 1.0.
|
||||
#expect(abs(gen.previousOutputValue - 1.0) < 1e-12,
|
||||
"after delay expected 1.0 got \(gen.previousOutputValue)")
|
||||
|
||||
advanceBlock(engine)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
// Block 3: back to idle, output = 0.
|
||||
#expect(gen.previousOutputValue == 0.0,
|
||||
"post-pulse block expected 0.0 got \(gen.previousOutputValue)")
|
||||
}
|
||||
|
||||
/// Output block is flat (discrete, not ramped).
|
||||
@Test func outputBlockIsFlat() {
|
||||
// Fire in the first block so we can verify all samples.
|
||||
let durSamples: Int = 1 // min dur; fires at sample 1 = within block.
|
||||
let dur: Double = Double(durSamples) / 44100.0
|
||||
let trig: Closure = Closure { _, i in i == 0 ? 1.0 : 0.0 }
|
||||
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: trig, dur: dur)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let block: [Double] = Array(out[0].prefix(blockSize))
|
||||
// Pulse fired mid-block at sample 1; block-final value = 1.0 (after fire).
|
||||
// All samples in the block should be 1.0 (flat output).
|
||||
let finalValue: Double = gen.previousOutputValue
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(block[i] == finalValue,
|
||||
"sample \(i) expected \(finalValue) got \(block[i])")
|
||||
}
|
||||
}
|
||||
|
||||
/// floatValue returns previousOutputValue.
|
||||
@Test func floatValueReturnsPreviousOutputValue() {
|
||||
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: 0.0, dur: 0.1)
|
||||
#expect(gen.floatValue(graph: graph) == 0.0)
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
|
||||
}
|
||||
|
||||
/// Fan-out: two calls in same segment return identical channel.
|
||||
@Test func fanOutReturnsSameChannel() {
|
||||
let gen: TriggeredDelay.Control = TriggeredDelay.Control(input: 0.0, dur: 0.1)
|
||||
let out1: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
let out2: [Double] = Array(gen.calculatedSamples(graph: graph, count: blockSize)[0].prefix(blockSize))
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(out1[i] == out2[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user