v1.5.0 release: LyrebirdRandom + Live Scratch editor

LyrebirdRandom scalar distribution sampling (seedable value type + shared
Random) with DistributionKind.sample dispatcher. Live Scratch editor: rename to
LyrebirdScratch, App Support artifact staging, Format Code (⌥⌘F), Render to File
(⌥⌘R offline CAF), line-number gutter, manual completion, Foundation in snippets,
scroll-jump fix, scheduling/NRT docs.
This commit is contained in:
2026-07-22 07:53:12 -07:00
parent 8519cabe42
commit 62b2f5dbec
43 changed files with 2249 additions and 376 deletions
@@ -17,7 +17,7 @@ import Foundation
import CoreGraphics
import LyrebirdSynth
// Re-exported so consumers that link only the `LyrebirdLiveCompileUI` product
// (e.g. the LyrebirdScratchDemo app) get the `LyrebirdLiveCompile` API
// (e.g. the LyrebirdScratch app) get the `LyrebirdLiveCompile` API
// (`LiveSession`, …) without separately linking the `LyrebirdLiveCompile`
// dynamic product — which clashes in Xcode with the static target embed.
@_exported import LyrebirdLiveCompile
@@ -516,6 +516,103 @@ public final class LyrebirdScratchModel: ObservableObject {
///
/// No-op while a compile is in flight (matching the run methods' guard) — a
/// reset must not tear the session down under an active `compileAndRunAsync`.
/// Pretty-print the buffer with `swift-format` (off the main thread),
/// replacing the text on success. Logs the outcome to the console; on a
/// parse error or missing toolchain the buffer is left untouched.
public func formatBuffer() async {
guard !isCompiling else { return }
let source: String = text
guard !LyrebirdScratchModel.isBlank(source) else { return }
// Hold the busy flag across the subprocess round-trip so a second Format
// (or a Run) can't overlap, and re-check the buffer before applying: the
// user may keep typing while `swift-format` runs, and a result computed
// from the now-stale `source` must not clobber those edits. `reevalState`
// goes `.compiling` too so the Run/Render menu items (validated on
// `reevalState.canEvaluate`) actually disable during the format.
isCompiling = true
reevalState = .compiling
defer {
isCompiling = false
reevalState = derivedIdleState()
}
do {
let formatted: String = try await Task.detached(priority: .userInitiated) {
try SwiftFormatter.format(source: source)
}.value
guard text == source else {
appendConsole(.error, "Buffer changed during formatting — result discarded.")
return
}
if formatted != source {
text = formatted
}
appendConsole(.info, "Formatted.")
} catch {
appendConsole(.error, "Format failed: \(error)")
}
}
/// Render the current selection (or the whole buffer when nothing is
/// selected) to a CAF audio file, offline. Builds the snippet graph on a
/// throwaway engine driven by an `NRTDriver` — matching the live engine's
/// sample rate / channel count / bus layout — then renders `duration`
/// seconds off the main thread (`NRTDriver.start()` blocks). Logs the
/// outcome to the console.
/// - Returns: `true` when the file was rendered (so the caller can reveal it).
@discardableResult
public func renderToFile(url: URL, duration: Double) async -> Bool {
guard !isCompiling else { return false }
guard duration > 0, duration.isFinite else {
appendConsole(.error, "Render duration must be a positive number of seconds.")
return false
}
let code: String = hasSelection ? selectionText : text
guard !LyrebirdScratchModel.isBlank(code) else {
appendConsole(.error, "Nothing to render.")
return false
}
// Gate the Run/Render menu items (validated on `reevalState.canEvaluate`)
// and any re-entrancy for the render's duration.
isCompiling = true
reevalState = .compiling
defer {
isCompiling = false
reevalState = derivedIdleState()
}
let live: SynthesisEngine = session.engine
let offline: SynthesisEngine = SynthesisEngine(
blockSize: live.blockSize,
controlBlockSize: live.controlBlockSize,
sampleRate: live.sampleRate,
numOutputChannels: live.numOutputChannels,
numInputChannels: 0,
numAudioBuses: live.numAudioBuses,
numControlBuses: live.numControlBuses,
driver: NRTDriver(outputURL: url, duration: duration)
)
let offlineSession: LiveSession = LiveSession(engine: offline, attachScope: false)
do {
// Build the graph on the offline engine (compile off-main, add on main).
try await offlineSession.compileAndRunAsync(code, additive: true)
// Render synchronously, off the main thread; NRTDriver writes the CAF.
// `stop()` runs inside the detached task (where `offline` lives): the
// driver returns with the engine still `isRunning`, and only `stop()`
// drains the node tree — without it every rendered graph leaks
// (matches EngineHost's NRT job teardown). Keeping it here also avoids
// a cross-actor use of the non-Sendable engine after the detached run.
try await Task.detached(priority: .userInitiated) {
defer { offline.stop() }
try offline.start()
}.value
appendConsole(.info, "Rendered \(String(format: "%.2f", duration)) s → \(url.lastPathComponent)")
return true
} catch {
appendConsole(.error, "Render failed: \(error)")
return false
}
}
public func resetSession() {
guard !isCompiling else { return }
session.resetAll()