3.0 KiB
Live coding with LyrebirdScratchView
A SwiftUI scratchpad for hot-swapping snippets through LyrebirdLiveCompile.
Overview
LyrebirdScratchView pairs an NSTextView-backed Swift editor with a LiveSession
that owns the audio engine. Pressing ⌘↩ wraps the snippet body in the standard
harness, shells out to swiftc, dlopens the resulting dylib, and calls the
snippet's lyrebird_snippet_entry with a SnippetContext bound to the session.
Synthesizers played via context.play(_:) are tracked, so the next non-additive
run replaces them. ⌘⇧↩ layers a new snippet on top of the prior ones.
Compile diagnostics from a failing snippet are parsed by DiagnosticParser
and rendered beneath the editor with line numbers already remapped from the wrapped
source back to the user's snippet coordinates.
The view is macOS-only — LyrebirdLiveCompile shells out to swiftc, which is
unavailable on iOS. The nested LiveCompile/ sub-package declares
platforms: [.macOS(.v15)] and cannot be added to an iOS target.
Hosting
import LyrebirdSynth
import LyrebirdUI // LyrebirdWindow
import LyrebirdLiveCompile // LiveSession
import LyrebirdLiveCompileUI // LyrebirdScratchView
let engine = SynthesisEngine(/* … */)
try engine.start()
let session = LiveSession(engine: engine)
let window = LyrebirdWindow(title: "Lyrebird Scratch", width: 640, height: 480)
window.open {
LyrebirdScratchView(session: session)
}
Keyboard shortcuts
| Shortcut | Action |
|---|---|
| ⌘↩ | Compile + run. Removes synths from the previous run before executing. |
| ⌘⇧↩ | Compile + run additively. Keeps previously played synths in the engine. |
The toolbar exposes both as buttons plus a Clear Synths button that calls
LiveSession.clearTracked() directly.
Snippet API surface
A snippet has one local in scope:
context: SnippetContext
SnippetContext is intentionally minimal in v1:
context.engine— the hostSynthesisEngine.context.play(_:)— add aSynthesizerto the engine and register it for cleanup on the next non-additive run.context.reportMessage(_:)— write back to the host (used by tests; handy forprint-style probes during development).
Threading
All UI is @MainActor-isolated. LiveSession.compileAndRun is invoked
synchronously inside the button action, so the editor freezes for the duration
of the swiftc invocation (≈ 1 second cold, much faster on warm cache). v1
accepts that trade-off; an off-main compile path is on the polish list.
Limitations
- macOS only.
- No syntax highlighting, code completion, or line-number gutter (deferred).
- Compiled dylibs are leaked by design — types in a loaded snippet may still be referenced by tracked synths; unloading is unsafe.
- The host
.appmust not be sandboxed and must allow library validation to be disabled, sinceswiftcis invoked viaProcessand the resulting unsigned dylib isdlopen'd.
Topics
LyrebirdScratchViewLyrebirdScratchModel