Files
ShoeGaze/Sources/LyrebirdShoeGaze/Tempo/TempoSyncableParam.swift
T
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

87 lines
3.6 KiB
Swift

// TempoSyncableParam.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdUI
/// Descriptor for one pedal parameter that can be locked to the tempo.
///
/// A ``Pedal`` publishes an array of these (via ``Pedal/tempoSyncableParams``) so the shared
/// tempo-sync machinery can recompute and rewrite each synced parameter without knowing the
/// concrete pedal type. The stable ``key`` doubles as the persistence key in ``PedalState``.
public struct TempoSyncableParam {
/// How the synced period maps onto the parameter's value.
public enum Kind {
/// A duration in seconds — the value is the synced period `T` directly (delay time).
case time
/// A frequency in Hz — the value is `1 / T` (one LFO cycle per division).
case rate
}
/// Stable identifier for this parameter within its pedal (e.g. `"time"`, `"time1"`, `"rate"`).
/// Used as the ``TempoSync`` dictionary key and the persistence key, so it must never change.
public let key: String
/// Short display label, matching the parameter's knob label.
public let label: String
/// The live parameter node the synced value is written to.
public let node: UIParamNode
/// Whether the value is a duration (`.time`) or a frequency (`.rate`, inverse of the period).
public let kind: Kind
/// Inclusive value range the parameter accepts, in its own units. A synced value outside this
/// range is clamped (a long division can exceed a delay's maximum buffered time).
public let range: ClosedRange<Double>
/// Creates a tempo-syncable parameter descriptor.
///
/// - Parameters:
/// - key: Stable identifier and persistence key. Must be unique within the pedal and stable
/// across versions.
/// - label: Short display label, matching the knob label.
/// - node: The live parameter node (`pedal.$time`, `pedal.$rate`, …).
/// - kind: `.time` for a duration, `.rate` for a frequency (inverse of the period).
/// - range: Inclusive value range in the parameter's units; the synced value is clamped to it.
public init(
key: String,
label: String,
node: UIParamNode,
kind: Kind,
range: ClosedRange<Double>
) {
self.key = key
self.label = label
self.node = node
self.kind = kind
self.range = range
}
private static let secondsPerMinute: Double = 60.0
/// The unclamped parameter value for `division` at `bpm`. For `.time` this is the note's period
/// in seconds; for `.rate` it is `1 / period` in Hz (one LFO cycle per division).
public func rawValue(for division: NoteDivision, bpm: Double) -> Double {
let period: Double = (TempoSyncableParam.secondsPerMinute / bpm) * division.beats
switch kind {
case .time:
return period
case .rate:
return period > 0 ? 1.0 / period : range.upperBound
}
}
/// The value actually written for `division` at `bpm`, plus whether it had to be clamped to the
/// parameter's range. A clamped result means the division can't be represented accurately at this
/// tempo (e.g. a whole note longer than the maximum delay time) — the UI disables/flags it.
public func value(for division: NoteDivision, bpm: Double) -> (value: Double, clamped: Bool) {
let raw: Double = rawValue(for: division, bpm: bpm)
let clampedValue: Double = min(max(raw, range.lowerBound), range.upperBound)
return (clampedValue, clampedValue != raw)
}
}