80 lines
3.8 KiB
Swift
80 lines
3.8 KiB
Swift
// Synthesizer.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
/// Synthesizer — a node in the engine's signal graph that produces or processes audio.
|
|
///
|
|
/// Conform to this protocol (or subclass SynthesizerBase) to create an instrument or effect.
|
|
/// The engine calls build() once when the Synthesizer is added to the graph, then calls
|
|
/// evaluate(graph:count:) each block segment during audio rendering.
|
|
///
|
|
/// The default evaluate() implementation is a no-op. Most users subclass SynthesizerBase,
|
|
/// which wires evaluate() to a set of registered output generators.
|
|
///
|
|
/// ## Actor isolation
|
|
///
|
|
/// The language-side surface (`build`, `release`, `free`) is `@MainActor`-isolated so a
|
|
/// consumer building under Xcode 26's default `SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor`
|
|
/// can subclass `SynthesizerBase` and override `build()` with **no** concurrency
|
|
/// annotations — the consumer's MainActor default now *matches* the base instead of
|
|
/// conflicting with a nonisolated override. The single RT-thread entry point, `evaluate`,
|
|
/// is explicitly `nonisolated` so the engine's render callback reaches it with no actor
|
|
/// hop. See `ARCHITECTURE.md` §9.
|
|
///
|
|
/// Isolation is declared **per-requirement** rather than at the protocol level on purpose:
|
|
/// the consumer guarantee comes from `SynthesizerBase` being a `@MainActor` class (so its
|
|
/// subclasses inherit MainActor), while the engine-internal `Group` / `ParallelGroup`
|
|
/// conformers stay fully nonisolated — a whole-type `@MainActor` protocol would infer
|
|
/// MainActor onto their RT-owned linked-list / snapshot state and break the render path.
|
|
public protocol Synthesizer: AnyObject {
|
|
|
|
/// Configure the internal generator DAG. Called once by the engine when this Synthesizer
|
|
/// is added to the node tree. Allocate all generators here — not during evaluate.
|
|
///
|
|
/// `@MainActor` — called once on the main thread when the synth is added.
|
|
@MainActor func build()
|
|
|
|
/// Evaluate one block segment. The engine calls this once per segment (normally once per
|
|
/// block; may be called multiple times if scheduled events split the block).
|
|
///
|
|
/// `nonisolated` — the RT render thread's only entry point into a Synthesizer. Must not
|
|
/// hop actors. See the type-level isolation note.
|
|
/// - Parameters:
|
|
/// - graph: The current SynthGraph context (engine + busOffset).
|
|
/// - count: Number of samples to produce for this segment.
|
|
nonisolated func evaluate(graph: SynthGraph, count: Int)
|
|
|
|
/// Begin a graceful release of this synthesizer (e.g., set gate → 0).
|
|
///
|
|
/// Synthesizers that support gated release should override this to post a `gate = 0.0`
|
|
/// parameter update. The default throws `SynthesizerError.noGate`.
|
|
///
|
|
/// Language-side only — never call from the RT audio thread.
|
|
@MainActor func release(engine: SynthesisEngine) throws
|
|
|
|
/// Immediately remove this synthesizer from the node tree.
|
|
///
|
|
/// Posts a remove event at the current sample time. The synthesizer stops evaluating
|
|
/// as soon as the RT thread applies the event (next block boundary or segment split).
|
|
///
|
|
/// Language-side only — never call from the RT audio thread.
|
|
@MainActor func free(engine: SynthesisEngine)
|
|
}
|
|
|
|
extension Synthesizer {
|
|
/// Default: no-op. SynthesizerBase overrides this to drive registered output generators.
|
|
nonisolated public func evaluate(graph: SynthGraph, count: Int) {}
|
|
|
|
/// Default: throws `.noGate`. Override in synthesizers with EnvelopeGen gate inputs.
|
|
@MainActor public func release(engine: SynthesisEngine) throws {
|
|
throw SynthesizerError.noGate
|
|
}
|
|
|
|
/// Default: posts an immediate remove event via the engine's scheduling queue.
|
|
@MainActor public func free(engine: SynthesisEngine) {
|
|
engine.remove(self)
|
|
}
|
|
}
|