diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/abi.zig | 14 | ||||
| -rw-r--r-- | src/anim/Painter.zig | 12 | ||||
| -rw-r--r-- | src/anim/root.zig | 186 | ||||
| -rw-r--r-- | src/anim/typst.zig | 10 | ||||
| -rw-r--r-- | src/gpu/Executor.zig | 26 | ||||
| -rw-r--r-- | src/host/Host.zig | 5 | ||||
| -rw-r--r-- | src/host/audio.zig | 89 | ||||
| -rw-r--r-- | src/sketch/root.zig | 7 | ||||
| -rw-r--r-- | src/sketch/web.zig | 6 | ||||
| -rw-r--r-- | src/vector/svg.zig | 96 |
10 files changed, 422 insertions, 29 deletions
diff --git a/src/abi.zig b/src/abi.zig index 286fb09..2e80a35 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 }, diff --git a/src/anim/Painter.zig b/src/anim/Painter.zig index b5d492f..f317dc1 100644 --- a/src/anim/Painter.zig +++ b/src/anim/Painter.zig @@ -56,11 +56,13 @@ pub fn fit(canvas: sk.Rect) f32 { } /// Object `o`'s drawing, centered on its origin, and its bounds. -fn built(p: *Painter, o: anim.Object, svgs: anytype) !Built { +fn built(p: *Painter, o: anim.Object, svgs: anytype) anim.BuildError!Built { const id = o.id(); if (p.drawings.get(id)) |b| return b; const arena = p.arena.?.allocator(); - const d = try anim.build(arena, o, svgs); + // A part, or what is left of an object parts were taken from: shapes + // of a whole drawing, which is built (parsed) once. + const d = if (o.source == .part or o.cut.len > 0) try anim.sift(arena, (try p.built(o.whole(), svgs)).drawing, o) else try anim.build(arena, o, svgs); const b: Built = .{ .drawing = d, .bounds = d.bounds().rect() }; try p.drawings.put(arena, id, b); return b; @@ -122,7 +124,11 @@ fn paintLayers(p: *Painter, layers: anytype, f: *sk.Frame, shot: anim.Shot, svgs // Morphing: from where this object began the transform to where // its target will stand. const morph = shot.morphs[i]; - const target = (try p.built(shot.objects[pose.into], svgs)).drawing; + // Into the whole target, parts and all: parts taken from it + // once it is made stand where they were and show as it does. + var into = shot.objects[pose.into]; + into.cut = &.{}; + const target = (try p.built(into, svgs)).drawing; const between = try vector.morph.between(f.scratch, d, world.mul(model(morph.from)), target, world.mul(model(morph.to)), pose.morph); try r.draw(between, .identity, pose.opacity); shapes += between.shapes.len; diff --git a/src/anim/root.zig b/src/anim/root.zig index 4acecb3..560d71b 100644 --- a/src/anim/root.zig +++ b/src/anim/root.zig @@ -141,6 +141,9 @@ pub const Source = union(enum) { polygon: []const [2]f32, /// Drawn by the scene's own code (`custom`). custom: Custom, + /// The shapes of `of` whose bounds' centre lies in `area` (in `of`'s + /// units), around the area's centre (`part`). + part: struct { of: *const Object, area: vector.geom.Rect }, }; /// An object the scene draws itself with 2D commands (`f.fill`, `sk.draw`, @@ -225,6 +228,8 @@ pub const Object = struct { fill: ?u32 = null, stroke: ?u32 = null, width: f32 = 4, + /// Areas whose shapes parts were taken from it: it draws the rest. + cut: []const vector.geom.Rect = &.{}, /// Identifies what the object looks like (not where it is), to cache /// its drawing by. @@ -238,11 +243,25 @@ pub const Object = struct { .circle => |c| h.update(std.mem.asBytes(&c.r)), .line => |l| h.update(std.mem.asBytes(&[_]f32{ l.dx, l.dy, @floatFromInt(@intFromBool(l.arrow)) })), .custom => |c| h.update(std.mem.asBytes(&[_]usize{ @intFromPtr(c.draw), @intFromPtr(c.ctx) })), + .part => |p| { + h.update(std.mem.asBytes(&p.of.id())); + h.update(std.mem.asBytes(&p.area)); + }, } h.update(std.mem.asBytes(&[_]u64{ o.fill orelse 1 << 32, o.stroke orelse 1 << 32 })); h.update(std.mem.asBytes(&o.width)); + h.update(std.mem.sliceAsBytes(o.cut)); return h.final(); } + + /// What a part or a cut object is drawn from (`sift`): the object + /// without its cut, or the one a part was taken from. + pub fn whole(o: Object) Object { + if (o.cut.len == 0) return o.source.part.of.*; + var w = o; + w.cut = &.{}; + return w; + } }; /// Pose fields to animate towards; the others stay as they are. @@ -526,6 +545,25 @@ const adding = struct { } }; +/// `part`'s area: `.x`, `.y` its centre (0 when left out), `.w`, `.h`. +fn areaOf(spec: anytype) vector.geom.Rect { + const w: f32 = spec.w; + const h: f32 = spec.h; + return .{ .x = number(spec, "x", 0) - w / 2, .y = number(spec, "y", 0) - h / 2, .w = w, .h = h }; +} + +/// Where a part whose origin is `c` in its object's units stands, when the +/// object stands at `p`. +fn partPose(p: Pose, c: vector.geom.Vec) Pose { + var q = p; + const at = vector.geom.Affine.rotate(p.rotation).apply(c * vector.geom.splat(p.scale)); + q.x += at[0]; + q.y += at[1]; + q.into = Pose.none; + q.morph = 0; + return q; +} + fn primitive(source: Source, spec: anytype) Object { var pose: Pose = .{}; inline for (.{ "x", "y", "scale", "rotation", "opacity", "visible" }) |name| { @@ -553,13 +591,36 @@ pub const Builder = struct { values: []const f32 = &.{}, plays: []const Play = &.{}, updaters: []const Updater = &.{}, + /// Parts, each following its object through the plays laid before it + /// was taken. + follows: []const Follow = &.{}, /// Where the next play starts. cursor: f32 = 0, + const Follow = struct { obj: u16, of: u16, plays: usize, at: vector.geom.Vec }; + pub fn add(b: *Builder, o: Object) Obj { b.objects = b.objects ++ .{o}; return @fromBackingInt(@intCast(b.objects.len - 1)); } + + /// The shapes of `of` whose bounds' centre lies in `area` (`.x`, `.y` + /// its centre, `.w`, `.h`, in `of`'s units: those of `s.rect` laid + /// over `of` at the origin, at scale 1), as an object of their own, + /// around the area's centre; `of` draws the rest. It stands where they + /// stood and shows as `of` does after the plays so far; plays after it + /// move one or the other. A shape goes to the first part that takes it. + pub fn part(b: *Builder, of: Obj, area: anytype) Obj { + const i = @backingInt(of); + const r = areaOf(area); + const whole = b.objects[i]; + var cut = whole; + cut.cut = whole.cut ++ .{r}; + b.objects = b.objects[0..i] ++ .{cut} ++ b.objects[i + 1 ..]; + const o = b.add(.{ .source = .{ .part = .{ .of = &whole, .area = r } }, .pose = partPose(whole.pose, r.center()) }); + b.follows = b.follows ++ .{Follow{ .obj = @backingInt(o), .of = i, .plays = b.plays.len, .at = r.center() }}; + return o; + } pub const svg = adding.svg; pub const typst = adding.typst; pub const rect = adding.rect; @@ -620,13 +681,23 @@ pub fn Scene(comptime construct: fn (*Builder) void) type { var value_keys: [recorded.values.len][]const Key(f32) = @splat(&.{}); var camera_keys: []const Key(Camera) = &.{}; var marks: []const f32 = &.{}; - for (recorded.plays) |play| { + for (recorded.plays, 0..) |play, played| { var buf: [2 * play.animations.len]Change = undefined; + var touched: [n]bool = @splat(false); for (changes(&buf, &poses, play.animations)) |c| { const k = play.key(Pose, c.from, c.to, c.ease); keys[c.obj] = keys[c.obj] ++ .{k}; poses[c.obj] = k.at(k.end); + touched[c.obj] = true; } + for (recorded.follows) |f| if (played < f.plays and touched[f.of]) { + var k = keys[f.of][keys[f.of].len - 1]; + k.from = partPose(k.from, f.at); + k.to = partPose(k.to, f.at); + keys[f.obj] = keys[f.obj] ++ .{k}; + poses[f.obj] = k.at(k.end); + touched[f.obj] = true; + }; for (play.animations) |a| if (a == .value) { const i = @backingInt(a.value.value); value_keys[i] = value_keys[i] ++ .{play.key(f32, values[i], a.value.to, null)}; @@ -723,9 +794,12 @@ fn latest(comptime P: type, keys: []const Key(P), t: f32) ?Key(P) { return found; } +pub const BuildError = error{ TypstNotCompiled, CustomHasNoDrawing } || vector.svg.Error; + /// The drawing of an object, centered on its origin, in pixels at scale 1. /// `compiled` has `find(source) ?[]const u8` for Typst objects' SVGs. -pub fn build(arena: std.mem.Allocator, o: Object, compiled: anytype) !Drawing { +pub fn build(arena: std.mem.Allocator, o: Object, compiled: anytype) BuildError!Drawing { + if (o.source == .part or o.cut.len > 0) return sift(arena, try build(arena, o.whole(), compiled), o); const document: ?[]const u8 = switch (o.source) { .svg => |bytes| bytes, .typst => |source| compiled.find(source) orelse return error.TypstNotCompiled, @@ -738,8 +812,14 @@ pub fn build(arena: std.mem.Allocator, o: Object, compiled: anytype) !Drawing { // Keep the SVG's pixel size (Typst's points are 4/3 px), centered. const s = if (d.view.w > 0) d.width / d.view.w else 1; const c = d.view.center(); + const centring = vector.geom.Affine.scale(s, s).mul(.translate(-c[0], -c[1])); const shapes = try arena.dupe(Drawing.Shape, d.shapes); - for (shapes) |*shape| shape.transform = vector.geom.Affine.scale(s, s).mul(.translate(-c[0], -c[1])).mul(shape.transform); + for (shapes) |*shape| { + shape.transform = centring.mul(shape.transform); + if (shape.clip) |*q| for (q) |*v| { + v.* = centring.apply(v.*); + }; + } var out = d; out.shapes = shapes; out.view = .{ .x = -d.view.w * s / 2, .y = -d.view.h * s / 2, .w = d.view.w * s, .h = d.view.h * s }; @@ -779,13 +859,43 @@ pub fn build(arena: std.mem.Allocator, o: Object, compiled: anytype) !Drawing { style.stroke = .none; } }, - .svg, .typst, .custom => unreachable, + .svg, .typst, .custom, .part => unreachable, } try b.endShape(.identity, style); const r = @max(extent(o.source), 1); return b.finish(.{ .x = -r, .y = -r, .w = 2 * r, .h = 2 * r }, 2 * r, 2 * r); } +/// The drawing of a part, or of an object parts were taken from, out of +/// `whole`, the drawing of `o.whole()`: the shapes whose bounds' centre +/// lies in the part's area, around the area's centre; or those in none of +/// the object's `cut`, where they were. Shapes and geometry are shared. +pub fn sift(arena: std.mem.Allocator, whole: Drawing, o: Object) error{OutOfMemory}!Drawing { + const areas: []const vector.geom.Rect = if (o.cut.len > 0) o.cut else &.{o.source.part.area}; + const keep_inside = o.cut.len == 0; + const c: vector.geom.Vec = if (keep_inside) areas[0].center() else .{ 0, 0 }; + const to = vector.geom.Affine.translate(-c[0], -c[1]); + var shapes: std.ArrayList(Drawing.Shape) = .empty; + for (whole.shapes) |shape| { + const b = whole.shapeBounds(shape); + const mid = shape.transform.apply((b.min + b.max) * vector.geom.splat(0.5)); + const inside = for (areas) |a| { + if (mid[0] >= a.x and mid[0] < a.x + a.w and mid[1] >= a.y and mid[1] < a.y + a.h) break true; + } else false; + if (inside != keep_inside) continue; + var kept = shape; + kept.transform = to.mul(shape.transform); + if (kept.clip) |*q| for (q) |*v| { + v.* = to.apply(v.*); + }; + try shapes.append(arena, kept); + } + var out = whole; + out.shapes = shapes.items; + if (keep_inside) out.view = .{ .x = -areas[0].w / 2, .y = -areas[0].h / 2, .w = areas[0].w, .h = areas[0].h }; + return out; +} + fn extent(s: Source) f32 { return switch (s) { .rect => |r| @max(r.w, r.h) / 2, @@ -796,7 +906,7 @@ fn extent(s: Source) f32 { for (pts) |p| m = @max(m, @max(@abs(p[0]), @abs(p[1]))); break :blk m; }, - .svg, .typst, .custom => 0, + .svg, .typst, .custom, .part => 0, }; } @@ -912,6 +1022,19 @@ pub const Frame = struct { pub const polygon = adding.polygon; pub const custom = adding.custom; + /// The shapes of `of` inside `area`, as an object of their own, showing + /// as `of` does now; `of` draws the rest (see `Builder.part`). + pub fn part(s: *Frame, of: Obj, area: anytype) Obj { + const i = @backingInt(of); + const r = areaOf(area); + const whole = s.new(s.objects.items[i]); + s.objects.items[i].cut = std.mem.concat(s.arena, vector.geom.Rect, &.{ whole.cut, &.{r} }) catch @panic("anim: out of memory"); + const o = s.add(.{ .source = .{ .part = .{ .of = whole, .area = r } }, .pose = partPose(whole.pose, r.center()) }); + s.settled.items[@backingInt(o)] = partPose(s.settled.items[i], r.center()); + s.shown.items[@backingInt(o)] = partPose(s.shown.items[i], r.center()); + return o; + } + pub fn value(s: *Frame, initial: f32) Value { s.push(&s.values, .{ .settled = initial, .shown = initial }); return @fromBackingInt(@intCast(s.values.items.len - 1)); @@ -1800,3 +1923,56 @@ test "beside runs alongside the main line; clear fades out what shows" { const gone = try live.shot(arena.allocator(), .{ .t = 2 }); try std.testing.expect(!gone.poses[0].visible and !gone.poses[1].visible and gone.duration == 2); } + +/// Three squares, 100 units each, the middle one red; the right one is +/// taken as a part once the row shows, then each moves its own way. +fn partScene(s: anytype) void { + const row = s.svg( + \\<svg viewBox="0 0 300 100" width="300" height="100"><rect width="100" height="100"/> + \\<rect x="100" width="100" height="100" fill="red"/><rect x="200" width="100" height="100"/></svg> + , .{ .x = 10, .scale = 2 }); + s.play(.{row.fadeIn()}, .{}); + const right = s.part(row, .{ .x = 100, .w = 80, .h = 80 }); + s.play(.{ right.move(.{ .y = 50 }), row.move(.{ .x = 0 }) }, .{}); +} + +test "a part takes its shapes from its object, stands where they stood and follows it until taken" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const compiled = Scene(struct { + fn construct(s: *Builder) void { + partScene(s); + } + }.construct); + const live = Live(struct { + fn frame(s: *Frame) void { + partScene(s); + } + }.frame, struct {}); + for ([_]f32{ 0, 0.5, 1, 1.5, 2, 3 }) |t| { + const a = try compiled.shot(arena.allocator(), .{ .t = t }); + const b = try live.shot(arena.allocator(), .{ .t = t }); + try std.testing.expectEqualSlices(Pose, a.poses, b.poses); + try std.testing.expectEqual(a.objects[0].id(), b.objects[0].id()); + try std.testing.expectEqual(a.objects[1].id(), b.objects[1].id()); + } + // Fading in with the row, before it was taken; where the right square + // stood (10 + 2 * 100) once it was; then on its own. + try std.testing.expectEqual(compiled.pose(0, 0.5).opacity, compiled.pose(1, 0.5).opacity); + try std.testing.expectEqual(Pose{ .x = 210, .scale = 2, .visible = true }, compiled.pose(1, 1)); + try std.testing.expectEqual(@as(f32, 50), compiled.pose(1, 2).y); + try std.testing.expectEqual(@as(f32, 0), compiled.pose(0, 2).x); + try std.testing.expectEqual(@as(f32, 210), compiled.pose(1, 2).x); + // The row keeps two squares; the part is the third, around its centre. + const no_typst = struct { + fn find(_: []const u8) ?[]const u8 { + return null; + } + }; + const rest = try build(arena.allocator(), compiled.objects[0], no_typst); + const taken = try build(arena.allocator(), compiled.objects[1], no_typst); + try std.testing.expectEqual(@as(usize, 2), rest.shapes.len); + try std.testing.expectEqual(@as(usize, 1), taken.shapes.len); + try std.testing.expectEqual(vector.geom.Rect{ .x = -50, .y = -50, .w = 100, .h = 100 }, taken.bounds().rect()); + try std.testing.expectEqual(vector.geom.Rect{ .x = -150, .y = -50, .w = 200, .h = 100 }, rest.bounds().rect()); +} diff --git a/src/anim/typst.zig b/src/anim/typst.zig index d9df734..05a0d4e 100644 --- a/src/anim/typst.zig +++ b/src/anim/typst.zig @@ -50,12 +50,18 @@ pub fn generate(comptime SceneFile: type, comptime sources: []const []const u8, try out.writeFile(io, .{ .sub_path = "typst.zig", .data = module }); } -/// Runs `typst compile` on `preamble ++ source` and caches the SVG. +/// Runs `typst compile` on `preamble ++ source` and caches the SVG. The +/// root is `/`, so an absolute path in a source is the file's own path +/// (`#image("/home/me/manual.pdf", page: 31)`) and a relative one is +/// in the cache. Typst only reads what a source names, and a scene's +/// sources are its author's code, built by a build that can already read +/// anything. A file named this way is not part of the cache key: changed +/// in place, it keeps its old SVG until its entry in the cache goes. fn compile(gpa: std.mem.Allocator, io: std.Io, cache: std.Io.Dir, name: []const u8, preamble: []const u8, source: []const u8) ![]const u8 { const typ = try std.fmt.allocPrint(gpa, "{s}.typ", .{name}); try cache.writeFile(io, .{ .sub_path = typ, .data = try std.mem.concat(gpa, u8, &.{ preamble, "\n", source, "\n" }) }); const result = try std.process.run(gpa, io, .{ - .argv = &.{ "typst", "compile", "--format", "svg", typ, name }, + .argv = &.{ "typst", "compile", "--root", "/", "--format", "svg", typ, name }, .cwd = .{ .dir = cache }, }); if (result.term != .exited or result.term.exited != 0) { diff --git a/src/gpu/Executor.zig b/src/gpu/Executor.zig index 460b2d5..797dcbf 100644 --- a/src/gpu/Executor.zig +++ b/src/gpu/Executor.zig @@ -503,6 +503,32 @@ pub fn download(g: *Gpu, d: @FieldType(abi.Command, "download")) !void { }; } +/// Copies `out.len` bytes of a buffer from `offset` back now, waiting for +/// the GPU to finish the work submitted before (sound that must be queued +/// this frame). +pub fn read(g: *Gpu, buffer: abi.Buffer, offset: u32, out: []u8) !void { + const source = g.storage(buffer) orelse return error.UnknownBuffer; + const size: u32 = @intCast(out.len); + if (size == 0 or @as(u64, offset) + size > source.size) return error.DownloadOutOfBounds; + const transfer = for (g.spare.items, 0..) |t, i| { + if (t.size >= size) break g.spare.swapRemove(i); + } else Transfer{ + .buffer = c.SDL_CreateGPUTransferBuffer(g.device, &.{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD, .size = size, .props = 0 }) orelse return error.GpuBuffer, + .size = size, + }; + defer g.spare.append(g.gpa, transfer) catch c.SDL_ReleaseGPUTransferBuffer(g.device, transfer.buffer); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const copy = c.SDL_BeginGPUCopyPass(cmd); + c.SDL_DownloadFromGPUBuffer(copy, &.{ .buffer = source.buffer, .offset = offset, .size = size }, &.{ .transfer_buffer = transfer.buffer, .offset = 0 }); + c.SDL_EndGPUCopyPass(copy); + const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; + defer c.SDL_ReleaseGPUFence(g.device, fence); + if (!c.SDL_WaitForGPUFences(g.device, true, &fence, 1)) return error.GpuFence; + const mapped: [*]const u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, transfer.buffer, false) orelse return error.GpuMap); + defer c.SDL_UnmapGPUTransferBuffer(g.device, transfer.buffer); + @memcpy(out, mapped[0..size]); +} + /// The downloads the GPU has finished, copied into `arena`, in the order /// they were asked for. pub fn collect(g: *Gpu, arena: std.mem.Allocator) ![]const abi.Download { diff --git a/src/host/Host.zig b/src/host/Host.zig index 4f45ace..002c704 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -486,6 +486,7 @@ fn draw(self: *Host, now: u64, started: u64) !u64 { } } self.input.streams = self.audio.streamStatus(); + self.input.lead = self.audio.lead; self.input.sample_rate = sound.sample_rate; self.input.microphone = self.audio.record(arena); self.input.downloads = if (self.passes) |*g| try g.collect(arena) else &.{}; @@ -513,6 +514,10 @@ fn draw(self: *Host, now: u64, started: u64) !u64 { .voice => |voice| self.audio.declare(voice), .gain => |g| self.audio.gains[g.group] = g.gain, .audio => |a| self.audio.push(a.key, a.samples, a.clear), + .audio_buffer => |a| if (self.passes) |*g| if (a.count > 0) { + const samples = try arena.alloc(f32, a.count); + if (g.read(a.buffer, a.offset, std.mem.sliceAsBytes(samples))) self.audio.push(a.key, samples, false) else |err| self.print("gpu audio: {t} ({s})", .{ err, c.SDL_GetError() }); + }, .quit => self.quit = true, .gpu => |pass| if (self.passes) |*g| self.runGpu(g, pass) catch |err| self.print("gpu pass: {t} ({s})", .{ err, c.SDL_GetError() }), .post_process => |p| if (self.passes) |*g| self.runPostProcess(g, p.source, p.target, p.effect) catch |err| self.print("gpu postprocess: {t} ({s})", .{ err, c.SDL_GetError() }), diff --git a/src/host/audio.zig b/src/host/audio.zig index bf5fdc4..e95e38e 100644 --- a/src/host/audio.zig +++ b/src/host/audio.zig @@ -28,6 +28,19 @@ pub const Audio = struct { heard_gains: [8]f32 = @splat(1), streams: std.ArrayList(Stream) = .empty, status: std.ArrayList(abi.Stream) = .empty, + /// Samples the device takes from each stream at once. + pull: u32 = 1024, + /// The wall-clock time (ns) between the ends of the last frames' + /// commands, where pushes are done; and when the last ended. + intervals: [64]u64 = @splat(0), + frame: u64 = 0, + last_frame: u64 = 0, + /// Grown by underruns, shrinking back while there are none. + margin: u32 = 0, + /// A stream ran dry since the lead was last worked out. + dry: bool = false, + /// `Input.lead`: see `lead`. + lead: u32 = 4800, scope: Scope = .{}, /// The default recording device, bound to this stream while declared. microphone: ?*c.SDL_AudioStream = null, @@ -44,12 +57,25 @@ pub const Audio = struct { /// Mixed since a script last asked (`expect-audio`). sounded: bool = false, }; - const Stream = struct { key: u32, out: *c.SDL_AudioStream, pushed: u64 = 0 }; + const Stream = struct { + key: u32, + out: *c.SDL_AudioStream, + pushed: u64 = 0, + /// Pushed to this frame, and the last. + fed: bool = false, + was_fed: bool = false, + underruns: u32 = 0, + }; pub fn open(self: *Audio) !void { const device = c.SDL_OpenAudioDevice(c.SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec); if (device == 0) return error.OpenAudio; self.device = device; + var format: c.SDL_AudioSpec = undefined; + var frames: c_int = 0; + if (c.SDL_GetAudioDeviceFormat(device, &format, &frames) and frames > 0 and format.freq > 0) { + self.pull = @intCast(@divFloor(@as(i64, frames) * sample_rate + format.freq - 1, format.freq)); + } self.voices_out = try self.bind(); self.scope.attach(device) catch {}; } @@ -176,9 +202,18 @@ pub const Audio = struct { if (clear) { _ = c.SDL_ClearAudioStream(s.out); s.pushed = 0; + s.was_fed = false; } if (samples.len == 0) return; + // Fed last frame and empty now: the device came up short since + // (counted at the push, as a pull before it would hide it by the + // next frame). + if (s.was_fed and !s.fed and c.SDL_GetAudioStreamQueued(s.out) == 0) { + s.underruns += 1; + self.dry = true; + } if (c.SDL_PutAudioStreamData(s.out, samples.ptr, @intCast(samples.len * @sizeOf(f32)))) s.pushed += samples.len; + s.fed = true; } fn streamFor(self: *Audio, key: u32) ?*Stream { @@ -192,14 +227,22 @@ pub const Audio = struct { return &self.streams.items[self.streams.items.len - 1]; } - /// Drops undeclared voices, keeps the voice mix queued ahead, and opens - /// or closes the microphone. + /// Drops undeclared voices, works out the lead for the next frame, keeps + /// the voice mix queued ahead, and opens or closes the microphone. pub fn endFrame(self: *Audio) void { self.reconcileMicrophone(); var i: usize = 0; while (i < self.voices.items.len) { if (self.voices.items[i].seen) i += 1 else _ = self.voices.swapRemove(i); } + const now = c.SDL_GetTicksNS(); + if (self.last_frame != 0) { + self.intervals[self.frame % self.intervals.len] = now -| self.last_frame; + self.frame += 1; + } + self.last_frame = now; + self.lead = lead(self.intervals, self.pull, &self.margin, self.dry); + self.dry = false; const out = self.voices_out orelse return; const queued = c.SDL_GetAudioStreamQueued(out); if (queued < 0) return; @@ -257,16 +300,33 @@ pub const Audio = struct { return false; } + /// The streams as a frame starts. pub fn streamStatus(self: *Audio) []const abi.Stream { self.status.clearRetainingCapacity(); - for (self.streams.items) |s| { - const queued = c.SDL_GetAudioStreamQueued(s.out); - self.status.append(self.gpa, .{ .key = s.key, .queued = @intCast(@divFloor(@max(queued, 0), @sizeOf(f32))), .pushed = s.pushed }) catch {}; + for (self.streams.items) |*s| { + const queued: u32 = @intCast(@divFloor(@max(c.SDL_GetAudioStreamQueued(s.out), 0), @sizeOf(f32))); + s.was_fed = s.fed; + s.fed = false; + self.status.append(self.gpa, .{ .key = s.key, .queued = queued, .pushed = s.pushed, .underruns = s.underruns }) catch {}; } return self.status.items; } }; +/// How many samples to keep queued: the slowest of the last frames' worth +/// (pushes come once a frame, `intervals` apart), plus the device's pull +/// (it takes that much at once), plus `margin`: half a pull more at each +/// underrun (`dry`), back down by a sample a frame. +fn lead(intervals: [64]u64, pull: u32, margin: *u32, dry: bool) u32 { + var slowest: u64 = 0; + for (intervals) |ns| slowest = @max(slowest, ns); + margin.* = if (dry) @min(margin.* + pull / 2, max_lead) else margin.* -| 1; + const frame: u32 = @intCast(@min(slowest * sample_rate / std.time.ns_per_s, 1 << 20)); + return @min(frame + pull + margin.*, max_lead); +} +/// 170 ms: a frame slower than that is a stall, not a pace. +const max_lead = 8192; + /// Where the voice mix stops being passed through: -2.5 dBFS. Quieter /// mixes keep their true volume to the bit; a clip normalised to full scale /// only has its peaks bent (1 comes out at 0.875). @@ -462,3 +522,20 @@ test "voices at another rate are resampled linearly, and a group's gain scales i try std.testing.expect(out[0] > 0.375 and out[0] < 0.38); try std.testing.expectEqual(@as(f32, 0.5), out[1023]); } + +test "the lead the mixer gives streams outlasts the slowest recent frame and the device's pull, more after an underrun" { + const expectEqual = std.testing.expectEqual; + var intervals: [64]u64 = @splat(20 * std.time.ns_per_ms); + var margin: u32 = 0; + // 50 Hz and a pull of 1024: a frame's 960 samples and the pull. + try expectEqual(@as(u32, 960 + 1024), lead(intervals, 1024, &margin, false)); + // One 50 ms frame in the window sets it. + intervals[5] = 50 * std.time.ns_per_ms; + try expectEqual(@as(u32, 2400 + 1024), lead(intervals, 1024, &margin, false)); + // An underrun adds half a pull, which goes a sample a frame. + try expectEqual(@as(u32, 2400 + 1024 + 512), lead(intervals, 1024, &margin, true)); + try expectEqual(@as(u32, 2400 + 1024 + 511), lead(intervals, 1024, &margin, false)); + // A stall is not a pace: the lead stops at 170 ms. + intervals[6] = std.time.ns_per_s; + try expectEqual(@as(u32, max_lead), lead(intervals, 1024, &margin, false)); +} diff --git a/src/sketch/root.zig b/src/sketch/root.zig index 9f4782f..2a7c64a 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -215,6 +215,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 } }); } diff --git a/src/sketch/web.zig b/src/sketch/web.zig index 49b9d42..804a4df 100644 --- a/src/sketch/web.zig +++ b/src/sketch/web.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; @@ -220,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| { diff --git a/src/vector/svg.zig b/src/vector/svg.zig index 31d9b15..f1b7daf 100644 --- a/src/vector/svg.zig +++ b/src/vector/svg.zig @@ -4,11 +4,12 @@ //! `ellipse`, `line`, `polyline`, `polygon`. Styling: presentation //! attributes and `style="..."`, inherited as SVG specifies; solid colors, //! linear and radial gradients; fill rules; stroke width, caps, joins, miter -//! limit and dashes; `opacity`, `fill-opacity`, `stroke-opacity`. +//! limit and dashes; `opacity`, `fill-opacity`, `stroke-opacity`. Images +//! that are SVG documents in base64 data URIs (Typst's SVG and PDF images). //! //! Not supported: text (Typst outlines it), filters, masks, clipping, -//! patterns, markers, images, CSS stylesheets. Group opacity is approximated -//! by multiplying it into each shape. +//! patterns, markers, raster images, CSS stylesheets. Group opacity is +//! approximated by multiplying it into each shape. const std = @import("std"); const geom = @import("math"); const Drawing = @import("Drawing.zig"); @@ -23,23 +24,26 @@ pub fn parse(arena: std.mem.Allocator, source: []const u8) Error!Drawing { const root = doc.root orelse return error.InvalidSvg; if (!std.mem.eql(u8, doc.nodes.items[root].name, "svg")) return error.InvalidSvg; - var p: Parser = .{ .doc = &doc, .b = .init(arena), .arena = arena, .view = .{} }; - const n = doc.nodes.items[root]; - const w = doc.attr(n, "width"); - const h = doc.attr(n, "height"); + const port = viewport(&doc, doc.nodes.items[root]); + var p: Parser = .{ .doc = &doc, .b = .init(arena), .arena = arena, .view = port.view }; + try p.children(root, .identity, .{}); + return p.b.finish(port.view, port.width, port.height); +} + +/// A root `svg` element's view box and size in pixels, each standing in +/// for the other when missing. +fn viewport(doc: *const Document, n: Node) struct { view: geom.Rect, width: f32, height: f32 } { var view: geom.Rect = .{}; if (doc.attr(n, "viewBox")) |vb| { var nums = Numbers{ .text = vb }; view = .{ .x = nums.next() orelse 0, .y = nums.next() orelse 0, .w = nums.next() orelse 0, .h = nums.next() orelse 0 }; } - var width = if (w) |v| lengthPx(v, view.w) else view.w; - var height = if (h) |v| lengthPx(v, view.h) else view.h; + var width = if (doc.attr(n, "width")) |v| lengthPx(v, view.w) else view.w; + var height = if (doc.attr(n, "height")) |v| lengthPx(v, view.h) else view.h; if (view.w <= 0 or view.h <= 0) view = .{ .w = width, .h = height }; if (width <= 0) width = view.w; if (height <= 0) height = view.h; - p.view = view; - try p.children(root, .identity, .{}); - return p.b.finish(view, width, height); + return .{ .view = view, .width = width, .height = height }; } // --------------------------------------------------------------------------- @@ -183,7 +187,7 @@ const Parser = struct { fn element(p: *Parser, index: u32, parent_transform: Affine, parent: Inherited) Error!void { const n = p.doc.nodes.items[index]; const name = n.name; - const skip = [_][]const u8{ "defs", "symbol", "linearGradient", "radialGradient", "clipPath", "mask", "pattern", "marker", "style", "title", "desc", "metadata", "filter", "text", "image", "foreignObject" }; + const skip = [_][]const u8{ "defs", "symbol", "linearGradient", "radialGradient", "clipPath", "mask", "pattern", "marker", "style", "title", "desc", "metadata", "filter", "text", "foreignObject" }; for (skip) |s| if (std.mem.eql(u8, name, s)) return; if (p.depth > 64) return; var inherited = parent; @@ -229,6 +233,52 @@ const Parser = struct { } return p.element(target, transform, inherited); } + if (std.mem.eql(u8, name, "image")) { + // An SVG as a base64 data URI, Typst's way to place an SVG or a + // PDF page: its document, mapped into the image's box. Raster + // images draw nothing. + const href = p.doc.attr(n, "href") orelse p.doc.attr(n, "xlink:href") orelse return; + const prefix = "data:image/svg+xml;base64,"; + if (!std.mem.startsWith(u8, href, prefix)) return; + const decoder = std.base64.standard.Decoder; + const bytes = try p.arena.alloc(u8, decoder.calcSizeForSlice(href[prefix.len..]) catch return); + decoder.decode(bytes, href[prefix.len..]) catch return; + var doc = Document.parse(p.arena, bytes) catch |err| switch (err) { + error.OutOfMemory => return err, + error.InvalidSvg => return, + }; + const root = doc.root orelse return; + if (!std.mem.eql(u8, doc.nodes.items[root].name, "svg")) return; + const port = viewport(&doc, doc.nodes.items[root]); + const v = port.view; + const w = if (p.doc.attr(n, "width")) |a| lengthPx(a, p.view.w) else port.width; + const h = if (p.doc.attr(n, "height")) |a| lengthPx(a, p.view.h) else port.height; + if (v.w <= 0 or v.h <= 0 or w <= 0 or h <= 0) return; + transform = transform.mul(.translate(p.number(n, "x"), p.number(n, "y"))); + // Stretched with `preserveAspectRatio="none"`, else centered at + // the largest scale that fits; drawn unclipped. + if (std.mem.startsWith(u8, std.mem.trim(u8, p.doc.attr(n, "preserveAspectRatio") orelse "", " "), "none")) { + transform = transform.mul(.scale(w / v.w, h / v.h)); + } else { + const s = @min(w / v.w, h / v.h); + transform = transform.mul(.translate((w - v.w * s) / 2, (h - v.h * s) / 2)).mul(.scale(s, s)); + } + transform = transform.mul(.translate(-v.x, -v.y)); + // A document of its own: its ids, gradients and percentages, and + // nothing inherited but opacity. + const outer = p.*; + defer { + p.doc = outer.doc; + p.view = outer.view; + p.gradients = outer.gradients; + } + p.doc = &doc; + p.view = v; + p.gradients = .empty; + p.depth += 1; + defer p.depth -= 1; + return p.children(root, transform, .{ .style = .{ .opacity = inherited.style.opacity } }); + } const b = &p.b; if (std.mem.eql(u8, name, "path")) { @@ -766,3 +816,23 @@ test "colors and lengths" { try std.testing.expectEqual(@as(f32, 20), parseLength("15pt")); try std.testing.expectEqual(@as(f32, 0.5), parseOpacity("50%")); } + +test "an SVG image in a data URI: its own ids, placed, stretched or fitted" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + // <svg viewBox="0 0 10 20"><defs><path id="p" d="M0 0h10v20h-10z"/></defs><use href="#p" fill="#00ff00"/></svg> + const inner = "data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMTAgMjAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PHBhdGggaWQ9InAiIGQ9Ik0wIDBoMTB2MjBoLTEweiIvPjwvZGVmcz48dXNlIGhyZWY9IiNwIiBmaWxsPSIjMDBmZjAwIi8+PC9zdmc+"; + const d = try parse(arena.allocator(), "<svg viewBox=\"0 0 100 100\"><defs><path id=\"p\" d=\"M0 0h1v1z\"/></defs>" ++ + "<image x=\"5\" y=\"5\" width=\"20\" height=\"20\" preserveAspectRatio=\"none\" href=\"" ++ inner ++ "\"/>" ++ + "<image x=\"5\" y=\"5\" width=\"20\" height=\"20\" xlink:href=\"" ++ inner ++ "\"/>" ++ + "<image href=\"data:image/png;base64,AAAA\"/></svg>"); + try std.testing.expectEqual(@as(usize, 2), d.shapes.len); + for (d.shapes) |shape| try std.testing.expectEqual(Drawing.Color{ 0, 1, 0, 1 }, shape.style.fill.color); + // The far corner (10, 20) of the image's own path: stretched to the + // box's corner, or fitted into its middle. + for (d.shapes, [_]geom.Vec{ .{ 25, 25 }, .{ 20, 25 } }) |shape, corner| { + const far = d.cubicsOf(d.subpathsOf(shape)[0])[1][3]; + try std.testing.expectEqual(geom.vec(10, 20), far); + try std.testing.expectEqual(corner, shape.transform.apply(far)); + } +} |
