Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/SustainerPedalTests.swift
T
josh 8983e8829e feat(pedals): oversample nonlinear drive stages; Sustainer safety limiter
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>
2026-07-31 20:41:06 -07:00

96 lines
4.1 KiB
Swift

// SustainerPedalTests.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("SustainerPedal")
struct SustainerPedalTests {
private func render(configure: (SustainerPedal) -> Void = { _ in }) 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: SustainerPedal = SustainerPedal(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)
}
@Test func buildsAndProducesNonSilentOutput() throws {
let samples: [Float] = try render()
#expect(!samples.isEmpty)
#expect(hasNonSilentOutput(samples), "SustainerPedal produced silence")
}
@Test func exposesSustainAndToneKnobs() throws {
let pedal: SustainerPedal = SustainerPedal()
#expect(pedal.effectKnobs.map { $0.label } == ["Sustain", "Tone"])
}
@Test func sustainAmountChangesOutput() throws {
// Min vs max sustain drive the compressor very differently → measurably different output.
let gentle: [Float] = try render { $0.sustain = 0.0 }
let squashed: [Float] = try render { $0.sustain = 10.0 }
#expect(hasNonSilentOutput(gentle) && hasNonSilentOutput(squashed))
let skip: Int = 1024, window: Int = 2048
guard gentle.count >= skip + window, squashed.count >= skip + window else { return }
let ssd: Double = zip(gentle[skip..<(skip + window)], squashed[skip..<(skip + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd > 0, "sustain knob had no effect on output")
}
@Test func exposesSafetySwitch() throws {
let pedal: SustainerPedal = SustainerPedal()
#expect(pedal.effectSwitches.map { $0.label } == ["Safety"])
#expect(pedal.effectSwitches.first?.options.map { $0.title } == ["Off", "On"])
}
@Test func safetyLimiterCapsPeaks() throws {
// Max sustain drives a low threshold, high ratio, and +18 dB makeup — the compressor's
// non-zero attack lets transients overshoot. With Safety on, the output peak must stay
// near full scale, and the two switch states must differ.
let off: [Float] = try render {
$0.sampleRate = 44100.0; $0.sustain = 10.0; $0.safetyLimiter = 0.0
}
let on: [Float] = try render {
$0.sampleRate = 44100.0; $0.sustain = 10.0; $0.safetyLimiter = 1.0
}
#expect(hasNonSilentOutput(off) && hasNonSilentOutput(on))
let fill: Int = 2048
guard on.count > fill else { return }
let onPeak: Float = on[fill...].reduce(0) { Swift.max($0, abs($1)) }
#expect(onPeak <= 1.05, "safety limiter failed to cap peak: \(onPeak)")
let window: Int = 2048
guard off.count >= fill + window, on.count >= fill + window else { return }
let ssd: Double = zip(off[fill..<(fill + window)], on[fill..<(fill + window)]).reduce(0.0) { acc, pair in
let d: Double = Double(pair.0 - pair.1); return acc + d * d
}
#expect(ssd > 0, "safety switch had no effect on output")
}
}