# AUv3 Plugin Export Ship a `SynthesizerBase` subclass as an Audio Unit v3 (`.appex`) plugin loadable by Logic, GarageBand, Ableton (via Bridge), Loopy Pro, AUM, and any other AUv3 host on macOS or iOS. ## Overview Lyrebird's AUv3 layer is the headless Swift API that bridges the engine into the `AUAudioUnit` contract: - ``AUv3Driver`` — passive `AudioDriver` driven from the AU host's render block. - ``AUv3RingBuffer`` — fixed-block → variable-frame adapter, one per AU. - ``LyrebirdAudioUnit`` — open `AUAudioUnit` subclass; provides parameter tree, output bus topology, `internalRenderBlock`, and `fullState` save/restore. - ``LyrebirdAUParameterMapping`` — Mirror-driven discovery that exposes every `@LiveParam` / `@UIParam` slot as an `AUParameter`. The framework ships no Xcode `.appex` extension target — each plugin's `.appex` wrapper is supplied by the host app project. The classes above are what that wrapper subclasses. ## Minimal subclass pattern ```swift import LyrebirdSynth final class MyFMSynth: SynthesizerBase { @LiveParam var carrierFreq: Double = 440 @LiveParam var modIndex: Double = 0.5 override func build() { let mod = OscSin.Audio(freq: $carrierFreq * 2, mul: $modIndex * $carrierFreq) let car = OscSin.Audio(freq: $carrierFreq + mod) output(bus: 0, source: car * 0.3) } } public final class MyFMSynthAudioUnit: LyrebirdAudioUnit { public override class func makeSynthesizer() -> SynthesizerBase { MyFMSynth() } public override class func parameterMetadata() -> [String: LyrebirdAUParameterInfo] { [ "carrierFreq": .init(min: 20, max: 8000, unit: .hertz), "modIndex": .init(min: 0, max: 4), ] } } ``` The Xcode `.appex` extension target registers an `AUViewController` subclass that conforms to `AUAudioUnitFactory` as the `NSExtensionPrincipalClass`. Its `createAudioUnit(with:)` instantiates `MyFMSynthAudioUnit` and returns it to the host. The audio component itself is declared in the extension's `Info.plist` under `NSExtension.NSExtensionAttributes.AudioComponents`. ## Architecture ### Render path ``` ┌─────────────────┐ internalRenderBlock ┌─────────────────┐ │ AU host │ ────────────────────▶ │ LyrebirdAudioUnit │ └─────────────────┘ (frameCount=N) └────────┬────────┘ │ render(N, ABL) ▼ ┌─────────────────┐ │ AUv3Driver │ │ + AUv3RingBuffer│ └────────┬────────┘ │ ring.availableToRead < N? (yes loop)│ engine.processBlock(...) ▼ ┌─────────────────┐ │ SynthesisEngine │ │ (blockSize=256) │ └────────┬────────┘ │ engine.audioBuses[0.. SynthesizerBase { StereoCompressorSynth() } public override class var lyrebirdOutputBusses: [LyrebirdAUBusSpec] { [LyrebirdAUBusSpec(name: "Output", channelCount: 2)] } public override class var lyrebirdInputBusses: [LyrebirdAUBusSpec] { [ .init(name: "Main In", channelCount: 2), .init(name: "Sidechain", channelCount: 2), ] } } ``` The synth's `build()` reads from each declared input bus via ``AUInput/Audio`` with an explicit `busIndex`: ```swift override func build() { let mainL = AUInput.Audio(busIndex: 0, channel: 0) let mainR = AUInput.Audio(busIndex: 0, channel: 1) let scL = AUInput.Audio(busIndex: 1, channel: 0) let scR = AUInput.Audio(busIndex: 1, channel: 1) let sidechain = (scL + scR) * 0.5 let env = AmplitudeFollower.Control(input: sidechain) let gain = Compressor.Control(env: env, threshold: $threshold, ratio: $ratio) output(bus: 0, source: mainL * gain) output(bus: 1, source: mainR * gain) } ``` ### Engine bus layout The `SynthesisEngine`'s contiguous `audioBuses` array holds output busses first, then input busses, each occupying `channelCount` slots in declaration order: ``` engine.audioBuses ┌──────────────────────────────┬──────────────────────────────┐ │ Output busses (sum of all) │ Input busses (sum of all) │ └──────────────────────────────┴──────────────────────────────┘ [0, [numOutputChannels, Σ outputSpec.channelCount) numOutputChannels + Σ inputSpec.channelCount) ``` The driver populates ``SynthesisEngine/auInputBusOffsets`` and ``SynthesisEngine/auOutputBusOffsets`` so generators / debugging code can map AU bus → engine bus index at render time. ### Driver render contract ``AUv3Driver/render(frameCount:outputBusNumber:outputData:timestamp:pullInput:)`` takes an `outputBusNumber` argument and reads from the matching per-bus ring. AU hosts call `internalRenderBlock` once per output bus per render cycle; each call advances the engine only if the requested bus's ring needs more frames. Other busses' rings buffer their output until the host pulls them. For multi-input-bus effect AUs, `_fillInputStagingFromHost` calls the host's `AURenderPullInputBlock` once per input bus per engine block (passing the input bus index as the 4th arg) and copies the host samples into the matching engine input bus slice. ### Topics added - ``LyrebirdAUBusSpec`` - ``LyrebirdAudioUnit/lyrebirdOutputBusses`` - ``LyrebirdAudioUnit/lyrebirdInputBusses`` - ``AUv3Driver/configureBusMap(outputBusChannelCounts:inputBusChannelCounts:)`` - ``AUInput/Audio/init(busIndex:channel:)`` - ``lyrebirdAUDefaultOutputBusName`` - ``lyrebirdAUDefaultInputBusName`` ## Capabilities The wrapper handles MIDI input dispatch, host-audio input (effect AUs), a Mirror-driven SwiftUI parameter grid, factory presets, and source-slot preset save/restore. ### MIDI input Install a closure on ``LyrebirdAudioUnit/midiHandler`` and it will receive ``MIDIMessage`` values parsed from the AU host's `AURenderEvent` chain on the render thread. The closure must obey RT rules (no allocation, no locks). The dispatch helper itself walks the event chain linearly and parses each `.MIDI` event into a typed `MIDIMessage`: ```swift au.midiHandler = { msg, sampleTime in switch msg { case .noteOn(_, let note, let velocity): voicePool.noteOn(note: Int(note)) // configure voice from velocity, etc. case .noteOff(_, let note, _): voicePool.noteOff(note: Int(note)) default: break } } ``` For polyphonic voice-pool integration use ``LyrebirdAUVoicePoolBinding`` — pass it a `VoicePool` instance plus per-voice configure closures, and wire `au.midiHandler` to `binding.handle(message:sampleTime:)`. ### Effect AUs Override ``LyrebirdAudioUnit/lyrebirdInputChannels`` to return a positive value to declare an `aufx`-type AU with a host input bus. The synth's `build()` reads host audio via ``AUInput/Audio``: ```swift override func build() { let leftIn = AUInput.Audio(channel: 0) let rightIn = AUInput.Audio(channel: 1) let wet = FreeVerb.Audio(input: leftIn + rightIn) * $wetMix output(bus: 0, source: wet[0] + leftIn * (1 - $wetMix)) output(bus: 1, source: wet[1] + rightIn * (1 - $wetMix)) } ``` ``AUv3Driver`` pulls one engine block of input per `engine.processBlock` call via the host's `AURenderPullInputBlock` and stages it into the engine's input buses (`[numOutputChannels, numOutputChannels + numInputChannels)`). `AUInput.Audio(channel:)` reads from those buses at render time, with a defensive zero-fill if the AU was instantiated as instrument-only. ### SwiftUI parameter grid `LyrebirdUI` exposes `LyrebirdSynthView` — an auto-generated parameter-knob grid built from a `SynthesizerBase` instance by walking its Mirror for `@UIParam` slots (matches the discovery order in `SynthesizerBase._autoRegisterLiveParams()`). Pass an optional `metadata` dictionary keyed by property name to control per-knob range, unit, and display name: ```swift LyrebirdSynthView( synth: au.synthesizer, metadata: MyAUClass.parameterMetadata() ) ``` For a hand-tuned UI, override the AU view controller and skip `LyrebirdSynthView` entirely — use `ParameterKnob` / `ParameterSlider` directly. Only `@UIParam` slots appear; `@LiveParam` slots have no `@Observable` projected value and are skipped. ### Factory presets Override ``LyrebirdAudioUnit/factoryPresetsForLyrebird()`` to return a list of named ``LyrebirdAUFactoryPreset``. Each entry combines a human-readable name, a `[String: AUValue]` scalar values dictionary keyed by `@LiveParam` / `@UIParam` property name, and optional source-slot generator descriptors: ```swift public override class func factoryPresetsForLyrebird() -> [LyrebirdAUFactoryPreset] { [ .init(name: "Bright", scalarValues: ["cutoff": 4000, "amp": 0.8]), .init(name: "Quiet", scalarValues: ["cutoff": 400, "amp": 0.1]), ] } ``` The AU base class auto-bridges this into `factoryPresets` / `currentPreset` so AU hosts can show the names in their preset menu. ### Source-slot save / restore Generators wired into a `ParameterNode.source` slot persist across `fullState` save / restore + factory-preset application when the generator conforms to ``LyrebirdAUPresetCodable`` and is registered with ``LyrebirdAUPresetSourceRegistry``. The protocol surface is: ```swift public protocol LyrebirdAUPresetCodable: Generator { static var presetTypeID: String { get } var presetEncodedParameters: [String: AUValue] { get } static func presetMake(from parameters: [String: AUValue]) -> any Generator } ``` Call `LyrebirdAUPresetSourceRegistry.register(MyGenerator.self)` once per type before instantiating the AU. Unknown `presetTypeID`s in a saved state are silently skipped — the slot reverts to its scalar target. ## Out of scope | Feature | Notes | |---------|-------| | MIDI 2.0 UMP | Not yet supported; AU base reports MIDI 1.0 protocol | | Per-sample parameter ramping (`AUParameterAutomationEventType.ramp`) | Host writes apply at next sub-block boundary via standard atomic ramp | | AU view controller hand-tuned default | The default Mirror-driven grid is intentionally minimal; users override `LyrebirdSynthView` or build a custom view for any non-trivial UI | ## Topics ### Plugin Wrapper - ``LyrebirdAudioUnit`` - ``LyrebirdAudioUnitError`` - ``lyrebirdAUDefaultBlockSize`` - ``lyrebirdAUDefaultSampleRate`` - ``lyrebirdAUFullStateKey`` ### Render Bridge - ``AUv3Driver`` - ``AUv3RingBuffer`` - ``auv3DriverDefaultMaxFramesToRender`` ### Parameter Mapping - ``LyrebirdAUParameterMapping`` - ``LyrebirdAUParameterEntry`` - ``LyrebirdAUParameterInfo`` - ``lyrebirdAUParameterDefaultBound`` - ``lyrebirdAUParameterDefaultFlags`` ### MIDI + Voice Pool - ``LyrebirdAUVoicePoolBinding`` - ``midiVelocityMax`` ### Effect AU - ``AUInput`` ### Presets - ``LyrebirdAUFactoryPreset`` - ``LyrebirdAUPresetCodable`` - ``LyrebirdAUPresetSourceDescriptor`` - ``LyrebirdAUPresetSourceRegistry`` - ``lyrebirdAUFullStateScalarKey`` - ``lyrebirdAUFullStateSourcesKey`` - ``lyrebirdAUFullStateSourceTypesKey``