// 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) } }