// ChorusTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @MainActor @Suite("Chorus") struct ChorusTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100) var graph: SynthGraph { makeSynthGraph(engine: engine) } let testSampleRate: Double = 44100 init() throws { try engine.start() } // MARK: - Deterministic first sample @Test func firstSampleDeterministic() { // input=0.5, depth=0, rate=0, mix=0.5 → first sample = 0.25 on both channels. // Delay line starts all-zero; wet = 0; output = 0.5*(1-0.5) + 0*0.5 = 0.25. let chorus: Chorus.Audio = Chorus.Audio( input: ConstantGeneratorInput(0.5), delayTime: 0.025, depth: 0.0, rate: 0.0, mix: 0.5, sampleRate: testSampleRate ) let out: [[Double]] = chorus.calculatedSamples(graph: graph, count: 1) let expected: Double = 0.25 #expect(abs(out[0][0] - expected) < 1e-10, "Left ch first sample must be \(expected)") #expect(abs(out[1][0] - expected) < 1e-10, "Right ch first sample must be \(expected)") } // MARK: - Rate @Test func rateIsAudio() { let chorus: Chorus.Audio = Chorus.Audio( input: 0.0, sampleRate: testSampleRate ) #expect(chorus.rate == .audio) } // MARK: - Output is stereo (2 channels) @Test func outputIsStereo() { let chorus: Chorus.Audio = Chorus.Audio( input: ConstantGeneratorInput(0.5), sampleRate: testSampleRate ) let out: [[Double]] = chorus.calculatedSamples(graph: graph, count: 512) #expect(out.count == 2, "Chorus.Audio must produce exactly 2 output channels") #expect(out[0].count >= 512, "Channel 0 must have at least 512 samples") #expect(out[1].count >= 512, "Channel 1 must have at least 512 samples") } // MARK: - Silent input → near-zero output @Test func silentInputProducesNearZeroOutput() { let chorus: Chorus.Audio = Chorus.Audio( input: 0.0, mix: 0.5, sampleRate: testSampleRate ) let out: [[Double]] = chorus.calculatedSamples(graph: graph, count: 512) let samplesL: [Double] = Array(out[0].prefix(512)) let samplesR: [Double] = Array(out[1].prefix(512)) let allNearZeroL: Bool = samplesL.allSatisfy { abs($0) < 1e-12 } let allNearZeroR: Bool = samplesR.allSatisfy { abs($0) < 1e-12 } #expect(allNearZeroL, "Left channel: zero input must produce near-zero output") #expect(allNearZeroR, "Right channel: zero input must produce near-zero output") } // MARK: - Non-silent input produces non-zero output @Test func nonSilentInputProducesNonZeroOutput() { let chorus: Chorus.Audio = Chorus.Audio( input: ConstantGeneratorInput(0.5), mix: 0.5, sampleRate: testSampleRate ) // Let delay line fill. for _ in 0..<5 { _ = chorus.calculatedSamples(graph: graph, count: 512) advanceBlock(engine) } let out: [[Double]] = chorus.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512) let rmsL: Double = rmsValue(Array(out[0].prefix(512))) let rmsR: Double = rmsValue(Array(out[1].prefix(512))) #expect(rmsL > 0, "Left channel must have non-zero output with non-silent input") #expect(rmsR > 0, "Right channel must have non-zero output with non-silent input") } // MARK: - Left and right channels are distinct (LFOs 180° apart) @Test func leftAndRightChannelsAreDistinct() { // With a varying input (sine wave), different delay offsets driven by the two LFOs // (180° apart) will read different values from the delay line, producing distinct // left and right outputs. Use mix=1.0 (all wet) so only tap differences matter. let input: OscSin.Audio = OscSin.Audio(freq: 880.0) let chorus: Chorus.Audio = Chorus.Audio( input: input, delayTime: 0.010, depth: 0.9, rate: 5.0, mix: 1.0, sampleRate: testSampleRate ) // Let delay line fill with the sine wave content and LFOs diverge. for _ in 0..<20 { _ = chorus.calculatedSamples(graph: graph, count: 512) advanceBlock(engine) } let out: [[Double]] = chorus.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512) let anyDiff: Bool = zip(out[0].prefix(512), out[1].prefix(512)) .contains { abs($0 - $1) > 1e-8 } #expect(anyDiff, "Left and right channels must differ due to 180° LFO phase offset") } // MARK: - Fan-out @Test func fanOutSingleEvaluation() { let chorus: Chorus.Audio = Chorus.Audio( input: ConstantGeneratorInput(0.3), sampleRate: testSampleRate ) let out1: [[Double]] = chorus.calculatedSamples(graph: graph, count: 512) let out2: [[Double]] = chorus.calculatedSamples(graph: graph, count: 512) let s1L: [Double] = Array(out1[0].prefix(512)) let s2L: [Double] = Array(out2[0].prefix(512)) let s1R: [Double] = Array(out1[1].prefix(512)) let s2R: [Double] = Array(out2[1].prefix(512)) #expect(s1L == s2L, "Fan-out: left channel must be identical on second call in same segment") #expect(s1R == s2R, "Fan-out: right channel must be identical on second call in same segment") } // MARK: - State advances once per block @Test func stateAdvancesOncePerBlock() { let chorus: Chorus.Audio = Chorus.Audio( input: ConstantGeneratorInput(0.3), sampleRate: testSampleRate ) _ = chorus.calculatedSamples(graph: graph, count: 512) _ = chorus.calculatedSamples(graph: graph, count: 512) // cache hit advanceBlock(engine) let block2: [[Double]] = chorus.calculatedSamples(graph: makeSynthGraph(engine: engine), count: 512) let allFiniteL: Bool = Array(block2[0].prefix(512)).allSatisfy { $0.isFinite } let allFiniteR: Bool = Array(block2[1].prefix(512)).allSatisfy { $0.isFinite } #expect(allFiniteL, "Left channel must be finite after block advance") #expect(allFiniteR, "Right channel must be finite after block advance") } // MARK: - mulAdd applied @Test func mulAddApplied() { // Zero input → zero wet and dry → output 0. After mul:2 add:1 → output 1. let chorus: Chorus.Audio = Chorus.Audio( input: 0.0, mix: 0.5, sampleRate: testSampleRate, mul: 2.0, add: 1.0 ) let out: [[Double]] = chorus.calculatedSamples(graph: graph, count: 512) let samplesL: [Double] = Array(out[0].prefix(512)) let samplesR: [Double] = Array(out[1].prefix(512)) let allNearOneL: Bool = samplesL.allSatisfy { abs($0 - 1.0) < 1e-10 } let allNearOneR: Bool = samplesR.allSatisfy { abs($0 - 1.0) < 1e-10 } #expect(allNearOneL, "Left channel: mulAdd(mul:2, add:1) on zero input must produce 1.0") #expect(allNearOneR, "Right channel: mulAdd(mul:2, add:1) on zero input must produce 1.0") } // MARK: - Channel aliases @Test func channelAliasesMatchSubscripts() { let chorus: Chorus.Audio = Chorus.Audio( input: ConstantGeneratorInput(0.5), sampleRate: testSampleRate ) let leftOut: [[Double]] = chorus.left.calculatedSamples(graph: graph, count: 64) let rightOut: [[Double]] = chorus.right.calculatedSamples(graph: graph, count: 64) let fullOut: [[Double]] = chorus.calculatedSamples(graph: graph, count: 64) for i: Int in 0..<64 { #expect(abs(leftOut[0][i] - fullOut[0][i]) < 1e-10, "left alias must match channel 0 at index \(i)") #expect(abs(rightOut[0][i] - fullOut[1][i]) < 1e-10, "right alias must match channel 1 at index \(i)") } } // MARK: - Integration with engine @Test func integrationWithEngine() throws { final class ChorusSynth: SynthesizerBase { override func build() { let chorus: Chorus.Audio = Chorus.Audio( input: OscSin.Audio(freq: 220.0), delayTime: 0.025, depth: 0.002, rate: 1.5, mix: 0.5, sampleRate: 44100 ) addOutputGenerator(Out.Audio(bus: 0, source: chorus.left)) addOutputGenerator(Out.Audio(bus: 1, source: chorus.right)) } } let intEngine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100) try intEngine.start() let synth: ChorusSynth = ChorusSynth() try intEngine.add(synth) for _ in 0..<4 { intEngine.processBlock(inputSamples: []) } intEngine.stop() } }