8983e8829e
Anti-aliasing (Tier 1): every hard nonlinearity ran at the engine rate, so the harmonics it generated folded back as inharmonic aliasing — worst on loud/bright input at high drive. Add two oversampled RT generators built on Lyrebird's halfband up/down-samplers and move the drive pedals onto them: - OversampledSaturator (+ SaturationCurve) — tanh / normalised soft-clip / tanh+hard-clip curves at 2× or 4×. DistortionPedal now does its tanh→gain→hard clip at 4× (clip before the tone low-pass), with a final hard clip after the low-pass to re-establish the strict output bound; FuzzPedal (tanh) and OverdrivePedal (soft-clip) at 2×. - OversampledRingModulator — input × sine carrier at 2×, carrier synthesized at the oversampled rate so the sum sideband can't alias. Phase wraps both directions (safe for negative carrier frequencies) and a sampleRate guard prevents NaN latching. RingModulatorPedal uses it. Dynamics (Tier 2): SustainerPedal gains an opt-in Safety switch (off by default) that inserts a brick-wall LookAheadLimiter after its compressor to catch the transient overshoot a low-threshold/high-makeup setting can push past full scale. Tests: generator-level alias-reduction proofs (Goertzel alias/fundamental ratio vs a naive base-rate baseline) for both oversampled generators, hard-clip/soft bounds, negative-carrier stability, distortion output-bound at high drive, and Sustainer safety switch exposure + peak-capping. Full suite 478 pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
102 lines
4.0 KiB
Swift
102 lines
4.0 KiB
Swift
// DistortionPedalTests.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("DistortionPedal")
|
|
struct DistortionPedalTests {
|
|
|
|
private func render(
|
|
configure: (DistortionPedal) -> 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: DistortionPedal = DistortionPedal(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 windowedSSD(_ a: [Float], _ b: [Float]) -> Double {
|
|
let skipSamples: Int = 2048
|
|
let windowSize: Int = 2048
|
|
guard a.count >= skipSamples + windowSize,
|
|
b.count >= skipSamples + windowSize else { return 0.0 }
|
|
let aWindow: ArraySlice<Float> = a[skipSamples..<(skipSamples + windowSize)]
|
|
let bWindow: ArraySlice<Float> = b[skipSamples..<(skipSamples + windowSize)]
|
|
return zip(aWindow, bWindow).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), "distortion output was silent")
|
|
}
|
|
|
|
@Test func driveChangesOutput() throws {
|
|
let low: [Float] = try render { pedal in pedal.drive = 2.0 }
|
|
let high: [Float] = try render { pedal in pedal.drive = 45.0 }
|
|
#expect(hasNonSilentOutput(high), "high-drive distortion was silent")
|
|
#expect(windowedSSD(low, high) > 0, "changing drive did not change the output")
|
|
}
|
|
|
|
@Test func toneChangesOutput() throws {
|
|
let dark: [Float] = try render { pedal in pedal.drive = 30.0; pedal.tone = 700.0 }
|
|
let bright: [Float] = try render { pedal in pedal.drive = 30.0; pedal.tone = 5500.0 }
|
|
#expect(hasNonSilentOutput(bright), "bright distortion was silent")
|
|
#expect(windowedSSD(dark, bright) > 0, "changing tone did not change the output")
|
|
}
|
|
|
|
@Test func exposesDriveToneKnobs() throws {
|
|
let pedal: DistortionPedal = DistortionPedal()
|
|
let labels: [String] = pedal.effectKnobs.map { $0.label }
|
|
#expect(labels == ["Drive", "Tone"])
|
|
}
|
|
|
|
@Test func outputStaysBoundedAtHighDrive() throws {
|
|
// The final hard clip (after the tone low-pass) must re-establish the ±1 output bound
|
|
// even at extreme drive, despite the oversampled clip sitting before the low-pass.
|
|
let samples: [Float] = try render { pedal in pedal.drive = 50.0; pedal.tone = 6000.0 }
|
|
let skip: Int = 2048
|
|
guard samples.count > skip else { return }
|
|
let peak: Float = samples[skip...].reduce(0) { Swift.max($0, abs($1)) }
|
|
#expect(peak <= 1.05, "distortion output exceeded the clip ceiling: peak=\(peak)")
|
|
}
|
|
|
|
@Test func registeredInBuiltInRegistry() throws {
|
|
let pedal: Pedal? = PedalRegistry.makePedal(type: "Distortion")
|
|
#expect(pedal is DistortionPedal)
|
|
}
|
|
}
|