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

141 lines
6.5 KiB
Swift

// RunningMinControlTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("RunningMin.Control")
struct RunningMinControlTests {
let engine: SynthesisEngine
var graph: SynthGraph { makeSynthGraph(engine: engine) }
let blockSize: Int = 64
let sampleRate: Double = 44100
init() throws {
engine = makeTestEngine(blockSize: 64, sampleRate: 44100)
try engine.start()
}
@Test func rateIsControl() {
let gen: RunningMin.Control = RunningMin.Control(input: 0.0)
#expect(gen.rate == .control)
}
/// Output count matches request.
@Test func outputCountMatchesRequest() {
let gen: RunningMin.Control = RunningMin.Control(input: 0.5)
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
}
/// Block-final extreme is flat (vDSP_vfillD): all samples equal the final min.
@Test func outputBlockIsFlat() {
// Decreasing input: the scan finds a new minimum on each audio sample in the block.
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.001 }
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal)
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
let expected: Double = out[0][blockSize - 1]
for i: Int in 0..<blockSize {
#expect(out[0][i] == expected,
"expected flat block, sample \(i) = \(out[0][i]) differs from last = \(expected)")
}
}
/// Block-final value matches the running minimum of all audio samples in the block.
@Test func blockFinalValueIsRunningMinOfBlock() {
// Input descends 1.0 → 0.0 over the block. The minimum after scanning = 1 - (blockSize-1)*0.01.
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
let expectedMin: Double = 1.0 - Double(blockSize - 1) * 0.01
#expect(abs(gen.previousOutputValue - expectedMin) < 1e-12,
"block final value should be \(expectedMin), got \(gen.previousOutputValue)")
}
/// Trig rising edge in a block resets extreme to current input at that frame.
@Test func trigEdgeInBlockResetsExtreme() {
// Input is 0.1 for samples 0..31, then 0.8 for 32..63.
// Trig rises at sample 32. At reset, the current input is 0.8, so the
// new minimum after the block is 0.8 (input stays at 0.8 after reset).
let inputSignal: Closure = Closure { _, i in i < 32 ? 0.1 : 0.8 }
let trigSignal: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal, trig: trigSignal)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
// After reset at sample 32 (input=0.8), remaining samples are also 0.8.
// Running min after reset = 0.8.
#expect(abs(gen.previousOutputValue - 0.8) < 1e-12,
"after trig reset at sample 32 (input=0.8), block min should be 0.8, got \(gen.previousOutputValue)")
}
/// floatValue returns previousOutputValue.
@Test func floatValueReturnsPreviousOutputValue() {
let gen: RunningMin.Control = RunningMin.Control(input: 0.5)
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue)
}
/// Fan-out: two calls same segment return identical first channel.
@Test func fanOut() {
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal)
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
for i: Int in 0..<blockSize {
#expect(out1[0][i] == out2[0][i])
}
}
/// Sub-block bypass: second call in same segment is cache hit; state not re-advanced.
@Test func subBlockBypassCacheHit() {
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
let stateAfterFirst: Double = gen.previousOutputValue
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.previousOutputValue == stateAfterFirst,
"second call must be a cache hit — state must not re-advance")
}
/// Cross-block continuity: extreme carries across audio blocks.
@Test func crossBlockContinuity() {
let firstSerial: UInt64 = engine.segmentSerial
// Block 1: input = 0.2, sets min to 0.2.
// Block 2: input = 0.9, min stays 0.2.
let inputSignal: Closure = Closure { graph, _ in
graph.engine.segmentSerial == firstSerial ? 0.2 : 0.9
}
let gen: RunningMin.Control = RunningMin.Control(input: inputSignal)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.2) < 1e-12)
advanceBlock(engine)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.2) < 1e-12,
"extreme 0.2 from block 1 should persist in block 2 (0.9 > 0.2)")
}
/// mul/add: output is original*2+1.
@Test func mulAdd() {
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
let genBase: RunningMin.Control = RunningMin.Control(input: inputSignal)
let genScaled: RunningMin.Control = RunningMin.Control(
input: inputSignal,
mul: 2.0,
add: 1.0
)
let base: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
let scaled: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
for i: Int in 0..<blockSize {
let expected: Double = base[0][i] * 2.0 + 1.0
#expect(abs(scaled[0][i] - expected) < 1e-12,
"mulAdd sample \(i): expected \(expected) got \(scaled[0][i])")
}
}
}