Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/LooperTimelineView.swift
T
josh 3007b76798 Looper: multitrack redesign with smart Loop footswitch
Replace the one-at-a-time slot model with a synchronized multitrack looper:
four track players summed to one output, track A's first take defining the
master loop length, and tracks B–D recorded as phase-locked overdub layers
on the same grid (overdub recorders run continuously while playing, reset to
the top at play-start, gated per-track by recLevel so only the selected
track layers new input — instant, drift-free overdub toggles).

A single smart Loop footswitch drives the whole transport by state:
record A → set end (quantized) → auto-overdub on wrap → toggle overdub/play;
B–D toggle overdub aligned to A's play-head. Selecting a track ends the
prior overdub. Stop / Cue Stop pause; Loop resumes. Reset returns to clear.

UI: transport row is now Loop / Stop / Cue Stop / Reverse / Reset; the old
action row is replaced by a one-line state indicator; the A/B/C/D timeline
dots are tappable to select a track; every stomp button shows a live caption
of what its next press does (Record → Set End → Overdub → Play, etc.).

Overdub-undo dropped. All transport routines keep the transportGeneration
guard on deferred beat/stop callbacks.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 14:24:46 -07:00

211 lines
9.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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 badge, tappable A–D track dots, a loop-length
/// read-out, a sweeping play-head track, and a one-line state indicator describing what the
/// pedal is doing. 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.
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 borderLineWidth: CGFloat = 1
private static let dotSpacing: CGFloat = 4
private static let dotDiameter: CGFloat = 18
private static let dotLabelFontSize: CGFloat = 10
private static let dotBrightness: Double = 0.16
private static let dotFilledOpacity: Double = 0.55
private static let dotEmptyOpacity: Double = 0.45
private static let activeRingWidth: CGFloat = 1.5
private static let indicatorFontSize: CGFloat = 11
private static let indicatorOpacity: Double = 0.85
private static let overdubRingColor: Color = Color.red
private static let dotLabels: [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 = .clear
@State private var lengthSeconds: Double = 0.0
@State private var lengthBeats: Double = 0.0
@State private var selectedTrack: Int = 0
@State private var overdubbingTrack: Int?
@State private var trackContent: [Bool] = Array(repeating: false, count: LooperPedal.trackCount)
@State private var stopArmed: Bool = false
@State private var stateDescription: String = ""
init(pedal: LooperPedal) {
self.pedal = pedal
}
// MARK: - Body
var body: some View {
VStack(spacing: LooperTimelineView.sectionSpacing) {
HStack(spacing: LooperTimelineView.sectionSpacing) {
stateBadge
Spacer(minLength: 0)
trackDots
Spacer(minLength: 0)
lengthLabel
}
track
stateIndicator
}
.task {
while !Task.isCancelled {
position = pedal.playbackPosition01()
state = pedal.transportState
lengthSeconds = pedal.loopLengthSeconds
lengthBeats = pedal.loopLengthBeats
selectedTrack = pedal.selectedTrackIndex
overdubbingTrack = pedal.overdubbingTrackIndex
trackContent = (0 ..< LooperPedal.trackCount).map { pedal.trackHasContent($0) }
stopArmed = pedal.isStopAtLoopEndArmed
stateDescription = pedal.stateDescription
try? await Task.sleep(nanoseconds: LooperTimelineView.pollIntervalNanoseconds)
}
}
}
// MARK: - Subviews
/// One tappable labelled dot per track: filled when it holds a loop, white-ringed when
/// selected, red-ringed while overdubbing. Tapping selects the track.
private var trackDots: some View {
HStack(spacing: LooperTimelineView.dotSpacing) {
ForEach(0 ..< LooperPedal.trackCount, id: \.self) { index in
let isSelected: Bool = index == selectedTrack
let isOverdub: Bool = index == overdubbingTrack
let hasContent: Bool = index < trackContent.count && trackContent[index]
Button {
pedal.selectTrack(index)
} label: {
Text(LooperTimelineView.dotLabels[index])
.font(.system(size: LooperTimelineView.dotLabelFontSize, weight: .bold, design: .monospaced))
.foregroundStyle(hasContent ? Color.white : Color.white.opacity(LooperTimelineView.dotEmptyOpacity))
.frame(width: LooperTimelineView.dotDiameter, height: LooperTimelineView.dotDiameter)
.background(
Circle().fill(hasContent ? Color.green.opacity(LooperTimelineView.dotFilledOpacity)
: Color(white: LooperTimelineView.dotBrightness))
)
.overlay(
Circle().stroke(isOverdub ? LooperTimelineView.overdubRingColor
: (isSelected ? Color.white : Color.clear),
lineWidth: LooperTimelineView.activeRingWidth)
)
}
.buttonStyle(.plain)
}
}
}
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))
}
/// One-line description of what the pedal is doing now — replaces the old action-button row.
private var stateIndicator: some View {
Text(stateDescription)
.font(.system(size: LooperTimelineView.indicatorFontSize, weight: .medium))
.foregroundStyle(Color.white.opacity(LooperTimelineView.indicatorOpacity))
.frame(maxWidth: .infinity, alignment: .leading)
}
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)
)
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 .clear: return Color(white: 0.3)
case .armedRecord, .armedClose: return Color.orange
case .recordingMaster: return Color.red
case .playing: return Color.green
case .stopped: return Color.orange
}
}
private var stateText: String {
switch state {
case .clear: return "CLEAR"
case .armedRecord: return "ARM"
case .recordingMaster: return "REC"
case .armedClose: 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")
}
}