176 lines
7.0 KiB
Swift
176 lines
7.0 KiB
Swift
// LyrebirdSynthView.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import SwiftUI
|
|
import LyrebirdSynth
|
|
|
|
/// Auto-generated SwiftUI parameter grid for a `SynthesizerBase` subclass.
|
|
///
|
|
/// Walks the synth's `Mirror` hierarchy for `@UIParam` properties and renders
|
|
/// one `ParameterKnob` per slot. Optional `metadata` (the same
|
|
/// `[String: LyrebirdAUParameterInfo]` dictionary the AU subclass returns from
|
|
/// `parameterMetadata()`) supplies the per-knob `range`.
|
|
///
|
|
/// **`@UIParam` only.** Slots declared with `@LiveParam` are skipped — they
|
|
/// have no `@Observable` projected value and so cannot drive a SwiftUI knob
|
|
/// without additional plumbing. If you want a slot to appear in the
|
|
/// auto-generated UI, declare it as `@UIParam`. RT semantics are identical
|
|
/// across both wrappers (both back onto an atomic store-and-ramp path).
|
|
///
|
|
/// **Usage from an AU view controller.** Inside a
|
|
/// `LyrebirdAudioUnitViewController` subclass:
|
|
///
|
|
/// ```swift
|
|
/// public override func viewDidLoad() {
|
|
/// super.viewDidLoad()
|
|
/// guard let au = audioUnit as? LyrebirdFMAudioUnit else { return }
|
|
/// let host = NSHostingController(
|
|
/// rootView: LyrebirdSynthView(
|
|
/// synth: au.synthesizer,
|
|
/// metadata: LyrebirdFMAudioUnit.parameterMetadata()
|
|
/// )
|
|
/// )
|
|
/// addChild(host)
|
|
/// view.addSubview(host.view)
|
|
/// host.view.frame = view.bounds
|
|
/// host.view.autoresizingMask = [.width, .height]
|
|
/// }
|
|
/// ```
|
|
///
|
|
/// **Customising the layout.** The default lays out knobs in a wrap-around
|
|
/// `LazyVGrid` with adaptive columns (minimum width 120 pt). For a hand-tuned
|
|
/// view, do not use this view — assemble `ParameterKnob` instances directly,
|
|
/// or use this view's `entries` static helper to discover the slots and
|
|
/// build your own layout around it.
|
|
///
|
|
/// - Parameters:
|
|
/// - synth: The `SynthesizerBase` instance to introspect. Walked via
|
|
/// `Mirror` to discover `@UIParam` slots.
|
|
/// - metadata: Optional per-parameter metadata keyed by property name —
|
|
/// same dictionary the AU subclass returns from `parameterMetadata()`.
|
|
/// Names absent from the dictionary fall back to
|
|
/// `lyrebirdSynthViewDefaultMin...lyrebirdSynthViewDefaultMax`.
|
|
/// - entries: Alternative init that takes a pre-built list of discovered
|
|
/// entries (e.g. produced by a custom Mirror walk). Bypasses the
|
|
/// `synth` + `metadata` discovery path.
|
|
public struct LyrebirdSynthView: View {
|
|
|
|
public let entries: [Entry]
|
|
|
|
/// - Parameters:
|
|
/// - synth: The `SynthesizerBase` instance to introspect.
|
|
/// - metadata: Optional per-parameter metadata keyed by property name.
|
|
public init(
|
|
synth: SynthesizerBase,
|
|
metadata: [String: LyrebirdAUParameterInfo] = [:]
|
|
) {
|
|
self.entries = LyrebirdSynthView.discover(synth: synth, metadata: metadata)
|
|
}
|
|
|
|
/// - Parameter entries: Pre-built list of discovered entries.
|
|
public init(entries: [Entry]) {
|
|
self.entries = entries
|
|
}
|
|
|
|
public var body: some View {
|
|
ScrollView {
|
|
LazyVGrid(
|
|
columns: [GridItem(.adaptive(minimum: lyrebirdSynthViewColumnMinWidth),
|
|
spacing: lyrebirdSynthViewGridSpacing)],
|
|
spacing: lyrebirdSynthViewGridSpacing
|
|
) {
|
|
ForEach(entries) { entry in
|
|
VStack(spacing: lyrebirdSynthViewItemSpacing) {
|
|
ParameterKnob(
|
|
entry.displayName,
|
|
node: entry.node,
|
|
range: entry.range
|
|
)
|
|
}
|
|
}
|
|
}
|
|
.padding()
|
|
}
|
|
}
|
|
|
|
/// One discovered `@UIParam` slot, paired with its display name + range.
|
|
public struct Entry: Identifiable {
|
|
public let id: String
|
|
public let propertyName: String
|
|
public let displayName: String
|
|
public let node: UIParamNode
|
|
public let range: ClosedRange<Double>
|
|
|
|
public init(
|
|
propertyName: String,
|
|
displayName: String,
|
|
node: UIParamNode,
|
|
range: ClosedRange<Double>
|
|
) {
|
|
self.id = propertyName
|
|
self.propertyName = propertyName
|
|
self.displayName = displayName
|
|
self.node = node
|
|
self.range = range
|
|
}
|
|
}
|
|
|
|
/// Walks the synth's `Mirror` (with superclass chain) looking for `@UIParam`
|
|
/// properties and emits one ``Entry`` per slot. Slot order matches the
|
|
/// `Mirror` walk used by `SynthesizerBase._autoRegisterLiveParams()` and
|
|
/// `LyrebirdAUParameterMapping.discoverLiveParameters(of:)`.
|
|
public static func discover(
|
|
synth: SynthesizerBase,
|
|
metadata: [String: LyrebirdAUParameterInfo] = [:]
|
|
) -> [Entry] {
|
|
var entries: [Entry] = []
|
|
var mirrors: [Mirror] = []
|
|
var current: Mirror? = Mirror(reflecting: synth)
|
|
while let m: Mirror = current {
|
|
mirrors.append(m)
|
|
current = m.superclassMirror
|
|
}
|
|
for mirror: Mirror in mirrors.reversed() {
|
|
for child: Mirror.Child in mirror.children {
|
|
guard let label: String = child.label else { continue }
|
|
let key: String = label.hasPrefix("_") ? String(label.dropFirst()) : label
|
|
guard let wrapper: any LiveParameterWrapper = child.value as? LiveParameterWrapper else { continue }
|
|
guard let node: UIParamNode = wrapper._projectedAsValidInput as? UIParamNode else { continue }
|
|
let info: LyrebirdAUParameterInfo? = metadata[key]
|
|
let lo: Double = info.map { Double($0.minValue) } ?? lyrebirdSynthViewDefaultMin
|
|
let hi: Double = info.map { Double($0.maxValue) } ?? lyrebirdSynthViewDefaultMax
|
|
let range: ClosedRange<Double> = lo...hi
|
|
let displayName: String = info?.displayName ?? key
|
|
entries.append(Entry(
|
|
propertyName: key,
|
|
displayName: displayName,
|
|
node: node,
|
|
range: range
|
|
))
|
|
}
|
|
}
|
|
return entries
|
|
}
|
|
}
|
|
|
|
/// Minimum column width (points) for ``LyrebirdSynthView``'s adaptive grid.
|
|
public let lyrebirdSynthViewColumnMinWidth: Double = 120
|
|
|
|
/// Spacing (points) between grid items, both horizontally and vertically.
|
|
public let lyrebirdSynthViewGridSpacing: Double = 16
|
|
|
|
/// Spacing (points) between elements inside each grid item's `VStack`.
|
|
public let lyrebirdSynthViewItemSpacing: Double = 6
|
|
|
|
/// Default lower bound used when `parameterMetadata` does not supply a range.
|
|
/// Matches the conventional `[0, 1]` LFO / normalised parameter range —
|
|
/// safer than `lyrebirdAUParameterDefaultBound` (`±1e10`) for UI knobs, which
|
|
/// would be unusable.
|
|
public let lyrebirdSynthViewDefaultMin: Double = 0
|
|
|
|
/// Default upper bound used when `parameterMetadata` does not supply a range.
|
|
public let lyrebirdSynthViewDefaultMax: Double = 1
|