145 lines
6.2 KiB
Swift
145 lines
6.2 KiB
Swift
// RunningMaxAudioTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("RunningMax.Audio")
|
|
struct RunningMaxAudioTests {
|
|
|
|
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: RunningMax.Audio = RunningMax.Audio(input: 0.0)
|
|
#expect(gen.rate == .audio)
|
|
}
|
|
|
|
/// Output count matches request.
|
|
@Test func outputCountMatchesRequest() {
|
|
let gen: RunningMax.Audio = RunningMax.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.3
|
|
let gen: RunningMax.Audio = RunningMax.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 increasing extreme over a varying input.
|
|
///
|
|
/// Input starts at 0.0 and increases by 0.01 per sample.
|
|
/// The running maximum should track every new maximum.
|
|
@Test func tracksIncreasingExtreme() {
|
|
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
|
let gen: RunningMax.Audio = RunningMax.Audio(input: inputSignal)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
for i: Int in 0..<blockSize {
|
|
let expected: Double = Double(i) * 0.01
|
|
#expect(abs(out[0][i] - expected) < 1e-12,
|
|
"sample \(i): expected max \(expected), got \(out[0][i])")
|
|
}
|
|
}
|
|
|
|
/// Ignores values less than the current maximum.
|
|
@Test func ignoresValuesLessThanCurrentMax() {
|
|
// Input: 0.8 at sample 0 (init), then alternating 0.1 and 0.8.
|
|
// The maximum should stay at 0.8 throughout.
|
|
let inputSignal: Closure = Closure { _, i in i == 0 ? 0.8 : (i % 2 == 0 ? 0.8 : 0.1) }
|
|
let gen: RunningMax.Audio = RunningMax.Audio(input: inputSignal)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
for i: Int in 0..<blockSize {
|
|
#expect(abs(out[0][i] - 0.8) < 1e-12,
|
|
"maximum should stay 0.8, got \(out[0][i]) at sample \(i)")
|
|
}
|
|
}
|
|
|
|
/// Rising trig edge resets extreme to the current input value at that frame.
|
|
@Test func trigRisingEdgeResetsToCurrentInput() {
|
|
// Input: 0.9 for first 32 samples, then 0.2 for 32+.
|
|
// Trig rises at sample 32 while input is 0.2, resetting max to 0.2.
|
|
let inputSignal: Closure = Closure { _, i in i < 32 ? 0.9 : 0.2 }
|
|
let trigSignal: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 }
|
|
let gen: RunningMax.Audio = RunningMax.Audio(input: inputSignal, trig: trigSignal)
|
|
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// Samples 0..31: running max = 0.9.
|
|
for i: Int in 0..<32 {
|
|
#expect(abs(out[0][i] - 0.9) < 1e-12,
|
|
"sample \(i): expected 0.9, got \(out[0][i])")
|
|
}
|
|
// Sample 32: reset to input at frame 32 = 0.2.
|
|
#expect(abs(out[0][32] - 0.2) < 1e-12,
|
|
"sample 32 after reset: expected 0.2, got \(out[0][32])")
|
|
// Samples 33+: running max of 0.2 (input stays at 0.2, no further reset).
|
|
for i: Int in 33..<blockSize {
|
|
#expect(abs(out[0][i] - 0.2) < 1e-12,
|
|
"sample \(i): expected 0.2 (no new maximum), got \(out[0][i])")
|
|
}
|
|
}
|
|
|
|
/// Cross-block continuity: extreme carries across audio blocks.
|
|
@Test func crossBlockContinuity() {
|
|
let firstSerial: UInt64 = engine.segmentSerial
|
|
// Block 1: input = 0.7 (sets max to 0.7).
|
|
// Block 2: input = 0.3 (max stays 0.7).
|
|
let inputSignal: Closure = Closure { graph, _ in
|
|
graph.engine.segmentSerial == firstSerial ? 0.7 : 0.3
|
|
}
|
|
let gen: RunningMax.Audio = RunningMax.Audio(input: inputSignal)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
advanceBlock(engine)
|
|
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// The maximum from block 1 (0.7) should persist in block 2.
|
|
for i: Int in 0..<blockSize {
|
|
#expect(abs(out2[0][i] - 0.7) < 1e-12,
|
|
"block 2 sample \(i): expected max 0.7, 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: RunningMax.Audio = RunningMax.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() {
|
|
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
|
let genBase: RunningMax.Audio = RunningMax.Audio(input: inputSignal)
|
|
let genScaled: RunningMax.Audio = RunningMax.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])")
|
|
}
|
|
}
|
|
}
|