// ShoeGaze+Tuner.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Foundation import LyrebirdSynth // MARK: - ShoeGaze + Tuner extension ShoeGaze { // MARK: - Engage / disengage /// Engages the tuner: taps the clean pre-effects input and, if ``tunerMutesRig`` is on, /// mutes the rig output for the duration (restoring the prior mute state on disengage). /// /// A no-op if already engaged. Prints a note and does nothing if no input bus can be /// resolved (mirrors `startRecording()`'s tolerant `.liveInput`/`.testLoop` fallback). public func engageTuner() { guard !tunerEngaged else { return } guard let busIndex: Int = resolvedInputBusIndex() else { print("[ShoeGaze] engageTuner: no input bus available — tuner not engaged.") return } do { // The scratch control bus is allocated once and kept for the session's // lifetime rather than released on every disengage: `engine.remove(_:)` only // enqueues the tap's removal for the next render block, so releasing (and // thus making reallocatable) the bus synchronously here would race a // not-yet-dequeued write from the outgoing tap. let scratchBus: ControlBus if let existing: ControlBus = tunerScratchControlBus { scratchBus = existing } else { scratchBus = try engine.busAllocator.allocateControlBus() tunerScratchControlBus = scratchBus } let tap: TunerTap = TunerTap(inputBusIndex: busIndex, scratchControlBus: scratchBus) try engine.add(tap, position: .after(engine.mainGroup)) tunerTap = tap } catch { print("[ShoeGaze] engageTuner: could not add tuner tap. (\(error))") return } if tunerMutesRig { tunerRestoreMuteOnDisengage = rigMuted setRigMuted(true) } tunerEngaged = true } /// Disengages the tuner and removes its tap. Restores the rig's prior mute state if /// ``engageTuner()`` muted it. /// /// The scratch control bus is intentionally NOT released here — see the comment in /// ``engageTuner()``. public func disengageTuner() { guard tunerEngaged else { return } if let tap: TunerTap = tunerTap { _ = engine.remove(tap) } tunerTap = nil if tunerMutesRig { setRigMuted(tunerRestoreMuteOnDisengage) } tunerEngaged = false } /// Flips ``tunerEngaged`` — convenience for a single stomp-style toggle/MIDI action. public func toggleTunerEngaged() { if tunerEngaged { disengageTuner() } else { engageTuner() } } // MARK: - Live pitch read /// The most recently detected pitch, in Hz, or `nil` while the tuner is not engaged. /// Safe to poll from a SwiftUI `.task` loop on the main thread. public var currentPitchHz: Double? { guard tunerEngaged else { return nil } return tunerTap?.pitchCapture?.detectedFrequencyHz } /// The YIN tracking confidence in `[0, 1]` for the current pitch, or `nil` while the /// tuner is not engaged. `1.0` = certain, `0.0` = no stable pitch. The UI latches its /// display when this drops below its threshold (e.g. during a note attack/restrike). public var currentPitchConfidence: Double? { guard tunerEngaged else { return nil } return tunerTap?.pitchCapture?.detectedConfidence } }