summaryrefslogtreecommitdiff
path: root/src/sketch/web.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/sketch/web.zig')
-rw-r--r--src/sketch/web.zig35
1 files changed, 26 insertions, 9 deletions
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 937b491..804a4df 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -2,7 +2,7 @@
//! the imports below: drawing on a 2D canvas, GPU work on WebGPU, sound and
//! the microphone on Web Audio, files through the page. State, `update` and
//! `draw` are the same Zig as on native. There is no timeline, and nothing
-//! persists: Store lasts as long as the page.
+//! persists.
const std = @import("std");
const abi = @import("abi");
const root = @import("root.zig");
@@ -48,6 +48,8 @@ const js = struct {
extern "gpu" fn download(id: u32, offset: u32, size: u32, key: u32) void;
extern "audio" fn rate() u32;
extern "audio" fn push(key: u32, ptr: [*]const f32, len: usize, clear: u32) void;
+ /// `count` samples from a GPU buffer, queued on arrival.
+ extern "audio" fn buffer(key: u32, id: u32, offset: u32, count: u32) void;
extern "audio" fn queued(key: u32) u32;
/// Keeps the microphone open; it answers through `sk_microphone`.
extern "audio" fn listen() void;
@@ -76,14 +78,12 @@ pub fn exportWeb(comptime App: type) void {
}
fn Driver(comptime App: type) type {
- const G = root.Glue(App);
+ const G = root.Glue(App, .{});
const gpa = std.heap.wasm_allocator;
return struct {
var ready = false;
var state: G.State = undefined;
var session: G.Session = undefined;
- // No disk on the web: the store lasts as long as the page.
- var store: G.Store = undefined;
var events: [1024]abi.Event = undefined;
var event_count: usize = 0;
var input: abi.Input = .{};
@@ -105,9 +105,7 @@ fn Driver(comptime App: type) type {
.input = &input,
.scratch = scratch.allocator(),
.session = gpa,
- .store = gpa,
.session_root = std.mem.asBytes(&session),
- .store_root = std.mem.asBytes(&store),
.commands = &commands,
};
}
@@ -115,7 +113,6 @@ fn Driver(comptime App: type) type {
fn init() callconv(.c) void {
G.defaults(.state, std.mem.asBytes(&state));
G.defaults(.session, std.mem.asBytes(&session));
- G.defaults(.store, std.mem.asBytes(&store));
input.width = G.description.width;
input.height = G.description.height;
if (input.window_width == 0) {
@@ -225,6 +222,10 @@ fn Driver(comptime App: type) type {
s.pushed += v.samples.len;
js.push(v.key, v.samples.ptr, v.samples.len, @intFromBool(v.clear));
},
+ .audio_buffer => |v| {
+ stream(v.key).pushed += v.count;
+ js.buffer(v.key, @backingInt(v.buffer), v.offset, v.count);
+ },
.microphone => js.listen(),
.upload => |v| _ = js.write(@backingInt(v.buffer), v.offset, v.bytes.ptr, v.bytes.len),
.compute => |v| {
@@ -241,9 +242,9 @@ fn Driver(comptime App: type) type {
},
.download => |v| js.download(@backingInt(v.buffer), v.offset, v.size, v.key),
.open_file => |v| js.openFile(v.command.ptr, v.command.len, v.pattern.ptr, v.pattern.len),
- // Synthesised voices are not on the web yet; quitting a page
+ // Voices (and so their groups) are not on the web yet; quitting a page
// means nothing; `run` lines go after the frame.
- .voice, .quit, .run, .backdrop => {},
+ .voice, .gain, .quit, .run, .backdrop => {},
}
}
@@ -411,8 +412,10 @@ fn Driver(comptime App: type) type {
.textSize = textSize,
.loadSound = loadSound,
.decodeSound = unsupportedSound,
+ .newSound = newSound,
.samples = samples,
.readFile = readFile,
+ .appendFile = appendFile,
.env = env,
.clock = clock,
};
@@ -460,6 +463,16 @@ fn Driver(comptime App: type) type {
fn unsupportedSound(_: *anyopaque, _: []const u8) abi.Audio {
return .none;
}
+ /// Kept, unnamed, for `samples` (voices are silent on the web).
+ fn newSound(_: *anyopaque, ptr: [*]const f32, len: usize) callconv(.c) abi.Audio {
+ for (ptr[0..len]) |v| if (!std.math.isFinite(v)) return .none;
+ const copy = gpa.dupe(f32, ptr[0..len]) catch return .none;
+ sounds.append(gpa, .{ .name = "", .samples = copy }) catch {
+ gpa.free(copy);
+ return .none;
+ };
+ return @fromBackingInt(@intCast(sounds.items.len));
+ }
fn samples(_: *anyopaque, a: abi.Audio) []const f32 {
const i = @backingInt(a);
return if (i == 0 or i > sounds.items.len) &.{} else sounds.items[i - 1].samples;
@@ -467,6 +480,10 @@ fn Driver(comptime App: type) type {
fn readFile(_: *anyopaque, _: []const u8) []const u8 {
return &.{};
}
+ /// No files on the web: nothing is written, and the path is "".
+ fn appendFile(_: *anyopaque, _: [*]const u8, _: usize, _: [*]const u8, _: usize, _: bool) callconv(.c) [*:0]const u8 {
+ return "";
+ }
fn env(_: *anyopaque, _: []const u8) ?[]const u8 {
return null;
}