Fix tuner leaking its pitch signal into the audio output
TunerTap wrote its pitch-capture generator to the scratch bus via output(bus:), which always builds an Out.Audio — so the write went to *audio*-bus memory at the control bus's index. Control-bus indices are low, so they collide with the hardware output buses (e.g. index 1 = right channel), and because the tap runs .after(mainGroup) the write lands after the master's rig-mute gate. Result: the tracked frequency (e.g. 82 Hz) was summed onto an output channel, bypassing the mute — audible as crackle while the pitch was unstable, stopping once a note locked. Confirmed by render: the leaked value appears verbatim on the output bus. Route through controlOutput (Out.Control → control-bus memory) instead. The scratch bus is a control bus; it was only ever meant to give the engine a reason to evaluate the side-effecting capture generator, and nothing reads it. Test engagedTunerDoesNotLeakIntoOutput renders a muted rig with the tuner engaged and asserts the settled output tail is silent (fails without the fix — the tail carries the leaked pitch value; passes with it). 465 tests pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -55,6 +55,12 @@ final class TunerTap: SynthesizerBase {
|
|||||||
)
|
)
|
||||||
let capture: TunerPitchCapture = TunerPitchCapture(input: filtered)
|
let capture: TunerPitchCapture = TunerPitchCapture(input: filtered)
|
||||||
pitchCapture = capture
|
pitchCapture = capture
|
||||||
output(bus: scratchControlBus, source: capture)
|
// Route through `controlOutput` (Out.Control → control-bus memory), NOT `output`
|
||||||
|
// (Out.Audio → *audio*-bus memory at the same index). The scratch bus exists only to give
|
||||||
|
// the engine a reason to evaluate the capture generator; writing the capture's pitch signal
|
||||||
|
// to an audio bus at the control-bus index would land it on a hardware output channel (a low
|
||||||
|
// control index collides with the output buses) — audible as crackle while the pitch is
|
||||||
|
// unstable — and, because this tap runs after `mainGroup`, would bypass the rig mute.
|
||||||
|
controlOutput(bus: scratchControlBus, source: capture)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,4 +120,45 @@ struct TunerTests {
|
|||||||
shoeGaze.engageTuner()
|
shoeGaze.engageTuner()
|
||||||
#expect(shoeGaze.rigMuted, "Second engageTuner() call while already engaged must be a no-op")
|
#expect(shoeGaze.rigMuted, "Second engageTuner() call while already engaged must be a no-op")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// With the rig muted, an engaged tuner must leave the audio output silent. Guards against the
|
||||||
|
/// tuner's pitch-capture signal leaking into a hardware output channel: its scratch bus is a
|
||||||
|
/// *control* bus, but writing it through the audio-rate `output(bus:)` helper would treat that
|
||||||
|
/// index as an *audio* bus (colliding with the low output-bus indices) and, since the tap runs
|
||||||
|
/// after `mainGroup`, bypass the rig mute — audible as crackle while the pitch is unstable.
|
||||||
|
@Test func engagedTunerDoesNotLeakIntoOutput() throws {
|
||||||
|
let renderDuration: Double = 0.4
|
||||||
|
let sampleRate: Double = 44100.0
|
||||||
|
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: renderDuration, numAudioBuses: 256)
|
||||||
|
defer { try? FileManager.default.removeItem(at: url) }
|
||||||
|
let shoeGaze: ShoeGaze = ShoeGaze(engine: engine, inputSource: .testLoop)
|
||||||
|
|
||||||
|
try withExtendedLifetime(shoeGaze) {
|
||||||
|
let board: PedalBoard = try shoeGaze.addBoard()
|
||||||
|
try board.add(VolumePedal())
|
||||||
|
try shoeGaze.finalizeBoard(board)
|
||||||
|
|
||||||
|
// Feed the test-loop signal so the pitch tracker produces a real (non-zero) estimate —
|
||||||
|
// the value that would leak if the scratch write hit an audio bus.
|
||||||
|
shoeGaze.setInputMuted(false)
|
||||||
|
shoeGaze.tunerMutesRig = true
|
||||||
|
shoeGaze.engageTuner()
|
||||||
|
#expect(shoeGaze.rigMuted)
|
||||||
|
|
||||||
|
try engine.start()
|
||||||
|
let samples: [Float] = readStereoCAF(url: url, sampleRate: sampleRate)
|
||||||
|
|
||||||
|
// Ignore the opening window: the rig-mute gain ramps up over its first blocks (a separate,
|
||||||
|
// pre-existing BoardOutputMaster behaviour), briefly passing the test-loop signal. Once it
|
||||||
|
// settles the output must be exactly silent — the tuner leak, if present, persists for the
|
||||||
|
// whole render (it bypasses the mute), so the settled tail catches it.
|
||||||
|
let channels: Int = 2
|
||||||
|
let settleSeconds: Double = 0.15
|
||||||
|
let tailStart: Int = Int(settleSeconds * sampleRate) * channels
|
||||||
|
let tail: [Float] = Array(samples[min(tailStart, samples.count)...])
|
||||||
|
#expect(!tail.isEmpty)
|
||||||
|
#expect(!hasNonSilentOutput(tail),
|
||||||
|
"once the rig mute settles, an engaged tuner must not leak its pitch signal into the output")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user