// 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 = 1.0 ... 20.0 /// Tone knob range in Hz (low-pass cutoff). private let toneRange: ClosedRange = 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 ) } }