93 lines
3.6 KiB
Swift
93 lines
3.6 KiB
Swift
// 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])
|
|
}
|
|
}
|
|
}
|