// AutoGranulatorPedalTests.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("AutoGranulatorPedal") struct AutoGranulatorPedalTests { private func render( configure: (AutoGranulatorPedal) -> 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: AutoGranulatorPedal = AutoGranulatorPedal(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.minGrain = 0.01; pedal.maxGrain = 0.03 } #expect(!samples.isEmpty) #expect(hasNonSilentOutput(samples), "auto granulator output was silent") } @Test func responseSwitchChangesOutput() throws { let loudShort: [Float] = try render { pedal in pedal.minGrain = 0.008; pedal.maxGrain = 0.02; pedal.response = 0.0; pedal.mix = 1.0 } let loudLong: [Float] = try render { pedal in pedal.minGrain = 0.008; pedal.maxGrain = 0.02; pedal.response = 1.0; pedal.mix = 1.0 } #expect(hasNonSilentOutput(loudLong), "flipped-response granulator was silent") #expect(totalSSD(loudShort, loudLong) > 0, "flipping the response did not change the output") } @Test func exposesKnobsAndResponseSwitch() throws { let pedal: AutoGranulatorPedal = AutoGranulatorPedal() #expect(pedal.effectKnobs.map { $0.label } == ["Min Grain", "Max Grain", "Overlap", "Spread"]) #expect(pedal.effectSwitches.map { $0.label } == ["Response"]) #expect(pedal.effectSwitches.first?.options.map { $0.title } == ["Loud→Short", "Loud→Long"]) } @Test func presetRoundTripPreservesResponse() throws { let pedal: AutoGranulatorPedal = AutoGranulatorPedal() pedal.response = 1.0 pedal.minGrain = 0.03 let state: PedalState = pedal.captureState() let restored: AutoGranulatorPedal = AutoGranulatorPedal() restored.applyState(state) #expect(restored.response == 1.0) #expect(restored.minGrain == 0.03) } @Test func registeredInBuiltInRegistry() throws { let pedal: Pedal? = PedalRegistry.makePedal(type: "Auto Granulator") #expect(pedal is AutoGranulatorPedal) } }