Files
ShoeGaze/Sources/LyrebirdShoeGaze/Pedals/CompressorPedal.swift
T
josh f29ab609bd Add Chorus, Sustainer, Ring Modulator pedals + pedal-info UI
Pedals (each registered, with PedalType static, enclosure color, palette order,
summary, and tests):
- Chorus: true-stereo widener (two anti-phase modulated delay lines), distinct
  from the mono-summed Modulation chorus.
- Sustainer: one-knob compressor (sustain drives threshold/ratio/makeup via
  LinLin) + tone low-pass.
- Ring Modulator: dry × tunable sine carrier.

Pedal-info UI:
- Pedal.summary (open var, overridden by all 21 pedals; mandatory going forward).
- PedalRegistry.summary(for:) for the picker.
- PedalPaletteView rows show the summary as a subtitle.
- PedalView gains an ⓘ button → PedalInfoView sheet showing the description and
  the effect controls (knobs/sliders/switches) with ranges/options — excludes
  the common Mix/Level/bypass.

Tests: per-pedal build/param-change/knob-list; every built-in pedal has a
non-empty summary; info excludes common controls. 183 tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 18:05:30 -07:00

135 lines
4.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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 {
/// 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
// 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
)
}
}