947dfe427a
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>
56 lines
2.2 KiB
Swift
56 lines
2.2 KiB
Swift
// ShoeGaze+Tempo.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Foundation
|
|
|
|
// MARK: - ShoeGaze + Tempo
|
|
|
|
extension ShoeGaze {
|
|
|
|
/// Default tempo, in BPM, for a newly-created ``ShoeGaze/clock``.
|
|
public static let defaultTempoBpm: Double = 120.0
|
|
|
|
// MARK: - Tap tempo
|
|
|
|
/// Minimum plausible interval between taps, in seconds — guards against a double-tap
|
|
/// or debounce glitch registering as an implausibly fast tempo (`60 / minTapInterval`
|
|
/// ≈ 300 BPM cap).
|
|
private static let minTapInterval: TimeInterval = 0.2
|
|
|
|
/// Maximum interval since the last tap before the tap history resets — a pause this
|
|
/// long means the user is starting a new tap-tempo sequence, not continuing one.
|
|
private static let tapTimeoutInterval: TimeInterval = 2.0
|
|
|
|
/// Number of most-recent inter-tap intervals averaged into the computed tempo.
|
|
private static let tapHistoryCount: Int = 4
|
|
|
|
/// Registers one tap-tempo press. Call this each time the user taps (mouse click,
|
|
/// screen tap, or MIDI trigger). Averages the last few inter-tap intervals and sets
|
|
/// ``ShoeGaze/clock``'s `bpm` once at least two taps have landed within
|
|
/// `tapTimeoutInterval` of each other. A pause longer than `tapTimeoutInterval`
|
|
/// discards prior taps and starts a fresh sequence.
|
|
public func tapTempo() {
|
|
let now: Date = Date()
|
|
if let last: Date = tapTimestamps.last, now.timeIntervalSince(last) > ShoeGaze.tapTimeoutInterval {
|
|
tapTimestamps.removeAll()
|
|
}
|
|
tapTimestamps.append(now)
|
|
if tapTimestamps.count > ShoeGaze.tapHistoryCount + 1 {
|
|
tapTimestamps.removeFirst(tapTimestamps.count - (ShoeGaze.tapHistoryCount + 1))
|
|
}
|
|
guard tapTimestamps.count >= 2 else { return }
|
|
|
|
let intervals: [TimeInterval] = zip(tapTimestamps, tapTimestamps.dropFirst())
|
|
.map { $1.timeIntervalSince($0) }
|
|
.filter { $0 >= ShoeGaze.minTapInterval }
|
|
guard !intervals.isEmpty else { return }
|
|
|
|
let averageInterval: TimeInterval = intervals.reduce(0, +) / Double(intervals.count)
|
|
let secondsPerMinute: Double = 60.0
|
|
clock.bpm = secondsPerMinute / averageInterval
|
|
}
|
|
}
|