465e3e6dfd
Checkpoint before the multitrack redesign. Adds 4 loop slots (one player swapped via setBuffer), a play-head timeline with A/B/C/D dots, Loop/Stop/ Cue Stop/Reverse stomp toggles below the knobs, beat-quantized transport, overdub undo, reverse, and feedback-decay overdub — all reviewed for RT safety (transportGeneration guards on deferred beat/undo/cue callbacks). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
191 lines
8.1 KiB
Swift
191 lines
8.1 KiB
Swift
// 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
|
|
private static let slotDotSpacing: CGFloat = 4
|
|
private static let slotDotDiameter: CGFloat = 18
|
|
private static let slotLabelFontSize: CGFloat = 10
|
|
private static let slotDotBrightness: Double = 0.16
|
|
private static let slotFilledOpacity: Double = 0.55
|
|
private static let slotEmptyOpacity: Double = 0.45
|
|
private static let slotActiveRingWidth: CGFloat = 1.5
|
|
private static let slotLabels: [String] = ["A", "B", "C", "D", "E", "F", "G", "H"]
|
|
|
|
// 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
|
|
@State private var activeSlot: Int = 0
|
|
@State private var slotContent: [Bool] = Array(repeating: false, count: LooperPedal.slotCount)
|
|
@State private var stopArmed: Bool = false
|
|
|
|
init(pedal: LooperPedal) {
|
|
self.pedal = pedal
|
|
}
|
|
|
|
// MARK: - Body
|
|
|
|
var body: some View {
|
|
VStack(spacing: LooperTimelineView.sectionSpacing) {
|
|
HStack(spacing: LooperTimelineView.sectionSpacing) {
|
|
stateBadge
|
|
Spacer(minLength: 0)
|
|
slotDots
|
|
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
|
|
activeSlot = pedal.activeSlotIndex
|
|
slotContent = (0 ..< LooperPedal.slotCount).map { pedal.slotHasContent($0) }
|
|
stopArmed = pedal.isStopAtLoopEndArmed
|
|
try? await Task.sleep(nanoseconds: LooperTimelineView.pollIntervalNanoseconds)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Subviews
|
|
|
|
/// One labelled dot per slot: filled when it holds a loop, ringed when it is the active slot.
|
|
private var slotDots: some View {
|
|
HStack(spacing: LooperTimelineView.slotDotSpacing) {
|
|
ForEach(0 ..< LooperPedal.slotCount, id: \.self) { index in
|
|
let isActive: Bool = index == activeSlot
|
|
let hasContent: Bool = index < slotContent.count && slotContent[index]
|
|
Text(LooperTimelineView.slotLabels[index])
|
|
.font(.system(size: LooperTimelineView.slotLabelFontSize,
|
|
weight: .bold, design: .monospaced))
|
|
.foregroundStyle(hasContent ? Color.white : Color.white.opacity(LooperTimelineView.slotEmptyOpacity))
|
|
.frame(width: LooperTimelineView.slotDotDiameter, height: LooperTimelineView.slotDotDiameter)
|
|
.background(
|
|
Circle().fill(hasContent ? Color.green.opacity(LooperTimelineView.slotFilledOpacity)
|
|
: Color(white: LooperTimelineView.slotDotBrightness))
|
|
)
|
|
.overlay(
|
|
Circle().stroke(isActive ? Color.white : Color.clear,
|
|
lineWidth: LooperTimelineView.slotActiveRingWidth)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
if stopArmed {
|
|
Text("→END")
|
|
.font(.system(size: LooperTimelineView.labelFontSize, weight: .semibold, design: .monospaced))
|
|
.foregroundStyle(Color.orange)
|
|
}
|
|
}
|
|
}
|
|
|
|
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
|
|
case .stopped: return Color.orange
|
|
}
|
|
}
|
|
|
|
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"
|
|
case .stopped: return "STOP"
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|