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 | 8e8110e357b22bbbc2d1e566f24d2e33e6ecb0fe (patch) | |
| tree | 06d89aadd338b67adaa9de7c9bc05d7bfb09a25d /examples/spheres/src/main.zig | |
| parent | 562b503d5007e3afe1de426fc1175eadfbbb45c4 (diff) | |
| download | sketchbook-8e8110e357b22bbbc2d1e566f24d2e33e6ecb0fe.tar.gz sketchbook-8e8110e357b22bbbc2d1e566f24d2e33e6ecb0fe.zip | |
spheres: its sound streamed from the GPU's block, at the host's lead
The synth block goes to the speakers with f.audioBuffer, made to keep
in.lead queued; in_flight, discard and lead_seconds are gone, and a
chord's jump is the stream's clear alone (nothing is on its way back to
drop). The block is still read back, a frame later, for the scope.
The sound is the same: with the block fixed at 800 samples a frame, the
play script and a script that plays every chord key (seven jumps) push
the same samples before and after, to the bit (849600 and 1164007 of
them), the new one a frame sooner. A change leaves for the device in
19 to 27 ms on average (three one-minute runs on a loaded machine)
instead of 96 (dummy driver, 60 Hz, SDL's 1024-sample pulls). The
empty Store declaration the brief mentioned is not in this sketch
(main.zig says "No Store"); nothing was touched there.
Tests: zig build test, 75/75 steps, 3/3 tests (its play script now also
expects the stream fed).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/spheres/src/main.zig')
| -rw-r--r-- | examples/spheres/src/main.zig | 31 |
1 files changed, 9 insertions, 22 deletions
diff --git a/examples/spheres/src/main.zig b/examples/spheres/src/main.zig index 60b498f..062aebd 100644 --- a/examples/spheres/src/main.zig +++ b/examples/spheres/src/main.zig @@ -27,8 +27,6 @@ pub const height = 800; /// The audio stream's key. const stream = 1; -/// How much sound is kept queued ahead: synthesised, read back, queued. -const lead_seconds = 0.1; /// Systems integrated: system 0 and its cloud. const systems = layouts.max_systems; /// How far apart the copies start. @@ -87,8 +85,6 @@ const Travel = struct { to: chords.Pick, speed: f64 }; pub const Session = struct { reset: sk.Handled = .{}, started: bool = false, - /// Synthesised samples not back from the GPU yet. - in_flight: usize = 0, /// System 0 as last read back, and whether it has been. lead: layouts.Lead = std.mem.zeroes(layouts.Lead), have_lead: bool = false, @@ -113,8 +109,6 @@ pub const Session = struct { travel: ?Travel = null, /// The snapshot whose chord is held. held: ?chords.Pick = null, - /// Samples still to come back from before a chord was chosen, dropped. - discard: usize = 0, gpu: Gpu = .{}, }; @@ -418,18 +412,13 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { const g = &ss.gpu; setup(g, f); - // What came back: sound for the speakers and the scope, and system 0. + // What came back: the sound, for the scope, and system 0. for (in.downloads) |d| switch (@as(Readback, @enumFromInt(d.key))) { .audio => { const samples = std.mem.bytesAsSlice(f32, d.bytes[0 .. d.bytes.len / 4 * 4]); const aligned = f.scratch.alloc(f32, samples.len) catch continue; for (aligned, samples) |*to, from| to.* = from; - ss.in_flight -|= aligned.len; - // Sound made before a chord was chosen is not played. - const dropped = @min(ss.discard, aligned.len); - ss.discard -= dropped; - f.audio(stream, aligned[dropped..], .{}); - ss.scope.push(aligned[dropped..]); + ss.scope.push(aligned); }, .lead => if (d.bytes.len >= @sizeOf(layouts.Lead)) { ss.lead = std.mem.bytesToValue(layouts.Lead, d.bytes[0..@sizeOf(layouts.Lead)]); @@ -456,7 +445,6 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { ss.held = pick; ss.travel = .{ .to = pick, .speed = @max(@abs(pick.time() - ss.time) / 1.2, 2) }; f.audio(stream, &.{}, .{ .clear = true }); - ss.discard = ss.in_flight; } }; @@ -517,13 +505,12 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { ss.trail_filled = @min(ss.trail_filled + trail.points, layouts.trail); f.download(g.lead, .{ .size = @sizeOf(layouts.Lead), .key = @intFromEnum(Readback.lead) }); - // The sound: enough to keep `lead_seconds` queued, counting what is - // still on its way back from the GPU. - const queued = in.stream(stream).queued + ss.in_flight; - const want: usize = @min(@as(usize, @intFromFloat(lead_seconds * rate)) -| queued, layouts.max_block); + // The sound: as much as keeps the host's lead queued, streamed straight + // from the GPU's block; and read back for the scope. + const want: u32 = @min(in.lead -| in.stream(stream).queued, layouts.max_block); if (want > 0) { const params = gpu.uniform(f.scratch, layouts.SynthParams{ - .count = @intCast(want), + .count = want, .mode = @intFromEnum(s.mode), .rate = @floatCast(rate), // Loud enough for a phone's speaker; tanh rounds the peaks off. @@ -535,11 +522,11 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { .readonly = &.{ g.voices, g.phases }, .readwrite = &.{g.block}, .uniforms = params, - .groups = .{ @intCast((want + 63) / 64), 1, 1 }, + .groups = .{ (want + 63) / 64, 1, 1 }, }); f.compute(.{ .label = "advance.comp", .kernel = advance_kernel, .readwrite = &.{ g.voices, g.phases }, .uniforms = params }); - f.download(g.block, .{ .size = @intCast(want * @sizeOf(f32)), .key = @intFromEnum(Readback.audio) }); - ss.in_flight += want; + f.audioBuffer(stream, g.block, .{ .count = want }); + f.download(g.block, .{ .size = want * @sizeOf(f32), .key = @intFromEnum(Readback.audio) }); } const l = Layout.of(in.width, in.height, s.instrument); |
