// PhasorAudioTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Phasor.Audio") struct PhasorAudioTests { 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: Phasor.Audio = Phasor.Audio() #expect(gen.rate == .audio) } /// Default range [0, 1), default rate 1.0/sample — output count correct. @Test func outputCountMatchesRequest() { let gen: Phasor.Audio = Phasor.Audio() let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) #expect(Array(out[0].prefix(blockSize)).count == blockSize) } /// With no trigger, phase should start at `start` (0.0) and ramp by `rate` per sample. @Test func defaultRampFromZero() { // rate = 0.01 so 64 samples reaches 0.64 without wrapping. let rate: Double = 0.01 let gen: Phasor.Audio = Phasor.Audio(rate: rate) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) for i: Int in 0..0 at sample 32. let trigInput: Closure = Closure { _, i in i < 32 ? 0.0 : 1.0 } let gen: Phasor.Audio = Phasor.Audio(trig: trigInput, rate: rate, resetPos: resetPos) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) // Samples 0..31 ramp from 0 by 0.01 each step. for i: Int in 0..<32 { let expected: Double = Double(i) * rate #expect(abs(out[0][i] - expected) < 1e-12, "pre-trig sample \(i): expected \(expected) got \(out[0][i])") } // At sample 32 the trig fires → phase resets to resetPos before output. #expect(abs(out[0][32] - resetPos) < 1e-12, "trig sample 32: expected \(resetPos) got \(out[0][32])") // Samples 33+ continue from resetPos + rate. for i: Int in 33.. span wraps via truncatingRemainder without hanging. @Test func largeRateWrapsWithoutHang() { // rate = 5.0, span = 1.0 → wraps 5 times per sample; should still produce valid output. let gen: Phasor.Audio = Phasor.Audio(rate: 5.0, start: 0.0, end: 1.0) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) // All samples should be in [0, 1). for i: Int in 0..= 0.0, "sample \(i) below start: \(out[0][i])") #expect(out[0][i] < 1.0, "sample \(i) at or above end: \(out[0][i])") } } /// Negative rate ramps down and wraps up at start. @Test func negativeRateRampsDown() { // Start at 0.5 (via trig), rate = -0.01. Phase decreases each sample and wraps at 0.0. // Use a rate that is exactly representable in IEEE 754 to avoid FP-drift issues. // 0.5 - k * 0.01 in IEEE 754 accumulates error; verify monotonic decrease and wrap behaviour // rather than exact sample-level equality past the first few steps. let rate: Double = -0.01 let gen: Phasor.Audio = Phasor.Audio( trig: Closure { _, i in i == 0 ? 1.0 : 0.0 }, rate: rate, start: 0.0, end: 1.0, resetPos: 0.5 ) let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize) // Sample 0 = resetPos = 0.5 (trig fires at sample 0 resetting to 0.5). #expect(abs(out[0][0] - 0.5) < 1e-12) // Subsequent samples are strictly decreasing until a wrap occurs. var foundWrap: Bool = false for i: Int in 1.. out[0][i - 1] { foundWrap = true // After wrap, output should be in [0, 1). #expect(out[0][i] >= 0.0 && out[0][i] < 1.0, "wrapped sample \(i) out of range: \(out[0][i])") break } // Before wrap: still in [0, 1) and decreasing. #expect(out[0][i] >= 0.0 && out[0][i] < 0.5, "pre-wrap sample \(i) should be in [0, 0.5): \(out[0][i])") } // A wrap must have occurred within 64 samples (0.5 / 0.01 = 50 steps). #expect(foundWrap, "expected a wrap within \(blockSize) samples with rate=\(rate)") } /// mul/add: with mul=2.0 add=1.0, output equals original*2+1. @Test func mulAdd() { let rate: Double = 0.01 let genBase: Phasor.Audio = Phasor.Audio(rate: rate) let genScaled: Phasor.Audio = Phasor.Audio(rate: rate, 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..