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>
47 lines
1.9 KiB
Swift
47 lines
1.9 KiB
Swift
// SaturationCurve.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Darwin
|
|
|
|
/// The waveshaping curve applied per (oversampled) sample by ``OversampledSaturator``.
|
|
///
|
|
/// Each case reproduces the nonlinearity a specific drive pedal used to build inline from
|
|
/// stock generators, so moving that pedal onto the oversampled path is voicing-preserving:
|
|
///
|
|
/// - `tanhPreGain` — `tanh(drive · x)`. Symmetric soft saturation with the drive applied
|
|
/// *before* the `tanh` (the fuzz voicing: hard pre-gain squares the wave off).
|
|
/// - `softClipNormalized` — `tanh(drive · x) / tanh(drive)`. The same curve normalised so a
|
|
/// unity-drive signal passes near-linearly (the overdrive voicing).
|
|
/// - `tanhPostGainHardClip(low:high:)` — `clip(tanh(x) · drive, low, high)`. A gentle `tanh`
|
|
/// stage, post-gained by drive into a hard clipper (the distortion voicing). The hard clip
|
|
/// is the dominant harmonic source and the reason this curve wants the highest oversampling
|
|
/// factor.
|
|
public enum SaturationCurve: Sendable {
|
|
case tanhPreGain
|
|
case softClipNormalized
|
|
case tanhPostGainHardClip(low: Double, high: Double)
|
|
|
|
/// Applies the curve to a single sample.
|
|
///
|
|
/// - Parameters:
|
|
/// - x: Input sample (already at the oversampled rate).
|
|
/// - drive: Drive amount. The caller clamps this to a small positive floor so
|
|
/// `softClipNormalized`'s `tanh(drive)` denominator is never zero.
|
|
/// - Returns: The shaped sample.
|
|
@inline(__always)
|
|
func apply(_ x: Double, drive: Double) -> Double {
|
|
switch self {
|
|
case .tanhPreGain:
|
|
return tanh(drive * x)
|
|
case .softClipNormalized:
|
|
return tanh(drive * x) / tanh(drive)
|
|
case .tanhPostGainHardClip(let low, let high):
|
|
let shaped: Double = tanh(x) * drive
|
|
return Swift.min(high, Swift.max(low, shaped))
|
|
}
|
|
}
|
|
}
|