Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/TempoView.swift
T
josh 5090a9255f Looper reverse fix + header layout compaction + tempo toggle
Looper:
- Reverse now continues from the current play-head instead of jumping to
  duration-minus-position; anchor-based position tracking survives direction
  changes. Sync mode defers the flip to the next beat; Free applies immediately.
- Sync switch relabeled "Sync to Tempo".

Tempo/Tuner:
- Add a Tempo on/off toggle (ShoeGaze.setTempoEnabled) that hides the indicator
  and forces every tempo-dependent pedal switch (PedalSwitch.tempoDependent) off;
  PedalSwitchView disables such switches via the shoeGazeTempoEnabled environment.
- Tuner readout laid out inline; Tempo/Tuner pinned to fixed heights so toggling
  state no longer shifts surrounding layout.

Header:
- Collapsed to two rows: title + Tempo/Tuner (left, stacked) and engine options
  over input controls (right, trailing-aligned). TAP is a standard-height button.
- Removed the redundant Boards button; moved Clear into the board bar.
- Fixed the rig mute glyph size so toggling mute doesn't shift the volume row.

Help: added a Looper section documenting transport, Free/Sync, knobs, tracks,
and MIDI-assignable actions.

Tests: TempoEnableTests + looper reverse/deferral coverage.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 12:31:11 -07:00

121 lines
4.5 KiB
Swift

// TempoView.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import SwiftUI
import LyrebirdSynth
import LyrebirdShoeGaze
/// Tempo indicator: a BPM readout/stepper, a beat-synced flashing LED, and a tap area that
/// responds to mouse/touch taps (call ``ShoeGaze/tapTempo()`` directly for a MIDI trigger).
///
/// Polls ``ShoeGaze/clock``'s `currentBeat` on a `.task` loop (matching the scope/CPU-meter
/// polling convention) since `LyrebirdClock` isn't `@Observable` — it's sample-accurate RT
/// state, not push-notified UI state.
public struct TempoView: View {
// MARK: - Constants
private static let pollIntervalNanoseconds: UInt64 = 33_333_333 // ~30 Hz
/// Fraction of each beat the LED stays lit — a short flash, not a 50% duty-cycle blink.
private static let flashDutyCycle: Double = 0.15
private static let minBpm: Double = 30.0
private static let maxBpm: Double = 300.0
private static let bpmStep: Double = 1.0
private static let ledDiameter: CGFloat = 14
private static let rowSpacing: CGFloat = 10
/// Shared width for the Tempo / Tuner engage toggles so they left-align in the stacked column.
static let toggleWidth: CGFloat = 96
/// Fixed row height = the enabled (BPM/TAP-visible) height, so toggling doesn't shift layout.
static let enabledHeight: CGFloat = 34
// MARK: - Properties
private let shoeGaze: ShoeGaze
@State private var displayedBpm: Double
@State private var ledOn: Bool = false
// MARK: - Init
/// - Parameter shoeGaze: The session whose ``ShoeGaze/clock`` this view displays and drives.
public init(shoeGaze: ShoeGaze) {
self.shoeGaze = shoeGaze
self._displayedBpm = State(initialValue: shoeGaze.clock.bpm)
}
// MARK: - Body
public var body: some View {
HStack(spacing: TempoView.rowSpacing) {
Button(shoeGaze.tempoEnabled ? "Tempo: On" : "Tempo: Off") {
shoeGaze.setTempoEnabled(!shoeGaze.tempoEnabled)
}
.buttonStyle(.bordered)
.tint(shoeGaze.tempoEnabled ? .green : .gray)
.frame(width: TempoView.toggleWidth, alignment: .leading)
.help("Show the tempo indicator and enable tempo-synced controls")
if shoeGaze.tempoEnabled {
led
Text("\(Int(displayedBpm.rounded())) BPM")
.font(.system(size: 18, weight: .semibold, design: .rounded))
.foregroundStyle(Color.white)
Stepper(
"",
value: Binding(
get: { displayedBpm },
set: { newValue in
let clamped: Double = min(max(newValue, TempoView.minBpm), TempoView.maxBpm)
shoeGaze.clock.bpm = clamped
displayedBpm = clamped
}
),
in: TempoView.minBpm ... TempoView.maxBpm,
step: TempoView.bpmStep
)
.labelsHidden()
tapArea
}
}
// Pin to the enabled height (BPM / stepper / TAP visible) so toggling tempo on/off never
// shifts the surrounding layout.
.frame(height: TempoView.enabledHeight)
.task(id: shoeGaze.tempoEnabled) {
while shoeGaze.tempoEnabled && !Task.isCancelled {
let beat: Double = shoeGaze.clock.currentBeat
let phase: Double = beat - beat.rounded(.down)
ledOn = phase < TempoView.flashDutyCycle
displayedBpm = shoeGaze.clock.bpm
try? await Task.sleep(nanoseconds: TempoView.pollIntervalNanoseconds)
}
}
}
// MARK: - Subviews
private var led: some View {
Circle()
.fill(ledOn ? Color.red : Color.red.opacity(0.15))
.frame(width: TempoView.ledDiameter, height: TempoView.ledDiameter)
}
private var tapArea: some View {
// A standard-height bordered button (not a tall circle) so the tempo row matches the
// height of the title and the engine-option buttons and top-aligns cleanly with them.
Button {
shoeGaze.tapTempo()
displayedBpm = shoeGaze.clock.bpm
} label: {
Text("TAP")
.font(.system(size: 13, weight: .bold, design: .rounded))
}
.buttonStyle(.bordered)
.tint(.gray)
.foregroundStyle(Color.white)
}
}