diff --git a/README.md b/README.md index d6f143c..c5287bc 100644 --- a/README.md +++ b/README.md @@ -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..