// ConvolutionReverbTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth @Suite("ConvolutionReverb.Audio — partitioned overlap-save FFT convolution") struct ConvolutionReverbTests { let engine: SynthesisEngine = makeTestEngine(blockSize: 512, sampleRate: 44100) var graph: SynthGraph { makeSynthGraph(engine: engine) } init() throws { try engine.start() } // MARK: - Helpers /// Build a single-channel IR with `frames` frames; sample at `frame` is `value`. private func makeUnitImpulseIR(frames: Int, position: Int = 0) -> LyrebirdBuffer { let buf: LyrebirdBuffer = LyrebirdBuffer(count: frames, channelCount: 1) buf.setValue(1.0, at: position) return buf } /// Build a multi-channel IR with the same shape on every channel. private func makeMultiChannelImpulseIR(frames: Int, channels: Int) -> LyrebirdBuffer { let buf: LyrebirdBuffer = LyrebirdBuffer(count: frames, channelCount: channels) for c: Int in 0.. [[Double]] { _checkCount(count) for i: Int in 0.. partitionSize) @Test func multiPartitionIRConvolves() { // IR longer than one partition: impulse at frame 0 AND at frame 100 (across // partition boundary at 64). Convolves dirac input → both echoes. let partitionSize: Int = 64 let irFrames: Int = 200 let ir: LyrebirdBuffer = LyrebirdBuffer(count: irFrames, channelCount: 1) ir.setValue(1.0, at: 0) ir.setValue(0.5, at: 100) let inputCount: Int = 512 let src: ConvolutionReverbTests.SequenceSource = SequenceSource([1.0] + Array(repeating: 0.0, count: inputCount - 1)) let verb: ConvolutionReverb.Audio = ConvolutionReverb.Audio( input: src, ir: ir, partitionSize: partitionSize, mix: 1.0 ) let out: [[Double]] = verb.calculatedSamples(graph: graph, count: inputCount) // Echo at sample partitionSize + 0 = 64 with amplitude 1.0 // Echo at sample partitionSize + 100 = 164 with amplitude 0.5 #expect(abs(out[0][partitionSize] - 1.0) < 1e-4, "First echo at \(partitionSize) = \(out[0][partitionSize])") #expect(abs(out[0][partitionSize + 100] - 0.5) < 1e-4, "Second echo at \(partitionSize + 100) = \(out[0][partitionSize + 100])") } // MARK: - 8. mulAdd applied @Test func mulAddApplied() { let ir: LyrebirdBuffer = makeUnitImpulseIR(frames: 1024) let verb: ConvolutionReverb.Audio = ConvolutionReverb.Audio( input: ConstantGeneratorInput(0.0), ir: ir, mul: 2.0, add: 1.0 ) let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 64) for i: Int in 0..<64 { #expect(abs(out[0][i] - 1.0) < 1e-12, "Silent input + mul=2,add=1 → 1.0; got \(out[0][i]) at \(i)") } } // MARK: - 9. Stereo IR produces two distinct channels @Test func stereoIRProducesDistinctChannels() { // Left = impulse at 0; right = impulse at 32. Mono dirac input. let partitionSize: Int = 64 let irFrames: Int = partitionSize let ir: LyrebirdBuffer = LyrebirdBuffer(count: irFrames, channelCount: 2) ir.setValue(1.0, frame: 0, channel: 0) ir.setValue(1.0, frame: 32, channel: 1) let src: ConvolutionReverbTests.SequenceSource = SequenceSource([1.0] + Array(repeating: 0.0, count: 255)) let verb: ConvolutionReverb.Audio = ConvolutionReverb.Audio( input: src, ir: ir, partitionSize: partitionSize, mix: 1.0 ) let out: [[Double]] = verb.calculatedSamples(graph: graph, count: 256) #expect(abs(out[0][partitionSize] - 1.0) < 1e-4, "Left echo at \(partitionSize) = \(out[0][partitionSize])") #expect(abs(out[1][partitionSize + 32] - 1.0) < 1e-4, "Right echo at \(partitionSize + 32) = \(out[1][partitionSize + 32])") // Right channel should NOT have a spike at partitionSize. #expect(abs(out[1][partitionSize]) < 1e-4, "Right at \(partitionSize) = \(out[1][partitionSize]) (should be silent)") } // MARK: - 10. Fan-out: single evaluation per segment @Test func fanOutSingleEvaluation() { let ir: LyrebirdBuffer = makeUnitImpulseIR(frames: 256) let verb: ConvolutionReverb.Audio = ConvolutionReverb.Audio( input: ConstantGeneratorInput(0.5), ir: ir, mix: 0.5 ) let out1: [[Double]] = verb.calculatedSamples(graph: graph, count: 64) let out2: [[Double]] = verb.calculatedSamples(graph: graph, count: 64) for i: Int in 0..<64 { #expect(out1[0][i] == out2[0][i], "Fan-out: identical samples at \(i)") } } // MARK: - 11. .left/.right channel aliases @Test func leftRightAliases() { let ir: LyrebirdBuffer = makeMultiChannelImpulseIR(frames: 64, channels: 2) let verb: ConvolutionReverb.Audio = ConvolutionReverb.Audio( input: ConstantGeneratorInput(0.0), ir: ir ) // Just verify they construct without trapping; the underlying ChannelSelect // uses subscript indexing. _ = verb.left _ = verb.right } // MARK: - 12. partitionSize must be power of two ≥ 4 @Test func partitionSizeMustBePowerOfTwo() { // Valid pow-of-two passes. let ir: LyrebirdBuffer = makeUnitImpulseIR(frames: 4) _ = ConvolutionReverb.Audio( input: ConstantGeneratorInput(0.0), ir: ir, partitionSize: 4 ) _ = ConvolutionReverb.Audio( input: ConstantGeneratorInput(0.0), ir: ir, partitionSize: 1024 ) // Non-pow-of-two would trap; we don't exercise via #expect(throws:) since // precondition aborts the process. The build-time check is sufficient. } // MARK: - 13. Energy preservation: convolution with normalised IR ≈ input energy @Test func unitImpulseIRPreservesEnergy() { // x convolved with δ[0] should equal x, so energy preserved exactly. let partitionSize: Int = 128 let ir: LyrebirdBuffer = makeUnitImpulseIR(frames: partitionSize) let blockSize: Int = 512 let blocks: Int = 8 let inputCount: Int = blockSize * blocks let src: ConvolutionReverbTests.SequenceSource = SequenceSource((0..