// LyrebirdSynthView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if canImport(SwiftUI) 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, Sendable { public let id: String public let propertyName: String public let displayName: String public let node: UIParamNode public let range: ClosedRange public init( propertyName: String, displayName: String, node: UIParamNode, range: ClosedRange ) { 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 = 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 #else // Linux/GTK4 counterpart to `LyrebirdUI.LyrebirdSynthView`: an auto-generated // parameter grid for a `SynthesizerBase` subclass, one ``ParameterKnob`` per // `@UIParam` slot. The `@UIParam` discovery (`Mirror` walk), the ``Entry`` type, // and the range/default constants are identical to the Apple side — only the // `body` swaps the SwiftUI `ScrollView`/`LazyVGrid` for SwiftOpenUI's data-based // `LazyVGrid` (`columns:data:content:`, no per-item ForEach). Adaptive columns // map onto GtkGridView's min-column-count behaviour. // // SwiftOpenUI is always present in this target (see Package.swift), so no // `canImport` guard is needed. import SwiftOpenUI import LyrebirdSynth /// Auto-generated parameter grid for a `SynthesizerBase` subclass on Linux/GTK4. /// Mirrors `LyrebirdUI.LyrebirdSynthView`: walks the synth's `Mirror` chain for /// `@UIParam` slots and renders one ``ParameterKnob`` per slot in an adaptive /// grid. Same public API/init as the Apple side. /// /// **`@UIParam` only.** `@LiveParam` slots are skipped — they have no /// `@Observable` projected value to drive a knob. public struct LyrebirdSynthView: View { public let entries: [Entry] /// - Parameter synth: The `SynthesizerBase` instance to introspect. /// /// > Note: The Apple side also accepts a `[String: LyrebirdAUParameterInfo]` /// > metadata dictionary for per-knob ranges, but that type lives under /// > `#if canImport(AudioToolbox)` (Apple-only), so on Linux every slot uses /// > the default `lyrebirdSynthViewDefaultMin...Max` range. Pass pre-built /// > ``Entry`` values via `init(entries:)` for custom ranges. public init(synth: SynthesizerBase) { self.entries = LyrebirdSynthView.discover(synth: synth) } /// - 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))], data: entries ) { entry in ParameterKnob( entry.displayName, node: entry.node, range: entry.range ) } .padding() } } /// One discovered `@UIParam` slot, paired with its display name + range. public struct Entry: Identifiable, Sendable { public let id: String public let propertyName: String public let displayName: String public let node: UIParamNode public let range: ClosedRange public init( propertyName: String, displayName: String, node: UIParamNode, range: ClosedRange ) { 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()`. Every /// slot uses the default range (no `LyrebirdAUParameterInfo` on Linux). public static func discover(synth: SynthesizerBase) -> [Entry] { var entries: [Entry] = [] var mirrors: [Mirror] = [] var current: Mirror? = Mirror(reflecting: synth) while let m: Mirror = current { mirrors.append(m) current = m.superclassMirror } let range: ClosedRange = lyrebirdSynthViewDefaultMin...lyrebirdSynthViewDefaultMax 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 } entries.append(Entry( propertyName: key, displayName: key, node: node, range: range )) } } return entries } } /// Minimum column width (points) for ``LyrebirdSynthView``'s adaptive grid. public let lyrebirdSynthViewColumnMinWidth: Double = 120 // NOTE: the Apple side's `lyrebirdSynthViewGridSpacing` / `lyrebirdSynthViewItemSpacing` // are intentionally absent on Linux — SwiftOpenUI's `LazyVGrid(columns:data:content:)` // exposes no per-item spacing parameter (grid layout is driven by GtkGridView's // column count), so there is nothing to apply them to. The Linux grid uses // GtkGridView's default gutter. /// Default lower bound used when `parameterMetadata` does not supply a range. public let lyrebirdSynthViewDefaultMin: Double = 0 /// Default upper bound used when `parameterMetadata` does not supply a range. public let lyrebirdSynthViewDefaultMax: Double = 1 #endif