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 /src/sketch/root.zig | |
| 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 'src/sketch/root.zig')
| -rw-r--r-- | src/sketch/root.zig | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/sketch/root.zig b/src/sketch/root.zig index d15046d..0ab04a7 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -219,6 +219,13 @@ pub const Frame = struct { pub fn audio(f: *Frame, key: u32, samples: []const f32, options: struct { clear: bool = false }) void { f.emit(.{ .audio = .{ .key = key, .samples = samples, .clear = options.clear } }); } + /// Streams `count` samples a compute shader wrote to `buffer` (f32 from + /// `offset`) on stream `key`, as `audio` streams samples: a GPU synth's + /// output, heard without a readback of the sketch's own. Make + /// `in.lead -| in.stream(key).queued` of them a frame. + pub fn audioBuffer(f: *Frame, key: u32, buffer: abi.Buffer, options: struct { offset: u32 = 0, count: u32 }) void { + f.emit(.{ .audio_buffer = .{ .key = key, .buffer = buffer, .offset = options.offset, .count = options.count } }); + } pub fn pixels(f: *Frame, img: Image, w: u32, h: u32, rgba: []const u8, options: struct { premultiplied: bool = false }) void { f.emit(.{ .pixels = .{ .image = img, .w = w, .h = h, .rgba = rgba, .premultiplied = options.premultiplied } }); } |
