fix(noise-gate): smooth asymmetric close + Attack/Release knobs
Reported bug: a sustained note popped into a different tone mid-decay (~3.63s in the user's recording). Root cause confirmed by rendering the user's rig with their input file: the gate keyed off the raw guitar and, as a note decayed through the threshold, snapped its gain 1.0 -> floor in ~10 ms (symmetric 10 ms Lag). With heavy downstream gain (the rig's Fuzz, ~50x) amplifying that step, the ~-9 dB duck read as an audible pop. Fix: replace the symmetric Lag with an asymmetric LagUpDown — fast open (so a note's attack isn't softened) / slow close (fades the gate shut and rides out envelope chatter at the threshold). Verified against the user's input file: the gate gain now fades 1.0 -> floor smoothly over ~70 ms, monotonic, no snap. Feature (per request): expose Attack (open) and Release (close) time as knobs in the advanced pop-over rather than a separate near-duplicate "Vari" gate. Placed in advancedKnobs so they persist append-only in advancedValues — gates saved before this load with the timing at defaults, no migration (verified by test). Release default 0.08 = the smooth close; range 10 ms..500 ms. Tests: gate close lingers longer with a longer Release and the default lingers well past a fast close (guards the pop fix + the Release control); Attack/ Release exposed in the pop-over (not the face), round-trip through state, and a legacy no-advancedValues preset defaults the timing. 675 tests pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -89,4 +89,85 @@ struct NoiseGatePedalTests {
|
||||
let samples: [Float] = try render(threshold: 0.95, floor: 1.0)
|
||||
#expect(hasNonSilentOutput(samples), "Unity-floor gate muted the signal")
|
||||
}
|
||||
|
||||
/// A sine that steps from loud (0.3, gate open) down to a constant below-threshold level (0.03)
|
||||
/// over 50 ms and holds there — so the gate opens, then closes, while the source level stays
|
||||
/// constant. That isolates the gate's close fade (release-dependent) from any source decay.
|
||||
/// `Line` holds its `end` value after `dur`, giving the sustained low tail.
|
||||
private final class DecaySineSource: SynthesizerBase {
|
||||
private let bus: Int
|
||||
init(bus: Int) { self.bus = bus; super.init() }
|
||||
override func build() {
|
||||
output(bus: bus, source: OscSin.Audio(freq: 220.0) * Line.Control(start: 0.3, end: 0.03, dur: 0.05))
|
||||
}
|
||||
}
|
||||
|
||||
/// Energy in the gate's *close window* — the source steps down through the threshold ~50 ms in
|
||||
/// then holds a constant below-threshold level, so the output energy after that reflects only how
|
||||
/// long the gate lingers before it finishes fading shut. A longer `release` = more lingering
|
||||
/// energy; a snap close = little. (floor 0, so once fully closed the tail is silent.)
|
||||
private func closeWindowEnergy(release: Double) throws -> Double {
|
||||
let sampleRate: Double = 48000.0
|
||||
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.6, sampleRate: sampleRate)
|
||||
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: DecaySineSource = DecaySineSource(bus: srcBus.index)
|
||||
let gate: NoiseGatePedal = NoiseGatePedal(inputBus: srcBus.index, outputBus: dstBus.index)
|
||||
gate.sampleRate = sampleRate
|
||||
gate.threshold = 0.06
|
||||
gate.floor = 0.0
|
||||
gate.release = release
|
||||
let master: TestRouteMaster = TestRouteMaster(inputBusIndex: dstBus.index)
|
||||
try engine.add(source)
|
||||
try engine.add(gate, position: NodePosition.after(source))
|
||||
try engine.add(master, position: NodePosition.after(gate))
|
||||
try engine.start()
|
||||
let inter: [Float] = readStereoCAF(url: url, sampleRate: sampleRate)
|
||||
let mono: [Double] = stride(from: 0, to: inter.count, by: 2).map { Double(inter[$0]) }
|
||||
let start: Int = Int(0.12 * sampleRate) // after the step-down; the gate is closing/closed
|
||||
var energy: Double = 0.0
|
||||
for i: Int in start..<mono.count { energy += mono[i] * mono[i] }
|
||||
return energy
|
||||
}
|
||||
|
||||
@Test func releaseTunesCloseTime() throws {
|
||||
// A long Release lets the gate linger (more energy through the close) vs a short Release that
|
||||
// snaps shut — and the shipped default (0.08) lingers well past a near-instant close, so the
|
||||
// gate fades rather than pops. Guards both the Release control and the pop fix.
|
||||
let fast: Double = try closeWindowEnergy(release: 0.02)
|
||||
let slow: Double = try closeWindowEnergy(release: 0.4)
|
||||
let deflt: Double = try closeWindowEnergy(release: 0.08)
|
||||
#expect(slow > fast * 1.5, "long Release did not linger longer than short (fast=\(fast) slow=\(slow))")
|
||||
#expect(deflt > fast * 1.2, "default close is nearly as abrupt as the fast close (\(deflt) vs \(fast))")
|
||||
}
|
||||
|
||||
@Test func exposesAttackReleaseInAdvancedPopover() throws {
|
||||
#expect(NoiseGatePedal().advancedKnobs.map { $0.label } == ["Attack", "Release"])
|
||||
// Attack/Release are pop-over only — not on the face.
|
||||
let face: [String] = NoiseGatePedal().effectKnobs.map { $0.label }
|
||||
#expect(face == ["Threshold", "Floor"])
|
||||
}
|
||||
|
||||
@Test func attackReleaseRoundTripThroughState() throws {
|
||||
let pedal: NoiseGatePedal = NoiseGatePedal()
|
||||
pedal.attack = 0.02
|
||||
pedal.release = 0.3
|
||||
let restored: NoiseGatePedal = NoiseGatePedal()
|
||||
restored.applyState(pedal.captureState())
|
||||
#expect(restored.attack == 0.02 && restored.release == 0.3, "Attack/Release did not round-trip")
|
||||
}
|
||||
|
||||
@Test func legacyGateWithoutAdvancedValuesDefaultsTiming() throws {
|
||||
// A gate saved before Attack/Release existed has empty advancedValues → timing defaults, and
|
||||
// Threshold/Floor still restore correctly (append-only, no migration).
|
||||
let legacy: PedalState = PedalState(
|
||||
id: UUID(), type: "Noise Gate",
|
||||
knobValues: [0.03, 0.4, 1.0, 0.7], selectorValues: [], advancedValues: [], bypass: 0.0)
|
||||
let pedal: NoiseGatePedal = NoiseGatePedal()
|
||||
pedal.applyState(legacy)
|
||||
#expect(pedal.threshold == 0.03 && pedal.floor == 0.4, "legacy Threshold/Floor mis-mapped")
|
||||
#expect(pedal.attack == 0.005 && pedal.release == 0.08, "legacy gate should default Attack/Release")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user