// RingModulatorPedalTests.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("RingModulatorPedal") struct RingModulatorPedalTests { private func render(frequency: Double) 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: RingModulatorPedal = RingModulatorPedal(inputBus: srcBus.index, outputBus: dstBus.index) pedal.frequency = frequency 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) } @Test func buildsAndProducesNonSilentOutput() throws { let samples: [Float] = try render(frequency: 220.0) #expect(!samples.isEmpty) #expect(hasNonSilentOutput(samples), "RingModulatorPedal produced silence") } @Test func frequencyChangesOutput() throws { let low: [Float] = try render(frequency: 100.0) let high: [Float] = try render(frequency: 800.0) #expect(hasNonSilentOutput(low) && hasNonSilentOutput(high)) let skip: Int = 1024, window: Int = 2048 guard low.count >= skip + window, high.count >= skip + window else { return } let ssd: Double = zip(low[skip..<(skip + window)], high[skip..<(skip + window)]).reduce(0.0) { acc, pair in let d: Double = Double(pair.0 - pair.1); return acc + d * d } #expect(ssd > 0, "carrier frequency had no effect on output") } }