Skip to content

wavetable: band-limited rungs by pitch, - #1251

Open
rt-rtos wants to merge 4 commits into
shorepine:mainfrom
rt-rtos:feat/wavetable-ladder-frame-spread
Open

rt-rtos wants to merge 4 commits into
shorepine:mainfrom
rt-rtos:feat/wavetable-ladder-frame-spread

Conversation

@rt-rtos

@rt-rtos rt-rtos commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Four commits for WAVETABLE. The unison frame spread that was commit 2 here is now #1259.

  1. A WAVE change clears the osc's table. A fix on its own: a mid-note switch between LUT waves kept reading the previous wave's table until the next note-on.
  2. Band-limited rungs, picked by pitch. Four band-limited copies of a preset's table are built on request, and render_wavetable picks one by pitch from the set, as the saw does, so a wavetable stops folding higher up the keyboard. Nothing in the audio changes unless rungs are built.
  3. The rung builder by FFT, in fixed point. One real FFT per frame and an inverse per rung, no float: a 64-frame table builds in about 90 ms on a 240 MHz ESP32-S3 instead of 304 ms, and runs on a core without an FPU. The rungs move by at most 1 LSB. Kept separate from commit 2 so the two builders can be compared; squash at merge if you prefer.
  4. zW builds and frees a preset's rungs over the wire. zW<preset> builds, zW<preset>,0 frees; wavetable_ladder in Python.

The commit messages carry the test-by-test detail; docs/synth.md on the branch carries the user-facing semantics.

Open

  • Rung size. 256 samples per rung as it stands (128 KB per 64-frame table), or 256/128/64/32 (60 KB). The numbers and renders are in the thread below.

Settled in the thread: rungs are built on request through zW, not by default, and the frame spread is its own PR (#1259).

1. amy: a WAVE change clears the osc's table

The saw, pulse and triangle pick a table only when synth->lut is NULL, and only note-on cleared it, so a mid-note switch kept reading the previous wave's table until the next note-on. The table is now cleared when the wave changes; the same wave sent again keeps the table picked at note-on. It changes behaviour for everyone, which is why it is its own commit.

tests/test_wave_switch: SAW_DOWN -> TRIANGLE, TRIANGLE -> SAW_DOWN and TRIANGLE -> PULSE mid-note (all three read the old wave's lutset before) and a repeated wave.

2. wavetable: band-limited rungs per preset, picked by pitch like the saw

A cycle drawn with overtones to the 127th folds above about F3 at 44.1 kHz,
and a scan or a detuned cluster moves the folded partials around, which is
the metallic smear a wavetable note gets higher up the keyboard. On the
shipped tables the folded energy in the worst frame is -12 dB (111) to -5 dB
(SINE2SAW) relative to the frame at C4, -3 dB at C5.

01-wavetable-ladder.mp4
// After the table is loaded (pcm_load or a shipped preset), off the render thread:
pcm_wavetable_ladder_build(preset);
// Then any WAVETABLE osc on that preset reads the rung its pitch allows:
amy_add_message("v0w19p11d0.3n72l1Z");

The wire form and the Python keyword come with commit 4. amy.h gains
pcm_wavetable_ladder_build, pcm_wavetable_ladder_free,
pcm_wavetable_lutset and AMY_WAVETABLE_LADDER_RUNGS.

Context

  • The five ROM wavetables have no preset object, so their rungs sit in a
    static table in pcm.c, freed by pcm_deinit; asking the embedder to
    pcm_load a copy instead would cost a 32 KB copy of each shipped table
    before its ladder. A memory preset's rungs go with the preset.
  • One allocation of four times the table (128 KB for 64 frames). Publish,
    free and unload take amy_render_lock, so a block reads the whole set or
    none; the compute runs outside it. A second build rewrites the rungs in
    place, so a block rendered meanwhile may read a half-written rung once,
    and build must not run while that preset is loading or unloading. amy.h
    says so.
  • The pick is per block, where the saw's is lazy at note-on, because pitch
    moves within a note: a rung picked at note-on folds once a bend or a sweep
    carries the note above its limit. It is per group, not per osc, because
    the groups sit at octave intervals: each group reads the rung for its own
    sharpest copy, so an octave-up group does not fold on the base group's
    table. render_wavetable clears synth->lut once the copies hold their
    tables, so no osc keeps a pointer into a preset's set between blocks and a
    free or unload under the lock leaves nothing dangling. One lookup and one
    short list walk per osc per block; nothing in the per-sample path.

Verification

  • tests/test_wavetable_ladder (new, fixed point): each rung within 1 LSB of
    the series, notes byte-identical to a plain osc on the rung their pitch
    names and to no ladder below the base's limit, a two-octave cluster against
    plain oscs on its rungs at -57 dB, and the lifecycle. test_unison and
    test_pcm_memory_preset pass unchanged.
  • Host renders at 44.1 kHz of the five shipped tables, with and without
    rungs (the video). From C4 up the ladder takes 9 to 24 dB out of the notes
    (the difference against the shipped render; 111 the least, BRAIDS01 the
    most). Peaks unchanged within 0.01 dB.
  • The hard cut overshoots: 85 to 325 samples per rung of 111 clamp at int16.
    Not measured for audibility. A windowed cut would lower it and change the
    rungs' content.

3. wavetable: ladder rungs by a fixed-point FFT

Commit 2's builder redid the harmonic analysis for every rung, in float: a
soft-float call per multiply-add on a core without an FPU. This commit does
one real FFT per frame and an inverse per rung, in fixed point, no float in
the builder. About 2 KB of stack on the calling thread (the old builder
0.5 KB); amy.h says so.

64-frame table, S3 at 240 MHz build rungs
series per rung, float (commit 2) 304 ms reference
real FFT, fixed point (this commit) 88 ms 1 LSB on about 1 % of samples

A float FFT measured 56 ms on the same board and is what my own firmware
runs; fixed point is the shape that serves every target AMY ships on.
Against the series computed in double, both builders are within 0.54 LSB
peak and 0.26 LSB mean on the five shipped tables and a synthetic one.

Verification

  • test_wavetable_ladder and test_unison pass unchanged; the ladder test
    holds every rung within 1 LSB of the series.
  • A note swept across the keyboard with the frame scanned, rendered with
    each builder's rungs: -94 dB difference, no sample more than 1 LSB apart.
    The video above was rendered with commit 2's rungs.

4. wavetable: zW builds and frees a preset's ladder over the wire

zW<preset> builds, zW<preset>,0 frees, for a built-in wavetable or a memory preset. The Python keyword is wavetable_ladder: wavetable_ladder=3 sends zW3, wavetable_ladder=(3, 0) sends zW3,0. Generated JS and Godot map, and a row in docs/api.md.

  • Refused, with a log line: a preset a z load is still writing, a zF preset, and anything that is not a mono table of 16,384 samples. That includes zS recordings, which are stereo.
  • Out of memory fails with a log line and publishes nothing.
  • Loading new data on a preset's number, a built-in wavetable's too, drops its rungs.
  • The build runs inline on the thread that parses the message. That is the audio thread on Pico, Teensy and ESP with multithread=0, and on every platform when the sequencer or a CV trigger fires the zW; there the block waits for the build. amy.h and docs/api.md say so.
  • tests/test_wavetable_ladder adds build and free over the wire, a refusal after a z header whose data has not arrived, a load over a ROM wavetable's number, and a 32-frame table refused.

Notes for reviewers

  • WAVETABLE_SAMPLES_PER_CYCLE and its log2 move from oscillators.c to
    amy.h, as pcm.c needs them; the _Static_assert tying the two stays in
    oscillators.c because amy.h is also compiled as C++.
  • Commit 3's 64-bit products are written the way the AMY_HAS_MUL64 branch
    of amy_fixedpoint.h writes them, inline with the builder's own Q26 shift;
    the builder is off the render path, so it takes no 32-bit alternative.

@dpwe

dpwe commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Sorry if this is explained and I missed it, but where do these band-limited versions of the wavetable come from? I was expecting to see some kind of data prep function that generated low-passed versions of existing wavetables, but is that not included?

@rt-rtos

rt-rtos commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Updated the branch and original PR description.


@dpwe

Yes, it is in pcm_wavetable_ladder_build() in src/pcm.c is that data prep function. It runs at runtime when the embedder calls it and computes the low-passed copies from whatever table the preset holds at that moment, built-in or loaded. The first Context bullet in the body describes how it works and what it costs.

I made it an explicit call rather than something that happens by default because the copies take a lot more memory per wavetable, and spending that on every AMY build felt like your call to make, not mine. The body lists two ways to make it a default under "Not done": a config flag that builds the shipped tables at start, or copies generated into the headers behind a define. A middle road would be to ship the copies for the built-in presets and leave the builder for anything an embedder loads or generates at runtime. Doing it in the import script instead would cover every preset and import that goes through the script, but a wavetable can be made without importing anything, so if it were up to me the runtime builder stays whichever way the default goes.

One correction, with the fix pushed. I measured the build on an ESP32-S3 and it was far slower than the body claimed: about a third of a second for a 64-frame table, not tens of milliseconds, and the builder was float, so a board without an FPU would have taken seconds. The third commit on the branch replaces it with an FFT per frame in fixed point: about 90 ms for a 64-frame table on the S3, no float anywhere in it, and the copies within 1 LSB of before. The body is updated with the method and the cost, default mode is up to you; building the five shipped tables at start is now about half a second on that board.

For what it is worth, my own firmware builds a preset's copies when a patch on it loads and frees them when the last synth using it is gone. That works, with a pause on the patch change that is exactly this cost, which is what started the rework.

The updated shape makes both building at boot and dynamically at runtime less costly.

@rt-rtos

rt-rtos commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

I rewrote the description. It was cluttered, hard to follow and the update made it longer.
The test-by-test detail and the builder measurements are in the commit messages, and the long version is in the edit history. The code is unchanged.

@dpwe

dpwe commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

This is amazing. TBH I don't hear a night-and-day comparison in the sounds - those wavetable voices are already mostly pretty gritty, so the aliasing doesn't scream out to me. But I'm sure there are more exposed cases, it's definitely a real problem.

This does seem like a lot of functionality (an entire FFT!) for a rather rare use-case. That's OK, I think it's contained in its impact, and Claude assures me that the object size impact is only a couple of kB.

I don't want to turn this on by default, because of the compute time and particularly the RAM impact. As I understand it, code that links the AMY library can now call the new functions to make use, but these new functions aren't part of any published API. I would feel better if we actually exposed this somehow, I'm having Claude look at it.

@dpwe

dpwe commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this, and for splitting it into commits that can be judged separately.

On the size question: I built the changed files at -Os for ESP32-S3 and RP2040. Without AMY_WAVETABLE the PR adds about 230–330 bytes of flash and no RAM. With it, it adds about 2–3 KB of flash and 2 KB of static RAM. That's small enough, and I like that nothing changes unless rungs are built.

On "rungs by default, or on request": I'd prefer on request, through the wire protocol. At the moment only C embedders can call pcm_wavetable_ladder_build, so Python, Tulip, web and sysex users can't reach the feature. Could you add a z subcommand next to the other sample commands in amy_parse_transfer_layer_message()? For example:

  • zW<preset>: build the ladder for that preset, whether it's a built-in wavetable or a memory preset.
  • zW<preset>,0: free it.

Plus a matching entry in the amy/__init__.py table (e.g. ('wavetable_ladder', 'zWI')), the generated JS, and a line in docs/api.md. This would cover tables loaded with load_sample, recorded with zS or filled in C with pcm_load(), and nothing is built unless someone asks. I'd rather have this than a load-time flag or rungs generated into the headers.

Things I'd like it to handle:

  1. Loading in progress. A z load is asynchronous, so a zW sent straight after zL could build from a half-filled buffer. It should refuse, or wait, while a transfer or zS recording is still writing into that preset.
  2. Unsuitable presets. Reject presets streamed from disk (zF) and anything that isn't 16,384 samples.
  3. Out of memory. Fail cleanly, with a log message, if the 4× allocation fails.
  4. Thread. The z commands run on the sending thread, which suits a build that takes about 90 ms. Can you confirm that no platform parses incoming messages in the audio task? If one does, zW would need deferring the way zF does on Tulip/AMYboard.
  5. Reload. Please check that loading new data into a preset number that has a ladder drops the old rungs, not just unloading it.

I don't need slicing or per-rung rebuilds yet.

The WAVE fix in amy.c (clearing synth->lut on a mid-note wave change) looks right, but it changes behaviour for everyone. Could you make it a separate commit so it's easy to spot in the history?

@rt-rtos

rt-rtos commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, and an entire FFT felt like a lot to me too, at the start.
The first builder avoided one on purpose and computed each rung straight from the Fourier series.
On an ESP32-S3 that took about 350 ms per 64-frame table.
The fixed-point FFT that replaced it takes 88 ms and is more friendly to non-fpu targets.

The default and how to expose it were where I was stuck, which is why it is not yet plugged in anywhere.
Now that there is a shape and a go-ahead I'll get to work hooking it up and take a look at your requirements

@dpwe

dpwe commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

This is the real Dan again. It strikes me that the successively band-limited wavetables don't need to be sampled so densely; I'm pretty sure we could halve the length each time we lowpass, so the entire ladder would only occupy 2x the original wavetable (instead of 4x for the current ladder). This is the approach of the lutset structure in oscillators.c, and I'm a bit sad that we're implementing a second version rather than figuring out how to unify them.

@rt-rtos

rt-rtos commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Fair point.
The aim was to add as little new machinery as possible: the rungs are ordinary LUT entries in a lutset,
picked with choose_from_lutset and played with render_lut, the same path as the saw.
Two things keep it apart from the existing lutset code.
The saw and triangle sets are generated offline by create_lutset in headers.py,
while a wavetable is only known at runtime (loaded, recorded or written by an app),
so its rungs are built on the device. And each rung is the same size as the base table,
256 samples per frame, so render_wavetable finds frame n at n x 256 in the base and in every rung alike.
With halved rungs that step has to come from the chosen rung's table_size instead,
which is the one change your halving needs in the render path.
I'll look at the sharing again with that in mind and see whether more of it can go through the existing code.

On the sizes, a first round of numbers from a host sim (fixed point, 48 kHz, the five shipped tables, steady notes on the middle frame): energy between the harmonics relative to the harmonics, mean over the tables, lower is better.

notes no ladder ladder today (4x) 128/64/32/16 (0.94x) 256/128/64/32 (1.9x)
F#3-D#4 -36 to -39 dB -42 dB -16 dB -42 dB
F#4-D#5 -25 to -35 dB -43 dB -10 dB -20 to -23 dB
F#5-D#6 -4 to -17 dB -60 dB -28 to -35 dB -40 dB
F#6-F#7 -3 to -4 dB -46 to -52 dB -15 to -24 dB -31 dB

I'll investigate the mechanism behind these numbers before reading more into them.
My first suspect is render_lut's linear interpolation at 2 samples per cycle of a rung's top harmonic, where the saw and triangle lutsets keep about 8 and play through render_lut_cub.

Pushing the previous round of changes with an update comment before digging more into the halving and lutset stuff.

@rt-rtos
rt-rtos force-pushed the feat/wavetable-ladder-frame-spread branch 2 times, most recently from f67f337 to af4c318 Compare November 7, 2026 21:23
@rt-rtos

rt-rtos commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Pushed: five commits now. The description still covers the ladder, frame spread and FFT builder as they were; this comment covers what is new, and with zW the "rungs by default, or on request" question there is settled as on request.

  • amy: a WAVE change clears the osc's table, first and on its own. The saw, pulse and triangle pick a table only when synth->lut is NULL, and only note-on cleared it, so a mid-note switch kept reading the previous wave's table until the next note-on. It now clears the table when the wave changes; the same wave sent again keeps the table picked at note-on. tests/test_wave_switch: SAW_DOWN -> TRIANGLE, TRIANGLE -> SAW_DOWN and TRIANGLE -> PULSE mid-note (all three read the old wave's lutset before) and a repeated wave.
  • The ladder, frame spread and FFT builder as before, with two additions. The ladder commit now carries its three new amy.h constants in amy/constants.py and the generated JS, which it had left out. synth.md's frame_spread text now says when the fan does nothing: the fan is per group, so a group of one copy sits on the osc's frame, and a cluster split over a stereo pair needs four copies per group before both oscs fan.
  • wavetable: zW builds and frees a preset's ladder over the wire. zW<preset> builds, zW<preset>,0 frees, built-in wavetable or memory preset. The Python keyword is wavetable_ladder, typed 'zWL' rather than 'zWI' so the free form goes through: wavetable_ladder=3 sends zW3, wavetable_ladder=(3, 0) sends zW3,0. Generated JS and Godot map, a row in docs/api.md, and synth.md gives the wire form first. tests/test_wavetable_ladder adds build and free over the wire, a refusal after a z header whose data has not arrived, a load over a ROM wavetable's number, and a 32-frame table refused.

On your list:

  1. Loading in progress. Refused with a log line while a z load is still writing that preset (a zS recording is refused anyway, see 2). Over sysex a zW sent mid-load arrives as transfer payload; sent through amy_add_message() it is parsed, and that case is in the test.
  2. Unsuitable presets. zF presets are refused, and so is anything that is not a mono table of 16,384 samples. That includes zS recordings, which are stereo, so a recorded table needs a mono copy before zW. The render plays any number of whole 256-sample cycles; the builder keeps to the documented 16,384.
  3. Out of memory. Fails with a log line and publishes nothing.
  4. Thread. Not on every platform. Off the audio thread: desktop, web (the worklet renders, the main thread parses), live Python, ESP with the default multithread=1, and Tulip and AMYboard, where parse_sysex hands the message to the MicroPython task. On the audio thread: Pico, Teensy and ESP with multithread=0, where amy_update_tasks() polls MIDI on the loop that renders and app messages come from that loop too. And on every platform a zW fired by the sequencer or a CV-trigger template parses inside amy_execute_deltas(), on the render thread; the render lock is recursive per thread, so that stalls the block rather than deadlocking. Deferring the way zF does on Tulip does not help the single-loop platforms, which have no other thread to defer to. I left it building inline, as zF loads inline there, and documented it. The alternative is building a few frames per amy_update() call, about 1.4 ms per frame on the S3; say if you want that.
  5. Reload. Loading over a memory preset already dropped its rungs, since pcm_load() and pcm_load_file() unload the old preset first. Loading on a built-in wavetable's number did not: its rungs stayed allocated under the new preset and came back once that was unloaded. Both loaders now drop them, and the test covers it.

On amy.h's "not while that preset is being loaded or unloaded": what remains is the exposure the z load already has, another sender unloading the preset while the build reads it unlocked. Closing that would take a copy under the lock with a generation check, or deferred frees, at 32-128 KB or a new lifetime mechanism; I judged that an AMY-wide question and out of scope for right now.

@rt-rtos

rt-rtos commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

The numbers, as promised. The first round I posted overstated the cost of halving: on one of the shipped tables the halved rungs overflowed the 32-bit multiply in render_lut's linear step (more on that below). With that multiply done in 64 bits, the same sim (fixed point, 48 kHz, the five shipped tables, middle frame; energy between the harmonics relative to the harmonics, mean over the tables, worst table in brackets):

notes no ladder 256 each (4x) 128/64/32/16 (0.94x) 256/128/64/32 (1.9x)
F#3-D#4 -37 (-33) dB -42 (-35) dB -33 (-26) dB -42 (-35) dB
F#4-D#5 -31 (-25) dB -43 (-36) dB -29 (-22) dB -30 (-23) dB
F#5-D#6 -8 (-1) dB -60 (-54) dB -31 (-25) dB -40 (-34) dB
F#6-F#7 -4 (3) dB -49 (-42) dB -23 (-18) dB -31 (-25) dB

So I'd keep 256 per rung. At 256, each rung down has twice the samples per cycle of its top harmonic (4, 8, 16, 32), and the linear interpolation's images fall with it; halving holds that at 2 (128/64/32/16) or 4 (256/128/64/32), and a table with energy right at a rung's limit then images at close to the partial's own level. create_lutset keeps about 8 (2^ceil(log2(8 x hmax)), so 512/256/128/64 for these limits, 960 samples per frame against the ladder's 1024), so the ladder already sits near the lutset density.

On sharing: the rungs already go through LUT, choose_from_lutset and render_lut. What isn't shared is that render_wavetable ignores the picked entry's size and does its own pick before unison_prepare; I can make it take the stride and log2 from the entry and drop the extra pick, a few lines. Generation can't go through create_lutset, which synthesises tables offline from a harmonic list, while the ladder cuts tables that only exist at runtime.

Separately: INTERP_LINEAR multiplies (c - b) * frac in 32 bits (MUL0_SS), which wraps once neighbouring samples differ by full scale or more. Four of the five shipped wavetables have frames like that in the base table (steps up to 2.0), so the plain WAVETABLE render already wraps on those frames, ladder or not. I'll open a small PR with the fix for it.

@dpwe

dpwe commented Nov 9, 2026 via email

Copy link
Copy Markdown
Collaborator

@dpwe

dpwe commented Nov 9, 2026 via email

Copy link
Copy Markdown
Collaborator

@rt-rtos

rt-rtos commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Splitting them makes sense. I have the frame spread as a standalone commit already; it only means a rebase for whichever goes in second.

is it commonplace to have this kind of laddered waveshape?

From my understanding Mutable does not do this: Braids oversamples 2x, and Plaits stores the integrated wave and differentiates after the read (likely due to being on ~20-32kb of ram). The usual choices seem to be band-limited copies (mip-maps) or oversampling, at the cost of memory or CPU; Surge XT and Odin 2 use copies. Building the copies on request softens the memory cost: only the tables in use carry them, and nothing extra sits in flash. It also means a wavetable generated at runtime gets them. I judged the band-limited approach to be more in line with how AMY does things.

I'll come back on to you on the ear check and measurements.

The saw, pulse and triangle renders pick a table from their lutset only
when synth->lut is NULL, and only note-on clears it. A WAVE change on a
sounding osc left the old wave's table in place, so a saw switched to
TRIANGLE mid-note kept reading the saw's band-limited table, and a
triangle switched to SAW_DOWN or PULSE kept reading the triangle's,
until the next note-on. play_delta now clears synth->lut when the wave
changes, and the new wave picks its own table on the next block. The
same wave sent again keeps the table picked at note-on, as before.

tests/test_wave_switch: SAW_DOWN -> TRIANGLE, TRIANGLE -> SAW_DOWN and
TRIANGLE -> PULSE mid-note, each reading the new wave's lutset on the
block after the switch (all three read the old set before); SAW_DOWN
sent again under a bend keeping its note-on table.
A wavetable preset plays one 256-sample cycle at every pitch. A cycle
drawn with overtones to the 127th folds above about F3, and a scan or a
detuned cluster moves the folded partials around, which is the metallic
smear a wavetable note gets higher up the keyboard. The saw, pulse and
triangle pick a table by pitch from a band-limited set; the wavetable
path now does the same from a ladder an embedder builds per preset:
pcm_wavetable_ladder_build(preset) computes copies of the table cut at
63, 31, 15 and 7 harmonics (per frame, the Fourier series cut at the
limit with the mean kept, 2 * limit * 256 multiply-adds) into one
allocation, the preset's rungs are published as a lutset beside the base
table, and render_wavetable picks from it with choose_from_lutset, per
block for the osc's sharpest copy and per group through unison_prepare.
The rungs live on the memory preset and go with it; the ROM wavetables
keep theirs in a static table, freed by pcm_deinit. With no ladder built
nothing changes, which keeps the default and every reference render as
they are.

Build and free are the embedder's to call off the render thread: a few
million multiply-adds for a 64-frame table. Build publishes the set and
free (or unloading the preset) takes it back under amy_render_lock, so a
block reads the whole set or none, and the block after a free plays the
base table. A second build rewrites the rungs in place from the current
table. The osc keeps no pointer into a preset's set past its block:
render_wavetable clears synth->lut once the copies hold their tables.
WAVETABLE_SAMPLES_PER_CYCLE moves to amy.h, as pcm.c needs it too.

tests/test_wavetable_ladder: the rung limits and each rung within 1 LSB
of the series on a synthetic table; a note below the base's limit
byte-identical to no ladder; notes at 262 and 523 Hz byte-identical to a
plain osc on the rung alone; two octave groups against four plain oscs
on their two rungs (-57 dB); a rebuild following a rewritten table;
free, unload, amy_stop dropping a ROM ladder, and the refusals (stereo,
short, missing preset); no pointer into the set kept between blocks, and
a mid-note switch to SAW_DOWN reading its own table. The pitch
expectations are checked against AMY_SAMPLE_RATE in the test.
The series builder cost about 300 ms per 64-frame table on a 240 MHz
ESP32-S3: every rung redid the harmonic analysis and both loops ran on one
dependent float accumulator, and on a core without an FPU every one of its
121k multiply-adds per frame was a soft-float call. The rungs are now
computed by a real FFT per frame (the even and odd samples packed as one
complex 128-point transform, then split to the 64 bins the rungs can hold)
and an inverse per rung with the bins above the limit zeroed, in fixed
point: samples at 2^-7 in int32, twiddles at Q26 from two 256-entry int16
cosine tables, each product pair summed in 64 bits and rounded once, the
forward transform unscaled behind seven guard bits and the inverse halving
every stage. No float in the builder. About 90 ms for a 64-frame table on
that board, and on a Cortex-M0+ about 11k 64-bit multiplies per frame with
no soft-float call.

The rungs are within 0.54 LSB of the series computed in double on the five
shipped tables and a synthetic one (the series builder's own were within
0.52) and differ from the series builder's on about 1 % of samples, by
1 LSB; test_wavetable_ladder and test_unison pass unchanged. Measured and
left out because they miss 1 LSB: 16-bit data halved per forward stage
(4 to 5 LSB mean error over the seven halvings) and Q15 twiddles without
the residual table (1.3 to 2.6 LSB). A float FFT is faster where there is
an FPU (56 ms here) but leaves the FPU-less targets at tens of thousands of
soft-float calls per frame; fixed point is the one shape that serves every
target. The set shape, the contract and the pick are untouched; the header
and synth.md carry the measured cost.
pcm_wavetable_ladder_build() was reachable only from C, so Python,
Tulip, web and sysex users could not build a ladder. zW<preset> builds
the preset's rungs, built-in wavetable or memory preset, and
zW<preset>,0 frees them; nothing is built unless asked. The Python
keyword is wavetable_ladder, a list ('zWL') so the free form goes
through: wavetable_ladder=3 sends zW3, wavetable_ladder=(3, 0) sends
zW3,0. The generated JS and Godot map follow, and docs/api.md and
synth.md give the wire form first.

The builder refuses, with a log line each:
- a preset that is not a memory or built-in wavetable preset, which
  covers file presets streamed by zF;
- one that is not a mono table of 16,384 samples, the wavetable format
  in the docs (the render reads any number of whole cycles; the builder
  keeps to the format), which covers zS recordings, as they are stereo;
- one a z load is still writing. Over sysex a zW sent during a load
  arrives as payload, but one sent through amy_add_message() would read
  a half-filled table;
- and fails the same way when the rungs cannot be allocated.

pcm_load() and pcm_load_file() already unloaded the old memory preset
and its rungs; a load on a built-in wavetable's number now drops that
table's rungs too. They used to stay allocated under the new preset
and came back after it was unloaded, built from data loaded before.

Like the z load, zW plays the deltas that are due before it builds:
a reset sent just before it (RESET_ALL_OSCS, RESET_SYNTHS) unloads
the memory presets on the render thread, and played during the build
it would free the table being read. The build reads the table
unlocked, so before publishing it looks the preset up again under the
render lock and returns 0, logged, if an unload or reload replaced it;
a first build then frees its rungs instead of writing into a freed
preset. The reads, and a rebuild's writes in place, still rely on the
contract that the preset is not unloaded meanwhile.

zW builds on the thread that parses it, like the other sample
commands. On Pico, Teensy and ESP with multithread=0 that is the loop
that renders, and on every platform a zW fired by the sequencer or a
CV trigger parses on the render thread; the audio waits for the build
there.

tests/test_wavetable_ladder: zW builds and zW,0 frees; a reset sent just
before zW lands first, so nothing is built (fails without the settle);
zW refused after a z header while the data has not arrived; a load over
a ROM wavetable's number leaving no rungs once it is unloaded; a
32-frame table refused.
@rt-rtos
rt-rtos force-pushed the feat/wavetable-ladder-frame-spread branch from af4c318 to 5942e00 Compare November 9, 2026 17:00
@rt-rtos rt-rtos changed the title wavetable: band-limited rungs by pitch, and a unison frame spread wavetable: band-limited rungs by pitch, Nov 9, 2026
@rt-rtos

rt-rtos commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor Author

The ear check and the measurements, as promised.

I'm not sure exactly what they measure

There is no reference render. For each steady note I take the spectrum and split it by position: what sits on a multiple of the note's frequency counts as harmonic, everything else as between the harmonics, and the figure is the second over the first. A note of the scale never divides 48000 evenly, so anything that folds lands between the harmonics. It does depend on the table: BRAIDS01 sets every worst-table figure.

What the rung-size columns pick up is the straight-line read between table samples. The fewer samples per cycle a rung keeps for its top harmonics, the louder their images, roughly 12 to 15 dB for each halving, and the images fold. The closed form for that matches the halved-rung figures within 0.5 dB; I can post it if it is useful.

if you (Rasmus) think the "4x" tables sound appreciably better than the "1.9x"

I could go either way on 1.9x. What it leaves stays in the background, as a texture behind the note, where no ladder makes the folding a significant component of the sound.

PPG_WA00

all-under-10MB.mp4

BRAIDS01

all-under-10MB.mp4

Imported Drawbar WT

all-under-10MB.mp4

Each video runs a glide from F#3 to F#7 in 12 s, four passes with the spectrogram (linear, 0 to 24 kHz): no ladder, 256 per rung, 256/128/64/32, and last the difference between those two raised 20 dB. PPG_WA00 and BRAIDS01 are at their middle frame. The third is a drawbar-organ table of mine at its last frame: nine sine partials at the same level, the 16th the highest, which makes it a hard case for the halved rungs. Everything is a single osc with no filter.

In the later part of the PPG_WA00 glide, 1.9x has audible folding where 256 per rung is clean (-32 dB between the harmonics there, against -65 dB). BRAIDS01 with no ladder turns into a spider's web of folding, and the drawbar table shows the no-ladder case most plainly: from A6 up its top partials bounce back down like a laser beam.

What I'd keep 256 per rung for is predictability. A rung at 256 holds some of the base table's harmonics at the base table's density, so building a ladder can't make a note worse than it was without one. Halved rungs lose that: BRAIDS01 at F#4 and A4 is at -23 dB with 256/128/64/32 against -31 and -29 dB with no ladder, and the drawbar table, which folds nothing below A6 and sits at -52 dB there with no ladder, is at -40 dB from F4 and -32 dB from F5.

Over the five shipped tables halving costs 7 to 34 dB depending on table and band. The worst cell is -23 dB, so it stays clear of your 10 dB line.

The sizes: the rungs are 128 KB per table at 256 each and 60 KB halved. A loaded or generated table also holds its own 32 KB, so 160 KB against 92 KB in all. It is spent only on presets someone sent zW for, and on Tulip and AMYboard it comes from PSRAM (ram_caps_sample).

I concede that losing the predictability, and the lesser audible aliasing that remains, could be worth halving the memory cost.

or is aliasing accepted as part of the deal?

What no ladder costs depends on the table and the register. BRAIDS01 from A5 up has more energy between the harmonics than in them; VIRAL is at -60 dB up to A4 with nothing to fix. That fits building on request.

There is some merit to it as part of the sound: a high note played from only 7 harmonics comes out somewhat sterilized. I still think that is a cleaner representation of the wavetable than whatever the aliasing leaves. And where the aliasing is wanted, nothing is lost: without zW a table plays as it does today. The other direction is not available. A partial that has folded sits among the wanted ones and can't be filtered back out, while a clean note can still be made dirty on purpose with the fold and crush stages.

This branch has not been deployed

No deployments
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