v1.4.0 release: live-coding scratch editor, interpreted Graph DSL, ClosureSynthesizer + SampleLoop
This commit is contained in:
@@ -123,6 +123,103 @@ final class SineTone: SynthesizerBase {
|
||||
- `evaluate(graph:count:)` is `final` — the engine drives it automatically.
|
||||
- Do not override `evaluate` in subclasses; override `build` instead.
|
||||
|
||||
### ClosureSynthesizer — a graph without a subclass
|
||||
|
||||
When all you want is to hear a generator graph — a scratchpad line, a demo, a
|
||||
one-off voice, a test — `ClosureSynthesizer` supplies the graph from a closure
|
||||
instead of a `build()` override:
|
||||
|
||||
```swift
|
||||
try engine.add(ClosureSynthesizer { OscSin.Audio(freq: 440, mul: 0.1) })
|
||||
try engine.add(ClosureSynthesizer(bus: 0) { Pan2(input: saw, pan: 0) })
|
||||
```
|
||||
|
||||
The closure runs inside `build()` — once, on the main thread, at add time. Each
|
||||
instance is its own graph, so construct the generators **inside** the closure
|
||||
rather than handing it instances shared with another synthesizer (the same
|
||||
graph-copy-per-instance rule as any `SynthesizerBase`). A multi-channel source
|
||||
fans out across consecutive buses from `bus`, exactly like `output(bus:source:)`.
|
||||
There are no `@LiveParam` slots — reach for a named subclass once a voice needs
|
||||
live-controllable parameters.
|
||||
|
||||
## Debugging: per-instance debug tap
|
||||
|
||||
> Canonical reference: the DocC article `<doc:Debugging>`
|
||||
> (`Sources/Lyrebird/Lyrebird.docc/Debugging.md`) covers this plus inspecting inner
|
||||
> nodes and stepping a generator's compute loop. The summary below is a quick pointer.
|
||||
|
||||
`evaluate(graph:count:)` is `final`, so you cannot hook it directly. Instead override
|
||||
`debugTap(graph:count:outputs:)` — an `open` no-op the engine calls once per scheduling
|
||||
segment, **after** every output generator has run, but only when `debugTapEnabled` is set on
|
||||
that instance. The tap's *invocation* is compiled out of release builds (the call site is
|
||||
`#if DEBUG`); the method and flag still exist so an override compiles unconditionally, but
|
||||
the hook never fires in release.
|
||||
|
||||
This is the canonical place to set a breakpoint on one specific voice.
|
||||
|
||||
```swift
|
||||
final class FMTone: SynthesizerBase {
|
||||
@LiveParam var carrierFreq: Double = 440.0
|
||||
|
||||
override func build() {
|
||||
let mod: any Generator = OscSin.Audio(freq: 220.0) * 660.0
|
||||
output(bus: 0, source: OscSin.Audio(freq: $carrierFreq + mod) * 0.18)
|
||||
}
|
||||
|
||||
override func debugTap(
|
||||
graph: SynthGraph,
|
||||
count: Int,
|
||||
outputs: [any Generator]
|
||||
) {
|
||||
// 👈 Breakpoint here. The RT thread halts with self, outputs, and graph
|
||||
// in scope. In the debugger:
|
||||
// po self.carrierFreq
|
||||
// po outputs.first?._cachedResult // Out's samples this block
|
||||
// po outputs.first?._result[0].prefix(8) // first 8 samples
|
||||
let block: Int = count
|
||||
_ = block
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Enable it per instance — flip only the voice you want to inspect:
|
||||
|
||||
```swift
|
||||
let voice: FMTone = FMTone()
|
||||
#if DEBUG
|
||||
voice.debugTapEnabled = true // main thread, before add — only this voice taps
|
||||
#endif
|
||||
try engine.add(voice)
|
||||
```
|
||||
|
||||
In a poly pool, gate the flag so a single note breaks:
|
||||
|
||||
```swift
|
||||
for note: Int in chord {
|
||||
let v: FMTone = FMTone()
|
||||
v.debugTapEnabled = (note == 60) // break only middle C
|
||||
try engine.add(v)
|
||||
}
|
||||
```
|
||||
|
||||
**What you get when it halts**
|
||||
|
||||
| In scope | Holds |
|
||||
|---|---|
|
||||
| `self` | Every `let` / `@LiveParam` on the voice. |
|
||||
| `outputs` | This instance's registered output-generator tree. Each generator's `_cachedResult` / `_result` = samples just computed for this block. |
|
||||
| `graph.engine` | Sample rate, block size, bus pool — read a bus mid-render. |
|
||||
| `count` | Samples in this scheduling segment (`≤ 512`; the engine splits a block at scheduled-event boundaries, so the tap can fire several times per audio block). |
|
||||
|
||||
**Rules**
|
||||
|
||||
- Runs on the **RT render thread.** A breakpoint is fine (it halts the thread and you look).
|
||||
Any *code* in the override obeys the RT rules — no allocation, locks, `print`, or `String`.
|
||||
- Fires per scheduling segment — hundreds of hits/sec while audio runs. Use a conditional
|
||||
breakpoint (`count == 512`) or an override-local frame counter to break every Nth.
|
||||
- Want continuous inspection without halting? Snapshot into a pre-allocated ring in the tap
|
||||
and drain it on the main thread instead — see `NodeTreeMirror` for the pattern.
|
||||
|
||||
## Adding to the Engine
|
||||
|
||||
```swift
|
||||
|
||||
@@ -3,6 +3,13 @@
|
||||
A step-by-step guide to implementing correct, RT-safe generators in Lyrebird.
|
||||
Every built-in generator in `Sources/Lyrebird/Generators/` follows the patterns here.
|
||||
|
||||
> Before you write a whole type: for one-off custom sample-accurate DSP — especially a
|
||||
> **single-sample feedback loop** (one-pole, waveguide, comb/allpass) — reach for `SampleLoop`
|
||||
> (`Sources/Lyrebird/Generators/Core/SampleLoop.swift`) first. It gives an RT-safe,
|
||||
> allocation-free per-sample body with typed `inout` feedback state and channel inference, no
|
||||
> new `Generator` declaration. Promote to a full generator (this guide) when you want
|
||||
> parameters, Quick Help, reuse, and raw-pointer speed.
|
||||
|
||||
---
|
||||
|
||||
## 1. Protocol Choice — `AudioGenerator` vs `ControlGenerator`
|
||||
@@ -609,6 +616,12 @@ final class MyGenerator: GeneratorBase, AudioGenerator {
|
||||
|
||||
## 10. Testing Checklist
|
||||
|
||||
> Debugging a generator that compiles but computes the wrong numbers: tag the
|
||||
> instance with `GeneratorBase.debugTag`, set an LLDB conditional breakpoint on its
|
||||
> `_computeSamples`, and/or read its post-block output from a Synthesizer debug tap.
|
||||
> Full workflow in the DocC article `<doc:Debugging>`
|
||||
> (`Sources/Lyrebird/Lyrebird.docc/Debugging.md`).
|
||||
|
||||
Write tests for every new generator using Swift Testing (`@Test`, `#expect`, `@Suite`).
|
||||
|
||||
> The `makeTestEngine`, `makeSynthGraph`, and `advanceBlock` helpers are defined in `Tests/LyrebirdTests/TestHelpers.swift`. Test files must use `@testable import LyrebirdSynth` and belong to the `LyrebirdTests` target to access them.
|
||||
|
||||
Reference in New Issue
Block a user