Files
Lyrebird/docs/tooling/repl.md
T
josh 560724d116 v1.3.0 release: graceful-release fix, quickstart template + demo, documentation accuracy pass
- Fixed: SynthesizerBase subclasses' release/free now dispatch correctly (gated
  synths were hard-freed instead of released).
- New: quickstart project template + LyrebirdQuickstartDemo.
- Docs: every example typechecked or provably illustrative (426->542); ~20
  wrong-API examples fixed; root docs brought under the compile guard.
- Fixed: NRT/Score demo Package.swift build from terminal again.

See RELEASES.md for details.
2026-07-17 14:05:01 -07:00

160 lines
5.6 KiB
Markdown

# REPL & Live Coding
## Launch
```sh
lyrebird repl
```
Prints a banner, runs `swift build` in the current directory, then launches `swift repl`
with `-I` / `-L` / `-l` flags pointing at the package's `.build/debug` artifacts. `import
Lyrebird` works immediately in the REPL session.
The raw command that `lyrebird repl` runs (useful for debugging or scripting):
```sh
swift build
swift repl \
-I .build/debug/Modules \
-I .build/debug/LyrebirdC.build \
-L .build/debug \
-lLyrebird
```
Run from the Lyrebird package directory itself, or from any project whose `Package.swift`
declares Lyrebird as a dependency — SPM resolves transitive deps during `swift build`
and stages them under the same `.build/debug` layout.
```sh
lyrebird # enter the lyrebird> interactive shell (LineNoise-powered)
lyrebird --help # usage help
lyrebird --version
```
## Interactive Shell (`lyrebird>`)
Running `lyrebird` with no arguments enters a LineNoise-powered command shell:
```
Lyrebird 0.1.0 — Swift audio synthesis
Type 'help' for available commands, or 'repl' to start a Swift REPL session.
Commands:
new <name> Scaffold a new Lyrebird Swift Package
run [--release] Build and run the current project
export <name> [--to p] Export a Synthesizer template to a .swift file
repl Launch Swift REPL with Lyrebird available
version Print lyrebird version
help Show this help
quit Exit (also Ctrl+D)
lyrebird> repl
```
Features:
- Arrow-key line editing (linenoise C library, pure Swift wrapper)
- Tab completion for all subcommands
- Persistent command history — saved to `~/.lyrebird_history` on exit
- Shell verbs are case-insensitive (`NEW`, `Run`, `EXPORT` all work);
argument values such as project/synth names preserve the user's original casing
## Basic REPL Session
Once inside the Swift REPL:
```swift
import LyrebirdSynth
// Create and start the engine
let engine = SynthesisEngine(
blockSize: 512, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 2,
driver: CoreAudioDriver()
)
try engine.start()
// Example — your synthesizer, defined by subclassing SynthesizerBase
final class SineTone: SynthesizerBase {
@LiveParam var freq: Double = 440.0
@LiveParam var amp: Double = 0.3
override func build() {
let osc = OscSin.Audio(freq: $freq)
output(bus: 0, source: osc * $amp)
}
}
// Add to engine — audio starts immediately
let tone = SineTone()
try engine.add(tone)
// Adjust while running — no try, no string
tone.freq = 880.0
// Remove
engine.remove(tone)
```
## Threading While Audio Runs
- REPL input and graph mutation: **main thread** (the `swift repl` process)
- Audio render callback: **RT thread** (Core Audio)
- `@LiveParam` assignments → `Atomic<UInt64>` store → RT acquiring load, per-sample ramp
- `engine.add` / `engine.remove` → `SPSCQueue` → applied at next block boundary
Audio never stops during node add/remove — the lock-free scheduling mechanism handles it.
## History
Command history for the `lyrebird>` interactive shell is persisted to `~/.lyrebird_history`
and shared across all shell sessions. The path is exposed as
`REPLLauncher.historyFilePath` and mirrored by `InteractiveShell.historyFilePath`.
## Implementation Notes
### `lyrebird repl` — `swift build` + `swift repl` with flags
Older versions of SPM shipped a `swift package repl` subcommand that resolved the whole
dependency graph and dropped you into an `import`-ready REPL with no flags. That
subcommand has been removed from the current Swift toolchain — running it now errors with
`error: Unknown subcommand or plugin name 'repl'`.
`REPLLauncher` replaces it with the canonical two-step invocation:
1. `swift build` — produces `.build/debug/Modules/LyrebirdSynth.swiftmodule`,
`.build/debug/libLyrebird.dylib`, and `.build/debug/LyrebirdC.build/module.modulemap`.
If the build fails, the REPL is not started and the user sees the compiler output
directly (stdout/stderr are inherited).
2. `swift repl -I .build/debug/Modules -I .build/debug/LyrebirdC.build -L .build/debug
-lLyrebird` — the same flags SPM used to compute internally. `-I` points the Swift
driver at the staged `.swiftmodule` and the LyrebirdC C-module modulemap; `-L` /
`-lLyrebird` links the dynamic library so `import LyrebirdSynth` succeeds and runtime
symbols resolve through the dylib's `@rpath` install name.
Run from the Lyrebird package directory itself, or from any project directory whose
`Package.swift` declares Lyrebird as a dependency — both produce the same `.build/debug`
layout after `swift build`.
### LineNoise
`LyrebirdCLI` depends on `andybest/linenoise-swift` (LineNoise 0.0.3). The interactive
shell (`InteractiveShell.run()`) owns the LineNoise instance and manages:
- History load/save (`~/.lyrebird_history`)
- Tab completion callback (prefix-matched subcommands)
The `swift repl` session that fires on `repl` has its own separate REPL history
managed by LLDB/Swift (typically in `~/.lldb/`).
### `CLICommand` parsing
`CLICommand.parse(_:)` is the canonical argument parser for the `lyrebird` binary.
`InteractiveShell.command(from:)` is its shell-line equivalent. Both apply the same
rules:
- Verb is lowercased before matching (case-insensitive commands)
- Positional name arguments are guarded against flag tokens (`hasPrefix("-")`)
- `--to` value is guarded against consuming a subsequent flag as a path
- `--overwrite` is parsed and threaded through to `SynthExporter.export`
See `docs/tooling/project-system.md` for the full `lyrebird new / run / export`
reference.