diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 01:01:34 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 562b503d5007e3afe1de426fc1175eadfbbb45c4 (patch) | |
| tree | 8f36dbdec4f0e8602902a656ddf036ee1e8d29b9 /web | |
| parent | f2829440b0f933422e1da5394870505d3c2d67d6 (diff) | |
| download | sketchbook-562b503d5007e3afe1de426fc1175eadfbbb45c4.tar.gz sketchbook-562b503d5007e3afe1de426fc1175eadfbbb45c4.zip | |
audio: a GPU buffer streamed within the frame, at a lead the host measures
A compute shader's output was heard only after the sketch read it back
(f.download, a frame later) and pushed it behind a lead of its own
choosing; spheres kept 100 ms, and a change it made left for the device
96 ms later on average (measured, dummy driver, 60 Hz).
f.audioBuffer(key, buffer, .{ .count, .offset }) streams `count` f32 a
shader wrote: the host copies them back while it runs the frame's
commands, waiting on the copy's fence (Executor.read: 0.2 ms on average
here, 2 ms at the 99th percentile on a loaded machine), and queues them
on the stream, so nothing is in flight between frames and the sketch
keeps no books. Input.lead says how far ahead to make sound: the slowest
of the last 64 frames' worth of samples (timed between the ends of the
frames' commands, where the pushes are), plus the device's pull (what
SDL takes from a stream at once, read from the device: 1024 here), plus
half a pull for each underrun, a sample less each frame after; capped at
8192. A sketch pushes `in.lead -| in.stream(key).queued` a frame, and
its change leaves after about what is queued ahead of it, a pull: 19 to
27 ms on average with SDL's 1024-sample pulls, 6.3 to 6.5 ms with 256
(SDL_AUDIO_DEVICE_SAMPLE_FRAMES; 17 under heavy load). Stream.underruns
counts the pushes that found a stream empty though the frame before had
fed it (a gap).
The voices keep their 2048 samples: mixed to the lead, a one-shot longer
than it is mixed over several frames, and voices.script's expect-audio
and expect-no-audio, which look one frame at a time, failed.
On the web, audio.buffer reads the buffer back with mapAsync and queues
it on arrival, in order, counting it as queued meanwhile; in.lead stays
at 100 ms there.
The measurements (a probe patched into the host for the purpose, kept
out of the tree) are in the 02-pardes-code scratch, .scratch/sketch/gpusynth.
Tests: the lead's arithmetic (audio.zig, in test-audio's filter); spheres'
play script checks its stream is fed. zig build test: 164/164 steps,
49/49 tests run (the rest cached); zig build test-audio: 48/48 steps, with
the voices sketch's wasm, so web.zig compiles; voices.script passes four
runs in four. web/host.js: node --check only, no browser run.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'web')
| -rw-r--r-- | web/host.js | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/web/host.js b/web/host.js index efc335d..32bdbfb 100644 --- a/web/host.js +++ b/web/host.js @@ -314,13 +314,16 @@ const gpu = { // Reads a buffer back; it reaches the sketch before a later frame. arrived: [], download(id, offset, size, key) { + gpu.read(id, offset, size, bytes => gpu.arrived.push({key, bytes})); + }, + read(id, offset, size, then) { const staging = gpu.spare.find(b => b.size >= size) ?? gpu.device.createBuffer({size: Math.max(size, 256), usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST}); gpu.spare = gpu.spare.filter(b => b !== staging); const commands = gpu.device.createCommandEncoder(); commands.copyBufferToBuffer(gpu.buffers[id], offset, staging, 0, size); gpu.device.queue.submit([commands.finish()]); staging.mapAsync(GPUMapMode.READ, 0, size).then(() => { - gpu.arrived.push({key, bytes: new Uint8Array(staging.getMappedRange(0, size)).slice()}); + then(new Uint8Array(staging.getMappedRange(0, size)).slice()); staging.unmap(); gpu.spare.push(staging); }, () => {}); @@ -392,7 +395,8 @@ const sound = { } }, queue(key) { - if (!sound.queues.has(key)) sound.queues.set(key, {chunks: [], at: 0, queued: 0}); + // `waiting`: GPU buffers on their way, in order (counted as queued). + if (!sound.queues.has(key)) sound.queues.set(key, {chunks: [], waiting: [], at: 0, queued: 0}); return sound.queues.get(key); }, listen() { @@ -660,9 +664,21 @@ const imports = { push(key, ptr, len, clear) { if (len) sound.hint(); const q = sound.queue(key); - if (clear) { q.chunks = []; q.at = 0; q.queued = 0; } + if (clear) { q.chunks = []; q.waiting = []; q.at = 0; q.queued = 0; } if (len) { q.chunks.push(new Float32Array(wasm.memory.buffer.slice(ptr, ptr + len * 4))); q.queued += len; } }, + buffer(key, id, offset, count) { + if (!gpu.buffers[id]) return; + sound.hint(); + const q = sound.queue(key); + const chunk = {samples: null}; + q.waiting.push(chunk); + q.queued += count; + gpu.read(id, offset, count * 4, bytes => { + chunk.samples = new Float32Array(bytes.buffer); + while (q.waiting.length && q.waiting[0].samples) q.chunks.push(q.waiting.shift().samples); + }); + }, queued: key => sound.queues.get(key)?.queued ?? 0, listen: () => sound.listen(), }, |
