Files
ShoeGaze/Sources/LyrebirdShoeGaze/Pedals/OverdrivePedal.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

101 lines
3.2 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.
// OverdrivePedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Overdrive pedal: soft-clipping saturation followed by a tonal low-pass filter.
///
/// Signal chain (inside `effect(_:)`):
/// ```
/// dry → OversampledSaturator(curve: .softClipNormalized, drive:) → RBJLowPass(freq: tone, q: 0.707) → mono out
/// ```
///
/// The wet/dry mix and bypass switching are handled by the `Pedal` base class.
///
/// ## Parameters
///
/// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus:
///
/// - Parameters:
/// - drive: Saturation amount. Higher values push more of the signal into soft-clip
/// territory. Range `[1, 20]`. Defaults to `4.0`.
/// - tone: Low-pass tone cutoff in Hz. Roll off high-frequency harshness.
/// Range `(0, 20000]`. Defaults to `4000.0`.
public final class OverdrivePedal: Pedal {
/// One-line description shown in the picker and info panel.
public override var summary: String {
"Warm soft-clip overdrive with a tone control, from gentle grit to singing sustain."
}
// MARK: - Parameters
/// Saturation drive amount. Range [1, 20]; higher = more clipping.
@UIParam public var drive: Double = 4.0
/// Tone filter cutoff frequency in Hz. Range (0, 20000].
@UIParam public var tone: Double = 4000.0
// MARK: - Constants
/// Q factor for the tone low-pass filter. Butterworth response (no resonant peak).
private let toneQ: Double = 0.707
// MARK: - Init
/// Creates an overdrive pedal.
///
/// - Parameters:
/// - inputBus: Audio bus index to read the dry input from. Defaults to `0`.
/// - outputBus: Audio bus index to write the processed output to. Defaults to `0`.
public init(
inputBus: Int = 0,
outputBus: Int = 0
) {
super.init(name: "Overdrive", inputBus: inputBus, outputBus: outputBus)
}
// MARK: - UI knobs
/// Drive knob range (waveshaper input gain).
private let driveRange: ClosedRange<Double> = 1.0 ... 20.0
/// Tone knob range in Hz (low-pass cutoff).
private let toneRange: ClosedRange<Double> = 200.0 ... 8000.0
public override var effectKnobs: [PedalKnob] {
[
PedalKnob(label: "Drive", node: $drive, range: driveRange),
PedalKnob(label: "Tone", node: $tone, range: toneRange),
]
}
// MARK: - Effect
/// Applies soft-clip saturation followed by a tonal low-pass filter.
///
/// Output is mono.
///
/// - Parameter dry: Dry mono input signal.
/// - Returns: Saturated and tone-shaped mono output.
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
// Same normalised soft-clip curve as before (`tanh(drive·x) / tanh(drive)`), but
// evaluated at 2× so high-drive harmonics don't alias.
let saturated: OversampledSaturator = OversampledSaturator(
dry,
drive: $drive,
curve: .softClipNormalized,
oversample: 2
)
return RBJLowPass.Audio(
input: saturated,
freq: $tone,
q: toneQ
)
}
}