112 lines
3.4 KiB
Swift
112 lines
3.4 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 → Compressor.Audio(threshold:, ratio:, attack:, release:, makeupGain:) → mono out
|
|
/// ```
|
|
///
|
|
/// Attack, release, knee, and makeup gain are fixed at musically sensible defaults.
|
|
/// Knobs expose the two most-musical parameters: 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:
|
|
/// - 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
|
|
|
|
/// 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
|
|
|
|
/// 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
|
|
|
|
/// 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: "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
|
|
|
|
/// 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 {
|
|
Compressor.Audio(
|
|
in: dry,
|
|
threshold: $threshold,
|
|
ratio: $ratio,
|
|
attack: attackTime,
|
|
release: releaseTime,
|
|
kneeWidth: kneeWidth,
|
|
makeupGain: makeupGainDB
|
|
)
|
|
}
|
|
}
|