fix(output,gate): unity mono output + RMS noise-gate detector

Two fixes from the guitar-input analysis:

- Mono board output no longer loses 3 dB. BoardOutputMaster panned a mono
  board to stereo centre with an equal-power Pan2 (×0.707 per channel). A
  mono rig should come out at the level it went in, so the signal is now
  written to both hardware buses directly at unity.

- Noise Gate gains a Peak/RMS detector switch (defaults to RMS). The peak
  follower (|x|) rides the waveform's peaks, so the gate's threshold decision
  tracked instantaneous peaks rather than loudness; the RMS detector
  (√ symmetric-average of x²) follows perceived level, so the gate opens and
  closes where the note actually is. Implemented as a rebuildsOnChange switch
  that builds only the chosen follower. Serialization-safe: a gate saved
  before the switch (empty switchValues) loads with the RMS default.

Tests cover the detector switch, RMS default (incl. legacy load), state
round-trip, and RMS closing earlier than Peak on a sustained tail.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-03 08:49:16 -08:00
parent 0ddf350f1f
commit a434b14f4b
3 changed files with 129 additions and 16 deletions
@@ -170,4 +170,69 @@ struct NoiseGatePedalTests {
#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")
}
@Test func exposesDetectorSwitch() throws {
let switches: [PedalSwitch] = NoiseGatePedal().effectSwitches
#expect(switches.map { $0.label } == ["Detector"])
#expect(switches.first?.options.map { $0.title } == ["Peak", "RMS"])
}
@Test func defaultsToRMSAndLegacyGateGetsRMS() throws {
#expect(NoiseGatePedal().detector == 1.0, "new gate should default to RMS")
// A gate saved before the detector switch (empty switchValues) loads with the RMS default.
let legacy: PedalState = PedalState(
id: UUID(), type: "Noise Gate",
knobValues: [0.03, 0.4, 1.0, 1.0], switchValues: [], advancedValues: [], bypass: 0.0)
let pedal: NoiseGatePedal = NoiseGatePedal()
pedal.applyState(legacy)
#expect(pedal.detector == 1.0, "legacy gate should default to the RMS detector")
}
@Test func detectorRoundTripsThroughState() throws {
let pedal: NoiseGatePedal = NoiseGatePedal()
pedal.detector = 0.0 // Peak
let restored: NoiseGatePedal = NoiseGatePedal()
restored.applyState(pedal.captureState())
#expect(restored.detector == 0.0)
}
@Test func rmsDetectorClosesEarlierThanPeak() throws {
// On a note held just above threshold, the RMS envelope (≈ 3 dB below the peak envelope for a
// steady tone) drops under the close threshold sooner than the peak envelope does — so the
// RMS gate shuts earlier and passes less of the sustained tail than the Peak gate. The source
// steps to a constant 0.03 (peak env ≈ 0.03, RMS env ≈ 0.021); with threshold 0.025 the Peak
// gate stays open on the tail while the RMS gate closes.
func tailEnergy(detector: Double) throws -> Double {
let sr: Double = 48000.0
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.6, sampleRate: sr)
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 = sr
gate.threshold = 0.025
gate.floor = 0.0
gate.detector = detector
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: sr)
let mono: [Double] = stride(from: 0, to: inter.count, by: 2).map { Double(inter[$0]) }
let start: Int = Int(0.15 * sr) // the sustained, below-peak-threshold tail
var energy: Double = 0.0
for i: Int in start..<mono.count { energy += mono[i] * mono[i] }
return energy
}
let peakTail: Double = try tailEnergy(detector: 0.0)
let rmsTail: Double = try tailEnergy(detector: 1.0)
#expect(peakTail > rmsTail * 2.0,
"RMS should close earlier than Peak on the sustained tail (peak=\(peakTail) rms=\(rmsTail))")
}
}