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

108 lines
4.2 KiB
Swift

// 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])
}
}
}