Files
ShoeGaze/Sources/LyrebirdShoeGaze/Tuner/ShoeGaze+Tuner.swift
T
josh 947dfe427a Add looper pedal: beat-quantized transport, undo, reverse, timeline
Extend the looper into a double-width tempo-aware pedal:

- Beat-quantized transport: record start and loop close both snap to the
  next beat via a LyrebirdRoutine waiting on the shared clock; loop length
  is an exact whole-beat span. Falls back to immediate/unquantized when no
  clock is present.
- Loop-length read-outs (frames/seconds/beats), transport state, and a
  sample-clock-derived play-head position for the UI timeline.
- Overdub undo (pre-dub snapshot + restore), reverse playback (live rate
  flip), and feedback-decay overdub (overdub preLevel via a Feedback knob).
- Inject the shared LyrebirdClock + SynthesisEngine into Pedal via
  PedalBoard/ChainRouter so tempo-aware pedals can reach them.
- New double-width LooperTimelineView (state badge, length read-out,
  sweeping play-head) wired into PedalView via Pedal.widthUnits.

Concurrency hardening from RT-safety review: a transportGeneration counter
drops superseded deferred beat/undo callbacks that LyrebirdRoutine.stop()
cannot recall once enqueued; the undo restore is deferred past the RT
overdub gate-close; transport routines use non-blocking stop().

Note: this commit also snapshots pre-existing uncommitted work-in-progress
already in the tree (MIDI control refactor, Tuner, Tempo, preset changes),
which is interleaved with the looper changes through shared files and could
not be split into a separately-building commit.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 09:22:20 -07:00

100 lines
3.6 KiB
Swift

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