// ChorusPedalTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation import LyrebirdSynth import LyrebirdUI @testable import LyrebirdShoeGaze @MainActor @Suite("ChorusPedal") struct ChorusPedalTests { private func render(configure: (ChorusPedal) -> Void = { _ in }) throws -> [Float] { let renderDuration: Double = 0.1 let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration, numAudioBuses: 64) defer { try? FileManager.default.removeItem(at: url) } let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) defer { engine.busAllocator.releaseAudioBus(srcBus) engine.busAllocator.releaseAudioBus(dstBus) } let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index) let pedal: ChorusPedal = ChorusPedal(inputBus: srcBus.index, outputBus: dstBus.index) configure(pedal) let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index) try engine.add(source) try engine.add(pedal, position: NodePosition.after(source)) try engine.add(master, position: NodePosition.after(pedal)) try engine.start() return readStereoCAF(url: url) } private func ssd(_ a: [Float], _ b: [Float]) -> Double { let skip: Int = 2048, window: Int = 2048 guard a.count >= skip + window, b.count >= skip + window else { return 0 } return zip(a[skip..<(skip + window)], b[skip..<(skip + window)]).reduce(0.0) { acc, pair in let d: Double = Double(pair.0 - pair.1); return acc + d * d } } @Test func buildsAndProducesNonSilentOutput() throws { let samples: [Float] = try render() #expect(!samples.isEmpty) #expect(hasNonSilentOutput(samples), "ChorusPedal produced silence") } @Test func rateChangesOutput() throws { let slow: [Float] = try render { $0.rate = 0.1 } let fast: [Float] = try render { $0.rate = 5.0 } #expect(hasNonSilentOutput(slow) && hasNonSilentOutput(fast)) #expect(ssd(slow, fast) > 0, "rate had no effect on output") } @Test func depthChangesOutput() throws { let shallow: [Float] = try render { $0.depth = 0.0005 } let deep: [Float] = try render { $0.depth = 0.008 } #expect(ssd(shallow, deep) > 0, "depth had no effect on output") } @Test func leftAndRightVoicesDiffer() throws { // The point of the widener: anti-phase LFOs make the two voices distinct. Activate the // right channel (which a stereo board region does via PedalBoard.add) by setting // `outputBusRight`, route each board bus to a separate hardware channel, and compare. let renderDuration: Double = 0.1 let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration, numAudioBuses: 64) defer { try? FileManager.default.removeItem(at: url) } let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) let dstL: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) let dstR: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) defer { engine.busAllocator.releaseAudioBus(srcBus) engine.busAllocator.releaseAudioBus(dstL) engine.busAllocator.releaseAudioBus(dstR) } let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index) let pedal: ChorusPedal = ChorusPedal(inputBus: srcBus.index, outputBus: dstL.index) pedal.outputBusRight = dstR.index // emulate the stereo region a board would create let master: StereoSplitTestMaster = StereoSplitTestMaster(left: dstL.index, right: dstR.index) try engine.add(source) try engine.add(pedal, position: NodePosition.after(source)) try engine.add(master, position: NodePosition.after(pedal)) try engine.start() // readStereoCAF returns interleaved [L, R, L, R, …]; even = left voice, odd = right voice. let samples: [Float] = readStereoCAF(url: url) let skipFrames: Int = 2048 let windowFrames: Int = 2048 guard samples.count >= 2 * (skipFrames + windowFrames) else { return } var lefts: [Float] = [] var rights: [Float] = [] for frame: Int in skipFrames ..< (skipFrames + windowFrames) { lefts.append(samples[2 * frame]) rights.append(samples[2 * frame + 1]) } #expect(hasNonSilentOutput(lefts) && hasNonSilentOutput(rights), "a chorus voice was silent") let ssdLR: Double = zip(lefts, rights).reduce(0.0) { acc, pair in let d: Double = Double(pair.0 - pair.1); return acc + d * d } #expect(ssdLR > 0, "left and right chorus voices are identical — no stereo width") } } /// Routes two mono board buses to hardware left (bus 0) and right (bus 1) so a stereo widener's /// channels can be captured separately by `readStereoCAF`. @MainActor private final class StereoSplitTestMaster: SynthesizerBase { private let left: Int private let right: Int init(left: Int, right: Int) { self.left = left self.right = right super.init() } override func build() { output(bus: 0, source: input(bus: left)) output(bus: 1, source: input(bus: right)) } }