Files
ShoeGaze/Sources/LyrebirdShoeGaze/Tempo/TempoSyncableParam.swift
T
josh 0ddf350f1f Migrate to Lyrebird 2.0.0; app version bump
Lyrebird 2.0 moved `@UIParam` / `UIParamNode` out of `LyrebirdUI` into the
core `LyrebirdSynth` module (the `Lyrebird` product); `LyrebirdUI` no longer
re-exports them. Migrate:
- add `import LyrebirdSynth` to every file that uses `@UIParam`/`UIParamNode`
  (the pedal controls, tempo-syncable params, and the SwiftUI views).
- Package.swift: the `LyrebirdShoeGazeUI` target now also depends on the
  `Lyrebird` product (previously only `LyrebirdUI`), so `import LyrebirdSynth`
  resolves there under explicit module maps.
- Package.resolved (SwiftPM + both Xcode projects) pinned to Lyrebird 2.0.0.

Also: app marketing/project version bumps, Xcode upgrade-check, and a
CHANGELOG wording tweak carried in the same working tree.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-23 21:17:30 -07:00

88 lines
3.6 KiB
Swift

// TempoSyncableParam.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdSynth
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)
}
}