1 Commits

Author SHA1 Message Date
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
+36
View File
@@ -47,6 +47,42 @@ let env = EnvelopeGen.Control(envelope: Envelope(
let voice = carrier * env * 0.2 // ready to route to a bus 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 ### 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: 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: