Add input gain stage to Compressor pedal
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>
This commit is contained in:
@@ -11,11 +11,13 @@ import LyrebirdUI
|
||||
///
|
||||
/// Signal chain (inside `effect(_:)`):
|
||||
/// ```
|
||||
/// dry → Compressor.Audio(threshold:, ratio:, attack:, release:, makeupGain:) → mono out
|
||||
/// dry → × inputGain → 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.
|
||||
/// 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.
|
||||
///
|
||||
@@ -24,6 +26,8 @@ import LyrebirdUI
|
||||
/// 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.
|
||||
@@ -32,6 +36,9 @@ 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
|
||||
|
||||
@@ -43,6 +50,10 @@ public final class CompressorPedal: Pedal {
|
||||
/// 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
|
||||
|
||||
@@ -57,6 +68,9 @@ public final class CompressorPedal: Pedal {
|
||||
|
||||
// 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
|
||||
|
||||
@@ -65,6 +79,7 @@ public final class CompressorPedal: Pedal {
|
||||
|
||||
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),
|
||||
]
|
||||
@@ -91,15 +106,18 @@ public final class CompressorPedal: Pedal {
|
||||
|
||||
// MARK: - Effect
|
||||
|
||||
/// Applies feed-forward dynamic range compression.
|
||||
/// 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 {
|
||||
Compressor.Audio(
|
||||
in: dry,
|
||||
// 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,
|
||||
|
||||
@@ -98,4 +98,64 @@ struct CompressorPedalTests {
|
||||
}
|
||||
#expect(ssd > 0, "ratio=1 and ratio=20 produced identical output — ratio knob has no effect")
|
||||
}
|
||||
|
||||
@Test func exposesInputThresholdRatioKnobs() throws {
|
||||
let pedal: CompressorPedal = CompressorPedal()
|
||||
let labels: [String] = pedal.effectKnobs.map { $0.label }
|
||||
#expect(labels == ["Input", "Threshold", "Ratio"])
|
||||
}
|
||||
|
||||
@Test func inputGainChangesOutput() throws {
|
||||
// The pre-compression input gain stage must alter the output: boosting the input
|
||||
// drives more of the signal past the threshold, changing the compressed result.
|
||||
let renderDuration: Double = 0.1
|
||||
let numAudioBuses: Int = 64
|
||||
|
||||
func renderWithInputGain(_ gainDB: Double) throws -> [Float] {
|
||||
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(
|
||||
duration: renderDuration,
|
||||
numAudioBuses: numAudioBuses
|
||||
)
|
||||
defer { try? FileManager.default.removeItem(at: url) }
|
||||
|
||||
let srcBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
let dstBus: AudioBus = try engine.busAllocator.allocateAudioBus(channelCount: 1)
|
||||
defer {
|
||||
engine.busAllocator.releaseAudioBus(srcBus)
|
||||
engine.busAllocator.releaseAudioBus(dstBus)
|
||||
}
|
||||
|
||||
let source: TestLoopSource = TestLoopSource(targetBus: srcBus.index)
|
||||
let pedal: CompressorPedal = CompressorPedal(
|
||||
inputBus: srcBus.index,
|
||||
outputBus: dstBus.index
|
||||
)
|
||||
pedal.inputGain = gainDB
|
||||
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
|
||||
try engine.add(source)
|
||||
try engine.add(pedal, position: NodePosition.after(source))
|
||||
try engine.add(master, position: NodePosition.after(pedal))
|
||||
try engine.start()
|
||||
return readStereoCAF(url: url)
|
||||
}
|
||||
|
||||
let unitySamples: [Float] = try renderWithInputGain(0.0)
|
||||
let boostedSamples: [Float] = try renderWithInputGain(24.0)
|
||||
|
||||
#expect(hasNonSilentOutput(unitySamples), "0 dB input-gain render was silent")
|
||||
#expect(hasNonSilentOutput(boostedSamples), "+24 dB input-gain render was silent")
|
||||
|
||||
let skipSamples: Int = 1024
|
||||
let windowSize: Int = 2048
|
||||
guard unitySamples.count >= skipSamples + windowSize,
|
||||
boostedSamples.count >= skipSamples + windowSize else { return }
|
||||
|
||||
let unityWindow: [Float] = Array(unitySamples[skipSamples..<(skipSamples + windowSize)])
|
||||
let boostedWindow: [Float] = Array(boostedSamples[skipSamples..<(skipSamples + windowSize)])
|
||||
let ssd: Double = zip(unityWindow, boostedWindow).reduce(0.0) { acc, pair in
|
||||
let diff: Double = Double(pair.0 - pair.1)
|
||||
return acc + diff * diff
|
||||
}
|
||||
#expect(ssd > 0, "input gain 0 dB and +24 dB produced identical output — input gain has no effect")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -423,9 +423,12 @@ struct PresetTests {
|
||||
// Apply the captured state (lightweight recall — no graph rebuild).
|
||||
sg.applyState(captured)
|
||||
|
||||
// threshold is the first effectKnob; applyState calls knob.node.update(value)
|
||||
// which sets the UIParamNode. The UIParamNode.value should now reflect the restored value.
|
||||
let thresholdKnobValue: Double = compressor.knobs[0].node.value
|
||||
// applyState calls knob.node.update(value) on each knob; the Threshold knob's
|
||||
// UIParamNode.value should now reflect the restored value. Look up by label rather
|
||||
// than position so the test is robust to knob additions.
|
||||
let thresholdKnobValue: Double = try #require(
|
||||
compressor.knobs.first(where: { $0.label == "Threshold" })?.node.value
|
||||
)
|
||||
#expect(
|
||||
abs(thresholdKnobValue - knownThreshold) < 1e-9,
|
||||
"threshold knob should be \(knownThreshold) after applyState, got \(thresholdKnobValue)"
|
||||
|
||||
Reference in New Issue
Block a user