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>
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
// LooperTimelineView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
/// The looper's loop timeline: a transport-state label, a loop-length read-out, and a track
|
||||
/// with a sweeping play-head. Rendered inside the double-width ``PedalView`` for a `LooperPedal`.
|
||||
///
|
||||
/// `LooperPedal`'s transport state is plain (non-`@Observable`) RT-adjacent data, so this view
|
||||
/// polls it on a `.task` loop — the same convention as `TempoView`'s clock read-out — rather
|
||||
/// than relying on SwiftUI change tracking.
|
||||
struct LooperTimelineView: View {
|
||||
|
||||
// MARK: - Constants
|
||||
|
||||
private static let trackHeight: CGFloat = 16
|
||||
private static let trackCornerRadius: CGFloat = 4
|
||||
private static let playheadWidth: CGFloat = 2
|
||||
private static let sectionSpacing: CGFloat = 6
|
||||
private static let labelFontSize: CGFloat = 11
|
||||
private static let trackBrightness: Double = 0.14
|
||||
private static let trackBorderOpacity: Double = 0.35
|
||||
private static let filledOpacity: Double = 0.22
|
||||
private static let pollIntervalNanoseconds: UInt64 = 33_000_000 // ~30 Hz
|
||||
private static let ledDiameter: CGFloat = 9
|
||||
private static let badgeInnerSpacing: CGFloat = 4
|
||||
private static let lengthLabelOpacity: Double = 0.75
|
||||
private static let emptyLedBrightness: Double = 0.3
|
||||
private static let borderLineWidth: CGFloat = 1
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
private let pedal: LooperPedal
|
||||
|
||||
@State private var position: Double?
|
||||
@State private var state: LooperTransportState = .empty
|
||||
@State private var lengthSeconds: Double = 0.0
|
||||
@State private var lengthBeats: Double = 0.0
|
||||
|
||||
init(pedal: LooperPedal) {
|
||||
self.pedal = pedal
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: LooperTimelineView.sectionSpacing) {
|
||||
HStack(spacing: LooperTimelineView.sectionSpacing) {
|
||||
stateBadge
|
||||
Spacer(minLength: 0)
|
||||
lengthLabel
|
||||
}
|
||||
track
|
||||
}
|
||||
.task {
|
||||
// Poll the pedal's transport read-outs; it isn't @Observable (RT-adjacent state).
|
||||
while !Task.isCancelled {
|
||||
position = pedal.playbackPosition01()
|
||||
state = pedal.transportState
|
||||
lengthSeconds = pedal.loopLengthSeconds
|
||||
lengthBeats = pedal.loopLengthBeats
|
||||
try? await Task.sleep(nanoseconds: LooperTimelineView.pollIntervalNanoseconds)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Subviews
|
||||
|
||||
private var stateBadge: some View {
|
||||
HStack(spacing: LooperTimelineView.badgeInnerSpacing) {
|
||||
Circle()
|
||||
.fill(stateColor)
|
||||
.frame(width: LooperTimelineView.ledDiameter, height: LooperTimelineView.ledDiameter)
|
||||
Text(stateText)
|
||||
.font(.system(size: LooperTimelineView.labelFontSize, weight: .semibold, design: .monospaced))
|
||||
.foregroundStyle(Color.white)
|
||||
}
|
||||
}
|
||||
|
||||
private var lengthLabel: some View {
|
||||
Text(lengthText)
|
||||
.font(.system(size: LooperTimelineView.labelFontSize, design: .monospaced))
|
||||
.foregroundStyle(Color.white.opacity(LooperTimelineView.lengthLabelOpacity))
|
||||
}
|
||||
|
||||
private var track: some View {
|
||||
GeometryReader { geometry in
|
||||
let width: CGFloat = geometry.size.width
|
||||
let pos: CGFloat = CGFloat(position ?? 0.0)
|
||||
ZStack(alignment: .leading) {
|
||||
RoundedRectangle(cornerRadius: LooperTimelineView.trackCornerRadius)
|
||||
.fill(Color(white: LooperTimelineView.trackBrightness))
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: LooperTimelineView.trackCornerRadius)
|
||||
.stroke(Color.white.opacity(LooperTimelineView.trackBorderOpacity),
|
||||
lineWidth: LooperTimelineView.borderLineWidth)
|
||||
)
|
||||
// Filled region up to the play-head, only while playing.
|
||||
if position != nil {
|
||||
RoundedRectangle(cornerRadius: LooperTimelineView.trackCornerRadius)
|
||||
.fill(Color.white.opacity(LooperTimelineView.filledOpacity))
|
||||
.frame(width: max(0, pos * width))
|
||||
Rectangle()
|
||||
.fill(Color.white)
|
||||
.frame(width: LooperTimelineView.playheadWidth)
|
||||
.offset(x: min(max(0, pos * width), width - LooperTimelineView.playheadWidth))
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(height: LooperTimelineView.trackHeight)
|
||||
}
|
||||
|
||||
// MARK: - State mapping
|
||||
|
||||
private var stateColor: Color {
|
||||
switch state {
|
||||
case .empty: return Color(white: LooperTimelineView.emptyLedBrightness)
|
||||
case .armedRecord, .armedStop: return Color.orange
|
||||
case .recording: return Color.red
|
||||
case .playing: return Color.green
|
||||
}
|
||||
}
|
||||
|
||||
private var stateText: String {
|
||||
switch state {
|
||||
case .empty: return "EMPTY"
|
||||
case .armedRecord: return "ARM"
|
||||
case .recording: return "REC"
|
||||
case .armedStop: return "END"
|
||||
case .playing: return "PLAY"
|
||||
}
|
||||
}
|
||||
|
||||
private var lengthText: String {
|
||||
guard lengthSeconds > 0 else { return "--" }
|
||||
let seconds: String = String(format: "%.2fs", lengthSeconds)
|
||||
guard lengthBeats > 0 else { return seconds }
|
||||
return seconds + " · \(Int(lengthBeats)) beat" + (lengthBeats == 1 ? "" : "s")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user