2 Commits

Author SHA1 Message Date
josh a175c437de v2.3.3 release: cross-platform driver in docs (makeDefaultAudioDriver) 2026-12-01 21:32:39 -08:00
josh 56726223f6 v2.3.2 release: README — add a 'write your own Generator' example
Adds a complete HardClip audio-rate distortion to the quick-taste section,
showing GeneratorBase + AudioGenerator conformance, init-time input
resolution, the _checkCount guard, and filling/returning the pre-allocated
_result in place — the Generator-authoring counterpart to the existing
graph-composition example.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-29 13:57:47 -08:00
23 changed files with 90 additions and 43 deletions
+1 -1
View File
@@ -156,7 +156,7 @@ let engine = SynthesisEngine(
sampleRate: 48_000,
numOutputChannels: 2,
numInputChannels: 0,
driver: CoreAudioDriver())
driver: makeDefaultAudioDriver())
try engine.start()
try engine.add(SineTone())
+39 -3
View File
@@ -47,6 +47,42 @@ let env = EnvelopeGen.Control(envelope: Envelope(
let voice = carrier * env * 0.2 // ready to route to a bus
```
### Generators — or write your own
A Generator is an RT-safe DSP node: subclass `GeneratorBase`, conform to `AudioGenerator`, fill the inherited pre-allocated `_result` buffer, and return it. No allocation, locks, or runtime type checks on the render thread — inputs are classified once at `init`. This is a complete hard-clip distortion:
```swift
import LyrebirdSynth
/// Hard-clip distortion — clamps its input signal to ±`level`.
final class HardClip: GeneratorBase, AudioGenerator {
private let input: any ValidGeneratorInput
private let level: Double
init(input: any ValidGeneratorInput, level: Double = 0.5) {
self.input = input._resolvedInput() // resolve/classify inputs off the RT thread
self.level = level
super.init() // allocates _result: one mono channel, 512 frames
}
func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] {
_checkCount(count) // bounds guard — no force-unwraps on the RT thread
guard let signal = input.calculatedSamples(graph: graph, count: count).first else {
return _result
}
for i in 0..<count {
_result[0][i] = max(-level, min(level, signal[i])) // clamp, in place
}
return _result // pre-allocated buffer; the engine reads it directly
}
}
// Wire it like any built-in generator — operators and literals compose it the same way:
let dirty = HardClip(input: OscSin.Audio(freq: 220.0), level: 0.2) * 0.5
```
`floatValue` comes free from the `AudioGenerator` extension. A generator that also makes sense at control rate (LFOs, envelopes) uses a paired `enum Name { class Audio; class Control }` namespace instead — see [`docs/protocols/writing-generators.md`](docs/protocols/writing-generators.md) for the full guide, shared helpers, and the multi-channel rules.
### Synthesizers — a graph plus live parameters
A `SynthesizerBase` subclass owns a graph built in `build()` and exposes `@LiveParam` (atomic, RT-safe) or `@UIParam` (SwiftUI-bindable) properties. The `$name` projection injects each parameter into the graph as a smoothed control-rate node:
@@ -76,7 +112,7 @@ let engine = SynthesisEngine(
sampleRate: 48_000,
numOutputChannels: 2,
numInputChannels: 0,
driver: CoreAudioDriver()) // NRTDriver for offline, iOSAudioDriver for iOS
driver: makeDefaultAudioDriver()) // CoreAudio on macOS, iOS driver on iOS, ALSA on Linux; NRTDriver for offline
try engine.start()
try engine.add(FMTone()) // sounds immediately
@@ -85,7 +121,7 @@ try engine.schedule(tone, after: 2.5) // sample-accurate, 2.5 s from now
tone.carrierFreq = 660 // live, atomic, no glitch
```
Swap `CoreAudioDriver()` for `NRTDriver(outputURL:duration:)` and the identical graph renders offline to a CAF file. See [`INSTALLATION_AND_USAGE.md`](INSTALLATION_AND_USAGE.md) to add the package and scaffold projects, and [`ARCHITECTURE.md`](ARCHITECTURE.md) for how the threading and scheduling work underneath.
Swap `makeDefaultAudioDriver()` for `NRTDriver(outputURL:duration:)` and the identical graph renders offline to a CAF file. See [`INSTALLATION_AND_USAGE.md`](INSTALLATION_AND_USAGE.md) to add the package and scaffold projects, and [`ARCHITECTURE.md`](ARCHITECTURE.md) for how the threading and scheduling work underneath.
### Live coding — prototype in the interpreter
@@ -100,7 +136,7 @@ import LyrebirdSynth
let engine = SynthesisEngine(
blockSize: 512, sampleRate: 48_000,
numOutputChannels: 2, numInputChannels: 0,
driver: CoreAudioDriver())
driver: makeDefaultAudioDriver())
try engine.start()
// Define a synth right in the session.
@@ -18,10 +18,12 @@ let engine = SynthesisEngine(
sampleRate: 48000,
numOutputChannels: 2,
numInputChannels: 0,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
```
> Note: ``makeDefaultAudioDriver()`` returns the live-audio driver for the platform the build targets — `CoreAudioDriver` on macOS, `iOSAudioDriver` on iOS, `ALSAAudioDriver` on a Linux build with libasound, and ``OfflineDriver`` (silent) where no live backend exists. Prefer it over a concrete driver so one source builds and runs on every platform; name a concrete driver only in code already locked to a single platform. Check ``isLiveAudioAvailable`` when silence would be a surprise.
The full parameter list is ``SynthesisEngine/init(blockSize:controlBlockSize:sampleRate:numOutputChannels:numInputChannels:numAudioBuses:numControlBuses:eventQueueCapacity:maxEventsPerBlock:driver:)``:
| Parameter | Meaning | Default |
@@ -86,7 +86,7 @@ let engine = SynthesisEngine(
sampleRate: 48000,
numOutputChannels: 2,
numInputChannels: 0,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
try engine.start()
try engine.add(MySynth())
+1 -1
View File
@@ -7,7 +7,7 @@ A native macOS audio synthesis framework for Swift.
Lyrebird provides a signal-graph engine backed by Core Audio. You define instruments and effects as ``Synthesizer`` instances containing networks of ``Generator`` objects, then add them to a running ``SynthesisEngine``. All signal processing runs on the Core Audio real-time thread with no heap allocation in steady state.
```swift
let driver = CoreAudioDriver()
let driver = makeDefaultAudioDriver()
let engine = SynthesisEngine(
blockSize: 512, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 0,
+1 -1
View File
@@ -25,7 +25,7 @@ Lyrebird provides two ways to use multiple cores:
let engine = SynthesisEngine(
blockSize: 512, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 0,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
let voices = ParallelGroup(engine: engine)
+1 -1
View File
@@ -22,7 +22,7 @@ NRT rendering is the correct approach for:
## Approach 1 — NRTDriver
Replace `CoreAudioDriver()` with `NRTDriver(outputURL:duration:)`. Everything else stays the same.
Replace `makeDefaultAudioDriver()` with `NRTDriver(outputURL:duration:)`. Everything else stays the same.
```swift
import Foundation
@@ -879,7 +879,7 @@ let events: [LyrebirdEvent] = [
// MARK: - Live performance
let driver = CoreAudioDriver()
let driver = makeDefaultAudioDriver()
let engine = SynthesisEngine(
blockSize: 512,
sampleRate: 44100,
@@ -111,7 +111,7 @@ let engine = SynthesisEngine(
sampleRate: 44100,
numOutputChannels: 2,
numInputChannels: 2,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
// engine.add(MySynth())
+1 -1
View File
@@ -50,7 +50,7 @@ import LyrebirdSynth
let engine = SynthesisEngine(
blockSize: 512, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 2,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
try engine.start()
+1 -1
View File
@@ -63,7 +63,7 @@ swift run LyrebirdDemo
Demonstrates: `SynthesizerBase`, `@LiveParam`, `OscSin`, `DelayLine`, `engine.add`/`remove`, and RT-safe parameter updates. Runs real-time audio via ``CoreAudioDriver``.
**Copy this target** when you want a headless synthesis starting point. Swap `CoreAudioDriver()` for `NRTDriver(outputURL:duration:)` for offline rendering — see <doc:NRTGuide>.
**Copy this target** when you want a headless synthesis starting point. Swap `makeDefaultAudioDriver()` for `NRTDriver(outputURL:duration:)` for offline rendering — see <doc:NRTGuide>.
### LyrebirdUIDemo — GUI starting point
+1 -1
View File
@@ -115,7 +115,7 @@ import LyrebirdSynth
let engine = SynthesisEngine(
blockSize: 512, sampleRate: 48000,
numOutputChannels: 2, numInputChannels: 2,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver() // CoreAudio (macOS) / iOS / ALSA (Linux)
)
do { try engine.start() } catch { print("Engine failed: \(error)"); exit(1) }
+1 -1
View File
@@ -52,7 +52,7 @@ let cfg = SynthesisEngineConfig(
sampleRate: 44100,
numOutputChannels: 2,
numInputChannels: 0,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
let engine = try host.makeEngine(role: .rtOutput(deviceID: 0), config: cfg)
```
+4 -4
View File
@@ -43,13 +43,13 @@ A Lyrebird program is a normal Swift program that uses the Lyrebird library. Her
import Foundation
import LyrebirdSynth
// The engine owns Core Audio and runs the audio graph.
// The engine owns the audio device and runs the audio graph.
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 512,
sampleRate: 48000,
numOutputChannels: 2,
numInputChannels: 0,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
// A synthesizer describes the sound.
@@ -98,7 +98,7 @@ let engine: SynthesisEngine = SynthesisEngine(
sampleRate: 48000,
numOutputChannels: 2,
numInputChannels: 0,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
```
@@ -108,7 +108,7 @@ let engine: SynthesisEngine = SynthesisEngine(
- `blockSize: 512` — the engine processes audio 512 samples at a time. (Lyrebird caps this at 512.)
- `sampleRate: 48000` — 48,000 samples per second.
- `numOutputChannels: 2` — stereo out. `numInputChannels: 0` — no microphone input.
- `driver: CoreAudioDriver()` — talk to the Mac's audio hardware. (iOS uses a different driver; guide 4.)
- `driver: makeDefaultAudioDriver()` — connect to the platform's audio hardware. It picks the backend automatically: Core Audio on macOS, the iOS driver on iOS, ALSA on Linux (and a silent offline driver where no live audio exists), so the same line works everywhere. (guide 4.)
### Describing the sound
+1 -1
View File
@@ -227,7 +227,7 @@ import LyrebirdSynth
let engine = SynthesisEngine(
blockSize: 512, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 0,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
try engine.start()
+1 -1
View File
@@ -39,7 +39,7 @@ let engine = SynthesisEngine(
sampleRate: 44100,
numOutputChannels: 2,
numInputChannels: 0,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
try engine.start()
+1 -1
View File
@@ -24,7 +24,7 @@ Use `NRTDriver` for most cases — it is a drop-in replacement for `CoreAudioDri
### Basic usage
Replace `CoreAudioDriver()` with `NRTDriver(outputURL:duration:)`. Everything else stays the same.
Replace `makeDefaultAudioDriver()` with `NRTDriver(outputURL:duration:)`. Everything else stays the same.
```swift
import Foundation
+1 -1
View File
@@ -146,7 +146,7 @@ let engine = SynthesisEngine(
sampleRate: 44100,
numOutputChannels: 2,
numInputChannels: 2,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
// Add your Synthesizers here.
+1 -1
View File
@@ -69,7 +69,7 @@ import LyrebirdSynth
let engine = SynthesisEngine(
blockSize: 512, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 2,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
try engine.start()
+1 -1
View File
@@ -63,7 +63,7 @@ swift run LyrebirdDemo
Demonstrates: `SynthesizerBase`, `@LiveParam`, `OscSin`, `DelayLine`, `engine.add`/`remove`, and RT-safe parameter updates from the main thread. Runs real-time audio via `CoreAudioDriver`, plays for a few seconds, then stops.
**Copy this target** when you want a headless synthesis starting point. Swap `CoreAudioDriver()` for `NRTDriver(outputURL:duration:)` to render offline to a file — see [nrt-guide.md](nrt-guide.md).
**Copy this target** when you want a headless synthesis starting point. Swap `makeDefaultAudioDriver()` for `NRTDriver(outputURL:duration:)` to render offline to a file — see [nrt-guide.md](nrt-guide.md).
### `LyrebirdUIDemo` — GUI starting point
+2 -2
View File
@@ -37,7 +37,7 @@ import LyrebirdSynth
let engine = SynthesisEngine(
blockSize: 512, sampleRate: 44100,
numOutputChannels: 2, numInputChannels: 2,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
try engine.start()
@@ -153,7 +153,7 @@ let engine = SynthesisEngine(
sampleRate: 44100,
numOutputChannels: 2,
numInputChannels: 2,
driver: CoreAudioDriver()
driver: makeDefaultAudioDriver()
)
let kick = KickDrum()
+18 -15
View File
@@ -6,8 +6,9 @@ LYREBIRD_START_HERE.md 0 a2547833f11acd72 none
LYREBIRD_START_HERE.md 3 c0653ad6f9dc0f5b none
LYREBIRD_START_HERE.md 4 3d0d1e2673fda229 none
README.md 0 1d7c7c19c5a6d112 none
README.md 1 3190dd60b6a8f7f8 none
README.md 3 4bd7e864af2f05fd none
README.md 1 9e7419986634c82c none
README.md 2 3190dd60b6a8f7f8 none
README.md 4 d4768129a1cfed0f none
Sources/Lyrebird/AudioDrivers/CoreAudioDriver.swift 0 2b7f04ec486a9a5d none
Sources/Lyrebird/AudioDrivers/DefaultAudioDriver.swift 0 5b79c5afb54ed465 none
Sources/Lyrebird/Buffers/WavetableSet.swift 0 d2f7469591d6a837 none
@@ -106,7 +107,7 @@ Sources/Lyrebird/Lyrebird.docc/DiagnosticsGuide.md 3 4f36c6bd97e3df32 none
Sources/Lyrebird/Lyrebird.docc/DiagnosticsGuide.md 4 f1c7f9029c6f48ea none
Sources/Lyrebird/Lyrebird.docc/DiagnosticsGuide.md 8 8efe9dc71d2dd8dd engine
Sources/Lyrebird/Lyrebird.docc/DiagnosticsGuide.md 9 a0ccfaf1137b9c84 engine
Sources/Lyrebird/Lyrebird.docc/EngineSetup.md 0 cfce51934da66872 none
Sources/Lyrebird/Lyrebird.docc/EngineSetup.md 0 7cfe8e6e89b6005e none
Sources/Lyrebird/Lyrebird.docc/EngineSetup.md 1 6a365ef4f437a5f0 engine
Sources/Lyrebird/Lyrebird.docc/EngineSetup.md 2 f4d72dca55661e7f engine
Sources/Lyrebird/Lyrebird.docc/EngineSetup.md 3 3d81a4c501cf0801 engine
@@ -240,8 +241,8 @@ Sources/Lyrebird/Lyrebird.docc/Generators/WritingGenerators.md 2 2134b4cd357e438
Sources/Lyrebird/Lyrebird.docc/Generators/WritingGenerators.md 4 b4dc205b9aa5fc5a none
Sources/Lyrebird/Lyrebird.docc/GettingStarted.md 0 3ab248f11418d799 none
Sources/Lyrebird/Lyrebird.docc/GettingStarted.md 1 45ba41d51b1116b5 none
Sources/Lyrebird/Lyrebird.docc/GettingStarted.md 2 df30c1eb8663478c synth
Sources/Lyrebird/Lyrebird.docc/Lyrebird.md 0 34c2d1c25110eceb synth
Sources/Lyrebird/Lyrebird.docc/GettingStarted.md 2 c5ca5bc6162803a3 synth
Sources/Lyrebird/Lyrebird.docc/Lyrebird.md 0 cf74375c53429db5 synth
Sources/Lyrebird/Lyrebird.docc/MultiCore.md 1 8c645d659f87c9e7 none
Sources/Lyrebird/Lyrebird.docc/NRTGuide.md 0 5493b7be98c51e79 none
Sources/Lyrebird/Lyrebird.docc/NRTGuide.md 10 2d0a41bdf5b1bbd8 none
@@ -269,10 +270,10 @@ Sources/Lyrebird/Lyrebird.docc/ParameterInputs.md 0 d31f119c2f2c873c none
Sources/Lyrebird/Lyrebird.docc/ParameterInputs.md 1 6320b52dca1af028 none
Sources/Lyrebird/Lyrebird.docc/ParameterInputs.md 6 6b933bd11ae2b8d3 engine
Sources/Lyrebird/Lyrebird.docc/PolyphonicVoices.md 1 9baae05324ee9c36 engine
Sources/Lyrebird/Lyrebird.docc/ProcessSystem.md 31 16ac8c55fec10e72 none
Sources/Lyrebird/Lyrebird.docc/ProjectSystem.md 1 5ca73171977bcda7 none
Sources/Lyrebird/Lyrebird.docc/ProcessSystem.md 31 5e03e7865c8f4ace none
Sources/Lyrebird/Lyrebird.docc/ProjectSystem.md 1 371e7bb6ce49e53f none
Sources/Lyrebird/Lyrebird.docc/ProjectSystem.md 2 bd3f90eb47fd6006 none
Sources/Lyrebird/Lyrebird.docc/REPLGuide.md 0 44e28203300162fe none
Sources/Lyrebird/Lyrebird.docc/REPLGuide.md 0 2dc70e99b6404557 none
Sources/Lyrebird/Lyrebird.docc/RoutineTask.md 0 97969b7e340a3fad none
Sources/Lyrebird/Lyrebird.docc/RoutineTask.md 14 86a37ee4afa1de70 none
Sources/Lyrebird/Lyrebird.docc/RoutineTask.md 16 62b70fad58edb312 engine
@@ -395,6 +396,7 @@ Sources/Lyrebird/TemporalStream/Algorithms/TemporalTendency.swift 0 c710ddd78ba4
Sources/Lyrebird/TemporalStream/Algorithms/TemporalTendency.swift 2 da1b4e7b74d72598 none
Sources/LyrebirdUI/LyrebirdUI.docc/GUISystem.md 0 a7c7ea54be39417e none
Sources/LyrebirdUI/MouseTracker.swift 0 5b5368aaa517dfa7 none
Sources/LyrebirdUI/MouseTracker.swift 1 5b5368aaa517dfa7 none
docs/diagnostics-guide.md 10 dc9b6d6a6320a78c engine
docs/diagnostics-guide.md 2 42a857719bcee2d7 none
docs/diagnostics-guide.md 3 4f36c6bd97e3df32 none
@@ -405,7 +407,7 @@ docs/engine/alsa-audio-driver.md 0 d255cf28c7eff929 none
docs/engine/bus-system.md 0 7aa0d214382e2c02 none
docs/engine/bus-system.md 4 1420e89a6b21dffd member
docs/engine/bus-system.md 5 5882de3452d3fb30 member
docs/engine/multi-engine-host.md 1 10672b6a8d764ee0 none
docs/engine/multi-engine-host.md 1 0032b031fd5a5dbc none
docs/engine/scheduling.md 0 2c95d843ffa87129 none
docs/engine/scheduling.md 5 18f17a73ca3ff9ae none
docs/engine/synthesis-engine.md 1 a5fd826f8cdc7143 engine
@@ -413,9 +415,9 @@ docs/engine/synthesis-engine.md 5 b86ed5cdad961753 engine
docs/engine/synthesis-engine.md 7 420a077205369626 engine
docs/engine/synthesis-engine.md 8 17aeb564649952e4 engine
docs/learning-swift/01-hello-world.md 0 03653506ac9beeb2 none
docs/learning-swift/01-hello-world.md 1 ed87a599242dc1ac none
docs/learning-swift/01-hello-world.md 1 960bf10c1da5bae7 none
docs/learning-swift/01-hello-world.md 2 0df7f63da21f430c none
docs/learning-swift/01-hello-world.md 3 7fb514e0b937b06b none
docs/learning-swift/01-hello-world.md 3 370a407cba47b238 none
docs/learning-swift/01-hello-world.md 4 5f8c95401204fb36 none
docs/learning-swift/02-swift-basics.md 0 560d2988df332558 none
docs/learning-swift/02-swift-basics.md 1 74425300ee178185 none
@@ -430,6 +432,7 @@ docs/learning-swift/03-types-and-functional.md 5 ba1ff2d440140b4d none
docs/learning-swift/03-types-and-functional.md 7 bd9afda685bee4eb none
docs/learning-swift/03-types-and-functional.md 8 1f72f3e7f59aa2ba none
docs/learning-swift/04-xcode-and-packages.md 3 391201fc9e1fa104 none
docs/migrating-to-2.0.md 2 d5ea5668715939f8 none
docs/protocols/generator.md 0 759d4352212759f3 none
docs/protocols/generator.md 2 a0f4ffb20378c07d none
docs/protocols/generator.md 3 0f13c732d11665c0 none
@@ -552,16 +555,16 @@ docs/stdlib/theory.md 7 cb778d54e742c021 none
docs/stdlib/theory.md 8 ac601091423d2985 none
docs/stdlib/theory.md 9 74dda0c11f3d34c4 none
docs/tooling/cli-guide.md 0 1407c405647e96de none
docs/tooling/cli-guide.md 1 5173fda0e36a5213 none
docs/tooling/midi-clock-sync.md 0 526a3779a15793c6 none
docs/tooling/cli-guide.md 1 d458830d43b7be3e none
docs/tooling/midi-clock-sync.md 0 dd95b6fa4608e119 none
docs/tooling/nrt-guide.md 0 046617aa0016f750 none
docs/tooling/nrt-guide.md 14 f06ebc900acda570 none
docs/tooling/nrt-guide.md 4 11c60dfed731ac6c none
docs/tooling/nrt-guide.md 7 5b70d42bbb32382d engine
docs/tooling/nrt-guide.md 9 7a2bd7072c940754 none
docs/tooling/project-system.md 1 f4208870a115401f none
docs/tooling/project-system.md 1 a92cddd62262b494 none
docs/tooling/project-system.md 3 491d32c269a92cbf none
docs/tooling/repl.md 0 28355170a44f09cd none
docs/tooling/repl.md 0 fe33a7d93a42d23c none
docs/tooling/routine-task.md 1 13ef865253b3351c none
docs/tooling/routine-task.md 11 b95b191b847d061e engine
docs/tooling/routine-task.md 9 be652d2b3b6f274f none
+7 -1
View File
@@ -253,9 +253,15 @@ def modules_dir(root: Path) -> Path:
proc = subprocess.run(["swift", "build", "--show-bin-path"], cwd=root,
capture_output=True, text=True)
if proc.returncode == 0 and proc.stdout.strip():
d = Path(proc.stdout.strip()) / "Modules"
bin_dir = Path(proc.stdout.strip())
d = bin_dir / "Modules"
if d.is_dir():
return d
# Newer SwiftPM build layouts (e.g. .build/out/Products/Debug) drop the
# Modules/ subdir and place the .swiftmodule bundles directly in the bin
# dir. Use the bin dir itself as the import search path in that case.
if any(bin_dir.glob("*.swiftmodule")):
return bin_dir
raise RuntimeError("could not locate the built Modules dir; run `swift build` first")