# Signal Flow ## Control Loop — Full Pass Executed once per audio render callback: ``` 1. Zero all audio bus memory 2. Copy hardware input samples → reserved input audio buses 3. (Control buses: no action — retain previous values) 4. Drain pendingQueue — collect all ScheduledEvents whose SampleTime falls in this block 5. Split block into segments at event boundaries: For each segment [segmentStart, segmentEnd): Apply any ScheduledEvents at segmentStart (add/remove nodes, parameter updates) Evaluate node tree top-to-bottom, passing count = segmentEnd - segmentStart: For each Group (in order): For each Synthesizer (in order): Evaluate its internal Generator DAG (leaves first, root last) Out generators accumulate into bus at correct offset (busOffset = segmentStart) Advance currentTime by count 6. Post freed Synthesizer instances to freeQueue (ARC deinit on background thread) 7. Copy reserved output audio buses → hardware AudioBufferList ``` **With no scheduled events**, there is exactly one segment spanning the full block — identical to non-scheduling behaviour with `count = blockSize` and `busOffset = 0`. ## DAG Evaluation (within a Synthesizer) Generators form a directed acyclic graph. Evaluation is **pull-based**: the root generator (typically `Out`) requests its inputs, which recursively request their inputs. ``` Out.Audio(bus: 0) └── BinaryOpGenerator (*) ├── OscSin (freq: 440) ← leaf: no inputs └── EnvelopeGen.Audio ← leaf: no inputs ``` Evaluation order: leaves computed first, root last. Each generator's `calculatedSamples(graph:count:)` is called once per segment (which is once per block when no scheduling events split it). ## Audio Rate vs Control Rate - **Audio generators** produce `[Float]` of length `blockSize` - **Control generators** produce a single `Float` value - When a control-rate value feeds an audio-rate generator, it is interpolated across the block (linear interpolation between previous and current value) ## In / Out Bus Interaction ``` Synthesizer A: Synthesizer B: Out.Audio(bus: 2) ─writes─► [bus 2] ─reads─► In.Audio(bus: 2) ``` Because the node tree is evaluated in order, Synthesizer A must appear before Synthesizer B in the tree if B reads A's output. Group ordering controls this. ## Parallel Groups Independent groups (no bus dependency between them) can evaluate concurrently using Swift structured concurrency (`async let` or `TaskGroup`). Groups that share bus data must be ordered sequentially. ```swift // Safe to parallelize only if effectsGroup does NOT read from instrumentGroup's buses // during the same cycle. If it does, they must be sequential. async let groupA = evaluate(instrumentGroup) async let groupB = evaluate(effectsGroup) ``` ## Accumulation Model `Out` uses `+=`. This means: - Two Synthesizers writing to bus 0 → their signals sum (mixing) - Three note instances of the same patch → polyphonic sum - An effect `In`-ing from bus 2 and `Out`-ing to bus 0 → FX insert chain