Music XML updates and optimizations

This commit is contained in:
2026-06-16 20:55:32 -07:00
parent ca1ee503cc
commit 73191c958f
97 changed files with 5459 additions and 2031 deletions
+20 -1
View File
@@ -997,7 +997,7 @@ Add these to the forbidden list in §6 alongside heap allocation and locking.
## 13. Shared RT-Safe Helper Functions
Three module-level free functions and one method exist to eliminate repeated boilerplate. **Always use them** — do not re-inline the patterns they replace.
A family of module-level free functions, base classes, and methods exist to eliminate repeated boilerplate. **Always use them** — do not re-inline the patterns they replace.
> **Out-of-tree extension packages:** these helpers are exposed as `@_spi(LyrebirdExtensions) public`. Add `@_spi(LyrebirdExtensions) import LyrebirdSynth` to any generator implementation file outside the core module. Full surface and stability policy: [extension-sdk.md](extension-sdk.md).
@@ -1167,6 +1167,18 @@ _zeroFallback = [Double](repeating: 0, count: lyrebirdDefaultResultBufferSize)
_zeroFallback = makeSampleBuffer()
```
### Accelerate-backed vectorisation helpers (Math / Filters / Delays)
These wrap the common pattern *"vectorise the full-block case, fall back to a scalar loop for short upstream blocks"* — the split-path strategy: branch on `n == count` (upstream block full-length, the normal case at any block size), run one Accelerate call, and keep a scalar fallback for `n < count`. All are RT-safe (no allocation; closures passed in capture only global functions).
- **`unaryVector{Sin,Cos,Exp,Log,Sqrt,Floor,Ceil,Abs}(_:into:count:)`** — `Generators/Math/UnaryMathVectorHelpers.swift`. One per unary `Math.Audio` waveshaper. Replaces the per-sample `Foundation.<fn>(i < n ? block[i] : pad)` loop with a single vForce (`vvsin`/`vvcos`/`vvexp`/`vvlog`/`vvsqrt`/`vvfloor`/`vvceil`) or `vDSP_vabsD` call. `Log` pads `1.0` (→ `log(1)=0`); the rest pad `0.0`. Built on the private `applyUnaryVectorTransform(_:pad:into:count:_:)` driver — add a new wrapper there if you add a unary op with a vForce/vDSP equivalent. `.Control` variants stay scalar.
- **`BinaryOpKind` + `binaryOpVectorScalarRHS` / `binaryOpScalarLHSVector` / `binaryOpVectorVector`** — `Generators/Math/BinaryOpVectorHelpers.swift`. The vDSP dispatch path for `BinaryOpGenerator`'s four arithmetic kinds. `+ − × ÷` map to correctly-rounded vDSP routines, so results are **bit-identical** to the scalar `lhs op rhs` loop. Divide uses true-division (`vDSP_vsdivD` / `vDSP_svdivD` / `vDSP_vdivD`), never reciprocal-multiply. Note `vDSP_vsubD` / `vDSP_vdivD` take the subtrahend / divisor pointer **first** (`C = A − B` / `A / B`). `BinaryOpGenerator` stores `kind: BinaryOpKind?` — `nil` means an arbitrary closure was supplied and the per-sample `op` fallback runs.
- **`runSVFBlock(...)`** — `Generators/Filters/SVFHelpers.swift`. The shared Chamberlin SVF per-block body for `SVF.Audio` and `SVF.Control` (four outputs: low / high / band / notch). When `freq` is generator-driven, the cutoff coefficient `f = 2·sin(π·freq/sr)` is evaluated with `sin()` only at the block's first/last sample and linearly interpolated between (same coefficient-ramp technique as `runRBJBiquadBlock`); the per-sample `freq > 0 → silence` guard is preserved exactly. `low` / `band` state mutated `inout`. (`LinLin.Audio` uses the same interpolation-free idea inline via `vDSP_vsmsaD` for its affine map — no helper, single call site.)
- **`_ButterworthCascadeBase` / `ButterworthSectionEngine`** — `Generators/Filters/ButterworthCascadeBase.swift`, `ButterworthSectionEngine.swift`. `ButterworthSectionEngine` is a reusable (non-Generator) order-1…8 biquad-cascade engine: per-stage Butterworth Q, scalar + audio-rate-interpolated paths, and a background-thread `vDSP_biquad_SetupD` builder (Option B, RT-safe). Coefficient formulas are injected as init closures + a thread name. `_ButterworthCascadeBase` is the thin Generator shell owning one engine (LP/HP subclass it); `ButterworthBandPass` chains two engines (HP→LP series) and `ButterworthBandReject` sums two parallel (LP+HP via `vDSP_vaddD`). Build a new Butterworth-family filter by composing engines — never re-inline the cascade/thread machinery.
- **`computeButterworthQs(order:)`** — `Generators/Filters/ButterworthHelpers.swift`. Init-time per-stage Butterworth pole-Q array. Call from any Butterworth-family `init`; do not inline the `−1/(2·cos(...))` loop.
- **`lyrebirdNextPowerOfTwo(_:)`** — `Math/PowerOfTwo.swift`. Smallest power of two `≥ n` (1 for `n ≤ 1`). Use at init to size any circular buffer whose index wrap should be a single `& (count − 1)` instead of `% count` (CombFilter / AllpassFilter / DelayLine / DelayLineFeedback / Chorus / Flanger). Keep the *semantic* length separate from the rounded buffer length so the audible range is unchanged.
- **`lyrebirdTwoPi`** — `Math/LyrebirdConstants.swift`. Canonical `2π` constant; use instead of an inline `let twoPi = 2.0 * .pi` local in any `_computeSamples`.
### Summary table
| Helper | Location | Use when |
@@ -1197,6 +1209,13 @@ _zeroFallback = makeSampleBuffer()
| `lyrebirdSeedXorshift64(from:mixConstant:)` | `LyrebirdConstants.swift` | Init-time PRNG seeding for Audio/Control pairs that must decorrelate via a per-variant `mixConstant` |
| `lyrebirdSeedXorshift64(from:)` | `LyrebirdConstants.swift` | Init-time PRNG seeding for stand-alone single-state owners (e.g. `DemandTemporalTendency`, `DemandTemporalBrownian`) — convenience for the two-arg form with `mixConstant: 0` |
| `_registerAutoFreeIfNeeded(_:)` | `Envelopes/DoneActionWatchable.swift` (extension) | Last line of every `DoneActionWatchable` generator's designated `init` (after `super.init`). If `doneAction == .freeNode`, self-registers with the currently-building `SynthesizerBase` so the synth auto-frees on completion. No-op outside `Synthesizer.build()`. |
| `unaryVector{Sin,Cos,Exp,Log,Sqrt,Floor,Ceil,Abs}(_:into:count:)` | `Generators/Math/UnaryMathVectorHelpers.swift` | Unary `Math.Audio` waveshaper inner loop — vForce / `vDSP_vabsD` full-block fast path with scalar fallback. Add a wrapper via `applyUnaryVectorTransform` for a new vectorisable unary op. `.Control` stays scalar. |
| `BinaryOpKind` + `binaryOpVectorScalarRHS` / `binaryOpScalarLHSVector` / `binaryOpVectorVector` | `Generators/Math/BinaryOpVectorHelpers.swift` | `BinaryOpGenerator` arithmetic dispatch. `+ − × ÷` → correctly-rounded vDSP (bit-identical to scalar). Divide is true-division; `vsubD`/`vdivD` take subtrahend/divisor first. `kind == nil` → closure fallback. |
| `runSVFBlock(...)` | `Generators/Filters/SVFHelpers.swift` | Chamberlin SVF per-block body shared by `SVF.Audio` / `SVF.Control`. Generator-driven cutoff uses block-boundary `sin()` interpolation; per-sample `freq > 0` silence guard preserved. `low`/`band` `inout`. |
| `ButterworthSectionEngine` / `_ButterworthCascadeBase` | `Generators/Filters/ButterworthSectionEngine.swift`, `ButterworthCascadeBase.swift` | Order-1…8 biquad cascade (scalar + audio-rate + background-thread vDSP setup). Compose for any Butterworth-family filter (LP/HP subclass the base; BandPass chains two engines, BandReject sums two). Never re-inline the cascade/thread machinery. |
| `computeButterworthQs(order:)` | `Generators/Filters/ButterworthHelpers.swift` | Init-time per-stage Butterworth pole-Q array. Call from any Butterworth-family `init`. |
| `lyrebirdNextPowerOfTwo(_:)` | `Math/PowerOfTwo.swift` | Init-time circular-buffer sizing so the index wrap is `& (count − 1)` not `% count`. Keep semantic length separate from the rounded buffer length. |
| `lyrebirdTwoPi` | `Math/LyrebirdConstants.swift` | Canonical `2π`; use instead of an inline `2.0 * .pi` local in `_computeSamples`. |
---