Files
ShoeGaze/Sources/LyrebirdShoeGaze/Tuner/TunerTap.swift
T
josh 551b650d45 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>
2026-07-13 19:58:41 -07:00

67 lines
3.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// TunerTap.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
/// An internal synthesizer that taps the clean pre-effects input bus and runs a
/// `TunerPitchCapture` on it, so `ShoeGaze` can poll the current detected pitch from the
/// main thread while the tuner is engaged.
///
/// Mirrors `RecordTap`'s shape: reads one bus via `input(bus:)`, wires an analysis generator,
/// and writes to a dedicated scratch `ControlBus` purely so the engine evaluates it every
/// block — nothing downstream reads that bus.
final class TunerTap: SynthesizerBase {
/// Anti-alias lowpass cutoff, in Hz, ahead of the decimating YIN tracker. Must sit below
/// the decimated Nyquist (`sampleRate / (2 × decimation)`; ≈3 kHz at 48 kHz / 8×) so
/// decimation doesn't fold high harmonics back into the analysis band. `1200` Hz clears
/// every guitar/bass fundamental (highest is E4 ≈330 Hz) and their first harmonics — which
/// help YIN lock — while giving solid anti-alias margin.
private static let lowpassCutoffHz: Double = 1200.0
/// Butterworth Q (maximally flat, no resonant peak) for the anti-alias lowpass.
private static let lowpassQ: Double = 0.707
/// Index of the input bus to analyse.
private let inputBusIndex: Int
/// Scratch control bus the capture generator's passthrough writes to. Unread by anything
/// else — its only purpose is to give the engine a reason to evaluate the generator.
private let scratchControlBus: ControlBus
/// The pitch-capture generator created during `build()`. `nil` until `build()` has run.
private(set) var pitchCapture: TunerPitchCapture?
/// Creates a tuner tap.
///
/// - Parameters:
/// - inputBusIndex: Audio bus to analyse.
/// - scratchControlBus: A control bus reserved for this tap's unread passthrough output.
init(inputBusIndex: Int, scratchControlBus: ControlBus) {
self.inputBusIndex = inputBusIndex
self.scratchControlBus = scratchControlBus
super.init()
}
override func build() {
let source: In.Audio = input(bus: inputBusIndex)
let filtered: RBJLowPass.Audio = RBJLowPass.Audio(
input: source,
freq: TunerTap.lowpassCutoffHz,
q: TunerTap.lowpassQ
)
let capture: TunerPitchCapture = TunerPitchCapture(input: filtered)
pitchCapture = 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)
}
}