// StereoGranulatorTests.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("StereoGranulator") struct StereoGranulatorTests { /// Minimal synth: a sine through a seeded `StereoGranulator`, routing one of its two channels /// to a mono bus so the offline render captures that channel's content. private final class GranSynth: SynthesizerBase { private let channel: Int private let spread: Double private let seed: UInt64 private let outBus: Int init(channel: Int, spread: Double, seed: UInt64, outBus: Int) { self.channel = channel self.spread = spread self.seed = seed self.outBus = outBus super.init() } override func build() { let osc: OscSin.Audio = OscSin.Audio(freq: 220.0) let gran: StereoGranulator = StereoGranulator( input: osc, maxGrainSize: 0.2, grainSize: 0.05, overlap: 4.0, spread: spread, sampleRate: 44100.0, seed: seed ) output(bus: outBus, source: channel == 0 ? gran.left : gran.right) } } private func renderChannel(_ channel: Int, spread: Double, seed: UInt64) throws -> [Float] { let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, sampleRate: 44100.0) defer { try? FileManager.default.removeItem(at: url) } let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1) defer { engine.busAllocator.releaseAudioBus(dstBus) } let synth: GranSynth = GranSynth(channel: channel, spread: spread, seed: seed, outBus: dstBus.index) let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index) try engine.add(synth) try engine.add(master, position: NodePosition.after(synth)) try engine.start() return readStereoCAF(url: url, sampleRate: 44100.0) } 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 producesNonSilentOutput() throws { let samples: [Float] = try renderChannel(0, spread: 0.7, seed: 7) #expect(hasNonSilentOutput(samples), "granulator output was silent") } @Test func deterministicForAFixedSeed() throws { let a: [Float] = try renderChannel(0, spread: 1.0, seed: 7) let b: [Float] = try renderChannel(0, spread: 1.0, seed: 7) #expect(totalSSD(a, b) == 0.0, "same seed must produce identical output") } @Test func zeroSpreadCentersBothChannels() throws { // spread 0 → every grain pans to centre → left and right channels are identical. let left: [Float] = try renderChannel(0, spread: 0.0, seed: 7) let right: [Float] = try renderChannel(1, spread: 0.0, seed: 7) #expect(totalSSD(left, right) == 0.0, "zero spread should center grains (L == R)") } @Test func fullSpreadSeparatesChannels() throws { // spread 1 → grains scatter → left and right differ. let left: [Float] = try renderChannel(0, spread: 1.0, seed: 7) let right: [Float] = try renderChannel(1, spread: 1.0, seed: 7) #expect(hasNonSilentOutput(left) && hasNonSilentOutput(right), "spread channels were silent") #expect(totalSSD(left, right) > 0.0, "full spread should separate L and R") } }