Files
josh 2772abdc56 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>
2026-07-10 08:47:22 -07:00

245 lines
10 KiB
Swift

// 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")
}
}