148 lines
6.4 KiB
Swift
148 lines
6.4 KiB
Swift
// RunningMinAudioTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("RunningMin.Audio")
|
|
struct RunningMinAudioTests {
|
|
|
|
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 rateIsAudio() {
|
|
let gen: RunningMin.Audio = RunningMin.Audio(input: 0.0)
|
|
#expect(gen.rate == .audio)
|
|
}
|
|
|
|
/// Output count matches request.
|
|
@Test func outputCountMatchesRequest() {
|
|
let gen: RunningMin.Audio = RunningMin.Audio(input: 0.5)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
|
}
|
|
|
|
/// First sample initializes extreme to the first input value.
|
|
@Test func firstSampleInitializesToFirstInput() {
|
|
let inputVal: Double = 0.7
|
|
let gen: RunningMin.Audio = RunningMin.Audio(input: inputVal)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(abs(out[0][0] - inputVal) < 1e-12,
|
|
"first sample should equal first input \(inputVal), got \(out[0][0])")
|
|
}
|
|
|
|
/// Tracks decreasing extreme over a varying input.
|
|
///
|
|
/// Input starts at 1.0 and decreases by 0.01 per sample.
|
|
/// The running minimum should track every new minimum.
|
|
@Test func tracksDecreasingExtreme() {
|
|
// Input: 1.0, 0.99, 0.98, ... → the minimum decreases each sample.
|
|
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
|
|
let gen: RunningMin.Audio = RunningMin.Audio(input: inputSignal)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
for i: Int in 0..<blockSize {
|
|
let expected: Double = 1.0 - Double(i) * 0.01
|
|
#expect(abs(out[0][i] - expected) < 1e-12,
|
|
"sample \(i): expected min \(expected), got \(out[0][i])")
|
|
}
|
|
}
|
|
|
|
/// Ignores values that are greater than the current minimum.
|
|
@Test func ignoresValuesGreaterThanCurrentMin() {
|
|
// Input: 0.5 at sample 0 (init), then alternating 1.0 and 0.5.
|
|
// The minimum should stay at 0.5 throughout.
|
|
let inputSignal: Closure = Closure { _, i in i == 0 ? 0.5 : (i % 2 == 0 ? 0.5 : 1.0) }
|
|
let gen: RunningMin.Audio = RunningMin.Audio(input: inputSignal)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
for i: Int in 0..<blockSize {
|
|
#expect(abs(out[0][i] - 0.5) < 1e-12,
|
|
"minimum should stay 0.5, got \(out[0][i]) at sample \(i)")
|
|
}
|
|
}
|
|
|
|
/// Rising trig edge resets extreme to the current input value at that frame.
|
|
@Test func trigRisingEdgeResetsToCurrentInput() {
|
|
// Input: starts at 0.1, trig rises at sample 32 while input is at 0.5.
|
|
// Before sample 32: running min of 0.1.
|
|
// At sample 32: reset to input value at frame 32.
|
|
let inputSignal: Closure = Closure { _, i in i < 32 ? 0.1 : 0.5 }
|
|
let trigSignal: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
|
let gen: RunningMin.Audio = RunningMin.Audio(input: inputSignal, trig: trigSignal)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// Samples 0..31: running min from 0.1 initial = 0.1.
|
|
for i: Int in 0..<32 {
|
|
#expect(abs(out[0][i] - 0.1) < 1e-12,
|
|
"sample \(i): expected 0.1, got \(out[0][i])")
|
|
}
|
|
// Sample 32: reset to input at frame 32 = 0.5.
|
|
#expect(abs(out[0][32] - 0.5) < 1e-12,
|
|
"sample 32 after reset: expected 0.5, got \(out[0][32])")
|
|
// Samples 33+: running min of 0.5 (input stays at 0.5, no further reset).
|
|
for i: Int in 33..<blockSize {
|
|
#expect(abs(out[0][i] - 0.5) < 1e-12,
|
|
"sample \(i): expected 0.5 (no new minimum), got \(out[0][i])")
|
|
}
|
|
}
|
|
|
|
/// Cross-block continuity: extreme carries across audio blocks.
|
|
@Test func crossBlockContinuity() {
|
|
// Block 1: input is 0.3 (sets min to 0.3).
|
|
// Block 2: input is 0.5 (min stays 0.3 since 0.5 > 0.3).
|
|
let firstSerial: UInt64 = engine.segmentSerial
|
|
let inputSignal: Closure = Closure { graph, _ in
|
|
graph.engine.segmentSerial == firstSerial ? 0.3 : 0.5
|
|
}
|
|
let gen: RunningMin.Audio = RunningMin.Audio(input: inputSignal)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
advanceBlock(engine)
|
|
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// The minimum from block 1 (0.3) should persist in block 2 since input is 0.5.
|
|
for i: Int in 0..<blockSize {
|
|
#expect(abs(out2[0][i] - 0.3) < 1e-12,
|
|
"block 2 sample \(i): expected min 0.3, got \(out2[0][i])")
|
|
}
|
|
}
|
|
|
|
/// Fan-out: two calls same segment return identical first channel.
|
|
@Test func fanOut() {
|
|
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
|
let gen: RunningMin.Audio = RunningMin.Audio(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])
|
|
}
|
|
}
|
|
|
|
/// mul/add: output is original*2+1.
|
|
@Test func mulAdd() {
|
|
// Decreasing input so min updates each sample.
|
|
let inputSignal: Closure = Closure { _, i in 1.0 - Double(i) * 0.01 }
|
|
let genBase: RunningMin.Audio = RunningMin.Audio(input: inputSignal)
|
|
let genScaled: RunningMin.Audio = RunningMin.Audio(
|
|
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])")
|
|
}
|
|
}
|
|
}
|