Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/OverdrivePedalTests.swift
T
2026-06-21 12:25:18 -07:00

183 lines
7.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// OverdrivePedalTests.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("OverdrivePedal")
struct OverdrivePedalTests {
@Test func buildsAndProducesNonSilentOutput() throws {
let renderDuration: Double = 0.1
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration)
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: OverdrivePedal = OverdrivePedal(
inputBus: srcBus.index,
outputBus: dstBus.index
)
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()
let samples: [Float] = readStereoCAF(url: url)
#expect(!samples.isEmpty)
#expect(hasNonSilentOutput(samples), "OverdrivePedal produced silence")
}
@Test func bypassYieldsDrySignal() throws {
// Render with bypass = 0 (engaged) and bypass = 1 (bypassed) and compare.
// The bypassed render should closely match a render of the dry signal alone.
let renderDuration: Double = 0.1
let numAudioBuses: Int = 64
// --- Dry reference render ---
let (dryEngine, dryURL): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: numAudioBuses
)
defer { try? FileManager.default.removeItem(at: dryURL) }
let drySrcBus: AudioBus = try dryEngine.busAllocator.allocateAudioBus(channelCount: 1)
defer { dryEngine.busAllocator.releaseAudioBus(drySrcBus) }
let drySource: TestLoopSource = TestLoopSource(targetBus: drySrcBus.index)
let dryMaster: TestRouteMaster = TestRouteMaster(inputBusIndex: drySrcBus.index)
try dryEngine.add(drySource)
try dryEngine.add(dryMaster, position: NodePosition.after(drySource))
try dryEngine.start()
let drySamples: [Float] = readStereoCAF(url: dryURL, sampleRate: 44100.0)
// --- Bypass render ---
let (bypassEngine, bypassURL): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
numAudioBuses: numAudioBuses
)
defer { try? FileManager.default.removeItem(at: bypassURL) }
let bpSrcBus: AudioBus = try bypassEngine.busAllocator.allocateAudioBus(channelCount: 1)
let bpDstBus: AudioBus = try bypassEngine.busAllocator.allocateAudioBus(channelCount: 1)
defer {
bypassEngine.busAllocator.releaseAudioBus(bpSrcBus)
bypassEngine.busAllocator.releaseAudioBus(bpDstBus)
}
let bpSource: TestLoopSource = TestLoopSource(targetBus: bpSrcBus.index)
let bpPedal: OverdrivePedal = OverdrivePedal(
inputBus: bpSrcBus.index,
outputBus: bpDstBus.index
)
bpPedal.bypass = 1.0 // fully bypassed
let bpMaster: TestRouteMaster = TestRouteMaster(inputBusIndex: bpDstBus.index)
try bypassEngine.add(bpSource)
try bypassEngine.add(bpPedal, position: NodePosition.after(bpSource))
try bypassEngine.add(bpMaster, position: NodePosition.after(bpPedal))
try bypassEngine.start()
let bypassSamples: [Float] = readStereoCAF(url: bypassURL, sampleRate: 44100.0)
// Both renders should be non-silent.
#expect(hasNonSilentOutput(drySamples), "dry reference was silent")
#expect(hasNonSilentOutput(bypassSamples), "bypass render was silent")
// The bypass has a 20 ms lag — skip the first 2048 samples (1024 frames × 2 ch)
// for the transition to settle, then compare a window of 2048 interleaved samples.
let skipSamples: Int = 2048
let windowSize: Int = 2048
guard drySamples.count >= skipSamples + windowSize,
bypassSamples.count >= skipSamples + windowSize else { return }
let dryWindow: [Float] = Array(drySamples[skipSamples..<(skipSamples + windowSize)])
let bypassWindow: [Float] = Array(bypassSamples[skipSamples..<(skipSamples + windowSize)])
// Sum of squared differences should be small (within 1% power of the dry window).
let ssd: Double = zip(dryWindow, bypassWindow).reduce(0.0) { acc, pair in
let diff: Double = Double(pair.0 - pair.1)
return acc + diff * diff
}
let dryPower: Double = dryWindow.reduce(0.0) { acc, s in
let v: Double = Double(s)
return acc + v * v
}
let relativeError: Double = dryPower > 0 ? ssd / dryPower : ssd
#expect(relativeError < 0.01, "Bypass output differed too much from dry: relErr=\(relativeError)")
}
@Test func mixChangesOutput() throws {
// A render with mix = 0.0 (fully dry) should differ from mix = 1.0 (fully wet).
let renderDuration: Double = 0.1
let blockSize: Int = 512
let sampleRate: Double = 44100.0
let numAudioBuses: Int = 64
func renderWithMix(_ mix: Double) throws -> [Float] {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
duration: renderDuration,
blockSize: blockSize,
sampleRate: sampleRate,
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: OverdrivePedal = OverdrivePedal(
inputBus: srcBus.index,
outputBus: dstBus.index
)
pedal.mix = mix
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, sampleRate: sampleRate)
}
let drySamples: [Float] = try renderWithMix(0.0)
let wetSamples: [Float] = try renderWithMix(1.0)
// Both should be non-silent.
#expect(hasNonSilentOutput(drySamples))
#expect(hasNonSilentOutput(wetSamples))
// Wet and dry should not be identical — the overdrive changes the signal.
// Compare a window after the first block to skip any transients.
let skipSamples: Int = blockSize * 2
let windowSize: Int = blockSize * 2
guard drySamples.count >= skipSamples + windowSize,
wetSamples.count >= skipSamples + windowSize else { return }
let dryWindow: [Float] = Array(drySamples[skipSamples..<(skipSamples + windowSize)])
let wetWindow: [Float] = Array(wetSamples[skipSamples..<(skipSamples + windowSize)])
let ssd: Double = zip(dryWindow, wetWindow).reduce(0.0) { acc, pair in
let diff: Double = Double(pair.0 - pair.1)
return acc + diff * diff
}
#expect(ssd > 0, "mix=0 and mix=1 produced identical output — mix parameter has no effect")
}
}