# REPL → Compile Workflow This guide walks from a live-coding sketch in the Lyrebird REPL to a fully compiled, testable Swift Package. Every step uses the `lyrebird` CLI so no manual project setup is required. ## Overview ``` lyrebird repl → prototype a patch in Swift lyrebird new → scaffold a compiled project lyrebird export → generate a Synthesizer source file (edit) → fill in the signal graph lyrebird run → compile and run swift test → verify with tests ``` Because Lyrebird has no separate scripting language, code written in the REPL (`swift build` + `swift repl` with `-I`/`-L`/`-l` flags) is already real Swift. The same `OscSin`, `Out`, and `SynthesizerBase` APIs that work in the REPL compile identically in a project. --- ## Step 1 — Prototype in the REPL Launch the REPL from the Lyrebird package directory: ```sh lyrebird repl ``` Write your synthesizer inline and iterate until it sounds right: ```swift import LyrebirdSynth 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 KickDrum: SynthesizerBase { @LiveParam var freq: LyrebirdFloat = 60.0 @LiveParam var decay: LyrebirdFloat = 0.3 override func build() { let osc = OscSin.Audio(freq: $freq) let env = EnvelopeGen.Audio(envelope: Envelope( segmentCount: 2, levels: [1.0, 1.0, 0.0], durations: [0.005, $decay] )) output(bus: 0, source: osc * env) } } let kick = KickDrum() try engine.add(kick) // Tune parameters while audio runs — no try, no string. kick.freq = 80.0 kick.decay = 0.2 ``` There is no REPL "state capture" step — you keep the code you wrote and copy it into the compiled project directly. --- ## Step 2 — Scaffold a Compiled Project When you are happy with the sketch, scaffold a new Swift Package in a directory of your choice: ```sh lyrebird new MyPiece ``` This creates: ``` MyPiece/ ├── Package.swift ← swift-tools-version 6.0, macOS 15 ├── Sources/ │ └── MyPiece/ │ └── main.swift ← engine setup + entry point └── Tests/ └── MyPieceTests/ └── MyPieceTests.swift ← starter test suite ``` --- ## Step 3 — Export a Synthesizer Template Generate a starter source file for your synthesizer: ```sh lyrebird export KickDrum --to MyPiece/Sources/MyPiece/KickDrum.swift ``` The generated file is ready to compile — it only needs the signal graph filled in with the code you wrote in the REPL: ```swift import LyrebirdSynth // MARK: - KickDrum /// A Lyrebird synthesizer — generated by `lyrebird export KickDrum` (your subclass). final class KickDrum: SynthesizerBase { // MARK: - Parameters @LiveParam var freq: LyrebirdFloat = 60.0 @LiveParam var decay: LyrebirdFloat = 0.3 // MARK: - Build override func build() { // Paste the signal graph from your REPL session here. let osc = OscSin.Audio(freq: $freq) let env = EnvelopeGen.Audio(envelope: Envelope( segmentCount: 2, levels: [1.0, 1.0, 0.0], durations: [0.005, $decay] )) output(bus: 0, source: osc * env) } } ``` You can export multiple synthesizers; each goes in its own file. All files in the same target share a module namespace so no imports are needed between them. --- ## Step 4 — Wire the Synthesizer in main.swift Open `MyPiece/Sources/MyPiece/main.swift` and instantiate your synthesizer: ```swift // MyPiece — entry point import Foundation import LyrebirdSynth let engine = SynthesisEngine( blockSize: 512, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 2, driver: CoreAudioDriver() ) let kick = KickDrum() try engine.add(kick) do { try engine.start() } catch { fatalError("Engine failed to start: \(error)") } print("MyPiece running — press Ctrl+C to stop") RunLoop.main.run() ``` --- ## Step 5 — Resolve the Lyrebird Dependency Open `MyPiece/Package.swift` and update the placeholder URL to the published Lyrebird repository: ```swift .package(url: "https://realizedsound.mooo.com/josh/Lyrebird", from: "1.0.0"), ``` Resolve the dependency graph: ```sh cd MyPiece swift package resolve ``` --- ## Step 6 — Run ```sh lyrebird run # debug build lyrebird run --release # release build (recommended for real-time audio) ``` `lyrebird run` exits with the same code as `swift run`, so CI pipelines can detect failures normally. --- ## Step 7 — Test The scaffolded `MyPieceTests.swift` provides a starter test. Add synthesizer-specific tests alongside it: ```swift // Tests/MyPieceTests/KickDrumTests.swift import Testing import LyrebirdSynth import MyPiece @Suite("KickDrum") struct KickDrumTests { @Test func outputIsNonSilentAfterOneBlock() throws { let engine = SynthesisEngine( blockSize: 512, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 2, driver: OfflineDriver() ) try engine.start() let kick = KickDrum() try engine.add(kick) engine.processBlock(inputSamples: []) #expect(engine.audioBuses[0].contains { $0 != 0.0 }) engine.stop() } } ``` Run all tests with: ```sh swift test ``` --- ## Why Code Compiles Identically There is no translation step between REPL and compiled code. - `lyrebird repl` runs `swift build`, then launches `swift repl` with module/library flags pointing at `.build/debug`, so the Lyrebird package is importable. The `import LyrebirdSynth` in the REPL and in a compiled project resolve to the same module. - `SynthesizerBase` is declared `open`, so it can be subclassed from any module — both in tests that use `@testable import LyrebirdSynth` and in standalone projects that import it as a dependency. - `@LiveParam`, generators (`OscSin`, `Out`, etc.), and the engine API are all `public` — no special REPL-only API exists. The only difference is that REPL code is interpreted and compiled code is optimised. For a release build of a real-time audio piece, `lyrebird run --release` enables full optimisations. --- ## Command Reference | Command | Description | |---|---| | `lyrebird repl` | Launch Swift REPL with Lyrebird pre-loaded | | `lyrebird new ` | Scaffold a new Swift Package | | `lyrebird export ` | Export a `SynthesizerBase` template to `.swift` | | `lyrebird export --to ` | Export to an explicit path | | `lyrebird export --to --overwrite` | Overwrite an existing file | | `lyrebird run` | Build and run the current project (debug) | | `lyrebird run --release` | Build and run in release mode | | `swift test` | Run the project's test suite | See `docs/tooling/repl.md` for REPL details and `docs/tooling/project-system.md` for the full CLI reference.