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:
@@ -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