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

162 lines
6.9 KiB
Swift

// SlopeControlTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("Slope.Control")
struct SlopeControlTests {
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: Slope.Control = Slope.Control(input: 0.0)
#expect(gen.rate == .control)
}
/// Output count matches request.
@Test func outputCountMatchesRequest() {
let gen: Slope.Control = Slope.Control(input: 0.5)
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
}
/// First block outputs 0.0 (lazy-init — no startup spike).
@Test func firstBlockOutputsZero() {
let gen: Slope.Control = Slope.Control(input: 5.0)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.0) < 1e-12,
"first block should output 0.0 (lazy-init), got \(gen.previousOutputValue)")
}
/// Constant input produces slope 0 after initialization.
@Test func constantInputSlopeIsZero() {
let gen: Slope.Control = Slope.Control(input: 3.0)
// Block 1: lazy-init → output 0.
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.0) < 1e-12,
"first block of constant input: slope = 0")
// Block 2: x stays at 3.0, prev = 3.0 → slope = 0.
advanceBlock(engine)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.0) < 1e-12,
"second block of constant input: slope should still be 0")
}
/// Control-rate slope uses controlRate = sampleRate/blockSize.
///
/// If the input steps from 0 to 1 between blocks, the slope is
/// (1.0 - 0.0) * controlRate = sampleRate / blockSize.
@Test func stepInputProducesControlRateSlope() {
let controlRate: Double = sampleRate / Double(blockSize)
let firstSerial: UInt64 = engine.segmentSerial
let inputSignal: Closure = Closure { graph, _ in
graph.engine.segmentSerial == firstSerial ? 0.0 : 1.0
}
let gen: Slope.Control = Slope.Control(input: inputSignal)
// Block 1: lazy-init → output 0, prevSample becomes 0.0.
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - 0.0) < 1e-12)
// Block 2: x = 1.0, prev = 0.0 → slope = (1.0 - 0.0) * controlRate.
advanceBlock(engine)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
let expectedSlope: Double = controlRate
#expect(abs(gen.previousOutputValue - expectedSlope) < 1e-9,
"step slope: expected \(expectedSlope), got \(gen.previousOutputValue)")
}
/// Output block is flat (vDSP_vfillD): all samples equal the computed slope.
@Test func outputBlockIsFlat() {
let gen: Slope.Control = Slope.Control(input: 0.5)
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)")
}
}
/// floatValue returns previousOutputValue.
@Test func floatValueReturnsPreviousOutputValue() {
let gen: Slope.Control = Slope.Control(input: 0.0)
#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 gen: Slope.Control = Slope.Control(input: 0.3)
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 gen: Slope.Control = Slope.Control(input: 1.0)
_ = 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: prevSample carries across blocks.
@Test func crossBlockContinuity() {
let controlRate: Double = sampleRate / Double(blockSize)
let firstSerial: UInt64 = engine.segmentSerial
// Block 1 input = 0.0, block 2+ = 0.5.
let inputSignal: Closure = Closure { graph, _ in
graph.engine.segmentSerial == firstSerial ? 0.0 : 0.5
}
let gen: Slope.Control = Slope.Control(input: inputSignal)
// Block 1: lazy-init, prevSample = 0.0, output = 0.
_ = gen.calculatedSamples(graph: graph, count: blockSize)
// Block 2: x = 0.5, prev = 0.0 → slope = 0.5 * controlRate.
advanceBlock(engine)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
let expectedSlope: Double = 0.5 * controlRate
#expect(abs(gen.previousOutputValue - expectedSlope) < 1e-9,
"cross-block slope: expected \(expectedSlope), got \(gen.previousOutputValue)")
}
/// mul/add: output is original*2+1.
@Test func mulAdd() {
let firstSerial: UInt64 = engine.segmentSerial
let inputSignal: Closure = Closure { graph, _ in
graph.engine.segmentSerial == firstSerial ? 0.0 : 1.0
}
let genBase: Slope.Control = Slope.Control(input: inputSignal)
let genScaled: Slope.Control = Slope.Control(input: inputSignal, mul: 2.0, add: 1.0)
// Block 1: both init to 0 → output 0 (base) and 1 (scaled: 0*2+1).
let base1: [[Double]] = genBase.calculatedSamples(graph: graph, count: blockSize)
let scaled1: [[Double]] = genScaled.calculatedSamples(graph: graph, count: blockSize)
for i: Int in 0..<blockSize {
let expected: Double = base1[0][i] * 2.0 + 1.0
#expect(abs(scaled1[0][i] - expected) < 1e-12,
"block 1 mulAdd sample \(i): expected \(expected) got \(scaled1[0][i])")
}
}
}