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