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

124 lines
4.3 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.
// FuzzPedal.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
import LyrebirdUI
/// Fuzz pedal: high-gain `tanh` saturation followed by a tonal low-pass filter.
///
/// Signal chain (inside `effect(_:)`):
/// ```
/// dry → (× fuzz drive) → Tanh → RBJLowPass(freq: tone, q: 0.707) → mono out
/// ```
///
/// The `fuzz` knob is a pre-gain (drive): it multiplies the input hard before the
/// `tanh` saturator, which squares the waveform off into the thick, sustaining fuzz
/// texture. Because `tanh` saturates toward ±1, the output level stays roughly constant
/// as `fuzz` rises — more drive means more grit and sustain, never less signal. A tone
/// filter then rolls off high-frequency harshness.
///
/// > Note: This deliberately differs from the earlier crossover-distortion model, where
/// > the knob set a silent dead-zone width and large values muted quiet signals.
///
/// The wet/dry mix and bypass switching are handled by the `Pedal` base class.
///
/// ## Parameters
///
/// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus:
///
/// - Parameters:
/// - fuzz: Pre-gain drive into the `tanh` saturator. Higher = more saturation and
/// sustain. Range `[1, 60]`. Defaults to `20.0`.
/// - tone: Low-pass tone cutoff in Hz. Roll off high-frequency fuzz harshness.
/// Range `[200, 8000]` Hz. Defaults to `3500.0`.
public final class FuzzPedal: Pedal {
/// One-line description shown in the picker and info panel.
public override var summary: String {
"Thick, saturated fuzz for aggressive, sustaining distortion."
}
// MARK: - Parameters
/// Pre-gain drive into the saturator. Range [1, 60]; higher = more fuzz.
@UIParam public var fuzz: Double = 20.0
/// Tone filter cutoff frequency in Hz. Range [200, 8000].
@UIParam public var tone: Double = 3500.0
// MARK: - Constants
/// Default wet/dry blend — fully wet so fuzz completely replaces dry signal.
private static let defaultMix: Double = 1.0
/// Q factor for the tone low-pass filter. Butterworth response (no resonant peak).
private let toneQ: Double = 0.707
/// Output trim applied after the `tanh` saturator so the fuzz sits at a sane level
/// relative to the other pedals (`tanh` output is roughly ±1).
private let outputTrim: Double = 0.6
// MARK: - UI knobs
/// Fuzz knob range (saturator pre-gain / drive).
private let fuzzRange: ClosedRange<Double> = 1.0 ... 60.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: "Fuzz", node: $fuzz, range: fuzzRange),
PedalKnob(label: "Tone", node: $tone, range: toneRange),
]
}
// MARK: - Init
/// Creates a fuzz 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: "Fuzz",
inputBus: inputBus,
outputBus: outputBus,
initialMix: FuzzPedal.defaultMix
)
}
// MARK: - Effect
/// Drives the input hard into a `tanh` saturator, then applies a tonal low-pass.
///
/// Output is mono.
///
/// - Parameter dry: Dry mono input signal.
/// - Returns: Fuzzed and tone-shaped mono output.
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
// Pre-gain the signal hard, then saturate. `tanh` squares the waveform off as the
// drive rises while keeping the level bounded — the hallmark of a fuzz. Evaluated at
// 2× so the high harmonics the hard pre-gain generates don't fold back as aliasing.
let saturated: OversampledSaturator = OversampledSaturator(
dry,
drive: $fuzz,
curve: .tanhPreGain,
oversample: 2
)
let trimmed: any ValidGeneratorInput = saturated * outputTrim
return RBJLowPass.Audio(
input: trimmed,
freq: $tone,
q: toneQ
)
}
}