Skip to content

Bulk PCM16LE codec builtins: buf_from_pcm16le / buf_to_pcm16le / buf_deinterleave (DAW WAV import is per-sample interpreted) #602

Description

@InauguralPhysicist

Measured demand (DeslanStudio, N3350)

Importing a 50 s stereo 48 kHz WAV costs 10.8 s in wav_read — the PCM16 byte decode is a per-sample interpreted loop (src/tools/wavio.eigs): two buf_gets, the s16 two's-complement fold, and / 32767 per sample, ~4.8 M times. The write path (wav_write) is the same shape in reverse. This is the byte-decode sibling of the #597 mix-down kernels, same contract: one C loop, VM-identical arithmetic.

Builtins

  • buf_from_pcm16le of [bytes, byte_off, count] → NEW float buffer of count samples decoded from little-endian signed 16-bit PCM at byte_off. Arithmetic exactly matches wavio's read loop: v = b0 + 256*b1; if v >= 32768: v -= 65536; sample = v / 32767 (num_guard per step, so the builtin is bit-identical to the interpreted loop).
  • buf_to_pcm16le of [floats, off, count] → NEW byte buffer (2*count elements), exactly wavio's write loop: clamp to [-1, 1], v = round(x * 32767), if v < 0: v += 65536, low byte v - floor(v/256)*256 (the ds_fmod expansion), high byte floor(v/256).
  • buf_deinterleave of [src, channel, nch, count?] → NEW buffer of every nch-th sample starting at index channel (frame-interleaved channel split, wavio's (i*nch + c) addressing). count defaults to the full available tail; explicit count is window-checked.

All three: pure compute over arguments → sandbox pure-compute allowlist; deterministic (nothing to tape); loud bounds per the #597 family (value / index_range raises, no silent truncation); available freestanding.

DoD

  • Differential suite leg: each builtin exactly equals the interpreted wavio loop on seeded pseudo-random data (byte-identical doubles), including a decode→deinterleave composition vs wav_read's fused loop, and encode→decode round-trip parity with the interpreted round trip.
  • Loud-bounds leg (bad windows, odd shapes, non-buffer, bad channel/nch).
  • Sandbox allowlist entries + n=5 micro-bench (interp vs builtin) in the PR body.
  • Full release + ASan suites; BUILTINS.md rows.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions