5a07a6d4b4
A pre-compression input gain knob (Input, dB, range [-12, +24], default 0) boosts/cuts the signal before it hits the compressor, so driving the input harder pushes more of the signal past the threshold. The dB value is converted to a linear multiplier via Exp(dB * ln(10)/20) and applied to the dry signal. Tests: knob list is [Input, Threshold, Ratio]; input gain changes the output. The preset recall test now finds the Threshold knob by label, not position. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
130 lines
4.5 KiB
Swift
130 lines
4.5 KiB
Swift
// 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`.
|
||
public final class CompressorPedal: Pedal {
|
||
|
||
// 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
|
||
|
||
// 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
|
||
|
||
// MARK: - UI knobs
|
||
|
||
/// Input-gain knob range in dB.
|
||
private let inputGainRange: ClosedRange<Double> = -12.0 ... 24.0
|
||
|
||
/// Threshold knob range in dB.
|
||
private let thresholdRange: ClosedRange<Double> = -40.0 ... 0.0
|
||
|
||
/// Ratio knob range.
|
||
private let ratioRange: ClosedRange<Double> = 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),
|
||
]
|
||
}
|
||
|
||
// 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
|
||
return Compressor.Audio(
|
||
in: boosted,
|
||
threshold: $threshold,
|
||
ratio: $ratio,
|
||
attack: attackTime,
|
||
release: releaseTime,
|
||
kneeWidth: kneeWidth,
|
||
makeupGain: makeupGainDB
|
||
)
|
||
}
|
||
}
|