// CompressorPedal.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import LyrebirdSynth import LyrebirdUI /// Compressor pedal: feed-forward dynamic range compressor. /// /// Signal chain (inside `effect(_:)`): /// ``` /// dry → × inputGain → Compressor.Audio(threshold:, ratio:, attack:, release:, makeupGain:) → mono out /// ``` /// /// An input gain stage boosts (or cuts) the signal *before* compression, so driving the /// input harder pushes more of the signal past the threshold. Attack, release, knee, and /// makeup gain are fixed at musically sensible defaults; knobs expose input gain, threshold, /// and ratio. /// /// The wet/dry mix and bypass switching are handled by the `Pedal` base class. /// /// ## Parameters /// /// Inherits `mix`, `bypass`, and `level` from `Pedal`, plus: /// /// - Parameters: /// - inputGain: Pre-compression input gain in dB. `0` = unity. Range `[-12, 24]`. /// Defaults to `0.0`. /// - threshold: Gain-reduction threshold in dB. Signals above this level are /// compressed. Range `[-40, 0]` dB. Defaults to `-12.0`. /// - ratio: Compression ratio `≥ 1.0`. `1.0` = bypass, `4.0` = 4:1 compression. /// Range `[1, 20]`. Defaults to `4.0`. /// /// A feed-forward compressor can overshoot on fast transients (its attack is non-zero), /// so a hot input plus makeup gain can push peaks past full scale and clip the output. /// The **Safety** switch inserts a brick-wall `LookAheadLimiter` after the compressor to /// catch those peaks; it is off by default (leave adding a limiter to the user) and adds /// a short look-ahead output delay when engaged. public final class CompressorPedal: Pedal { /// One-line description shown in the picker and info panel. public override var summary: String { "Feed-forward compressor that evens out dynamics, with a pre-gain input stage." } // MARK: - Parameters /// Pre-compression input gain in dB. Range [-12, 24]. `0` = unity. @UIParam public var inputGain: Double = 0.0 /// Compression threshold in dB. Range [-40, 0]. @UIParam public var threshold: Double = -12.0 /// Compression ratio. Range [1, 20]. @UIParam public var ratio: Double = 4.0 /// Safety limiter engage state. `0` = off (compressor may overshoot), `1` = on /// (brick-wall look-ahead limiter catches transient peaks). @UIParam public var safetyLimiter: Double = 0.0 // MARK: - Constants /// Default wet/dry blend — fully wet; the compressor is inserted in-line. private static let defaultMix: Double = 1.0 /// dB → natural-exponent scale, `ln(10) / 20`. `exp(dB · scale) == 10^(dB/20)`, the /// linear amplitude multiplier for a dB gain. Applied to the input-gain stage. private let dbToLinExpScale: Double = 0.115_129_254_649_702_28 /// Attack time constant in seconds — time to reach full gain reduction on a transient. private let attackTime: Double = 0.01 /// Release time constant in seconds — time for gain to recover after level drops below threshold. private let releaseTime: Double = 0.1 /// Soft-knee width in dB. Zero = hard knee for maximum punch. private let kneeWidth: Double = 0.0 /// Makeup gain in dB applied after compression to restore perceived loudness. private let makeupGainDB: Double = 4.0 /// Safety-limiter peak ceiling (linear amplitude), just below full scale. private let safetyCeiling: Double = 0.98 /// Safety-limiter look-ahead / output delay in seconds. Short enough to catch /// transients without smearing attack; only in circuit when Safety is engaged. private let safetyLookAheadTime: Double = 0.005 /// Safety-limiter gain-release time in seconds. private let safetyReleaseTime: Double = 0.05 /// Safety switch position values. private let safetyOffValue: Double = 0.0 private let safetyOnValue: Double = 1.0 // MARK: - UI knobs /// Input-gain knob range in dB. private let inputGainRange: ClosedRange = -12.0 ... 24.0 /// Threshold knob range in dB. private let thresholdRange: ClosedRange = -40.0 ... 0.0 /// Ratio knob range. private let ratioRange: ClosedRange = 1.0 ... 20.0 public override var effectKnobs: [PedalKnob] { [ PedalKnob(label: "Input", node: $inputGain, range: inputGainRange), PedalKnob(label: "Threshold", node: $threshold, range: thresholdRange), PedalKnob(label: "Ratio", node: $ratio, range: ratioRange), ] } public override var effectSwitches: [PedalSwitch] { [ PedalSwitch( label: "Safety", node: $safetyLimiter, options: [ PedalSwitch.Option(title: "Off", value: safetyOffValue), PedalSwitch.Option(title: "On", value: safetyOnValue), ] ) ] } // MARK: - Init /// Creates a compressor 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: "Compressor", inputBus: inputBus, outputBus: outputBus, initialMix: CompressorPedal.defaultMix ) } // MARK: - Effect /// Boosts the input by `inputGain` dB, then applies feed-forward dynamic range compression. /// /// Output is mono. /// /// - Parameter dry: Dry mono input signal. /// - Returns: Compressed mono output. public override func effect(_ dry: any ValidGeneratorInput) -> any ValidGeneratorInput { // Convert the input-gain dB to a linear multiplier (10^(dB/20)) and apply before compression. let inputGainLinear: Exp.Audio = Exp.Audio($inputGain * dbToLinExpScale) let boosted: any ValidGeneratorInput = dry * inputGainLinear let compressed: Compressor.Audio = Compressor.Audio( in: boosted, threshold: $threshold, ratio: $ratio, attack: attackTime, release: releaseTime, kneeWidth: kneeWidth, makeupGain: makeupGainDB ) // Brick-wall safety limiter after the compressor. Both branches are always built; // `Select` picks per the Safety switch (0 = compressor only, 1 = limited). let limited: LookAheadLimiter = LookAheadLimiter( in: compressed, maxAmplitude: safetyCeiling, lookAheadTime: safetyLookAheadTime, releaseTime: safetyReleaseTime, sampleRate: sampleRate ) return Select.Audio( which: $safetyLimiter, inputs: [compressed, limited] ) } }