// LyrebirdAUParameterMapping.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if canImport(AudioToolbox) import Foundation import AVFoundation import AudioToolbox /// Builds an `AUParameterTree` from a `SynthesizerBase` subclass by walking its /// `Mirror` for `@LiveParam` / `@UIParam` properties, assigning each one a stable /// `AUParameterAddress`, and creating one `AUParameter` per slot. /// /// Mirrors the discovery logic in `SynthesizerBase._autoRegisterLiveParams()`: /// walks the full `superclassMirror` chain (oldest first) so inherited live /// parameters are included in declaration order. /// /// **Min / max / unit metadata.** `@LiveParam` itself carries no bounds, so /// subclasses of `LyrebirdAudioUnit` should override `parameterMetadata()` to /// supply `LyrebirdAUParameterInfo` per parameter name. Names absent from the /// dictionary receive permissive defaults. /// /// **Threading.** Mirror reflection is main-thread only; do not invoke from the /// audio render thread. public enum LyrebirdAUParameterMapping { /// Walks the synthesizer's mirror hierarchy and returns every `@LiveParam` / /// `@UIParam` slot with a stable `AUParameterAddress`. public static func discoverLiveParameters( of synth: SynthesizerBase ) -> [LyrebirdAUParameterEntry] { var entries: [LyrebirdAUParameterEntry] = [] var address: AUParameterAddress = 0 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 } let projected: any ValidGeneratorInput = wrapper._projectedAsValidInput guard let node: any LiveParameterValue = projected as? LiveParameterValue else { continue } entries.append( LyrebirdAUParameterEntry(name: key, address: address, node: node) ) address += 1 } } return entries } /// Builds the AU parameter tree and a reverse-lookup table from address to the /// underlying `LiveParameterValue` node. /// /// - Parameters: /// - synth: The synthesizer to introspect. /// - metadata: Per-parameter overrides keyed by parameter name. Missing keys /// fall back to `LyrebirdAUParameterInfo()` defaults. /// - Returns: The constructed `AUParameterTree` and a dictionary mapping each /// `AUParameterAddress` to its backing `LiveParameterValue` node. The caller /// uses the dictionary in the AU's `implementorValueObserver` / /// `implementorValueProvider` to bridge host writes/reads to the underlying /// atomic store on each `ParameterNode` / `UIParamNode`. public static func buildParameterTree( for synth: SynthesizerBase, metadata: [String: LyrebirdAUParameterInfo] = [:] ) -> (tree: AUParameterTree, nodes: [AUParameterAddress: any LiveParameterValue]) { let entries: [LyrebirdAUParameterEntry] = discoverLiveParameters(of: synth) var auParameters: [AUParameter] = [] var nodes: [AUParameterAddress: any LiveParameterValue] = [:] auParameters.reserveCapacity(entries.count) nodes.reserveCapacity(entries.count) for entry: LyrebirdAUParameterEntry in entries { let info: LyrebirdAUParameterInfo = metadata[entry.name] ?? LyrebirdAUParameterInfo() let display: String = info.displayName ?? entry.name let param: AUParameter = AUParameterTree.createParameter( withIdentifier: entry.name, name: display, address: entry.address, min: info.minValue, max: info.maxValue, unit: info.unit, unitName: nil, flags: info.flags, valueStrings: nil, dependentParameters: nil ) // Seed from the node's current scalar value (clamped to the declared range). let current: AUValue = AUValue(entry.node.value) param.value = min(max(current, info.minValue), info.maxValue) auParameters.append(param) nodes[entry.address] = entry.node } let tree: AUParameterTree = AUParameterTree.createTree(withChildren: auParameters) return (tree, nodes) } } #endif