// 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 { // 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 = 1.0 ... 60.0 /// Tone knob range in Hz (low-pass cutoff). private let toneRange: ClosedRange = 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. let driven: any ValidGeneratorInput = dry * $fuzz let saturated: Tanh.Audio = Tanh.Audio(driven, mul: outputTrim) return RBJLowPass.Audio( input: saturated, freq: $tone, q: toneQ ) } }