// 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.. 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..