Files
Lyrebird/Tests/LyrebirdTests/Generators/Triggers/StepperAudioTests.swift
T
2026-06-21 11:45:58 -07:00

151 lines
6.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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)")
}
}
}