// BitcrusherPedalTests.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("BitcrusherPedal") struct BitcrusherPedalTests { private func render( configure: (BitcrusherPedal) -> 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: 1) defer { engine.busAllocator.releaseAudioBus(srcBus) engine.busAllocator.releaseAudioBus(dstBus) } let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index) let pedal: BitcrusherPedal = BitcrusherPedal(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 { _ in } #expect(!samples.isEmpty) #expect(hasNonSilentOutput(samples), "bitcrusher output was silent") } @Test func crushChangesOutput() throws { let light: [Float] = try render { pedal in pedal.crush = 1.0; pedal.bits = 16.0; pedal.tone = 8000.0; pedal.mix = 1.0 } let heavy: [Float] = try render { pedal in pedal.crush = 32.0; pedal.bits = 16.0; pedal.tone = 8000.0; pedal.mix = 1.0 } #expect(hasNonSilentOutput(heavy), "decimated bitcrusher was silent") #expect(totalSSD(light, heavy) > 0, "changing crush did not change the output") } @Test func bitsChangeOutput() throws { let clean: [Float] = try render { pedal in pedal.crush = 1.0; pedal.bits = 16.0; pedal.tone = 8000.0; pedal.mix = 1.0 } let crushed: [Float] = try render { pedal in pedal.crush = 1.0; pedal.bits = 3.0; pedal.tone = 8000.0; pedal.mix = 1.0 } #expect(hasNonSilentOutput(crushed), "bit-reduced output was silent") #expect(totalSSD(clean, crushed) > 0, "changing bit depth did not change the output") } @Test func exposesCrushBitsToneKnobs() throws { let pedal: BitcrusherPedal = BitcrusherPedal() let labels: [String] = pedal.effectKnobs.map { $0.label } #expect(labels == ["Crush", "Bits", "Tone"]) } @Test func registeredInBuiltInRegistry() throws { let pedal: Pedal? = PedalRegistry.makePedal(type: "Bitcrusher") #expect(pedal is BitcrusherPedal) } }