Files
Lyrebird/Sources/Lyrebird/AUv3/LyrebirdAUParameterInfo.swift
T
2026-06-01 18:27:40 -07:00

77 lines
2.9 KiB
Swift

// LyrebirdAUParameterInfo.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
import AVFoundation
import AudioToolbox
/// Per-parameter metadata supplied by a `LyrebirdAudioUnit` subclass to override the
/// auto-generated `AUParameter` for an `@LiveParam` slot.
///
/// `@LiveParam` itself does not carry `min` / `max` / unit information — its
/// declaration is just `@LiveParam var freq: Double = 440.0`. To expose meaningful
/// bounds and units to AU hosts, override `LyrebirdAudioUnit.parameterMetadata()`
/// and return a dictionary keyed by parameter name:
///
/// ```swift
/// public override class func parameterMetadata() -> [String: LyrebirdAUParameterInfo] {
/// [
/// "freq": .init(min: 20, max: 8000, unit: .hertz, displayName: "Frequency"),
/// "amp": .init(min: 0, max: 1, unit: .linearGain),
/// "modIndex": .init(min: 0, max: 4),
/// ]
/// }
/// ```
///
/// Names not present in the dictionary fall back to permissive defaults
/// (`min: -1e10`, `max: 1e10`, `unit: .generic`).
public struct LyrebirdAUParameterInfo: Sendable {
/// Lower bound exposed to the host as `AUParameter.minValue`. The AU's parameter
/// observer enforces this clamp; if the host writes a value outside `[min, max]`
/// `AUParameter` itself clamps before invoking the observer.
public var minValue: AUValue
/// Upper bound exposed to the host as `AUParameter.maxValue`.
public var maxValue: AUValue
/// AU-spec unit hint used by the host's UI (knob labels, automation ramp
/// interpretation, etc.). Defaults to `.generic`.
public var unit: AudioUnitParameterUnit
/// Human-readable parameter name shown in host UI. Defaults to the property name.
public var displayName: String?
/// AU flags exposed for the parameter. Defaults to readable/writable + ramped.
public var flags: AudioUnitParameterOptions
public init(
min: AUValue = -lyrebirdAUParameterDefaultBound,
max: AUValue = lyrebirdAUParameterDefaultBound,
unit: AudioUnitParameterUnit = .generic,
displayName: String? = nil,
flags: AudioUnitParameterOptions = lyrebirdAUParameterDefaultFlags
) {
self.minValue = min
self.maxValue = max
self.unit = unit
self.displayName = displayName
self.flags = flags
}
}
/// Default unbounded range for `LyrebirdAUParameterInfo` (effectively unbounded for
/// any audible parameter). Using a finite value keeps `AUParameter` happy — it does
/// not accept `.infinity` as a min/max.
public let lyrebirdAUParameterDefaultBound: AUValue = 1.0e10
/// Default parameter flags: readable + writable + ramping enabled.
public let lyrebirdAUParameterDefaultFlags: AudioUnitParameterOptions = [
.flag_IsReadable,
.flag_IsWritable,
.flag_CanRamp,
]