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>
112 lines
4.0 KiB
Swift
112 lines
4.0 KiB
Swift
// DistortionPedal.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import LyrebirdSynth
|
||
import LyrebirdUI
|
||
|
||
/// Mid-gain distortion — more saturated than `OverdrivePedal`, more controlled than `FuzzPedal`.
|
||
///
|
||
/// A DC-blocking high-pass feeds a `tanh` stage that is post-gained by the `Drive` knob into a
|
||
/// hard clipper — both evaluated at 4× oversampling so the clipper's harmonics don't alias. A
|
||
/// post-clip low-pass `Tone` control tames the harmonics, and a final hard clip re-bounds the
|
||
/// output (the tone low-pass can ring slightly past the ceiling on transients).
|
||
///
|
||
/// ## Knobs
|
||
/// - `drive`: Pre-clip gain into the `tanh` stage. Range `[1, 50]`.
|
||
/// - `tone`: Post-clip low-pass cutoff in Hz. Range `[500, 6000]`.
|
||
public final class DistortionPedal: Pedal {
|
||
|
||
/// One-line description shown in the picker and info panel.
|
||
public override var summary: String {
|
||
"Mid-gain saturated distortion sitting between overdrive and fuzz."
|
||
}
|
||
|
||
// MARK: - Parameters
|
||
|
||
/// Pre-clip gain into the `tanh` stage. Range [1, 50].
|
||
@UIParam public var drive: Double = 12.0
|
||
|
||
/// Post-clip low-pass cutoff in Hz. Range [500, 6000].
|
||
@UIParam public var tone: Double = 3000.0
|
||
|
||
// MARK: - Constants
|
||
|
||
/// Distortion is heard fully wet; the `Level` knob sets output gain.
|
||
private static let defaultMix: Double = 1.0
|
||
|
||
/// DC-blocking high-pass cutoff in Hz, applied before the clipper.
|
||
private let dcBlockFreq: Double = 80.0
|
||
|
||
/// Butterworth Q (no resonant peak) for the conditioning filters.
|
||
private let filterQ: Double = 0.707
|
||
|
||
/// Output hard-clip bounds — guarantees a bounded signal after tone shaping.
|
||
private let clipLow: Double = -1.0
|
||
private let clipHigh: Double = 1.0
|
||
|
||
// MARK: - Ranges
|
||
|
||
private let driveRange: ClosedRange<Double> = 1.0 ... 50.0
|
||
private let toneRange: ClosedRange<Double> = 500.0 ... 6000.0
|
||
|
||
// MARK: - Knobs
|
||
|
||
public override var effectKnobs: [PedalKnob] {
|
||
[
|
||
PedalKnob(label: "Drive", node: $drive, range: driveRange),
|
||
PedalKnob(label: "Tone", node: $tone, range: toneRange),
|
||
]
|
||
}
|
||
|
||
// MARK: - Init
|
||
|
||
/// Creates a distortion 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: "Distortion",
|
||
inputBus: inputBus,
|
||
outputBus: outputBus,
|
||
initialMix: DistortionPedal.defaultMix
|
||
)
|
||
}
|
||
|
||
// MARK: - Effect
|
||
|
||
public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput {
|
||
let dcBlocked: RBJHighPass.Audio = RBJHighPass.Audio(
|
||
input: dry,
|
||
freq: dcBlockFreq,
|
||
q: filterQ
|
||
)
|
||
// tanh soft stage → post-gain by drive → hard clip, all evaluated at 4× so the hard
|
||
// clipper's harmonics settle above the upsampled Nyquist instead of aliasing back.
|
||
// The tone low-pass now follows the clip (standard clip→tone order), which also
|
||
// filters any residual high-band content the downsampler leaves.
|
||
let clipped: OversampledSaturator = OversampledSaturator(
|
||
dcBlocked,
|
||
drive: $drive,
|
||
curve: .tanhPostGainHardClip(low: clipLow, high: clipHigh),
|
||
oversample: 4
|
||
)
|
||
let toned: RBJLowPass.Audio = RBJLowPass.Audio(
|
||
input: clipped,
|
||
freq: $tone,
|
||
q: filterQ
|
||
)
|
||
// Final hard clip re-establishes the strict output bound: the tone low-pass is linear
|
||
// and band-limited, so it can overshoot the ceiling slightly on transients, but
|
||
// clipping its (already band-limited) output adds negligible new aliasing.
|
||
return Clip.Audio(toned, lo: clipLow, hi: clipHigh)
|
||
}
|
||
}
|