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
+132
View File
@@ -301,6 +301,138 @@ complete.**
New work beyond this group is scoped from the **Deferred / Future** and **Not
planned** lists below as demand appears.
### Generator Consolidation + RT Performance group (GP-1 … GP-9)
Goal: collapse remaining duplicated per-sample / per-init Generator code into
shared helpers, and land a set of benchmark-gated real-time performance wins on
the audio render path (`_computeSamples` / `calculatedSamples`). Sourced from an
architecture sweep of `Sources/Lyrebird/Generators/` + `GeneratorCore/`.
Splits into two work-streams: **dedup** (no behaviour change, gated on tests
only) and **perf** (gated on before/after benchmark + tests + review). Each
phase is its own branch off `main` per the standing phase-branch policy.
**Locked decisions (with Josh):**
- **vDSP/vForce strategy = split-path, keep scalar fallback.** Each vectorised
generator branches on `n == count` (upstream block full-length — the normal
case at *any* block size, including small control blocks like 64). Only the
rare short-upstream-block case (`n < count`) falls to the existing scalar
loop. The vDSP/vForce call fires for 64-sample control blocks too — the gate
is upstream-fullness, not absolute block size. If benchmarks show vDSP call
overhead losing at *tiny* counts (1/2/4), add a `count >= minVectorCount`
threshold (strictly better than "vDSP everywhere"). Revisit a
"vDSP-everywhere + zero-pad" variant only if the split-path data warrants it.
- **Benchmark harness = existing `Sources/LyrebirdBenchmark/main.swift`.** Add
one timed case per perf phase (`ContinuousClock`, warmup + N timed iters,
reports ns/block + ns/sample; `OfflineDriver` for in-context `processBlock`).
Run release only: `swift run -c release LyrebirdBenchmark`. The control-block
sweep (512 → 1) already in the harness shows the win-curve across block sizes.
- **Per-phase gate.** Dedup phases: `swift test --no-parallel` green +
`/lyrebird_review` clean. Perf phases: same, *plus* a committed before/after
benchmark delta; keep the change only if it shows a real win with no test
regression. Capture baseline numbers in the phase branch's first commit.
#### Phase GP-0 — Benchmark baseline capture
Add timed cases to `LyrebirdBenchmark` for every perf target *before* touching
code: `BinaryOpGenerator` (scalar-mul, vector-add), unary math (`Sin`/`Exp`/
`Sqrt`/`Abs`), `LinLin.Audio`, SVF generator-driven `freq`, `Chorus.Audio`.
Run the control-block sweep on each. Commit the baseline numbers as the
reference all GP perf phases diff against. No production code change.
#### Phase GP-1 — Trivial / low-risk dedup (no perf risk) ✓ *(complete — merged to main)*
Pure consolidation, semantically identical, gated on tests + review only:
- **D1** — Merge byte-identical `normalizeLFPhase` / `normalizeBLPhase` into one
`normalizeOscPhase(_:)` helper. 8 oscillator `.Control` callers
(LF/Osc Sawtooth/Square/Triangle/DutyCycle).
- **D7** — Add `lyrebirdTwoPi` to `Math/LyrebirdConstants.swift`; replace 5
inline `let twoPi = 2.0 * .pi` locals (Chorus/Flanger/Phaser/Jitter/
ScannedSynthesizer). Removes magic-number-rule smell on the hot path.
- **D4** — Adopt existing `resolveGeneratorOrScalar(...)` in `Chorus`/`Flanger`/
`Phaser._computeSamples` — replaces ~17 hand-rolled 8-line input-resolution
blocks. Helper already proven in Flute/Clarinet/Pan*/PlateReverb.
- **D2** — Extract `computeButterworthQs(order:) -> [Double]` (new
`Filters/ButterworthHelpers.swift`); 4 Butterworth filters call it at init.
#### Phase GP-2 — SVF helper extraction + coefficient interpolation (D6 + P4) ✓ *(complete — merged to main; −5.9% on gen-driven-freq bench)*
Bundles a dedup and a perf win that touch the same code:
- **D6** — Extract `runSVFBlock(...)` (Chamberlin recurrence + denormal flush +
zero-fill); `SVF.Audio` and `SVF.Control._computeSamples` collapse to setup +
one call + `mulAdd`. Name `svfMinQDamping` / `svfFCoeffMax` constants.
- **P4** — In the generator-driven-`freq` path, replace per-sample
`Darwin.sin(piOverSr·freq)` with block-boundary `fFirst`/`fLast` + linear
interpolation (same technique as `runRBJBiquadBlock`). Eliminates ~510 of 512
`sin` calls/block on that path. Benchmark the gen-driven-freq case.
Gate: tests + review + benchmark (perf component).
#### Phase GP-3 — Butterworth LP/HP cascade engine extraction (D3) ✓ *(complete — merged to main; ~250 dup lines removed, behavior-identical)*
- **D3** — Extract shared background-thread vDSP cascade machinery
(`runScalar`/`runVDSP`/`runAudioRate`/deinit drain + the `_vdsp*Queue` set)
into a `ButterworthCascadeEngine` in `ButterworthHelpers.swift`. LP and HP
inject two coefficient closures and become thin wrappers. Removes ~250 lines
of duplicated thread lifecycle. Medium risk — match the thread teardown
exactly. Gate: tests + review. Sets up GP-9.
#### Phase GP-4 — Unary math vForce/vDSP fast path (P3) ✓ *(complete — merged to main; −66.9% / ~3× on the 4-op unary chain)*
- **P3** — Split-path vectorisation for `Sin`/`Cos`/`Exp`/`Log`/`Sqrt`/`Floor`/
`Ceil` (`vvsin`/`vvcos`/`vvexp`/`vvlog`/`vvsqrt`/`vvfloor`/`vvceil`) and
`Abs` (`vDSP_vabsD`). `n == count` → vectorised; `n < count` → existing scalar
loop. No allocation (write into pre-zeroed `_result`). Highest single-generator
throughput win (~4–10×). Benchmark each across the control-block sweep; apply
`minVectorCount` threshold if tiny blocks regress.
#### Phase GP-5 — `BinaryOpGenerator` enum dispatch + vDSP (P2) ✓ *(complete — merged to main; gen×gen −56%, gen×scalar −72%)*
- **P2** — Replace the stored `op: (Double,Double)->Double` closure with an
operator enum (`.add`/`.subtract`/`.multiply`/`.divide`) classified at init;
dispatch aligned gen-vs-gen / gen-vs-scalar paths to `vDSP_vaddD`/`vsubD`/
`vmulD`/`vdivD`/`vsaddD`/`vsmulD`. Keep the per-sample closure fallback for
non-arithmetic / unaligned-count cases. Highest *graph-wide* win — every
`osc * 0.5 + env` chain. Preserve IEEE-754 divide-by-zero behaviour. Medium
risk — enumerate all variants, keep closure escape hatch. Benchmark scalar-mul
+ vector-add cases.
#### Phase GP-6 — `LinLin` vDSP fast path (P7) ✓ *(complete — merged to main; −26.9%. LinExp/ExpLin deferred)*
- **P7** — `LinLin.Audio` → single `vDSP_vsmsaD` (`a·x + b`, coefficients
hoisted out of loop; range params are always block-scalar so unconditional).
Short-block fallback keeps the exact per-sample formula.
- *Deferred:* `LinExp`/`ExpLin` (build the linear stage via vDSP then `vvexp` —
needs a pre-allocated scratch buffer at init; modest incremental win, revisit
if a profile flags them).
#### Phase GP-7 — Power-of-two delay-line wrapping (P5) ✓ *(complete — merged to main; Chorus −3.3%)*
- **P5** — `Chorus`/`Flanger` circular delay lines: size up to next power of two
at init, replace per-sample `% delayLineCount` (3–4 per sample) with
`& (delayLineCount - 1)`. ~2× delay-buffer memory (≈64 KB at max delay) for
~6–9 cycles/sample saved. Add init-time `roundUpToPowerOfTwo(_:)`. Benchmark
`Chorus.Audio`.
#### Phase GP-8 — Butterworth band-pass / band-reject vDSP cascade (P6) ✓ *(complete — merged to main; shared ButterworthSectionEngine, BP/BR gain vDSP + dedup)*
- **P6** — Extend the GP-3 `ButterworthCascadeEngine` vDSP path to
`ButterworthBandPass` (sequenced HP→LP) and `ButterworthBandReject` (parallel
LP+HP summed via `vDSP_vaddD`). Matches the LP/HP acceleration. Depends on
GP-3. Band-reject needs two vDSP setups + dual kill-queue drain. Benchmark a
4th-order bandpass.
**Sequencing:** GP-0 (baseline) → GP-1 (safe dedup) → GP-2/GP-3 (helper
extractions, GP-3 before GP-8) → GP-4/GP-5 (highest-value perf) → GP-6/GP-7 →
GP-8. Each lands on `main` via `--no-ff` before the next starts, per policy.
**Not pursued:** D5 (`OscSin.Audio` inlined switch) — intentional audio-rate
choice (three branchless loops vs per-sample function call); leave as-is, revisit
only if `OscillatorTableReadHelpers` gains a bitmask-wrap variant.
### Longer-term — Not planned
#### Phase 96b — Ableton Link *(not planned)*