Files
Lyrebird/Sources/LyrebirdCLI/Resources/XcodeTemplates/LyrebirdProcess.xctemplate/___FILEBASENAME___.swift
T
2026-06-01 18:27:40 -07:00

91 lines
3.4 KiB
Swift

//___FILEHEADER___
import Foundation
import LyrebirdSynth
// MARK: - ___FILEBASENAMEASIDENTIFIER___
/// Factory for a self-contained `LyrebirdProcess`. The process allocates a
/// private route bus, spawns an envelope-driven synth that mixes from that
/// bus to `procOut`, and runs the `body` closure on an async task.
///
/// Lyrebird equivalent of SuperCollider's `ProcMod`.
///
/// ## Lifecycle
/// 1. `play(engine:)` — allocate resources, start the envelope + body task.
/// 2. While running — add synths via `ctx.add(_:)`, wait via `ctx.wait(_:)`,
/// mutate `amp` from any thread.
/// 3. `release()` — open the envelope gate; tail plays out, process auto-frees.
/// (Only meaningful when the envelope has a `releaseNode`.)
/// 4. `kill()` — force teardown regardless of envelope state.
///
/// ## Usage
/// ```swift
/// let factory = ___FILEBASENAMEASIDENTIFIER___()
/// let proc = factory.createProcess()
/// proc.play(engine: engine)
/// // …
/// proc.release() // graceful — runs the envelope tail
/// // or: proc.kill() // immediate
/// ```
public struct ___FILEBASENAMEASIDENTIFIER___: LyrebirdProcessFactory {
public init() {}
public func createProcess() -> LyrebirdProcess {
LyrebirdProcess(
id: "___FILEBASENAMEASIDENTIFIER___",
envelope: Envelope(
// 4-point envelope: attack → sustain → release → silence.
// releaseNode index 2 means "hold at level[2] until release() is called",
// then continue from there to the end.
levels: [0.0, 1.0, 0.6, 0.0],
durations: [0.05, 0.5, 1.5],
curves: [.sine, .exponential, .exponential],
releaseNode: 2
),
amp: 0.8,
ampLag: 0.02,
numChannels: 2,
procOut: 0, // hardware output; overridden by a parent EventList
target: .tail,
body: { ctx in
// The process body runs as an async Task. Schedule synths and
// wait between events. The task is cancelled on `release()` /
// `kill()` — `ctx.wait` re-throws CancellationError, which the
// surrounding LyrebirdProcess infrastructure handles cleanly.
//
// Replace the starter body below with your event sequence.
let synth: ExampleNote = ExampleNote()
ctx.add(synth)
await ctx.wait(0.1) // let the attack settle
},
onRelease: { ctx in
// Optional. Called on main thread when release() is invoked,
// before the envelope tail begins. Use to mutate / remove synths
// before the fade.
_ = ctx
},
onFinished: {
// Optional. Called on main thread after the group and route bus
// are freed. Resources are already gone — no context available.
}
)
}
}
// MARK: - Example Synthesizer used by the process body
/// Replace this stub with your own Synthesizer. The body closure above adds
/// one instance; rewrite the body to schedule whatever pattern fits your
/// process.
private final class ExampleNote: SynthesizerBase {
@LiveParam var frequency: Double = 440.0
override func build() {
let osc = OscSin.Audio(freq: $frequency)
output(bus: 0, source: osc * 0.5)
}
}