@maruaudio/dsp/tables/looped-region reads a bounded region of a host-loaded f32 sample table in play-order frames: forward or reversed, unlooped or looped continuously with a crossfade, with the (integer, fraction) cursor that plays it. Reversal is one subtraction (frames − 1 − index) applied last, at the read, so loop points and the crossfade span stay in play coordinates and mirror with it. A crossfaded loop blends its last span frames into the span frames that precede loopStart, with a smoothstep weight of t = (p + 1) / span that sums the two sides with equal gain, so the frame at loopEnd − 1 is loopStart − 1 itself and the wrap continues it.
Functions
| Name | Inputs | Output | Domain as coded |
|---|---|---|---|
regionFrame | table, base, frames, reverse, index | one frame | index assumed in [0, frames) |
loopedRegionFrameAt | region, loopMode, loopStart, loopEnd, crossfade, index | frame with the crossfade applied | 0 at or past frames; span clamped to the run-up before loopStart and to the loop length |
loopCrossfadeBlend | value, tail, position, crossfade | equal-gain smoothstep blend | Weight reaches exactly 1 at the span's last frame |
loopNextIndex | index, loopMode, loopStart, loopEnd | interpolation partner | In a continuous loop the frame after loopEnd − 1 is loopStart |
lerp | a, b, fraction | a + (b − a) × fraction | |
reverseReadIndex | frames, index | frames − 1 − index | |
loopWrapIndex | index, loopStart, loopEnd | index wrapped by whole loop lengths | Unchanged for an empty loop or an index inside it |
advanceLoopedCursor | cursor arrays, voice, step, loopMode, loopStart, loopEnd | writes the voice's index and f32 fraction | Fraction stored through Math.fround |
Loop modes are LOOPED_REGION_LOOP_NONE (0) and LOOPED_REGION_LOOP_CONTINUOUS (1); ping-pong is deliberately absent.
Lifecycle
None of its own. The caller owns the table, the region bounds and the Uint32Array index and Float32Array fraction cursors, and calls advanceLoopedCursor once per frame per voice. The table is never written.
When to use it
A sampler voice that loops a sustain region of a recording, possibly reversed or repitched, with a seam crossfade. voices/sampler-zone reads its zones through it.
Limits
- Mono reads; linear interpolation. Allocation-free with no loops on the sample path: the wrap is one integer division, not a
while, so Maru compiler's realtime prover needs no bound. - A loop starting at play frame 0 has no run-up and gets no crossfade; one starting at frame 200 gets at most 200 frames.
- The stored fraction is the one f32 boundary; every other value is a double.