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/abi.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/abi.zig')
| -rw-r--r-- | src/abi.zig | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/abi.zig b/src/abi.zig index 35c399b..271c6dc 100644 --- a/src/abi.zig +++ b/src/abi.zig @@ -165,6 +165,9 @@ pub const Stream = struct { queued: u32 = 0, /// Samples pushed since the stream was last cleared. pushed: u64 = 0, + /// Pushes that found it empty though the frame before had pushed to + /// it: each a gap in its sound. + underruns: u32 = 0, }; /// Everything a sketch learns about the world for one call. @@ -218,6 +221,11 @@ pub const Input = struct { keys: std.StaticBitSet(256) = .empty, streams: []const Stream = &.{}, sample_rate: u32 = 48_000, + /// How many samples a stream should hold after this frame's push, as + /// the host measures it: enough to outlast the slowest recent frame and + /// the device's next pull, and more after an underrun. A sketch that + /// makes its sound as it goes pushes `lead -| stream(key).queued`. + lead: u32 = 4800, /// Mono samples at `sample_rate` recorded since the previous frame, while /// `draw` declares `Command.microphone`. Only `draw` sees them: they come /// from a device, not from events, so they do not replay. @@ -377,6 +385,12 @@ pub const Command = union(enum) { voice: Voice, /// Mono samples at `Input.sample_rate` for stream `key`. audio: struct { key: u32, samples: []const f32, clear: bool = false }, + /// `count` mono samples (f32) that a compute shader left in `buffer` + /// at `offset`, for stream `key`: queued like `audio`'s once the GPU + /// has made them. Natively that is within this frame (the host waits + /// for the GPU's work so far); on the web they arrive a frame or two + /// later and count as queued meanwhile. + audio_buffer: struct { key: u32, buffer: Buffer, offset: u32 = 0, count: u32 }, /// The gain of a group of voices (`Voice.group`) this frame; a group /// not given one plays at 1. Declared like a voice, so it replays. gain: struct { group: u3, gain: f32 }, |
