diff --git a/Sources/LyrebirdShoeGaze/Pedals/CompressorPedal.swift b/Sources/LyrebirdShoeGaze/Pedals/CompressorPedal.swift index ee6d48d..8ad6577 100644 --- a/Sources/LyrebirdShoeGaze/Pedals/CompressorPedal.swift +++ b/Sources/LyrebirdShoeGaze/Pedals/CompressorPedal.swift @@ -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 = -12.0 ... 24.0 + /// Threshold knob range in dB. private let thresholdRange: ClosedRange = -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, diff --git a/Tests/LyrebirdShoeGazeTests/CompressorPedalTests.swift b/Tests/LyrebirdShoeGazeTests/CompressorPedalTests.swift index 2c6b61d..04d9724 100644 --- a/Tests/LyrebirdShoeGazeTests/CompressorPedalTests.swift +++ b/Tests/LyrebirdShoeGazeTests/CompressorPedalTests.swift @@ -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") + } } diff --git a/Tests/LyrebirdShoeGazeTests/PresetTests.swift b/Tests/LyrebirdShoeGazeTests/PresetTests.swift index beebab7..9bf56fe 100644 --- a/Tests/LyrebirdShoeGazeTests/PresetTests.swift +++ b/Tests/LyrebirdShoeGazeTests/PresetTests.swift @@ -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)"