159 lines
6.3 KiB
Swift
159 lines
6.3 KiB
Swift
// 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])")
|
||
}
|
||
}
|
||
}
|