Files
ShoeGaze/Sources/LyrebirdShoeGazeUI/TempoView.swift
T
josh e051521c04 Release 2.0.0: metronome, rig mute + shortcuts, set-list ordering, per-rig persistence
- Visual metronome tempo indicator (pendulum playhead) replacing the flashing LED
- Rig Mute (Cmd-Z) hard-silence, distinct from Bypass Rig; wired to header, board menu, MIDI
- Keyboard shortcuts: Cmd-1..9 board switch, Cmd-Shift-1..0 set-list rig load, Cmd-Z mute
  (macOS native menus + iOS/iPadOS hardware keyboards)
- Rig set-list ordering: drag or Move Up/Down in the picker; order saved and archive-portable
- Persist tempo, input selection, and engine I/O with a rig (I/O applied via opt-in
  applyChannelLayout, not forced on load)
- Fix archive import colliding set-list positions with existing rigs
- Remove ShoeGazeDemo / ShoeGazeiOSDemo apps and projects; keep the two Builder apps
- Bump marketing version to 2.0.0 on both Builder projects; pin Lyrebird 1.2.3
- Add CHANGELOG.md

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 12:30:45 -07:00

189 lines
8.2 KiB
Swift
Raw Permalink 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.
// 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
private static let minBpm: Double = 30.0
private static let maxBpm: Double = 300.0
private static let bpmStep: Double = 1.0
private static let rowSpacing: CGFloat = 10
// Metronome track: a dot swings wall-to-wall once per beat, pausing at each wall (the beat)
// so the eye can anticipate the hit. Pendulum easing comes for free from the cosine map.
private static let trackWidth: CGFloat = 150
private static let trackHeight: CGFloat = 16
private static let trackCornerRadius: CGFloat = 4
private static let trackBrightness: Double = 0.14
private static let trackBorderOpacity: Double = 0.35
private static let trackBorderWidth: CGFloat = 1
/// Vertical playhead sweeping horizontally across the track (Looper-style), full track height.
private static let lineWidth: CGFloat = 3
/// Playhead opacity floor at mid-swing; ramps to full at the walls so it fades as it travels
/// toward center and re-brightens into each beat.
private static let lineMinOpacity: Double = 0.25
/// Curve on the wall-proximity → brightness map; > 1 dims harder through the middle.
private static let brightnessFadeCurve: Double = 1.6
/// Soft glow drawn behind the playhead; its opacity peaks at the turnaround (on the beat).
private static let glowWidth: CGFloat = 16
private static let glowMaxOpacity: Double = 0.7
/// End-wall tick marks sit on the beat positions.
private static let wallTickWidth: CGFloat = 2
private static let wallTickOpacity: Double = 0.4
/// 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
/// Continuous beat position, polled from the clock; drives the metronome dot.
@State private var beat: Double = 0.0
// 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)
.fixedSize()
.help("Show the tempo indicator and enable tempo-synced controls")
if shoeGaze.tempoEnabled {
metronome
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 {
beat = shoeGaze.clock.currentBeat
displayedBpm = shoeGaze.clock.bpm
try? await Task.sleep(nanoseconds: TempoView.pollIntervalNanoseconds)
}
}
}
// MARK: - Subviews
/// Horizontal pendulum: the dot maps `beat` through a cosine so it eases to a stop at each
/// wall (an integer beat) and moves fastest at mid-swing — the wall pauses give the eye a
/// runway to anticipate the beat. `x01 = (1 − cos(π·swing)) / 2`, `swing = beat mod 2`, puts
/// the left wall on even beats and the right wall on odd beats (one wall hit per beat).
private var metronome: some View {
// Reduce beat mod 2 before the cosine so precision holds over a long session.
let swing: Double = beat.truncatingRemainder(dividingBy: 2)
let x01: Double = (1 - cos(.pi * swing)) / 2
// Proximity to a wall in [0, 1]: 1 at a wall (on the beat), 0 at mid-swing. The curve dims
// harder through the middle so the playhead visibly fades as it travels toward center.
let wallGlow: Double = pow(1 - 2 * min(x01, 1 - x01), TempoView.brightnessFadeCurve)
return ZStack(alignment: .leading) {
RoundedRectangle(cornerRadius: TempoView.trackCornerRadius)
.fill(Color(white: TempoView.trackBrightness))
.overlay(
RoundedRectangle(cornerRadius: TempoView.trackCornerRadius)
.strokeBorder(
Color.white.opacity(TempoView.trackBorderOpacity),
lineWidth: TempoView.trackBorderWidth
)
)
// Beat-position tick marks flush against each end wall.
.overlay(alignment: .leading) { wallTick }
.overlay(alignment: .trailing) { wallTick }
playhead(x01: x01, wallGlow: wallGlow)
}
.frame(width: TempoView.trackWidth, height: TempoView.trackHeight)
.clipShape(RoundedRectangle(cornerRadius: TempoView.trackCornerRadius))
}
private var wallTick: some View {
Rectangle()
.fill(Color.white.opacity(TempoView.wallTickOpacity))
.frame(width: TempoView.wallTickWidth)
}
private func playhead(x01: Double, wallGlow: Double) -> some View {
// Travel spans the track minus the line's own width so the playhead kisses each wall.
let travel: CGFloat = TempoView.trackWidth - TempoView.lineWidth
let x: CGFloat = CGFloat(x01) * travel
let lineOpacity: Double = TempoView.lineMinOpacity + (1 - TempoView.lineMinOpacity) * wallGlow
// Center glow + line share a leading-aligned box; offset positions the line's leading edge.
return ZStack {
Rectangle()
.fill(Color.red.opacity(TempoView.glowMaxOpacity * wallGlow))
.frame(width: TempoView.glowWidth, height: TempoView.trackHeight)
.blur(radius: TempoView.glowWidth / 3)
Rectangle()
.fill(Color.red.opacity(lineOpacity))
.frame(width: TempoView.lineWidth, height: TempoView.trackHeight)
}
.frame(width: TempoView.lineWidth, height: TempoView.trackHeight, alignment: .center)
.offset(x: x)
}
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)
}
}