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

149 lines
6.7 KiB
Swift

// ZeroCrossingControlTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@MainActor
@Suite("ZeroCrossing.Control")
struct ZeroCrossingControlTests {
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: ZeroCrossing.Control = ZeroCrossing.Control(input: 0.0)
#expect(gen.rate == .control)
}
/// Output count matches request.
@Test func outputCountMatchesRequest() {
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: 0.5)
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
}
/// Constant negative input: no rising crossing fires (prevSample=0 ≤ 0, x=-1 — condition
/// `prevSample<=0 && x>0` is false). Freq stays 0 throughout the block.
@Test func constantNegativeInputNoZeroCrossing() {
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: -1.0)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(gen.previousOutputValue == 0.0,
"constant negative input: freq should remain 0, got \(gen.previousOutputValue)")
}
/// Square wave with period P samples yields freq = sampleRate / P (measured
/// over the full audio block at audio rate, reported as flat block-final value).
///
/// Signal starts at -1, so lazy-init sets `_prevSample = -1` on sample 0 and no
/// spurious crossing fires. The first crossing fires at sample 8 (count=9>4, freq=sr/9);
/// subsequent crossings measure exactly P=16 (count=16>4, freq=sr/16). Block-final
/// value after 64 samples is sr/16.
@Test func squareWaveKnownPeriodYieldsExpectedFreq() {
// Period = 16 samples → expected freq = 44100 / 16 = 2756.25 Hz.
let period: Int = 16
let expectedFreq: Double = sampleRate / Double(period)
// -1 for first half, +1 for second half, repeating.
let inputSignal: Closure = Closure { _, i in
((i / (period / 2)) % 2 == 0) ? -1.0 : 1.0
}
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: inputSignal)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
#expect(abs(gen.previousOutputValue - expectedFreq) < 1e-9,
"expected freq \(expectedFreq), got \(gen.previousOutputValue)")
}
/// Output block is flat: all samples equal the block-final freq.
@Test func outputBlockIsFlat() {
let period: Int = 8
let inputSignal: Closure = Closure { _, i in
((i / (period / 2)) % 2 == 0) ? -1.0 : 1.0
}
let gen: ZeroCrossing.Control = ZeroCrossing.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)")
}
}
/// floatValue returns previousOutputValue.
@Test func floatValueReturnsPreviousOutputValue() {
let gen: ZeroCrossing.Control = ZeroCrossing.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 inputSignal: Closure = Closure { _, i in i % 8 < 4 ? 1.0 : -1.0 }
let gen: ZeroCrossing.Control = ZeroCrossing.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 a cache hit; state not re-advanced.
@Test func subBlockBypassCacheHit() {
let inputSignal: Closure = Closure { _, i in i % 8 < 4 ? 1.0 : -1.0 }
let gen: ZeroCrossing.Control = ZeroCrossing.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 in same segment must be a cache hit — state must not re-advance")
}
/// Cross-block continuity: counter carries across audio blocks.
@Test func crossBlockContinuity() {
// Use a slow square wave (period = 128 samples) that spans two blocks.
// After block 1, the counter should be non-zero and the freq in block 2 should be valid.
let period: Int = 32
let inputSignal: Closure = Closure { _, i in ((i / (period / 2)) % 2 == 0) ? -1.0 : 1.0 }
let gen: ZeroCrossing.Control = ZeroCrossing.Control(input: inputSignal)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
advanceBlock(engine)
_ = gen.calculatedSamples(graph: graph, count: blockSize)
// In block 2, crossings still detected — previousOutputValue should be non-zero.
#expect(gen.previousOutputValue > 0.0,
"freq should be non-zero in block 2 (state carries across blocks)")
}
/// mul/add: output is original*2+1.
@Test func mulAdd() {
let period: Int = 16
let inputSignal: Closure = Closure { _, i in
((i / (period / 2)) % 2 == 0) ? -1.0 : 1.0
}
let genBase: ZeroCrossing.Control = ZeroCrossing.Control(input: inputSignal)
let genScaled: ZeroCrossing.Control = ZeroCrossing.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)
// Both should have a consistent relationship: scaled[i] = base[i]*2+1.
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])")
}
}
}