84 lines
3.0 KiB
Swift
84 lines
3.0 KiB
Swift
// UIParam.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import LyrebirdSynth
|
|
|
|
/// `@UIParam` — property wrapper for synthesizer parameters that need both:
|
|
/// - SwiftUI binding: controls like `ParameterSlider` observe `$paramName` via `@Observable`.
|
|
/// - RT-safe audio delivery: the render callback reads via a lock-free `Atomic<UInt64>` load.
|
|
///
|
|
/// ## When to use @UIParam vs @LiveParam
|
|
///
|
|
/// Use `@UIParam` (this type, in `LyrebirdUI`) when SwiftUI controls bind to the parameter.
|
|
/// Use `@LiveParam` (in `Lyrebird`) when no UI is involved.
|
|
///
|
|
/// `@UIParam` is a strict superset of `@LiveParam`: identical RT thread behavior — the
|
|
/// projected `UIParamNode` is a `ControlGenerator` that delivers a linearly-interpolated
|
|
/// ramp from the previous block's value to the new atomic target — plus an `@Observable`
|
|
/// `value` that drives SwiftUI re-renders.
|
|
///
|
|
/// Both wrappers are auto-discovered by `SynthesizerBase` at `build()` time via `Mirror`
|
|
/// reflection. The property name (with leading `_` stripped) becomes the parameter key for
|
|
/// `updateParameter` / `LyrebirdMessageContext.set`.
|
|
///
|
|
/// ## Usage
|
|
///
|
|
/// ```swift
|
|
/// import LyrebirdUI
|
|
///
|
|
/// class MySynth: SynthesizerBase {
|
|
/// @UIParam var frequency: Double = 440.0
|
|
/// @UIParam var amplitude: Double = 0.5
|
|
///
|
|
/// override func build() {
|
|
/// let osc = OscSin.Audio(freq: $frequency) // ramped delivery
|
|
/// output(bus: 0, source: osc * $amplitude)
|
|
/// }
|
|
/// }
|
|
///
|
|
/// // Code update — RT-safe, triggers SwiftUI re-render
|
|
/// synth.frequency = 880.0
|
|
///
|
|
/// // SwiftUI binding
|
|
/// ParameterSlider("Frequency", node: synth.$frequency, range: 20...20000)
|
|
/// ```
|
|
///
|
|
/// ## Threading model
|
|
///
|
|
/// Writes to `wrappedValue` go through `UIParamNode.update(_:)`, which posts a
|
|
/// releasing atomic store and updates the `@Observable` value for the UI.
|
|
/// The RT thread reads only the atomic — it never touches `wrappedValue` or SwiftUI state.
|
|
@propertyWrapper
|
|
public struct UIParam: LiveParameterWrapper {
|
|
|
|
// MARK: - Storage
|
|
|
|
/// The `UIParamNode` backing object. Use this as a `ValidGeneratorInput` in `build()`.
|
|
///
|
|
/// Accessible via the `$paramName` projected-value syntax.
|
|
public let projectedValue: UIParamNode
|
|
|
|
/// `LiveParameterWrapper` conformance — used by `SynthesizerBase`'s Mirror walk to
|
|
/// populate the parameter table without knowing the concrete wrapper type.
|
|
public var _projectedAsValidInput: any ValidGeneratorInput { projectedValue }
|
|
|
|
// MARK: - Init
|
|
|
|
/// Creates an `@UIParam` with the given initial value.
|
|
public init(wrappedValue initialValue: Double) {
|
|
self.projectedValue = UIParamNode(initialValue)
|
|
}
|
|
|
|
// MARK: - wrappedValue
|
|
|
|
/// Read: returns the main-thread observable value from the backing node.
|
|
/// Write: calls `UIParamNode.update(_:)` to keep the atomic and `@Observable` in sync.
|
|
public var wrappedValue: Double {
|
|
get { projectedValue.value }
|
|
nonmutating set { projectedValue.update(newValue) }
|
|
}
|
|
}
|