Release 2.1.0: tempo-synced pedal parameters, editable BPM, control mapping badges
Tempo sync - Lock a delay time or modulation LFO rate to a note division (whole..sixteenth, dotted, triplet) of the shared clock. Value is derived from the tempo and follows tap tempo, the stepper, the editable BPM field, and MIDI. - Delay time: Delay, FeedbackDelay, PingPong, ReverseDelay, MultiTap (per tap). LFO rate (inverse mapping): Tremolo, Vibrato, Chorus, Flanger, Phaser, Modulation. - Core types NoteDivision / TempoSync / TempoSyncableParam; Pedal.tempoSyncableParams / tempoSync / applyTempoSync / setSync; ShoeGaze.setBpm broadcast recomputes all pedals; tempoSync persisted in PedalState (lenient decode) and recomputed on load + recall. - Observable TempoSyncRevision so the sync badge refreshes on any state-change path. UI - Division picker with drawn music-notation glyphs (NoteGlyphView + rasterized cache): noteheads, stems, flags, augmentation dot, bracketed 3 for triplets. Presented as a height-capped popover so it stays on-screen on iOS. - Editable BPM text field: integer-only, live-filtered, clamped 30-300, commit on return / Done / blur. - Divisions that clamp at the current tempo are disabled in the picker; an active clamped sync shows a warning marker. - In-context control mapping badges (PC / CC / KEY) per pedal control, with a legend. Delays - Maximum delay time raised to 5 s across the delay pedals (was 1-2 s). Docs: CHANGELOG.md, RELEASES.md, docs/plans/2.2-tempo-synced-parameters.md. All 454 tests pass; passed lyrebird_external_review (RT-safe, main-thread param writes). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
// ControlMappingStrip.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
/// The rig's current control map, injected into the board view tree so each ``PedalView`` can draw
|
||||
/// its own mapping strip without every call site having to thread the map through. `nil` when the
|
||||
/// rig has no bindings. Set once high in the builder (`builderColumn`) from the observable
|
||||
/// `ShoeGaze.midiMap`, so learning or clearing a binding updates every strip live.
|
||||
private struct ControlMappingMapKey: EnvironmentKey {
|
||||
static let defaultValue: MIDIMap? = nil
|
||||
}
|
||||
|
||||
extension EnvironmentValues {
|
||||
/// The rig's control map for in-context mapping badges, or `nil` when unset / empty.
|
||||
var controlMappingMap: MIDIMap? {
|
||||
get { self[ControlMappingMapKey.self] }
|
||||
set { self[ControlMappingMapKey.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
/// The kind of trigger a mapping badge represents, and its colour.
|
||||
enum MappingBadgeKind {
|
||||
case programChange
|
||||
case controlChange
|
||||
case key
|
||||
|
||||
/// The badge fill colour (matches the mapping-sheet chip colours).
|
||||
var fill: Color {
|
||||
switch self {
|
||||
case .programChange: return MappingBadge.pcFill
|
||||
case .controlChange: return MappingBadge.ccFill
|
||||
case .key: return MappingBadge.keyFill
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One coloured pill — e.g. `CC11`, `PC3`, `⇧B` — marking a control's binding.
|
||||
struct MappingBadge: View {
|
||||
|
||||
static let pcFill: Color = Color(red: 0.11, green: 0.37, blue: 0.65)
|
||||
static let ccFill: Color = Color(red: 0.33, green: 0.29, blue: 0.72)
|
||||
static let keyFill: Color = Color(red: 0.73, green: 0.46, blue: 0.09)
|
||||
|
||||
private static let fontSize: CGFloat = 9
|
||||
private static let paddingH: CGFloat = 5
|
||||
private static let paddingV: CGFloat = 1
|
||||
private static let cornerRadius: CGFloat = 4
|
||||
|
||||
private let kind: MappingBadgeKind
|
||||
private let text: String
|
||||
|
||||
/// Creates a mapping badge.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - kind: The trigger kind (drives the fill colour).
|
||||
/// - text: The badge label (e.g. `"CC11"`, `"PC3"`, `"⇧B"`).
|
||||
init(kind: MappingBadgeKind, text: String) {
|
||||
self.kind = kind
|
||||
self.text = text
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Text(text)
|
||||
.font(.system(size: MappingBadge.fontSize, weight: .medium))
|
||||
.foregroundStyle(Color.white)
|
||||
.padding(.horizontal, MappingBadge.paddingH)
|
||||
.padding(.vertical, MappingBadge.paddingV)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: MappingBadge.cornerRadius)
|
||||
.fill(kind.fill)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A one-line legend keying the badge colours (`PC` footswitch, `CC` expression, `KEY` letter key),
|
||||
/// shown once above the board canvas.
|
||||
struct ControlMappingLegend: View {
|
||||
|
||||
private static let itemSpacing: CGFloat = 10
|
||||
private static let pairSpacing: CGFloat = 5
|
||||
private static let fontSize: CGFloat = 11
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: ControlMappingLegend.itemSpacing) {
|
||||
legendItem(kind: .programChange, badge: "PC", caption: "footswitch")
|
||||
legendItem(kind: .controlChange, badge: "CC", caption: "expression")
|
||||
legendItem(kind: .key, badge: "KEY", caption: "letter key")
|
||||
}
|
||||
.foregroundStyle(Color.white.opacity(0.55))
|
||||
}
|
||||
|
||||
private func legendItem(kind: MappingBadgeKind, badge: String, caption: String) -> some View {
|
||||
HStack(spacing: ControlMappingLegend.pairSpacing) {
|
||||
MappingBadge(kind: kind, text: badge)
|
||||
Text(caption)
|
||||
.font(.system(size: ControlMappingLegend.fontSize))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One row of a pedal's mapping strip: a control label and the badges bound to it.
|
||||
struct PedalMappingRow: Identifiable {
|
||||
let id: String
|
||||
let label: String
|
||||
let badges: [PedalMappingBadge]
|
||||
}
|
||||
|
||||
/// A single badge within a ``PedalMappingRow``.
|
||||
struct PedalMappingBadge: Identifiable {
|
||||
let id: String
|
||||
let kind: MappingBadgeKind
|
||||
let text: String
|
||||
}
|
||||
|
||||
/// The per-pedal mapping strip: a compact footer listing this pedal's mapped controls and the
|
||||
/// footswitch / expression / key bindings driving them. Renders nothing when the pedal has no
|
||||
/// mappings, so unmapped pedals gain no height.
|
||||
struct PedalMappingStrip: View {
|
||||
|
||||
private static let rowSpacing: CGFloat = 3
|
||||
private static let labelFontSize: CGFloat = 10
|
||||
private static let badgeSpacing: CGFloat = 4
|
||||
private static let topPadding: CGFloat = 6
|
||||
private static let dividerOpacity: Double = 0.18
|
||||
|
||||
private let rows: [PedalMappingRow]
|
||||
|
||||
/// Creates a mapping strip for a pedal against the current map.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - pedal: The pedal whose bindings are listed.
|
||||
/// - map: The rig's control map, or `nil` when there are no bindings.
|
||||
init(pedal: Pedal, map: MIDIMap?) {
|
||||
self.rows = PedalMappingStrip.rows(for: pedal, in: map)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
if rows.isEmpty {
|
||||
EmptyView()
|
||||
} else {
|
||||
VStack(spacing: PedalMappingStrip.rowSpacing) {
|
||||
Divider().overlay(Color.white.opacity(PedalMappingStrip.dividerOpacity))
|
||||
ForEach(rows) { row in
|
||||
HStack(spacing: PedalMappingStrip.badgeSpacing) {
|
||||
Text(row.label)
|
||||
.font(.system(size: PedalMappingStrip.labelFontSize))
|
||||
.foregroundStyle(Color.white.opacity(0.6))
|
||||
Spacer(minLength: 0)
|
||||
ForEach(row.badges) { badge in
|
||||
MappingBadge(kind: badge.kind, text: badge.text)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.top, PedalMappingStrip.topPadding)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Row assembly
|
||||
|
||||
/// Builds the strip rows for a pedal from the map: one row per mapped CC control, one for a
|
||||
/// mapped bypass (footswitch + key), and one per mapped looper transport action.
|
||||
static func rows(for pedal: Pedal, in map: MIDIMap?) -> [PedalMappingRow] {
|
||||
guard let map: MIDIMap = map else { return [] }
|
||||
let pedalID: UUID = pedal.id
|
||||
var rows: [PedalMappingRow] = []
|
||||
|
||||
// Continuous controls driven by an expression pedal (CC).
|
||||
for controlPair: (offset: Int, element: PedalControl) in pedal.ccMappableControls.enumerated() {
|
||||
let target: MIDIMap.CCTarget = .pedalControl(
|
||||
pedalID: pedalID, controlIndex: controlPair.offset,
|
||||
pedalNameHint: nil, controlLabelHint: nil)
|
||||
guard let cc: MIDIMap.CCBinding = map.ccs.first(where: { $0.target.addresses(target) })
|
||||
else { continue }
|
||||
rows.append(PedalMappingRow(
|
||||
id: "cc-\(controlPair.offset)",
|
||||
label: controlPair.element.label,
|
||||
badges: [PedalMappingBadge(id: "cc", kind: .controlChange, text: "CC\(cc.controller)")]))
|
||||
}
|
||||
|
||||
// Bypass — a footswitch and/or a key.
|
||||
let bypassBadges: [PedalMappingBadge] = badges(for: .pedalBypass(pedalID: pedalID), in: map)
|
||||
if bypassBadges.isEmpty == false {
|
||||
rows.append(PedalMappingRow(id: "bypass", label: "Bypass", badges: bypassBadges))
|
||||
}
|
||||
|
||||
// Looper transport actions.
|
||||
if pedal is LooperPedal {
|
||||
for kind: MIDIMap.PedalActionKind in loopedActionOrder {
|
||||
let action: MIDIMap.Action = .pedalAction(pedalID: pedalID, kind: kind)
|
||||
let actionBadges: [PedalMappingBadge] = badges(for: action, in: map)
|
||||
if actionBadges.isEmpty { continue }
|
||||
rows.append(PedalMappingRow(
|
||||
id: "la-\(looperActionName(kind))",
|
||||
label: looperActionName(kind),
|
||||
badges: actionBadges))
|
||||
}
|
||||
}
|
||||
|
||||
return rows
|
||||
}
|
||||
|
||||
/// The footswitch (PC) and key badges bound to a toggle action, in that order.
|
||||
private static func badges(for action: MIDIMap.Action, in map: MIDIMap) -> [PedalMappingBadge] {
|
||||
var badges: [PedalMappingBadge] = []
|
||||
if let pc: MIDIMap.SwitchBinding = map.switches.first(where: { $0.action == action }) {
|
||||
badges.append(PedalMappingBadge(id: "pc", kind: .programChange, text: "PC\(pc.program)"))
|
||||
}
|
||||
if let key: MIDIMap.KeyBinding = map.keys.first(where: { $0.action == action }) {
|
||||
badges.append(PedalMappingBadge(id: "key", kind: .key, text: keyLabel(key)))
|
||||
}
|
||||
return badges
|
||||
}
|
||||
|
||||
/// A key binding formatted with modifier glyphs (macOS order: control, option, shift).
|
||||
private static func keyLabel(_ binding: MIDIMap.KeyBinding) -> String {
|
||||
var prefix: String = ""
|
||||
if binding.modifiers.contains(.control) { prefix += "⌃" }
|
||||
if binding.modifiers.contains(.option) { prefix += "⌥" }
|
||||
if binding.modifiers.contains(.shift) { prefix += "⇧" }
|
||||
return prefix + binding.key.uppercased()
|
||||
}
|
||||
|
||||
/// The looper transport actions in the order they appear in the mapping sheet.
|
||||
private static let loopedActionOrder: [MIDIMap.PedalActionKind] = [
|
||||
.looperRecordStop, .looperOverdubToggle, .looperLoopToggle, .looperStop,
|
||||
.looperCueStop, .looperReverse, .looperSyncToggle, .looperReset,
|
||||
.looperTrack(index: 0), .looperTrack(index: 1),
|
||||
.looperTrack(index: 2), .looperTrack(index: 3),
|
||||
]
|
||||
|
||||
/// A short user-facing name for a looper transport action.
|
||||
private static func looperActionName(_ kind: MIDIMap.PedalActionKind) -> String {
|
||||
switch kind {
|
||||
case .looperRecordStop: return "Record"
|
||||
case .looperOverdubToggle: return "Overdub"
|
||||
case .looperLoopToggle: return "Loop"
|
||||
case .looperStop: return "Stop"
|
||||
case .looperCueStop: return "Cue Stop"
|
||||
case .looperReverse: return "Reverse"
|
||||
case .looperSyncToggle: return "Sync"
|
||||
case .looperReset: return "Reset"
|
||||
case .looperTrack(let index): return "Track \(LooperPedal.trackLabel(index))"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
// NoteGlyphView.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import SwiftUI
|
||||
import LyrebirdShoeGaze
|
||||
|
||||
// MARK: - Notation spec
|
||||
|
||||
/// The notation elements that make up one ``NoteDivision`` glyph. Derived purely from the division;
|
||||
/// consumed by ``NoteGlyphView`` to draw the note.
|
||||
private struct NoteGlyphSpec {
|
||||
|
||||
/// `true` for a solid notehead (quarter and shorter), `false` for an open ring (whole, half).
|
||||
let filledHead: Bool
|
||||
/// `true` when the note has a vertical stem (everything except the whole note).
|
||||
let hasStem: Bool
|
||||
/// Number of flags on the stem (`0` = quarter/half, `1` = eighth, `2` = sixteenth).
|
||||
let flags: Int
|
||||
/// `true` for a dotted value — adds an augmentation dot right of the head.
|
||||
let dotted: Bool
|
||||
/// `true` for a triplet — adds a bracket with a "3" above the note.
|
||||
let triplet: Bool
|
||||
|
||||
init(_ division: NoteDivision) {
|
||||
switch division {
|
||||
case .whole:
|
||||
self.init(filledHead: false, hasStem: false, flags: 0, dotted: false, triplet: false)
|
||||
case .dottedHalf:
|
||||
self.init(filledHead: false, hasStem: true, flags: 0, dotted: true, triplet: false)
|
||||
case .half:
|
||||
self.init(filledHead: false, hasStem: true, flags: 0, dotted: false, triplet: false)
|
||||
case .dottedQuarter:
|
||||
self.init(filledHead: true, hasStem: true, flags: 0, dotted: true, triplet: false)
|
||||
case .quarter:
|
||||
self.init(filledHead: true, hasStem: true, flags: 0, dotted: false, triplet: false)
|
||||
case .dottedEighth:
|
||||
self.init(filledHead: true, hasStem: true, flags: 1, dotted: true, triplet: false)
|
||||
case .quarterTriplet:
|
||||
self.init(filledHead: true, hasStem: true, flags: 0, dotted: false, triplet: true)
|
||||
case .eighth:
|
||||
self.init(filledHead: true, hasStem: true, flags: 1, dotted: false, triplet: false)
|
||||
case .eighthTriplet:
|
||||
self.init(filledHead: true, hasStem: true, flags: 1, dotted: false, triplet: true)
|
||||
case .sixteenth:
|
||||
self.init(filledHead: true, hasStem: true, flags: 2, dotted: false, triplet: false)
|
||||
case .sixteenthTriplet:
|
||||
self.init(filledHead: true, hasStem: true, flags: 2, dotted: false, triplet: true)
|
||||
}
|
||||
}
|
||||
|
||||
private init(filledHead: Bool, hasStem: Bool, flags: Int, dotted: Bool, triplet: Bool) {
|
||||
self.filledHead = filledHead
|
||||
self.hasStem = hasStem
|
||||
self.flags = flags
|
||||
self.dotted = dotted
|
||||
self.triplet = triplet
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - NoteGlyphView
|
||||
|
||||
/// Draws a single music-notation glyph for a ``NoteDivision`` — notehead, stem, flags, an
|
||||
/// augmentation dot for dotted values, and a bracketed "3" for triplets — with `Canvas`.
|
||||
///
|
||||
/// Drawn in solid black so the rasterized image can be used as a **template** in a menu (only the
|
||||
/// alpha channel matters; the system tints it to the menu's foreground colour). See
|
||||
/// ``NoteGlyphImageCache``.
|
||||
struct NoteGlyphView: View {
|
||||
|
||||
// Logical drawing box. The note sits in the lower portion; the triplet bracket uses the top,
|
||||
// which is left clear on every glyph so the note baseline stays consistent across the menu.
|
||||
private static let boxWidth: CGFloat = 18
|
||||
private static let boxHeight: CGFloat = 26
|
||||
|
||||
// Notehead.
|
||||
private static let headCenterX: CGFloat = 6.5
|
||||
private static let headCenterY: CGFloat = 19.0
|
||||
private static let headRadiusX: CGFloat = 4.3
|
||||
private static let headRadiusY: CGFloat = 3.0
|
||||
private static let wholeHeadRadiusX: CGFloat = 5.0
|
||||
private static let openHeadLineWidth: CGFloat = 1.5
|
||||
|
||||
// Stem.
|
||||
private static let stemLineWidth: CGFloat = 1.3
|
||||
private static let stemTopY: CGFloat = 8.0
|
||||
|
||||
// Flags.
|
||||
private static let flagLineWidth: CGFloat = 1.5
|
||||
private static let flagSpread: CGFloat = 5.5
|
||||
private static let flagDrop: CGFloat = 6.0
|
||||
private static let flagGap: CGFloat = 3.5
|
||||
|
||||
// Augmentation dot — pushed clear of the notehead's right edge.
|
||||
private static let dotRadius: CGFloat = 1.5
|
||||
private static let dotGap: CGFloat = 3.8
|
||||
|
||||
// Triplet bracket + numeral. The "3" is centred in the bracket gap; `bracketTopY` leaves room
|
||||
// above it so the numeral is never clipped by the raster bounds.
|
||||
private static let bracketTopY: CGFloat = 4.0
|
||||
private static let bracketTickHeight: CGFloat = 2.0
|
||||
private static let bracketInsetX: CGFloat = 1.5
|
||||
private static let bracketLineWidth: CGFloat = 1.0
|
||||
private static let bracketNumberGap: CGFloat = 3.0
|
||||
private static let bracketNumberSize: CGFloat = 6.0
|
||||
|
||||
let division: NoteDivision
|
||||
|
||||
var body: some View {
|
||||
Canvas { context, size in
|
||||
let scaleX: CGFloat = size.width / NoteGlyphView.boxWidth
|
||||
let scaleY: CGFloat = size.height / NoteGlyphView.boxHeight
|
||||
let s: CGFloat = min(scaleX, scaleY)
|
||||
context.scaleBy(x: s, y: s)
|
||||
draw(NoteGlyphSpec(division), in: &context)
|
||||
}
|
||||
.frame(width: NoteGlyphView.boxWidth, height: NoteGlyphView.boxHeight)
|
||||
}
|
||||
|
||||
private func draw(_ spec: NoteGlyphSpec, in context: inout GraphicsContext) {
|
||||
let ink: GraphicsContext.Shading = .color(.black)
|
||||
|
||||
// Stem — draw before the head so the head overlaps its lower end cleanly.
|
||||
let stemX: CGFloat = NoteGlyphView.headCenterX + NoteGlyphView.headRadiusX - 0.3
|
||||
if spec.hasStem {
|
||||
var stem: Path = Path()
|
||||
stem.move(to: CGPoint(x: stemX, y: NoteGlyphView.headCenterY))
|
||||
stem.addLine(to: CGPoint(x: stemX, y: NoteGlyphView.stemTopY))
|
||||
context.stroke(stem, with: ink, lineWidth: NoteGlyphView.stemLineWidth)
|
||||
}
|
||||
|
||||
// Flags — hooks off the top of the stem, one per flag, stacked downward.
|
||||
for flagIndex: Int in 0 ..< spec.flags {
|
||||
let startY: CGFloat = NoteGlyphView.stemTopY + CGFloat(flagIndex) * NoteGlyphView.flagGap
|
||||
var flag: Path = Path()
|
||||
flag.move(to: CGPoint(x: stemX, y: startY))
|
||||
flag.addQuadCurve(
|
||||
to: CGPoint(x: stemX + NoteGlyphView.flagSpread, y: startY + NoteGlyphView.flagDrop),
|
||||
control: CGPoint(x: stemX + NoteGlyphView.flagSpread, y: startY)
|
||||
)
|
||||
context.stroke(flag, with: ink, lineWidth: NoteGlyphView.flagLineWidth)
|
||||
}
|
||||
|
||||
// Notehead.
|
||||
let headRadiusX: CGFloat = spec.hasStem ? NoteGlyphView.headRadiusX : NoteGlyphView.wholeHeadRadiusX
|
||||
let headRect: CGRect = CGRect(
|
||||
x: NoteGlyphView.headCenterX - headRadiusX,
|
||||
y: NoteGlyphView.headCenterY - NoteGlyphView.headRadiusY,
|
||||
width: headRadiusX * 2,
|
||||
height: NoteGlyphView.headRadiusY * 2
|
||||
)
|
||||
let headPath: Path = Path(ellipseIn: headRect)
|
||||
if spec.filledHead {
|
||||
context.fill(headPath, with: ink)
|
||||
} else {
|
||||
context.stroke(headPath, with: ink, lineWidth: NoteGlyphView.openHeadLineWidth)
|
||||
}
|
||||
|
||||
// Augmentation dot, right of the head.
|
||||
if spec.dotted {
|
||||
let dotX: CGFloat = NoteGlyphView.headCenterX + headRadiusX + NoteGlyphView.dotGap
|
||||
let dotRect: CGRect = CGRect(
|
||||
x: dotX - NoteGlyphView.dotRadius,
|
||||
y: NoteGlyphView.headCenterY - NoteGlyphView.dotRadius,
|
||||
width: NoteGlyphView.dotRadius * 2,
|
||||
height: NoteGlyphView.dotRadius * 2
|
||||
)
|
||||
context.fill(Path(ellipseIn: dotRect), with: ink)
|
||||
}
|
||||
|
||||
// Triplet bracket with a centred "3".
|
||||
if spec.triplet {
|
||||
drawTripletBracket(in: &context, ink: ink)
|
||||
}
|
||||
}
|
||||
|
||||
private func drawTripletBracket(in context: inout GraphicsContext, ink: GraphicsContext.Shading) {
|
||||
let leftX: CGFloat = NoteGlyphView.bracketInsetX
|
||||
let rightX: CGFloat = NoteGlyphView.boxWidth - NoteGlyphView.bracketInsetX
|
||||
let midX: CGFloat = (leftX + rightX) / 2
|
||||
let topY: CGFloat = NoteGlyphView.bracketTopY
|
||||
let tickBottomY: CGFloat = topY + NoteGlyphView.bracketTickHeight
|
||||
let gap: CGFloat = NoteGlyphView.bracketNumberGap
|
||||
|
||||
var bracket: Path = Path()
|
||||
// Left tick down, then horizontal to the number gap.
|
||||
bracket.move(to: CGPoint(x: leftX, y: tickBottomY))
|
||||
bracket.addLine(to: CGPoint(x: leftX, y: topY))
|
||||
bracket.addLine(to: CGPoint(x: midX - gap, y: topY))
|
||||
// Right side: horizontal from the gap out to the right tick, then down.
|
||||
bracket.move(to: CGPoint(x: midX + gap, y: topY))
|
||||
bracket.addLine(to: CGPoint(x: rightX, y: topY))
|
||||
bracket.addLine(to: CGPoint(x: rightX, y: tickBottomY))
|
||||
context.stroke(bracket, with: ink, lineWidth: NoteGlyphView.bracketLineWidth)
|
||||
|
||||
let numeral: GraphicsContext.ResolvedText = context.resolve(
|
||||
Text("3")
|
||||
.font(.system(size: NoteGlyphView.bracketNumberSize, weight: .semibold, design: .rounded))
|
||||
.foregroundColor(.black)
|
||||
)
|
||||
context.draw(numeral, at: CGPoint(x: midX, y: topY), anchor: .center)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Rasterization cache
|
||||
|
||||
/// Renders each ``NoteDivision`` glyph to a template `Image` once and caches it, so the tempo-sync
|
||||
/// menu can show notation icons without re-rasterizing on every render. Template images tint to the
|
||||
/// menu's foreground colour automatically.
|
||||
@MainActor
|
||||
enum NoteGlyphImageCache {
|
||||
|
||||
private static let renderScale: CGFloat = 3.0
|
||||
private static var cache: [NoteDivision: Image] = [:]
|
||||
|
||||
/// The cached notation glyph for `division`, rendered on first request.
|
||||
static func image(for division: NoteDivision) -> Image {
|
||||
if let existing: Image = cache[division] {
|
||||
return existing
|
||||
}
|
||||
let rendered: Image = render(division)
|
||||
cache[division] = rendered
|
||||
return rendered
|
||||
}
|
||||
|
||||
private static func render(_ division: NoteDivision) -> Image {
|
||||
let renderer: ImageRenderer = ImageRenderer(content: NoteGlyphView(division: division))
|
||||
renderer.scale = renderScale
|
||||
#if os(macOS)
|
||||
if let nsImage = renderer.nsImage {
|
||||
nsImage.isTemplate = true
|
||||
return Image(nsImage: nsImage)
|
||||
}
|
||||
#else
|
||||
if let uiImage = renderer.uiImage {
|
||||
return Image(uiImage: uiImage.withRenderingMode(.alwaysTemplate))
|
||||
}
|
||||
#endif
|
||||
// Fallback to the division's text label if rasterization ever fails.
|
||||
return Image(systemName: "music.note")
|
||||
}
|
||||
}
|
||||
@@ -180,6 +180,189 @@ struct FootswitchButton: View {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Tempo-sync knob
|
||||
|
||||
/// A knob that can be locked to a note division of the live tempo.
|
||||
///
|
||||
/// When free, it is an ordinary ``ParameterKnob``. A compact menu below the dial lets the user
|
||||
/// pick `Free` or a ``NoteDivision``; once a division is chosen the knob is driven by the tempo
|
||||
/// (its dial shows the derived value but is disabled, since the number is computed). The menu is
|
||||
/// shown only while tempo is enabled — the enclosing view gates on `shoeGazeTempoEnabled`.
|
||||
struct TempoSyncKnobView: View {
|
||||
|
||||
private static let cellSpacing: CGFloat = 4
|
||||
private static let syncedDialOpacity: Double = 0.55
|
||||
private static let menuFontSize: CGFloat = 10
|
||||
private static let labelGlyphSpacing: CGFloat = 3
|
||||
private static let warningIconSize: CGFloat = 8
|
||||
/// Height of the glyph in the collapsed menu button, and of the whole menu row, so the row is the
|
||||
/// same height whether it shows the "Free" text or a note glyph — the pedal card never grows.
|
||||
private static let labelGlyphHeight: CGFloat = 18
|
||||
private static let menuRowHeight: CGFloat = 20
|
||||
private static let freeKey: String = "free"
|
||||
|
||||
// Division-picker popover: a height-capped scroll list the system keeps on-screen (it flips
|
||||
// above the button near the bottom edge) rather than a native menu that can run off-screen.
|
||||
private static let pickerMaxHeight: CGFloat = 260
|
||||
private static let pickerGlyphHeight: CGFloat = 22
|
||||
private static let pickerRowSpacing: CGFloat = 2
|
||||
private static let pickerPadding: CGFloat = 8
|
||||
private static let pickerRowMinWidth: CGFloat = 84
|
||||
private static let checkmarkFontSize: CGFloat = 11
|
||||
|
||||
let pedal: Pedal
|
||||
let knob: PedalKnob
|
||||
let param: TempoSyncableParam
|
||||
|
||||
/// Presentation flag for the division-picker popover.
|
||||
@State private var showDivisionPicker: Bool = false
|
||||
|
||||
var body: some View {
|
||||
// Observe the pedal's sync-revision so the badge refreshes when `tempoSync` changes through
|
||||
// any path — this view's picker, a preset load, or a lightweight recall.
|
||||
let _ = pedal.tempoSyncRevision.value
|
||||
let currentKey: String = keyForCurrentSync()
|
||||
let isSynced: Bool = currentKey != TempoSyncKnobView.freeKey
|
||||
let currentBpm: Double? = pedal.clock?.bpm
|
||||
// Reading the live parameter value subscribes this view to Observation, so a tempo change
|
||||
// (which rewrites the node via `applyTempoSync`) re-renders the clamp warning without polling.
|
||||
let liveValue: Double = param.node.value
|
||||
let activeDivision: NoteDivision? = NoteDivision(rawValue: currentKey)
|
||||
let activeClamped: Bool = {
|
||||
_ = liveValue
|
||||
guard isSynced, let bpm: Double = currentBpm, let division: NoteDivision = activeDivision else {
|
||||
return false
|
||||
}
|
||||
return param.value(for: division, bpm: bpm).clamped
|
||||
}()
|
||||
|
||||
VStack(spacing: TempoSyncKnobView.cellSpacing) {
|
||||
ParameterKnob(knob.label, node: knob.node, range: knob.range)
|
||||
.foregroundStyle(Color.white)
|
||||
.disabled(isSynced)
|
||||
.opacity(isSynced ? TempoSyncKnobView.syncedDialOpacity : 1.0)
|
||||
|
||||
Button {
|
||||
showDivisionPicker = true
|
||||
} label: {
|
||||
HStack(spacing: TempoSyncKnobView.labelGlyphSpacing) {
|
||||
if isSynced, let division: NoteDivision = activeDivision {
|
||||
NoteGlyphImageCache.image(for: division)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(height: TempoSyncKnobView.labelGlyphHeight)
|
||||
.accessibilityLabel(Text(labelForKey(currentKey)))
|
||||
} else {
|
||||
Text("Free")
|
||||
.font(.system(size: TempoSyncKnobView.menuFontSize, weight: .semibold, design: .rounded))
|
||||
}
|
||||
if activeClamped {
|
||||
Image(systemName: "exclamationmark.triangle.fill")
|
||||
.font(.system(size: TempoSyncKnobView.warningIconSize))
|
||||
}
|
||||
Image(systemName: "chevron.down")
|
||||
.font(.system(size: TempoSyncKnobView.warningIconSize))
|
||||
.opacity(0.6)
|
||||
}
|
||||
.frame(height: TempoSyncKnobView.menuRowHeight)
|
||||
.foregroundStyle(activeClamped ? Color.orange : Color.white.opacity(isSynced ? 1.0 : 0.65))
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.fixedSize()
|
||||
.help(activeClamped
|
||||
? "This note can't be represented at the current tempo — \(knob.label) is clamped to its limit."
|
||||
: "Sync \(knob.label) to the tempo, or set it freely")
|
||||
.popover(isPresented: $showDivisionPicker) {
|
||||
divisionPicker
|
||||
.presentationCompactAdaptation(.popover)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Height-capped, scrollable list of `Free` + every note division. Presented as a popover so the
|
||||
/// system keeps it on-screen (flipping above the button near the bottom edge) and it scrolls when
|
||||
/// taller than ``pickerMaxHeight`` — unlike a native menu, which ran off the bottom on iOS.
|
||||
private var divisionPicker: some View {
|
||||
let currentBpm: Double? = pedal.clock?.bpm
|
||||
let currentKey: String = keyForCurrentSync()
|
||||
return ScrollView {
|
||||
VStack(alignment: .leading, spacing: TempoSyncKnobView.pickerRowSpacing) {
|
||||
pickerRow(
|
||||
key: TempoSyncKnobView.freeKey,
|
||||
glyph: nil,
|
||||
disabled: false,
|
||||
selected: currentKey == TempoSyncKnobView.freeKey
|
||||
)
|
||||
ForEach(NoteDivision.allCases, id: \.self) { division in
|
||||
// A division whose value clamps at the current tempo (e.g. a note longer than the
|
||||
// maximum delay time) can't be represented accurately, so it is shown but disabled.
|
||||
let unavailable: Bool = currentBpm.map { param.value(for: division, bpm: $0).clamped } ?? false
|
||||
pickerRow(
|
||||
key: division.rawValue,
|
||||
glyph: division,
|
||||
disabled: unavailable,
|
||||
selected: currentKey == division.rawValue
|
||||
)
|
||||
}
|
||||
}
|
||||
.padding(TempoSyncKnobView.pickerPadding)
|
||||
}
|
||||
.frame(maxHeight: TempoSyncKnobView.pickerMaxHeight)
|
||||
}
|
||||
|
||||
/// One selectable row in ``divisionPicker`` — a note glyph (or "Free" text), a trailing checkmark
|
||||
/// when it is the current selection, and a disabled state for divisions that clamp at this tempo.
|
||||
@ViewBuilder
|
||||
private func pickerRow(key: String, glyph: NoteDivision?, disabled: Bool, selected: Bool) -> some View {
|
||||
Button {
|
||||
select(key)
|
||||
showDivisionPicker = false
|
||||
} label: {
|
||||
HStack(spacing: TempoSyncKnobView.labelGlyphSpacing) {
|
||||
if let glyph: NoteDivision = glyph {
|
||||
NoteGlyphImageCache.image(for: glyph)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(height: TempoSyncKnobView.pickerGlyphHeight)
|
||||
} else {
|
||||
Text("Free")
|
||||
.font(.system(size: TempoSyncKnobView.menuFontSize, weight: .semibold, design: .rounded))
|
||||
}
|
||||
Spacer(minLength: TempoSyncKnobView.labelGlyphSpacing)
|
||||
if selected {
|
||||
Image(systemName: "checkmark")
|
||||
.font(.system(size: TempoSyncKnobView.checkmarkFontSize, weight: .semibold))
|
||||
}
|
||||
}
|
||||
.frame(minWidth: TempoSyncKnobView.pickerRowMinWidth, alignment: .leading)
|
||||
.contentShape(Rectangle())
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.disabled(disabled)
|
||||
.accessibilityLabel(Text(glyph?.label ?? "Free"))
|
||||
}
|
||||
|
||||
private func keyForCurrentSync() -> String {
|
||||
if case .division(let division) = pedal.tempoSync[param.key] ?? .free {
|
||||
return division.rawValue
|
||||
}
|
||||
return TempoSyncKnobView.freeKey
|
||||
}
|
||||
|
||||
private func labelForKey(_ key: String) -> String {
|
||||
key == TempoSyncKnobView.freeKey ? "Free" : (NoteDivision(rawValue: key)?.label ?? key)
|
||||
}
|
||||
|
||||
private func select(_ key: String) {
|
||||
// `setSync` bumps `pedal.tempoSyncRevision`, which this view observes — no local flag needed.
|
||||
if key == TempoSyncKnobView.freeKey {
|
||||
pedal.setSync(.free, for: param.key)
|
||||
} else if let division: NoteDivision = NoteDivision(rawValue: key) {
|
||||
pedal.setSync(.division(division), for: param.key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - PedalView
|
||||
|
||||
/// A stompbox-style enclosure view that renders all controls for a single `Pedal`.
|
||||
@@ -266,6 +449,13 @@ public struct PedalView: View {
|
||||
/// Presentation flag for the pedal info panel (description + control ranges).
|
||||
@State private var showInfo: Bool = false
|
||||
|
||||
/// The rig's control map, injected by the builder so the pedal can draw a mapping strip. `nil`
|
||||
/// when the rig has no bindings (or in contexts that don't set it) — the strip renders nothing.
|
||||
@Environment(\.controlMappingMap) private var controlMappingMap: MIDIMap?
|
||||
|
||||
/// Mirrors ``ShoeGaze/tempoEnabled`` so tempo-sync knob affordances hide when tempo is off.
|
||||
@Environment(\.shoeGazeTempoEnabled) private var tempoEnabled: Bool
|
||||
|
||||
// MARK: - Init
|
||||
|
||||
/// Creates a stompbox pedal view for the given pedal.
|
||||
@@ -355,6 +545,7 @@ public struct PedalView: View {
|
||||
sliderBank(sliders)
|
||||
Spacer(minLength: 0)
|
||||
bottomControls
|
||||
PedalMappingStrip(pedal: pedal, map: controlMappingMap)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -389,6 +580,7 @@ public struct PedalView: View {
|
||||
.id(ObjectIdentifier(pedal))
|
||||
}
|
||||
Spacer(minLength: 0)
|
||||
PedalMappingStrip(pedal: pedal, map: controlMappingMap)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -522,12 +714,12 @@ public struct PedalView: View {
|
||||
let rowItemSpacing: CGFloat = row.count >= columns
|
||||
? PedalView.knobSpacing
|
||||
: PedalView.knobRowItemSpacing
|
||||
HStack(spacing: rowItemSpacing) {
|
||||
// Top-align so every dial pins to the row's top edge — a tempo-sync cell grows taller
|
||||
// below the dial (glyph menu, warning) without dragging its neighbours' dials down.
|
||||
HStack(alignment: .top, spacing: rowItemSpacing) {
|
||||
Spacer(minLength: 0)
|
||||
ForEach(Array(row.enumerated()), id: \.offset) { knobPair in
|
||||
let knob: PedalKnob = knobPair.element
|
||||
ParameterKnob(knob.label, node: knob.node, range: knob.range)
|
||||
.foregroundStyle(Color.white)
|
||||
knobCell(knobPair.element)
|
||||
}
|
||||
Spacer(minLength: 0)
|
||||
}
|
||||
@@ -535,6 +727,25 @@ public struct PedalView: View {
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders one effect knob — a tempo-sync knob when the parameter is syncable and tempo is
|
||||
/// enabled, otherwise a plain ``ParameterKnob``. Syncable knobs are matched by node identity
|
||||
/// against `pedal.tempoSyncableParams`.
|
||||
@ViewBuilder
|
||||
private func knobCell(_ knob: PedalKnob) -> some View {
|
||||
if tempoEnabled, let param: TempoSyncableParam = syncableParam(for: knob) {
|
||||
TempoSyncKnobView(pedal: pedal, knob: knob, param: param)
|
||||
} else {
|
||||
ParameterKnob(knob.label, node: knob.node, range: knob.range)
|
||||
.foregroundStyle(Color.white)
|
||||
}
|
||||
}
|
||||
|
||||
/// The tempo-syncable descriptor whose parameter node backs `knob`, or `nil` if the knob is
|
||||
/// not syncable. Matched by `UIParamNode` identity so it is robust to label changes.
|
||||
private func syncableParam(for knob: PedalKnob) -> TempoSyncableParam? {
|
||||
pedal.tempoSyncableParams.first { $0.node === knob.node }
|
||||
}
|
||||
|
||||
/// Row of stepped selector knobs drawn from `pedal.effectSelectors`, beneath the knob grid.
|
||||
/// Renders nothing when the pedal publishes no selectors.
|
||||
@ViewBuilder
|
||||
|
||||
@@ -664,10 +664,14 @@ public struct ShoeGazeBuilderView: View {
|
||||
topSection
|
||||
}
|
||||
Divider().overlay(Color.white.opacity(ShoeGazeBuilderView.dividerOpacity))
|
||||
ControlMappingLegend()
|
||||
boardArea
|
||||
}
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||
.environment(\.shoeGazeTempoEnabled, shoeGaze.tempoEnabled)
|
||||
// Feed the rig's control map into the board tree so each pedal can draw its mapping strip.
|
||||
// Reading `shoeGaze.midiMap` here (observable) refreshes every strip when a binding changes.
|
||||
.environment(\.controlMappingMap, shoeGaze.midiMap)
|
||||
}
|
||||
|
||||
// MARK: - Compact layout (iPhone / narrow iPad)
|
||||
|
||||
@@ -22,6 +22,11 @@ public struct TempoView: View {
|
||||
private static let minBpm: Double = 30.0
|
||||
private static let maxBpm: Double = 300.0
|
||||
private static let bpmStep: Double = 1.0
|
||||
/// Width of the editable BPM text field — fits three digits at the readout font.
|
||||
private static let bpmFieldWidth: CGFloat = 44
|
||||
private static let bpmFontSize: CGFloat = 18
|
||||
/// Maximum digits accepted in the BPM field (max 300 BPM is three digits).
|
||||
private static let bpmMaxDigits: Int = 3
|
||||
private static let rowSpacing: CGFloat = 10
|
||||
|
||||
// Metronome track: a dot swings wall-to-wall once per beat, pausing at each wall (the beat)
|
||||
@@ -56,12 +61,18 @@ public struct TempoView: View {
|
||||
/// Continuous beat position, polled from the clock; drives the metronome dot.
|
||||
@State private var beat: Double = 0.0
|
||||
|
||||
/// The editable BPM text. Kept in sync with `displayedBpm` while the field isn't focused; on
|
||||
/// commit it is validated (integer only), clamped to `[minBpm, maxBpm]`, and applied.
|
||||
@State private var bpmText: String
|
||||
@FocusState private var bpmFieldFocused: Bool
|
||||
|
||||
// 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)
|
||||
self._bpmText = State(initialValue: String(Int(shoeGaze.clock.bpm.rounded())))
|
||||
}
|
||||
|
||||
// MARK: - Body
|
||||
@@ -78,18 +89,12 @@ public struct TempoView: View {
|
||||
|
||||
if shoeGaze.tempoEnabled {
|
||||
metronome
|
||||
Text("\(Int(displayedBpm.rounded())) BPM")
|
||||
.font(.system(size: 18, weight: .semibold, design: .rounded))
|
||||
.foregroundStyle(Color.white)
|
||||
bpmField
|
||||
Stepper(
|
||||
"",
|
||||
value: Binding(
|
||||
get: { displayedBpm },
|
||||
set: { newValue in
|
||||
let clamped: Double = min(max(newValue, TempoView.minBpm), TempoView.maxBpm)
|
||||
shoeGaze.clock.bpm = clamped
|
||||
displayedBpm = clamped
|
||||
}
|
||||
set: { newValue in applyBpm(newValue) }
|
||||
),
|
||||
in: TempoView.minBpm ... TempoView.maxBpm,
|
||||
step: TempoView.bpmStep
|
||||
@@ -106,11 +111,78 @@ public struct TempoView: View {
|
||||
while shoeGaze.tempoEnabled && !Task.isCancelled {
|
||||
beat = shoeGaze.clock.currentBeat
|
||||
displayedBpm = shoeGaze.clock.bpm
|
||||
// Don't overwrite the field mid-edit — only reflect external tempo changes when the
|
||||
// field isn't focused (tap tempo, stepper, MIDI, rig load).
|
||||
if !bpmFieldFocused {
|
||||
bpmText = String(Int(displayedBpm.rounded()))
|
||||
}
|
||||
try? await Task.sleep(nanoseconds: TempoView.pollIntervalNanoseconds)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - BPM field
|
||||
|
||||
/// Editable BPM readout. Accepts integer input, validated and clamped on commit (Return, or the
|
||||
/// keyboard's Done, or focus loss). Non-numeric or empty input reverts to the current tempo.
|
||||
private var bpmField: some View {
|
||||
HStack(spacing: TempoView.rowSpacing / 2) {
|
||||
TextField("", text: $bpmText)
|
||||
.textFieldStyle(.plain)
|
||||
.multilineTextAlignment(.trailing)
|
||||
.font(.system(size: TempoView.bpmFontSize, weight: .semibold, design: .rounded))
|
||||
.foregroundStyle(Color.white)
|
||||
.frame(width: TempoView.bpmFieldWidth)
|
||||
.fixedSize()
|
||||
.focused($bpmFieldFocused)
|
||||
.onSubmit { commitBpm() }
|
||||
#if os(iOS)
|
||||
.keyboardType(.numberPad)
|
||||
.toolbar {
|
||||
ToolbarItemGroup(placement: .keyboard) {
|
||||
Spacer()
|
||||
Button("Done") { bpmFieldFocused = false }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Text("BPM")
|
||||
.font(.system(size: TempoView.bpmFontSize, weight: .semibold, design: .rounded))
|
||||
.foregroundStyle(Color.white)
|
||||
}
|
||||
.onChange(of: bpmText) { _, newValue in
|
||||
// Reject non-numeric input as it is typed, and cap at three digits (max 300 BPM).
|
||||
let filtered: String = String(newValue.filter { $0.isNumber }.prefix(TempoView.bpmMaxDigits))
|
||||
if filtered != newValue {
|
||||
bpmText = filtered
|
||||
}
|
||||
}
|
||||
.onChange(of: bpmFieldFocused) { _, focused in
|
||||
// Commit when focus leaves the field (tap away / Done), the counterpart to onSubmit.
|
||||
if !focused {
|
||||
commitBpm()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates and applies the typed BPM: integers only, clamped to `[minBpm, maxBpm]`. Non-numeric
|
||||
/// or empty input reverts the field to the current tempo without changing it.
|
||||
private func commitBpm() {
|
||||
let digits: String = bpmText.filter { $0.isNumber }
|
||||
if let value: Int = Int(digits) {
|
||||
applyBpm(Double(value))
|
||||
} else {
|
||||
bpmText = String(Int(displayedBpm.rounded()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Clamps `bpm` to the allowed range, applies it to the session, and syncs the field text.
|
||||
private func applyBpm(_ bpm: Double) {
|
||||
let clamped: Double = min(max(bpm, TempoView.minBpm), TempoView.maxBpm)
|
||||
shoeGaze.setBpm(clamped)
|
||||
displayedBpm = clamped
|
||||
bpmText = String(Int(clamped.rounded()))
|
||||
}
|
||||
|
||||
// MARK: - Subviews
|
||||
|
||||
/// Horizontal pendulum: the dot maps `beat` through a cosine so it eases to a stop at each
|
||||
|
||||
Reference in New Issue
Block a user