v1.4.0 release: live-coding scratch editor, interpreted Graph DSL, ClosureSynthesizer + SampleLoop
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
// LiveSessionGraphDSL.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// LyrebirdLiveCompile
|
||||
//
|
||||
// The editor's instant, no-swiftc evaluation path. `runGraphDSL` lexes, parses,
|
||||
// and builds interpreted graph-DSL source into `ClosureSynthesizer`s and plays
|
||||
// them through the SAME tracking + named-slot machinery `compileAndRun` uses, so
|
||||
// non-additive re-evaluation, slot hot-swap, and `resetAll()` behave identically
|
||||
// across the Swift and DSL paths. The only difference is speed: a graph edit
|
||||
// re-evaluates in microseconds instead of a swiftc round-trip.
|
||||
|
||||
#if os(macOS)
|
||||
import LyrebirdSynth
|
||||
import LyrebirdGraphDSLEmbedded
|
||||
|
||||
extension LiveSession {
|
||||
|
||||
/// Evaluate interpreted graph-DSL `source` and play each statement.
|
||||
///
|
||||
/// Mirrors ``compileAndRun(_:additive:)`` but with no compiler: the source is
|
||||
/// interpreted in-process. Each statement becomes one output —
|
||||
///
|
||||
/// - an anonymous statement (`OscSin(freq: 440) * 0.1`) is added to the
|
||||
/// engine and tracked, so a later non-additive run removes it;
|
||||
/// - a named statement (`bass = OscSawtooth(freq: 55) * 0.2`) is installed into
|
||||
/// the matching named slot via ``swapSlot(_:synth:)`` — a persistent,
|
||||
/// hot-swappable voice that survives a non-additive re-evaluation and is
|
||||
/// replaced in place on the next run.
|
||||
///
|
||||
/// The whole program is lexed/parsed/built **before** any engine mutation, so
|
||||
/// a syntax or build error leaves the currently-playing graph untouched — a
|
||||
/// *malformed* edit never drops audio. Once the program is valid, playback is
|
||||
/// best-effort per statement: an engine-level rejection of one statement
|
||||
/// (e.g. `.pendingQueueFull`) is collected and surfaced but does not abort
|
||||
/// the others, matching ``SnippetContext/play(_:)``.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - source: DSL program text (one statement per line).
|
||||
/// - additive: When `false` (default), removes every previously tracked
|
||||
/// anonymous synth before playing (named slots persist). When `true`,
|
||||
/// leaves prior synths in place.
|
||||
/// - resources: Host resource table resolving bare-identifier references
|
||||
/// (e.g. buffers). Defaults to an empty table.
|
||||
/// - Throws: ``LiveSessionError/graphDSLFailed(diagnostics:underlying:)`` with
|
||||
/// editor-ready diagnostics for a lex/parse/build failure (before any
|
||||
/// mutation), or for one or more per-statement engine rejections (after the
|
||||
/// surviving statements have gone live).
|
||||
@MainActor
|
||||
public func runGraphDSL(
|
||||
_ source: String,
|
||||
additive: Bool = false,
|
||||
resources: DSLResourceResolver = DSLEmptyResources()
|
||||
) throws {
|
||||
// Build everything first — no engine mutation until we know the whole
|
||||
// program is valid.
|
||||
let outputs: [DSLOutput]
|
||||
do {
|
||||
outputs = try DSLInterpreter.evaluate(source, resources: resources)
|
||||
} catch {
|
||||
throw LiveSessionError.graphDSLFailed(
|
||||
diagnostics: GraphDSLDiagnosticMapper.diagnostics(for: error, source: source),
|
||||
underlying: error
|
||||
)
|
||||
}
|
||||
|
||||
if !additive { clearTracked() }
|
||||
|
||||
// Play every statement. An engine-level rejection of ONE statement
|
||||
// (e.g. `.pendingQueueFull`) must not abort the rest — the others still
|
||||
// go live, mirroring `SnippetContext.play`'s per-statement best-effort
|
||||
// behavior. Failures are collected and surfaced together; the graphs
|
||||
// that added successfully keep playing.
|
||||
var failures: [Diagnostic] = []
|
||||
var firstError: Error?
|
||||
for output: DSLOutput in outputs {
|
||||
let synth: ClosureSynthesizer = ClosureSynthesizer {
|
||||
// The graph is already built (eagerly, by the interpreter); the
|
||||
// closure just hands it to `build()`. Each `DSLOutput.graph` is
|
||||
// owned by exactly one synth, so this never shares a graph.
|
||||
output.graph
|
||||
}
|
||||
do {
|
||||
if let slot: String = output.slot {
|
||||
try swapSlot(slot, synth: synth)
|
||||
} else {
|
||||
try engine.add(synth)
|
||||
track(synth)
|
||||
}
|
||||
} catch {
|
||||
if firstError == nil { firstError = error }
|
||||
failures.append(contentsOf: GraphDSLDiagnosticMapper.diagnostics(for: error, source: source))
|
||||
}
|
||||
}
|
||||
if let firstError: Error = firstError {
|
||||
throw LiveSessionError.graphDSLFailed(diagnostics: failures, underlying: firstError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user