Skip to content

The DSP library streams: four processors, one arithmetic, any block size - #137

Merged
ninjudd merged 2 commits into
mainfrom
engine/streaming-dsp
Aug 11, 2026
Merged

ninjudd merged 2 commits into
mainfrom
engine/streaming-dsp

Conversation

@ninjudd

@ninjudd ninjudd commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Realtime chunk 1 (realtime.md §7): every processor in native/audiod/src/dsp/ becomes a stateful streaming class — Eq, Reverb, Limiter, Resampler — whose per-sample arithmetic is the whole-buffer function's exactly, with state carried across blocks. The whole-buffer free functions become thin wrappers over the classes, so render.cpp's only code change is the eq branch collapsing into dsp::eq(...); the offline render path is the same arithmetic reorganized, and the proof is that nothing else had to change: the render goldens in render-daemon.test.ts pass byte-for-byte, effect-properties' 18 measurements hold, and field build creek is a full cache hit — the content-addressed cache cannot tell the restructure happened, which is decision 3's "field build output should not change at all" in its strongest form.

The limiter is the interesting one. Its backward attack pass looks non-causal, but a peak's backward influence dies out at depth/attackSlope samples, so a bounded pending window sees everything the sweep consulted; the streaming form propagates each arrival backward through the window incrementally, and that equals the offline sweep bit-for-bit because float min and adding a constant are both monotone — the min of propagated candidates is the propagated min, and the early break is exact under the window's slope invariant. The window covers 240 dB of reduction depth (four attack ramps), a bound stated in the header. The resampler streams the same polyphase kernel from a taps-wide ring primed with the zeros before the source, emitting each output the moment its window completes and draining the rounded tail at flush; eq and reverb carry filter, comb, allpass, and predelay state across blocks. Every class reports its latency — the limiter its window, the resampler its taps/2 group delay, eq and reverb zero — which is the delay-compensation groundwork chunk 2 consumes.

Decision 6's floating-point half resolved the other way under review: neither path flushes. The first head shipped a scoped flush-to-zero in runRender with an argument that it was output-invariant; review disproved the argument twice over — DAZ zeroes subnormal float32 input samples, which are representable data, and a cold A/B on creek showed FZ moving three stems' decaying filter tails (~760 dB down, inaudible, real) — and keeping it would have forked the cache, since no action key moves when the arithmetic environment does. The double filter state makes denormal stalls unreachable (every feedback path decays and truncates near −60 dB), so both render paths run the default environment, the callback inherits that as a requirement, and the plan's decision 6 records the resolution with its evidence. The limiter's pending window is also now sized for the deepest reduction any finite float can demand rather than a fixed 240 dB, with infinities clamping to FLT_MAX, and the harness signal gained subnormal samples and a 1e20 peak so both properties are exercised rather than assumed.

The new harness, field-dsp-check (native/audiod/src/dsp-check.cpp, a JUCE-free target built alongside the daemon), runs each processor over a deterministic hostile signal — noise, an 18-dB-over spike, a silence stretch — whole versus partitioned at block sizes 1, 7, 128, 480, and 4096, and bit-compares every float; the resampler runs three ratios. All thirty cases are identical, and src/audio/dsp-check.test.ts is the CI caller, skipped like every native-gated test when the build is absent.

The plan flips Draft → Active with this PR — the readiness claim for a plan whose §8 records that nothing is open — and realtime moves from next.md to now.md with chunk 1 noted shipped.

Testing

From the repo root: pnpm build:native, then ./native/audiod/build/field-dsp-check — thirty "ok" lines and dsp-check: all block partitions bit-identical; any FAIL line names the processor, block size, and first diverging sample, which is what a real invariance break looks like. Then pnpm build && pnpm test — 40 files / 627 tests green (626 from main plus the dsp-check caller); the tests that would catch this PR being wrong are the byte-identity goldens in src/audio/render-daemon.test.ts (a changed hash means the restructure moved the arithmetic) and src/generators/effect-properties.test.ts (a changed measurement means it moved the sound). The check that actually pins the headline claim — review rightly pointed out that a warm cached build proves nothing about the DSP, since action keys hit whatever the arithmetic now computes — is a cold A/B: copy examples/bloom somewhere fresh, delete its .field, field build creek with this head's daemon, and byte-compare every WAV under .field/out against a warm tree's artifacts (which the pre-restructure binary rendered). On this head that is 15 of 15 artifacts byte-identical; any DIFF line names the stem whose arithmetic moved.

Realtime chunk 1: Eq, Reverb, Limiter, Resampler as stateful streaming classes with the whole-buffer functions as wrappers; per-processor latency reporting; scoped flush-to-zero in the render path; field-dsp-check proves block-size invariance and the untouched goldens prove the build unchanged. Flips realtime.md Active.
@minjudd

minjudd commented Aug 11, 2026

Copy link
Copy Markdown

Starting review of 9448368.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9448368b42

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread native/audiod/src/dsp/limiter.cpp Resolved
Comment thread native/audiod/src/dsp/denormals.h Resolved

@minjudd minjudd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 9448368. The streaming restructure itself is sound and the harness is a real instrument — but the headline claim does not hold: field build output does change, on creek, and I have the diff.

What I ran. pnpm build:native, then ./native/audiod/build/field-dsp-check — thirty ok lines and all block partitions bit-identical, exactly as the description says. pnpm typecheck clean, 40 files / 627 tests green, including the render goldens and effect-properties' measurements untouched by the diff. Then the check the description does not make: a genuine A/B, two daemons built from 79f6675 and this head, each doing a cold render of creek in the same tree.

songs/creek/track/kit-punch.wav   d1ae4edb90b7 → d04a46e31541
songs/creek/track/drums.wav       d577c5f3a422 → a89cea67c55a
songs/creek/track/phrase.wav      db8727b69dc2 → 58ba8aa1bc8d

Three of fifteen artifacts. Lengths are identical; the samples are not. Details are in the finding — the short version is that it is the denormal flush, it is inaudible, and it is real.

The limiter equivalence argument holds, which I checked before finding the above, and it is the part of this PR I would keep unchanged. The early break is exact for the reason stated: adding an arrival can only lower values, so the first sample it fails to lower is one where gain[k] ≤ gain[k+1] + slope already held, and the invariant behind it is undisturbed. The window bound is right too — a peak's reach is depth / attackSlope samples and attackSlope = 60/L, so a 4L window covers 240 dB, which is the number in the header. When I derived this on #135 I got the 60 dB figure for a one-ramp window; four ramps is that with margin, and stating it in the header is where the assumption belongs.

The resampler and the wrappers look right: limit() processes then flushes, so total output equals total input with no trimming, and latencySamples() is the graph-facing number rather than an offset the offline path applies. dsp-check covering block sizes 1 and 7 is what makes me believe the state-carrying, since those are where a ring buffer or a partial window would break first.

The docs moves are correct: Draft → Active is the readiness claim this PR is entitled to make, §8 having recorded that nothing is open, and realtime graduating next.md → now.md with chunk 1 noted matches what those files ask for.

One finding, and it takes the description's central claim with it.

Comment thread native/audiod/src/dsp/denormals.h Resolved
Drops ScopedFlushDenormals entirely (decision 6 resolved: neither path). Sizes the limiter window for the deepest reduction any finite float can demand, clamping infinities to FLT_MAX. dsp-check gains subnormal samples and a 1e20 peak. Cold A/B on creek: 15/15 artifacts byte-identical to the pre-restructure binary.
@minjudd

minjudd commented Aug 11, 2026

Copy link
Copy Markdown

Starting review of 5e4d0d3.

@minjudd minjudd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving 5e4d0d3. The claim is true now, verified by the method that falsified it: two daemons, one built at 79f6675 and one at this head, cold renders of creek in the same tree.

$ diff pre.txt new.txt
(no output)
15/15 byte-identical to the pre-restructure binary

All fifteen artifacts, where three differed before. field-dsp-check still reports thirty ok lines and all block partitions bit-identical, now over a signal carrying subnormals and a 1e20 peak, and git grep denormals.h finds nothing.

Dropping the flush from both paths is a better answer than any of the three I offered, and the reason given is one I had not reached: DAZ zeroes subnormal float32 input samples, which are representable data — a source file whose tail decays that far would be silently truncated on the way in, which is worse than the tail-shape difference I measured on the way out. Resolving decision 6 to "neither path flushes", with the double filter state making denormal stalls unreachable, keeps the requirement decision 6 exists for while giving up nothing.

The plan records what happened rather than just the outcome. The dated resolution names the mechanism, the evidence (three stems, ~760 dB down), and the cache consequence — "keeping it would have forked the cache, since no action key moves when the arithmetic environment does" — which is the part worth having on record, because the next person to reach for FTZ will meet that sentence first.

Sizing the window from FLT_MAX retires the assumption instead of widening it, which I did not ask for and is the right instinct:

deepest demandable reduction: 771.6 dB   →  capacity 3088 samples (64.3 ms)
old 4-ramp window covered:    240 dB
a 1e20 peak demands:          401.0 dB   —  past the old window

So the harness's new 1e20 peak is not decoration: it is a case the previous head's window would have got wrong, added in the same commit that makes it right. Clamping infinities to FLT_MAX is the honest edge — the whole-buffer sweep's reach on an infinite peak was the buffer itself, and the header says so rather than pretending the two agree there. And the note that a live construction may later choose a shallower horizon knowingly is the right thing to leave chunk 2.

pnpm typecheck clean, 40 files / 627 tests green, CI's linux job green on this SHA.

Everything else stands from 9448368: the limiter's early break exact under the slope invariant, the wrappers processing-then-flushing with no trimming, block sizes 1 and 7 in the harness being what makes the state-carrying believable, and the Draft → Active and next.md → now.md moves being the claims this PR is entitled to make.

No findings.

@ninjudd
ninjudd merged commit 1c91287 into main Aug 11, 2026
1 check passed
@ninjudd
ninjudd deleted the engine/streaming-dsp branch August 11, 2026 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants