363 lines
17 KiB
Markdown
363 lines
17 KiB
Markdown
# Lyrebird — Installation & Usage
|
|
|
|
How to add Lyrebird to a project and scaffold the five kinds of app it supports: real-time, real-time + UI, non-real-time (offline) render, AUv3 plugin, and iOS.
|
|
|
|
The package is hosted at <https://realizedsound.mooo.com/josh/Lyrebird>.
|
|
|
|
---
|
|
|
|
## Requirements
|
|
|
|
| Platform | OS | Xcode | Swift |
|
|
|---|---|---|---|
|
|
| macOS | 15+ | 16+ | 6.0 |
|
|
| iOS | 18+ | 16+ | 6.0 |
|
|
|
|
The macOS 15 / iOS 18 floor is hard: Lyrebird's lock-free queues use `Synchronization.Atomic`, which is unavailable earlier.
|
|
|
|
---
|
|
|
|
## Adding the package
|
|
|
|
### Swift Package Manager (`Package.swift`)
|
|
|
|
```swift
|
|
// swift-tools-version: 6.0
|
|
import PackageDescription
|
|
|
|
let package = Package(
|
|
name: "MyPiece",
|
|
platforms: [.macOS(.v15), .iOS(.v18)],
|
|
dependencies: [
|
|
.package(url: "https://realizedsound.mooo.com/josh/Lyrebird", from: "1.0.0"),
|
|
],
|
|
targets: [
|
|
.executableTarget(
|
|
name: "MyPiece",
|
|
dependencies: [
|
|
.product(name: "Lyrebird", package: "Lyrebird"),
|
|
.product(name: "LyrebirdUI", package: "Lyrebird"), // only if you need the GUI module
|
|
]),
|
|
]
|
|
)
|
|
```
|
|
|
|
The package vends three products:
|
|
|
|
| Product | Kind | Use |
|
|
|---|---|---|
|
|
| `Lyrebird` | library (static) | Core framework — Generators, Synthesizers, engine, drivers. |
|
|
| `LyrebirdUI` | library | SwiftUI parameter controls (`ParameterKnob`, scope/envelope views, `LyrebirdWindow`). |
|
|
| `LyrebirdCLI` | executable | The `lyrebird` command-line tool (scaffolding, REPL). |
|
|
|
|
### In Xcode
|
|
|
|
**File ▸ Add Package Dependencies…**, paste the repository URL, choose **Up to Next Major Version** from `1.0.0`, and add the `Lyrebird` (and optionally `LyrebirdUI`) products to your target.
|
|
|
|
### Local checkout (development)
|
|
|
|
To build against a local clone instead of the remote, use a path dependency:
|
|
|
|
```swift
|
|
.package(name: "Lyrebird", path: "/Users/me/Documents/LyrebirdII")
|
|
```
|
|
|
|
The scaffolder (below) does this automatically with `--lyrebird-path`.
|
|
|
|
### Linkage note
|
|
|
|
The consumer-facing `Lyrebird` product uses **automatic linkage** — SwiftPM/Xcode link it **statically** into your target. No dynamic framework is produced, so adding Lyrebird needs **no special build settings**: no `disable-library-validation` entitlement, no `SWIFT_USE_INTEGRATED_DRIVER` override. It just links.
|
|
|
|
> Internally the package also vends a second product, `LyrebirdDynamic` (`type: .dynamic`), used **only** by the bundled `LiveCompile/` live-coding sub-package — it needs a shared dylib so live-compiled snippets and the host resolve to one copy of every class. You never reference `LyrebirdDynamic` as an external consumer; ignore it.
|
|
|
|
---
|
|
|
|
## The `lyrebird` CLI
|
|
|
|
Project scaffolding goes through the `lyrebird` CLI. Install it once onto your `$PATH`:
|
|
|
|
```bash
|
|
swift build -c release --product LyrebirdCLI
|
|
cp .build/release/LyrebirdCLI /usr/local/bin/lyrebird
|
|
lyrebird --version # verify
|
|
```
|
|
|
|
(From a checkout you can also use `swift run lyrebird …` without installing.)
|
|
|
|
| Command | Purpose |
|
|
|---|---|
|
|
| `lyrebird new <name> [--template <t>] [--lyrebird-path <p>]` | Scaffold a new project. |
|
|
| `lyrebird run [--release]` | Build and run the project in the current directory. |
|
|
| `lyrebird export <name> [--to <p>] [--overwrite]` | Emit a standalone `SynthesizerBase` template file. |
|
|
| `lyrebird install-templates [--force]` | Install the Xcode file templates (New File… ▸ Lyrebird). |
|
|
| `lyrebird uninstall-templates` | Remove them. |
|
|
| `lyrebird repl` | Swift REPL with `import LyrebirdSynth` available. |
|
|
| `lyrebird` (no args) | Interactive `lyrebird>` shell. |
|
|
|
|
Full reference: [`docs/tooling/cli-guide.md`](docs/tooling/cli-guide.md).
|
|
|
|
---
|
|
|
|
## Generating projects
|
|
|
|
`lyrebird new <name> --template <template>` is the single entry point. The templates fall into two groups: **SPM-only** (a `Package.swift` + an `.xcworkspace` that references it — open and run with `swift`/Xcode, no `.xcodeproj`) and **Xcode-project** (ship a real `.xcodeproj`, needed for `.app`/`.appex` bundles, code signing, and iOS).
|
|
|
|
| Template | Group | What you get | Driver |
|
|
|---|---|---|---|
|
|
| `basic` *(default)* | SPM-only | Minimal engine + `RunLoop.main.run()`. | `CoreAudioDriver` |
|
|
| `demo` | SPM-only | A `SineTone` synth with `@LiveParam` frequency/amplitude. | `CoreAudioDriver` |
|
|
| `ui-demo` | SPM-only | AppKit window with `@UIParam` knobs + `LyrebirdWindow`. | `CoreAudioDriver` |
|
|
| `nrt` | SPM-only | Renders to `output.caf`, no hardware needed. | `NRTDriver` |
|
|
| `score` | SPM-only | Authors a `Score` with the notation DSL, exports `score.musicxml` (round-trips it), then renders the events offline to `.caf`. | `NRTDriver` |
|
|
| `macos-ui-xcode` | Xcode-project | macOS `.app` bundle (AppKit + SwiftUI), hardened runtime. | `CoreAudioDriver` |
|
|
| `ios-ui` | Xcode-project | iOS `.app` (SwiftUI), with `AVAudioSession` activation. | `iOSAudioDriver` (RemoteIO) |
|
|
| `auv3` | Xcode-project | AUv3 host `.app` + `aumu` `.appex` subclassing `LyrebirdAudioUnit`. | `AUv3Driver` |
|
|
|
|
After scaffolding:
|
|
|
|
```bash
|
|
open <name>/<name>.xcworkspace # SPM-only & Xcode-project templates both ship a workspace
|
|
# or, for the SPM-only ones:
|
|
cd <name> && lyrebird run
|
|
```
|
|
|
|
**Lyrebird dependency, default behaviour.** When run from a Lyrebird source checkout (e.g. `swift run LyrebirdCLI new …`), the scaffolder pins the generated project to *that same checkout* as a `path:` dependency (and an `XCLocalSwiftPackageReference` in Xcode-bearing templates). This keeps the emitted template source and the Lyrebird it builds against in lockstep — pre-1.0-stable, the published tag lags unreleased Score/Generator API. The CLI prints the chosen dependency, e.g. `Lyrebird dependency: local checkout at /Users/me/Documents/LyrebirdII`. If no local checkout is derivable (an installed binary with no source tree), it falls back to the published remote package and warns.
|
|
|
|
Override the checkout explicitly with `--lyrebird-path`:
|
|
|
|
```bash
|
|
lyrebird new <name> --template <t> --lyrebird-path /Users/me/Documents/LyrebirdII
|
|
```
|
|
|
|
### 1. Real-time
|
|
|
|
```bash
|
|
lyrebird new MyPiece --template demo
|
|
cd MyPiece && lyrebird run
|
|
```
|
|
|
|
`main.swift` boots a `CoreAudioDriver`, starts the engine, and adds a synth. The shape is:
|
|
|
|
```swift
|
|
import LyrebirdSynth
|
|
|
|
let engine = SynthesisEngine(
|
|
blockSize: 512,
|
|
sampleRate: 48_000,
|
|
numOutputChannels: 2,
|
|
numInputChannels: 0,
|
|
driver: CoreAudioDriver())
|
|
|
|
try engine.start()
|
|
try engine.add(SineTone())
|
|
RunLoop.main.run() // keep the process alive while audio runs
|
|
```
|
|
|
|
### 2. Real-time with UI
|
|
|
|
```bash
|
|
# SPM-only AppKit window:
|
|
lyrebird new MyApp --template ui-demo
|
|
# or a full macOS .app bundle:
|
|
lyrebird new MyApp --template macos-ui-xcode
|
|
```
|
|
|
|
UI templates depend on `LyrebirdUI` and drive parameters with `@UIParam` (SwiftUI-bindable) instead of `@LiveParam`. A knob binds straight to the projected parameter:
|
|
|
|
```swift
|
|
import SwiftUI
|
|
import LyrebirdSynth
|
|
import LyrebirdUI
|
|
|
|
struct ContentView: View {
|
|
@StateObject var synth = ToneSynth() // ToneSynth: SynthesizerBase with @UIParam
|
|
var body: some View {
|
|
ParameterKnob(value: synth.$frequency, in: 50...2000, label: "Freq")
|
|
}
|
|
}
|
|
```
|
|
|
|
### 3. Non-real-time (offline render)
|
|
|
|
```bash
|
|
lyrebird new MyRender --template nrt
|
|
cd MyRender && lyrebird run # writes output.caf
|
|
```
|
|
|
|
Identical graph code as real-time — only the driver changes. `NRTDriver` runs `processBlock` in a tight loop (faster than real time) and writes a CAF file:
|
|
|
|
```swift
|
|
let driver = NRTDriver(outputURL: outputURL, duration: totalDuration)
|
|
let engine = SynthesisEngine(
|
|
blockSize: 512,
|
|
sampleRate: 48_000,
|
|
numOutputChannels: 2,
|
|
numInputChannels: 0,
|
|
driver: driver)
|
|
|
|
try engine.schedule(FMTone(carrierFreq: 440), after: 0.0)
|
|
try engine.schedule(FMTone(carrierFreq: 660), after: 2.5) // sample-accurate
|
|
try engine.start() // blocks until `duration` is rendered, then closes the file
|
|
```
|
|
|
|
For batch offline rendering across cores, see `EngineHost.renderConcurrent(jobs:maxConcurrent:)` in [`docs/tooling/nrt-guide.md`](docs/tooling/nrt-guide.md).
|
|
|
|
### 4. AU plugin (AUv3)
|
|
|
|
```bash
|
|
lyrebird new MyPlugin --template auv3
|
|
open MyPlugin/MyPlugin.xcworkspace
|
|
```
|
|
|
|
This generates a host `.app` plus an `aumu` `.appex` extension. Run the host app once to register the extension with the system; it then loads in Logic, GarageBand, AUM, Loopy Pro, etc.
|
|
|
|
Your DSP is an ordinary `SynthesizerBase`; you expose it by subclassing `LyrebirdAudioUnit` and declaring parameter metadata:
|
|
|
|
```swift
|
|
import LyrebirdSynth
|
|
|
|
final class MyFMSynth: SynthesizerBase {
|
|
@LiveParam var carrierFreq: Double = 440
|
|
@LiveParam var modIndex: Double = 0.5
|
|
|
|
override func build() {
|
|
let mod = OscSin.Audio(freq: $carrierFreq * 2, mul: $modIndex * $carrierFreq)
|
|
let car = OscSin.Audio(freq: $carrierFreq + mod)
|
|
output(bus: 0, source: car * 0.3)
|
|
}
|
|
}
|
|
|
|
public final class MyFMSynthAudioUnit: LyrebirdAudioUnit {
|
|
public override class func makeSynthesizer() -> SynthesizerBase { MyFMSynth() }
|
|
public override class func parameterMetadata() -> [String: LyrebirdAUParameterInfo] {
|
|
[
|
|
"carrierFreq": .init(min: 20, max: 8000, unit: .hertz),
|
|
"modIndex": .init(min: 0, max: 4),
|
|
]
|
|
}
|
|
}
|
|
```
|
|
|
|
`LyrebirdAudioUnit` provides the parameter tree, bus topology, the host-driven `internalRenderBlock` (via the passive `AUv3Driver` + `AUv3RingBuffer` fixed-block→variable-frame adapter), `fullState` preset save/restore, MIDI dispatch, and an optional `LyrebirdSynthView` SwiftUI parameter grid. Full guide: `Sources/Lyrebird/Lyrebird.docc/AUv3.md`. The reference plugin is [`Demos/LyrebirdAUv3Demo`](Demos/LyrebirdAUv3Demo/).
|
|
|
|
### 5. iOS
|
|
|
|
```bash
|
|
lyrebird new MyApp --template ios-ui
|
|
open MyApp/MyApp.xcworkspace # set your signing team, then run on device/simulator
|
|
```
|
|
|
|
The iOS template uses `iOSAudioDriver` (RemoteIO) and activates an `AVAudioSession` at launch; the synth/UI code is otherwise identical to the macOS UI template. The reference app is [`Demos/LyrebirdiOSDemo`](Demos/LyrebirdiOSDemo/).
|
|
|
|
### 6. Score / notation (MusicXML + offline render)
|
|
|
|
```bash
|
|
lyrebird new MyScore --template score # aliases: score-musicxml, musicxml
|
|
cd MyScore && lyrebird run # writes score.musicxml, round-trips it, renders .caf
|
|
```
|
|
|
|
The `score` template authors music in the language-side **Score DSL** — a result-builder of `Note` / `Rest` / `Chord` / `Tuplet` with exact rational tuplet math (no float drift across nested ratios), spelling-aware pitches (`Note(.named(.c, 5))`), dynamics, and articulations. It exports MusicXML, parses it back for a round-trip sanity check, then schedules one self-freeing voice per `ScoreEvent` against an `NRTDriver`:
|
|
|
|
```swift
|
|
import LyrebirdSynth
|
|
|
|
let score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
|
Part("lead") {
|
|
Note(.named(.c, 5), dur: .quarter, dyn: .mf, art: .staccato)
|
|
Note(.named(.e, 5), dur: .eighth.dotted, dyn: .mf)
|
|
Rest(dur: .eighth)
|
|
Tuplet(3, inTimeOf: 2) {
|
|
Note(.named(.g, 5), dur: .quarter)
|
|
Note(.named(.a, 5), dur: .quarter)
|
|
Note(.named(.b, 5), dur: .quarter)
|
|
}
|
|
Chord([.named(.c, 5), .named(.e, 5), .named(.g, 5)], dur: .half, dyn: .f, art: .tenuto)
|
|
}
|
|
Part("bass", channel: 1, clef: .bass) {
|
|
Note(.named(.c, 3), dur: .whole, dyn: .mp)
|
|
}
|
|
}
|
|
|
|
let xml = score.toMusicXMLString() // export
|
|
let restored = try Score.fromMusicXML(data: score.toMusicXMLData()) // round-trip
|
|
for event in score.allEvents { // flat, absolute-beat events (rests excluded)
|
|
let hz = event.pitch.frequency ?? 0
|
|
// schedule a voice at event.beat * secondsPerBeat for event.performedDuration …
|
|
}
|
|
```
|
|
|
|
Notable score features:
|
|
|
|
- **Notation model** — per-part `Clef` (`.auto` infers from pitch range), `KeySignature` (score-level + per-part override), `TempoChange` (measure-indexed), `RepeatStructure` / volta `Ending`, transposing-instrument `Transpose`, grace notes, chord ties, and nested tuplet brackets. These are notation-only — `flatten()` / playback use the base `bpm`.
|
|
- **MusicXML round-trip** — `score.toMusicXMLString()` / `.toMusicXMLData()` out, `Score.fromMusicXML(data:)` back. Spelling (letter + accidental, microtonal nine-step grid) is preserved.
|
|
- **Sounding vs. notated duration** — `ScoreEvent.duration` is notated; `event.performedDuration` applies `articulation.durationScale` for audio / MIDI note-off.
|
|
- **Playback** — `ScorePlayer` walks the flat event list against a `LyrebirdClock` on a `LyrebirdRoutine` (mirrors `MIDIFilePlayer`); SMF bridges via `Score+MIDIFile`.
|
|
- **Pitch / scale theory toolkit** — spelling-aware `Interval` / `Scale` / `PitchCollection` / 12-tone rows, diatonic-vs-chromatic transposition, scale-to-scale mapping; results drop straight into the DSL (`Note(.named(...))`). See [`docs/stdlib/theory.md`](docs/stdlib/theory.md).
|
|
|
|
The template writes both files into the **project root** (next to `Package.swift`) via `LyrebirdPaths.projectRoot()` — see [File output locations](#file-output-locations) — so output lands in the same place whether you run with `lyrebird run`, `swift run`, or Xcode.
|
|
|
|
Full guide: `Sources/Lyrebird/Lyrebird.docc/ScoreSystem.md`. Reference project: [`Demos/LyrebirdScoreDemo`](Demos/LyrebirdScoreDemo/).
|
|
|
|
---
|
|
|
|
## File output locations
|
|
|
|
When a project writes files (an NRT `.caf`, a MusicXML export, a recording), *where* they land depends on how the binary is launched: `lyrebird run` / `swift run` set the working directory to the package root, but **Xcode sets it to `DerivedData`** and a bare binary inherits whatever shell launched it. Hardcoding `URL(fileURLWithPath: "out.caf")` therefore drops files in surprising places.
|
|
|
|
`LyrebirdPaths` (core `Lyrebird` module) resolves the common locations for you:
|
|
|
|
| Member | Resolves to | Use for |
|
|
|---|---|---|
|
|
| `LyrebirdPaths.projectRoot()` | Folder containing `Package.swift`, found by walking up from the **calling source file** at compile time. Returns `URL?`. | Dev-time render/export output you want beside your project, independent of launch method. |
|
|
| `LyrebirdPaths.documents` | `.documentDirectory` (`~/Documents` on macOS; the app sandbox Documents on iOS). | **Shipped apps** writing user-visible data. The right choice on iOS. |
|
|
| `LyrebirdPaths.applicationSupport` | `.applicationSupportDirectory` | App-managed data (presets, state) not meant to be user-visible. |
|
|
| `LyrebirdPaths.caches` | `.cachesDirectory` | Regenerable data the system may purge. |
|
|
| `LyrebirdPaths.temporary` | Per-process temp dir | Scratch files. |
|
|
| `LyrebirdPaths.home` | User home | — |
|
|
| `LyrebirdPaths.currentWorkingDirectory` | Process cwd | Equals project root under `lyrebird run`; a sensible `??` fallback for `projectRoot()`. |
|
|
| `LyrebirdPaths.desktop` *(macOS only)* | `.desktopDirectory`. Returns `URL?`. | Quick dev dumps. Needs an entitlement in a sandboxed app. |
|
|
|
|
```swift
|
|
import LyrebirdSynth
|
|
|
|
// Dev-time: write beside the project, regardless of launch method.
|
|
let dir = LyrebirdPaths.projectRoot() ?? LyrebirdPaths.currentWorkingDirectory
|
|
let cafURL = dir.appendingPathComponent("render.caf")
|
|
|
|
// Shipped app (iOS/macOS): write where the sandbox allows.
|
|
let userOut = LyrebirdPaths.documents.appendingPathComponent("render.caf")
|
|
```
|
|
|
|
`projectRoot()` is a compile-time, source-anchored path — it reflects where the binary was **built**, so it does not exist on another user's machine. Use it for development output only; use `documents` / `applicationSupport` in anything you distribute. It walks up to `Package.swift` by default; pass a dot-prefixed `marker: ".xcodeproj"` (or `.xcworkspace`) — matched by extension, so it finds `MyApp.xcodeproj` — for an Xcode-project layout with no manifest.
|
|
|
|
---
|
|
|
|
## Xcode file templates
|
|
|
|
Beyond whole projects, the CLI installs **New File…** templates so you can add a Generator, Synthesizer, Task, Oscillator, Filter, Spectral Processor, Score, AUv3 source-slot synth, test suite, and more without boilerplate:
|
|
|
|
```bash
|
|
lyrebird install-templates # New File… ▸ Lyrebird section appears in Xcode
|
|
lyrebird uninstall-templates
|
|
```
|
|
|
|
From a repo checkout you can equivalently run `bash scripts/install-xcode-templates.sh` (`--dry-run` to preview). Catalog: [`docs/tooling/xcode-templates.md`](docs/tooling/xcode-templates.md).
|
|
|
|
---
|
|
|
|
## Reference projects
|
|
|
|
The repo ships seven runnable demos in [`Demos/`](Demos/), opened via `Lyrebird.xcworkspace`, covering every template above — start there to see complete, working code:
|
|
|
|
| Demo | Mirrors template |
|
|
|---|---|
|
|
| [`LyrebirdDemo`](Demos/LyrebirdDemo/) | real-time CLI |
|
|
| [`LyrebirdMacOSDemo`](Demos/LyrebirdMacOSDemo/) | `macos-ui-xcode` |
|
|
| [`LyrebirdiOSDemo`](Demos/LyrebirdiOSDemo/) | `ios-ui` |
|
|
| [`LyrebirdNRTDemo`](Demos/LyrebirdNRTDemo/) | `nrt` |
|
|
| [`LyrebirdScoreDemo`](Demos/LyrebirdScoreDemo/) | `score` |
|
|
| [`LyrebirdAUv3Demo`](Demos/LyrebirdAUv3Demo/) | `auv3` |
|
|
| [`LyrebirdScratchDemo`](Demos/LyrebirdScratchDemo/) | live-coding scratchpad |
|
|
|
|
See [`LYREBIRD_START_HERE.md`](LYREBIRD_START_HERE.md) for per-demo notes and gotchas.
|