Skip to content

Render pipeline chunk 5: sample-rate conversion in the daemon, and afconvert retires - #57

Merged
ninjudd merged 5 commits into
mainfrom
render/resampler
Aug 8, 2026
Merged

ninjudd merged 5 commits into
mainfrom
render/resampler

Conversation

@ninjudd

@ninjudd ninjudd commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Chunk 5, and the last of this plan. Sample-rate conversion moves into the daemon's built-in tier, normalize renders through it, say is deleted, and nothing in the build shells out to a macOS binary any more.

The resampler

Fixed-ratio polyphase FIR, Kaiser-windowed sinc prototype, 128 taps per phase. The six project rates are pairwise rational with small denominators — 44.1→48k is exactly 160/147 — so every conversion field supports has precomputable per-phase coefficient rows. The stopband edge sits at the lower of the two Nyquist frequencies, which is the one detail that makes a single routine correct in both directions.

Measured on the built binary:

Passband 100 Hz – 19 kHz flat to ±0.0000 dB
Passband at 20 kHz −0.009 dB
Downsample 96→44.1k, image rejection −103 dB at 23 kHz, −113 to −119 above
Length, 2 s at 44.1k → 48k 88200 → 96000 exactly
Same-rate conversion bit-exact identity (1:1 ratio returns the input, it does not filter it)

JUCE's WindowedSinc was rejected on evidence, not preference. interpolateImpl advances pos += speedRatio against a kernel whose width never depends on speedRatio. Downsampling therefore filters at the input Nyquist and folds everything above the output Nyquist back into the band — not a quality difference but aliasing, and disqualifying for half the conversions the schema permits. The §3 table said "probably usable"; it now says why it is not.

The one rule that is not byte-identical

Every prior chunk transliterated a TypeScript reference and could prove equality against it, so all of them landed with no rule_version bump and no re-render. This one displaced a macOS binary there was never any way to diff against, so normalize goes to version: 2 and its artifacts re-render once. That is the cost chunks 2 and 3 priced and did not incur, arriving here instead.

With no oracle, the properties are the specification

So they were mutation-tested before being believed. Six properties, measured through the build rules:

Mutation in resample.cpp Result
filter at the input Nyquist (the JUCE failure mode) 30 kHz folds into a 44.1k file at 14.1 kHz and 0 dB — aliasing property fails, five stay green
tpdf = 0.0 (no dither) sub-LSB signal quantizes to digital black — dither property fails, five stay green

Each mutation is caught by exactly one property, which is what says they carry distinct weight rather than overlapping.

The normalize golden downsamples 96k→44.1k at 24-bit deliberately: that is the direction with the anti-alias filter engaged, and it puts the dither sequence in the hashed bytes. Designing the prototype calls libm sin several thousand times, so the digest is also the check that Apple's libm and glibc still agree.

say is deleted

say.ts held the second afconvert call — the AIFF macOS say emits, converted to a WAV at the project rate. Our reader does not read AIFF, so keeping the rule meant teaching the daemon a second container format for one macOS-only rule, or keeping afconvert for that rule alone. Either way the dependency survives.

This overturns voice-generation.md decision 1, which had say staying as the hermetic in-graph recipe opposite the generated tier, so §5 there records what changed rather than diverging quietly — including the cost, which is real: that plan is deferred and not started, so bloom's creek loses the source for its two lead-vocal clips and its vocal-96k normalize until something replaces it. An imported recording is the stopgap, which is what a scratch vocal is anyway.

Deleting it means all nine platform-guarded tests retire rather than the three a resampler alone would have freed. src/testing/platform.ts goes with them: both probes had no callers left, and a file of nothing but tombstones is worse than none.

Also

The bit-depth set is generated from zod alongside the rate set rather than copied, following #54's rule that a partial copy of a schema is itself schema drift. And the pre-existing -Wfloat-equal in validateSampleRate is fixed rather than duplicated — checkObject has already proved the value is an integer, so set membership is an integer question. The native build is warning-free in our own sources again.

Review round (7 findings, all correct)

P1 — bit_depth: 32 had regressed from integer PCM to IEEE float. afconvert was asked for LEI32, and linux-portability decision 1 says that float-to-integer conversion stands, so writing format tag 3 changed the meaning of an existing schema value under a version bump that only claims to change bytes. writeWavInt handles 32 now, undithered — its step is ~186 dB down, finer than the float32 input can resolve.

It slipped because a decoded sample sits on the same grid whichever writer ran, so the grid test could not see the writer selection at all. WavHeader now exposes format and bitsPerSample and the properties assert both.

Four coverage holes, each reproduced before fixing, each now failing exactly one property while the other eight stay green:

Mutation Was Now
16-bit through the float writer 6/6 passed format property fails
tapsPerPhase 128 → 32 6/6 passed (19 kHz fell 16 dB) passband property fails
input replaced by dither alone 6/6 passed dither property fails
outN floored 6/6 passed short-source property fails

Two more in the code. A two-frame 96 kHz source floored to an empty 44.1 kHz file — a one-shot vanishing under a successful action key, where afconvert produced one frame; the rule is now round-half-up with a one-frame floor for any non-empty source. And odd data chunks went unpadded, which only 24-bit mono with an odd frame count can reach — our own reader already advanced by size + (size % 2), so the writer was out of step with its own reader.

Two doc findings, both stale in opposite directions: the plan's open questions contradicted its own Shipped status, and stem.ts told users to fix a rate mismatch with afconvert — a tool this PR deletes and Linux never had.

The golden is unchanged, which is expected rather than lucky: it is 24-bit stereo at an integral length, so it touches none of the three paths that moved.

Verification

346 tests pass, pnpm typecheck clean, native build clean and warning-free in our own sources. RENDER_PLAN_VERSION is 3.

ninjudd added 3 commits August 7, 2026 18:30
Decisions 9 and 10 recorded with the evidence behind both.

The resampler is a fixed-ratio polyphase FIR with a Kaiser-windowed sinc
prototype whose stopband edge sits at the lower of the two Nyquist
frequencies, which is what makes one routine correct in both directions.
Measured: passband flat to ±0.0000 dB from 100 Hz to 19 kHz and −0.009 dB
at 20 kHz; downsampling 96k to 44.1k rejects a 23 kHz tone by 103 dB and
everything above it by 113 to 119; lengths exact; a 1:1 ratio returns the
input bit-for-bit rather than filtering it.

JUCE's WindowedSinc was rejected on evidence rather than preference.
interpolateImpl advances pos by speedRatio against a kernel whose width
never depends on speedRatio, so downsampling filters at the input Nyquist
and folds everything above the output Nyquist back into the band. The
§3 table said "probably usable"; it is not usable for half our
conversions, and now says so.

Dither is 1-LSB TPDF from a written-out splitmix64, not <random>, because
mt19937 paired with uniform_real_distribution is not specified to give
the same sequence on libstdc++ and libc++ — which would make every
rendered artifact platform-dependent.

Nothing calls this yet; the normalize op and the generators follow.
say.ts held the second afconvert call — the AIFF that macOS say emits,
converted to a WAV at the project rate. Chunk 5 can delete the first one
by putting a resampler in the daemon, but not this one: our WAV reader
does not read AIFF, so keeping the rule meant teaching the daemon a
second container format for one macOS-only rule, or keeping afconvert
for that rule alone. Either way the macOS dependency the chunk exists to
remove survives.

So the rule goes. That retires all nine platform-guarded tests rather
than the three a resampler alone would free, and it removes the last
macOS-only rule from the graph.

This overturns voice-generation.md decision 1, which had say staying as
the hermetic in-graph recipe opposite the generated tier, so §5 there
records what changed rather than diverging quietly — including the cost,
which is real: that plan is deferred and not started, so bloom's creek
loses the source for its lead-vocal clips and its vocal-96k normalize
until something replaces it. An imported recording is the stopgap, which
is what a scratch vocal is anyway.

Docs split by kind: build-graph.md and project-model.md describe current
design and are corrected; the plans get dated amendments. build-graph.md
keeps a short tombstone because say was its illustration of the hermetic
side of the determinism boundary — and a slightly false one, since
voice: Siri followed a Settings toggle the action key could not see.
The op takes rate and depth and nothing else: resample in full precision,
then quantize once at the end. Quantizing first would put the dither
through the resampler's filter, colouring it and defeating the point.
RENDER_PLAN_VERSION goes to 3, writeWavInt handles the 16- and 24-bit
targets, and the bit-depth set is generated from zod alongside the rate
set rather than copied — a partial copy of a schema is schema drift (#54).

normalize's rule_version goes to 2 and its artifacts re-render once. This
is the only rule in the migration whose output is not byte-identical to
what it replaced, and the reason is structural: every other chunk
transliterated a TypeScript reference and could prove equality against
it, while this one displaced a macOS binary there was never any way to
diff against.

With no oracle, the properties are the entire specification, so they were
mutation-tested before being trusted. Filtering at the input Nyquist
rather than the lower of the two — the JUCE failure mode from decision 9
— folds a 30 kHz tone into a 44.1 kHz file at 14.1 kHz and 0 dB; the
aliasing property catches it and nothing else does. Removing the dither
leaves a sub-LSB signal quantizing to digital black; one property catches
that and nothing else does. The other five stayed green both times.

The three afconvert guards are gone and src/testing/platform.ts with
them: with say already removed, both probes had no callers, and a file of
nothing but tombstones is worse than none. Nothing in the build shells
out to a macOS binary any more.

Also fixes the pre-existing -Wfloat-equal in validateSampleRate rather
than adding a second instance of it: checkObject has already proved the
value is an integer, so set membership is an integer question.
Comment thread native/audiod/src/render.cpp Resolved
Comment thread native/audiod/src/dsp/resample.cpp Resolved
Comment thread native/audiod/src/dsp/wavfile.cpp Resolved
Comment thread docs/projects/all/render-pipeline.md Resolved
Comment thread src/generators/stem.test.ts Resolved
#57 review, seven findings, all correct.

P1: bit_depth: 32 had regressed from integer PCM to IEEE float. afconvert
was asked for LEI32 and linux-portability decision 1 says that
float-to-integer conversion stands, so writing format tag 3 changed the
meaning of an existing schema value under a version bump that only claims
to change bytes. writeWavInt handles 32 now, undithered — its step is
about 186 dB down, finer than the float32 input can resolve.

The reason it slipped is the more useful half: the grid test cannot see
which writer ran, because a decoded sample sits on the same grid either
way. Routing 16-bit through the float writer left all six properties
green. The properties now read the format tag and width directly.

Three more coverage holes, each reproduced before fixing and each now
failing exactly one property: taps 128 to 32 dropped 19 kHz by 16 dB with
only a 1 kHz probe watching; replacing the input sample with the dither
alone passed a test that only asserted nonzero samples existed; flooring
outN turned a two-frame 96 kHz source into an empty 44.1 kHz file, where
afconvert produced one frame.

Odd data chunks are now word-aligned. Only reachable at 24-bit mono with
an odd frame count, so the stereo golden could not exercise it — and our
own reader already advanced by size + (size % 2), so the writer was out
of step with its own reader.

Docs: the plan's open questions are closed rather than left contradicting
its Shipped status, and stem.ts no longer tells users to fix a rate
mismatch with afconvert — a tool this PR deletes and that Linux never
had.

The golden is unchanged, which is the expected result rather than a lucky
one: it is 24-bit stereo at an integral length, so it touches none of the
three paths that moved.
Comment thread native/audiod/src/dsp/resample.h Resolved
Comment thread src/audio/wav.ts Resolved
Comment thread docs/projects/all/render-pipeline.md Resolved
…acts that hid the P1

#57 second review, three findings, all correct.

P2: readWavHeader copied the outer fmt tag, so every WAVE_FORMAT_EXTENSIBLE
file reported 65534 while readWav decoded the same bytes as PCM — two
readers in one module disagreeing about one file, and wrong for a format
this module explicitly supports. It now resolves the SubFormat tag at
body + 24 as readWav does. The existing extensible fixture covers the
header path too, and reverting the resolution fails it with the
reviewer's exact 65534.

The metadata was new in this PR, which is what made it wrong rather than
merely absent: `format` is documented as the actual encoding, so
resolving extensible is part of that contract and not an extra.

P3: the DSP contracts still described the float path. resample.h said 32
was a no-op because output is float; wavfile.h and wavfile.cpp said the
writer handled 16 and 24 and that every input arrived pre-quantized. All
three now say what is true: integer PCM at all three depths, quantized in
ditherTo at 16 and 24 where the dither is, and in the writer at 32 where
it is deliberately undithered. Worth doing carefully — this is the exact
contract area whose vagueness let the P1 through.

P3: "all answered" was false. The --no-native subset question is still
open, so §7 says so and the status line says so, the way
linux-portability's does.
@ninjudd
ninjudd merged commit bdc1d5a into main Aug 8, 2026
2 checks passed
ninjudd added a commit that referenced this pull request Aug 11, 2026
…urce, the converter's lifecycle

minjudd measured the claim the plan led with and it was false: no
cross-rate session has ever compiled, clean or dirty — the all-tracks
rate check refuses the song before the freeze could serve the cached
conversion — so §1 now says what actually exists (the graph routing)
and counts three gaps, cheapest first. The refusal count is three, not
two: the engine's own constructor guard joins the list and decision 1
names its replacement. The output-length rule gains its empty-source
branch (zero in, zero out — the #57 lesson the floor-at-one exists
for). The delivery converter gets a specified lifecycle across locate,
stop, swap, and natural end, plus a recorded heard-position bound. And
realtime.md decision 7 gains the amendment reconciling its
delay-compensation clause with the aligned materialized edge.
@ninjudd
ninjudd deleted the render/resampler branch September 28, 2026 04:11
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.

1 participant