// LimiterPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Limiter pedal: a clean gain-boost stage feeding a brick-wall look-ahead limiter. /// /// Signal chain (inside `effect(_:)`): /// ``` /// dry → × boost → LookAheadLimiter(maxAmplitude: ceiling) → mono out /// ``` /// /// The boost stage drives the signal up *before* the limiter, so cranking `boost` /// pushes more of the signal into limiting and raises sustain/perceived loudness while /// the look-ahead limiter guarantees the output peak never exceeds `ceiling`. /// /// The wet/dry mix and bypass switching are handled by the `Pedal` base class. /// /// ## Parameters /// /// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus: /// /// - Parameters: /// - boost: Pre-limit input gain as a linear multiplier. `1.0` = unity. Higher values /// drive harder into the limiter. Range `[1, 12]`. Defaults to `1.0`. /// - ceiling: Output peak ceiling as a linear amplitude in `[0, 1]`. The limited output /// never exceeds this absolute value. Range `[0.1, 1.0]`. Defaults to `0.95`. public final class LimiterPedal: Pedal { /// One-line description shown in the picker and info panel. public override var summary: String { "Brick-wall limiter with an input boost to push level safely to the ceiling." } // MARK: - Parameters /// Pre-limit input gain (linear multiplier). Range [1, 12]. @UIParam public var boost: Double = 1.0 /// Output peak ceiling (linear amplitude). Range [0.1, 1.0]. @UIParam public var ceiling: Double = 0.95 // MARK: - Constants /// Default wet/dry blend — fully wet; the limiter sits in-line. private static let defaultMix: Double = 1.0 /// Look-ahead / output delay in seconds. Fixed at a short window that catches /// transients without smearing attack. private let lookAheadTime: Double = 0.005 // MARK: - UI knobs /// Boost knob range (linear pre-limit gain). private let boostRange: ClosedRange = 1.0 ... 12.0 /// Ceiling knob range (linear output peak). private let ceilingRange: ClosedRange = 0.1 ... 1.0 public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Boost", node: $boost, range: boostRange), PedalKnob(label: "Ceiling", node: $ceiling, range: ceilingRange), ] } // MARK: - Init /// Creates a limiter 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: "Limiter", inputBus: inputBus, outputBus: outputBus, initialMix: LimiterPedal.defaultMix ) } // MARK: - Effect /// Boosts the dry signal then constrains its peak with a look-ahead limiter. /// /// Output is mono. /// /// - Parameter dry: Dry mono input signal. /// - Returns: Boosted, peak-limited mono output. public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { let boosted: any ValidGeneratorInput = dry * $boost return LookAheadLimiter( in: boosted, maxAmplitude: $ceiling, lookAheadTime: lookAheadTime, sampleRate: sampleRate ) } }