140 lines
6.3 KiB
Swift
140 lines
6.3 KiB
Swift
// RunningMaxControlTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("RunningMax.Control")
|
|
struct RunningMaxControlTests {
|
|
|
|
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: RunningMax.Control = RunningMax.Control(input: 0.0)
|
|
#expect(gen.rate == .control)
|
|
}
|
|
|
|
/// Output count matches request.
|
|
@Test func outputCountMatchesRequest() {
|
|
let gen: RunningMax.Control = RunningMax.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 max.
|
|
@Test func outputBlockIsFlat() {
|
|
let inputSignal: Closure = Closure { _, i in Double(i) * 0.001 }
|
|
let gen: RunningMax.Control = RunningMax.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 maximum of all audio samples in the block.
|
|
@Test func blockFinalValueIsRunningMaxOfBlock() {
|
|
// Input ascends 0.0 → 0.63 over the block. The maximum after scanning = (blockSize-1)*0.01.
|
|
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
|
let gen: RunningMax.Control = RunningMax.Control(input: inputSignal)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
let expectedMax: Double = Double(blockSize - 1) * 0.01
|
|
#expect(abs(gen.previousOutputValue - expectedMax) < 1e-12,
|
|
"block final value should be \(expectedMax), got \(gen.previousOutputValue)")
|
|
}
|
|
|
|
/// Trig rising edge in a block resets extreme to current input at that frame.
|
|
@Test func trigEdgeInBlockResetsExtreme() {
|
|
// Input: 0.9 for first 32 samples, then 0.2 for 32..63.
|
|
// Trig rises at sample 32. At reset, current input = 0.2,
|
|
// so new max after the reset = 0.2 (input stays at 0.2 for remaining samples).
|
|
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.Control = RunningMax.Control(input: inputSignal, trig: trigSignal)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
// After reset at sample 32 (input=0.2), remaining samples are also 0.2.
|
|
// Running max after reset = 0.2.
|
|
#expect(abs(gen.previousOutputValue - 0.2) < 1e-12,
|
|
"after trig reset at sample 32 (input=0.2), block max should be 0.2, got \(gen.previousOutputValue)")
|
|
}
|
|
|
|
/// floatValue returns previousOutputValue.
|
|
@Test func floatValueReturnsPreviousOutputValue() {
|
|
let gen: RunningMax.Control = RunningMax.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 Double(i) * 0.01 }
|
|
let gen: RunningMax.Control = RunningMax.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 Double(i) * 0.01 }
|
|
let gen: RunningMax.Control = RunningMax.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.8, sets max to 0.8.
|
|
// Block 2: input = 0.1, max stays 0.8.
|
|
let inputSignal: Closure = Closure { graph, _ in
|
|
graph.engine.segmentSerial == firstSerial ? 0.8 : 0.1
|
|
}
|
|
let gen: RunningMax.Control = RunningMax.Control(input: inputSignal)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(abs(gen.previousOutputValue - 0.8) < 1e-12)
|
|
advanceBlock(engine)
|
|
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
|
#expect(abs(gen.previousOutputValue - 0.8) < 1e-12,
|
|
"extreme 0.8 from block 1 should persist in block 2 (0.1 < 0.8)")
|
|
}
|
|
|
|
/// mul/add: output is original*2+1.
|
|
@Test func mulAdd() {
|
|
let inputSignal: Closure = Closure { _, i in Double(i) * 0.01 }
|
|
let genBase: RunningMax.Control = RunningMax.Control(input: inputSignal)
|
|
let genScaled: RunningMax.Control = RunningMax.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])")
|
|
}
|
|
}
|
|
}
|