Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/DiffuserPedalTests.swift
T
josh 8c701af128 Add stereo Diffuser pedal
A .widener pedal that splits the dry signal into overlapping RBJ band-pass bands
and pans each by frequency (low to left, high to right); the spread knob fans the
spectrum from mono (spread 0) to a wide diffuse image (spread 1), using linear
pan expressed purely as node arithmetic.

Registered, with PedalType.diffuser, enclosure color, palette order, summary,
and tests (non-silent stereo; L == R at spread 0; L != R at full spread).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 19:14:00 -07:00

99 lines
3.7 KiB
Swift

// DiffuserPedalTests.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("DiffuserPedal")
struct DiffuserPedalTests {
/// Renders the diffuser as a stereo widener, returning (leftSamples, rightSamples).
private func renderStereo(spread: Double) throws -> (left: [Float], right: [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 dstL: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
let dstR: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
defer {
engine.busAllocator.releaseAudioBus(srcBus)
engine.busAllocator.releaseAudioBus(dstL)
engine.busAllocator.releaseAudioBus(dstR)
}
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
let pedal: DiffuserPedal = DiffuserPedal(inputBus: srcBus.index, outputBus: dstL.index)
pedal.outputBusRight = dstR.index
pedal.spread = spread
let master: DiffuserStereoSplitMaster = DiffuserStereoSplitMaster(left: dstL.index, right: dstR.index)
try engine.add(source)
try engine.add(pedal, position: NodePosition.after(source))
try engine.add(master, position: NodePosition.after(pedal))
try engine.start()
let samples: [Float] = readStereoCAF(url: url)
let skipFrames: Int = 2048
let windowFrames: Int = 2048
var left: [Float] = []
var right: [Float] = []
guard samples.count >= 2 * (skipFrames + windowFrames) else { return ([], []) }
for frame: Int in skipFrames ..< (skipFrames + windowFrames) {
left.append(samples[2 * frame])
right.append(samples[2 * frame + 1])
}
return (left, right)
}
private func ssd(_ a: [Float], _ b: [Float]) -> Double {
zip(a, b).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
}
@Test func buildsAndProducesNonSilentOutput() throws {
let (left, right): ([Float], [Float]) = try renderStereo(spread: 0.6)
#expect(hasNonSilentOutput(left), "diffuser left channel was silent")
#expect(hasNonSilentOutput(right), "diffuser right channel was silent")
}
@Test func zeroSpreadIsMono() throws {
// At spread 0 every band sits centre → left and right are identical.
let (left, right): ([Float], [Float]) = try renderStereo(spread: 0.0)
#expect(hasNonSilentOutput(left))
#expect(ssd(left, right) == 0, "spread 0 should collapse to mono (L == R)")
}
@Test func spreadWidensTheImage() throws {
// At full spread the spectrum fans across the field → channels differ.
let (left, right): ([Float], [Float]) = try renderStereo(spread: 1.0)
#expect(ssd(left, right) > 0, "full spread should make L and R differ")
}
}
/// Routes two mono board buses to hardware left (0) and right (1) so a widener's channels can be
/// captured separately by `readStereoCAF`.
@MainActor
private final class DiffuserStereoSplitMaster: SynthesizerBase {
private let left: Int
private let right: Int
init(left: Int, right: Int) {
self.left = left
self.right = right
super.init()
}
override func build() {
output(bus: 0, source: input(bus: left))
output(bus: 1, source: input(bus: right))
}
}