// GranulatorPedalTests.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("GranulatorPedal") struct GranulatorPedalTests { private func render( configure: (GranulatorPedal) -> Void ) throws -> [Float] { let renderDuration: Double = 0.2 let numAudioBuses: Int = 64 let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 2) defer { engine.busAllocator.releaseAudioBus(srcBus) engine.busAllocator.releaseAudioBus(dstBus) } let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index) let pedal: GranulatorPedal = GranulatorPedal(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 totalSSD(_ a: [Float], _ b: [Float]) -> Double { zip(a, b).reduce(0.0) { acc, pair in let diff: Double = Double(pair.0 - pair.1) return acc + diff * diff } } @Test func buildsAndProducesNonSilentOutput() throws { let samples: [Float] = try render { pedal in pedal.grain = 0.02 } #expect(!samples.isEmpty) #expect(hasNonSilentOutput(samples), "granulator output was silent") } @Test func spreadChangesOutput() throws { let mono: [Float] = try render { pedal in pedal.grain = 0.01; pedal.spread = 0.0; pedal.mix = 1.0 } let wide: [Float] = try render { pedal in pedal.grain = 0.01; pedal.spread = 1.0; pedal.mix = 1.0 } #expect(hasNonSilentOutput(wide), "spread granulator was silent") #expect(totalSSD(mono, wide) > 0, "changing spread did not change the channel output") } @Test func grainSizeChangesOutput() throws { let small: [Float] = try render { pedal in pedal.grain = 0.01; pedal.mix = 1.0 } let large: [Float] = try render { pedal in pedal.grain = 0.04; pedal.mix = 1.0 } #expect(totalSSD(small, large) > 0, "changing grain size did not change the output") } @Test func exposesGrainOverlapSpreadKnobs() throws { let pedal: GranulatorPedal = GranulatorPedal() #expect(pedal.effectKnobs.map { $0.label } == ["Grain", "Overlap", "Spread"]) } @Test func registeredInBuiltInRegistry() throws { let pedal: Pedal? = PedalRegistry.makePedal(type: "Granulator") #expect(pedal is GranulatorPedal) } }