summaryrefslogtreecommitdiff
path: root/src/scope
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-28 21:07:13 -0300
committerGabriel Schneider <[email protected]>2026-11-12 13:22:02 -0300
commit590ba7b9b579b97e34b51ab7e3460ebc85713343 (patch)
tree6a73a7570ff4f79f41deff078455877b9bc6571b /src/scope
parentbc8e6eb1bfef0c5e9b3c40fc1e4fd5a1659e2b03 (diff)
downloadsketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.tar.gz
sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.zip
sketchbook: Zig 0.17, a Store on disk, requests, a scope (waveform, spectrum, spectrogram), audio codecs and fonts, web host
Diffstat (limited to 'src/scope')
-rw-r--r--src/scope/layouts.zig120
-rw-r--r--src/scope/root.zig394
-rw-r--r--src/scope/shaders/spectrogram.comp208
-rw-r--r--src/scope/shaders/spectrogram.frag45
-rw-r--r--src/scope/shaders/spectrum.comp128
-rw-r--r--src/scope/shaders/spectrum.frag51
-rw-r--r--src/scope/shaders/view.vert7
-rw-r--r--src/scope/shaders/waveform.comp72
-rw-r--r--src/scope/shaders/waveform.frag56
9 files changed, 1081 insertions, 0 deletions
diff --git a/src/scope/layouts.zig b/src/scope/layouts.zig
new file mode 100644
index 0000000..d8fc484
--- /dev/null
+++ b/src/scope/layouts.zig
@@ -0,0 +1,120 @@
+//! What the shaders read, as Zig structs: the only definition. The shaders
+//! include GLSL generated from these (see sketchbook's `glsl.zig`). No
+//! imports, so the shader build can compile this alone.
+
+/// FFT length: the analysis window, zero-padded to twice its size so the
+/// spectrum is sampled twice as finely as the window resolves.
+pub const fft_size = 8192;
+pub const window = 4096;
+/// The most columns a view can have; the column buffers hold this many.
+pub const max_columns = 2048;
+/// The tallest spectrogram; its buffer holds this many rows of
+/// `max_columns`.
+pub const max_rows = 1024;
+// All three analyses read samples `offset + i` of their sample buffer for
+// `0 <= i < samples`, so one buffer can hold several sounds.
+/// The spectrogram's window, zero-padded to `spectrogram_fft` points.
+pub const spectrogram_window = 2048;
+pub const spectrogram_fft = 4096;
+
+/// spectrum.comp: the window ending at `end`, and the columns to fill.
+pub const SpectrumParams = extern struct {
+ offset: u32,
+ /// Sample index one past the window's last sample.
+ end: u32,
+ /// Samples on the tape.
+ samples: u32,
+ columns: u32,
+ /// Nonzero: forget the smoothed levels and peaks.
+ reset: u32,
+ rate: f32,
+ min_freq: f32,
+ max_freq: f32,
+ /// Seconds since the previous analysis, for smoothing.
+ dt: f32,
+ pad0: u32 = 0,
+ pad1: u32 = 0,
+ pad2: u32 = 0,
+
+ pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } };
+};
+
+/// waveform.comp: column `c` covers samples `first + c * spp` up to the next
+/// column's start, and is written to slot `(slot + c) % ring`: the live
+/// view writes its newest columns into a ring, the others from slot 0.
+pub const WaveParams = extern struct {
+ offset: u32,
+ first: u32,
+ samples: u32,
+ columns: u32,
+ spp: f32,
+ slot: u32,
+ ring: u32,
+ pad: u32 = 0,
+
+ pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } };
+};
+
+pub const Tint = enum(u32) { take, recording, live };
+
+/// waveform.frag. Pixel column `x` shows slot `(x + scroll) % width`; only
+/// the last `filled` columns have anything in them.
+pub const WaveView = extern struct {
+ size: [2]f32,
+ /// Pixels; negative hides it.
+ playhead: f32,
+ gain: f32,
+ /// A `Tint`.
+ tint: u32,
+ scroll: u32,
+ filled: u32,
+ pad: u32 = 0,
+
+ pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } };
+};
+
+/// spectrum.frag.
+pub const SpectrumView = extern struct {
+ size: [2]f32,
+ floor_db: f32,
+ ceil_db: f32,
+ min_freq: f32,
+ max_freq: f32,
+ pad: [2]f32 = .{ 0, 0 },
+
+ pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } };
+};
+
+/// spectrogram.comp: the same columns as `WaveParams`, each analysed into
+/// `rows` log-spaced frequency rows, row 0 the lowest.
+/// Columns land in slots as `WaveParams` says.
+pub const SpectrogramParams = extern struct {
+ offset: u32,
+ first: u32,
+ samples: u32,
+ columns: u32,
+ rows: u32,
+ spp: f32,
+ rate: f32,
+ min_freq: f32,
+ max_freq: f32,
+ slot: u32,
+ ring: u32,
+ pad: u32 = 0,
+
+ pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } };
+};
+
+/// spectrogram.frag; `scroll` and `filled` as in `WaveView`.
+pub const SpectrogramView = extern struct {
+ size: [2]f32,
+ floor_db: f32,
+ ceil_db: f32,
+ /// Pixels; negative hides it.
+ playhead: f32,
+ scroll: u32,
+ filled: u32,
+ pad: f32 = 0,
+
+ pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } };
+};
diff --git a/src/scope/root.zig b/src/scope/root.zig
new file mode 100644
index 0000000..a71fa53
--- /dev/null
+++ b/src/scope/root.zig
@@ -0,0 +1,394 @@
+//! Live views of a stream of sound, analysed and drawn on the GPU: the last
+//! few seconds as a scrolling waveform over its spectrogram, the smoothed
+//! spectrum of the newest samples, and an oscilloscope. A sketch feeds a
+//! `Scope` (kept in its Session) with `push` and draws it once a frame with
+//! `draw`, giving the rectangles of the views it wants.
+//!
+//! The analyses (see the shaders for the details):
+//! - spectrogram: a 2048-point Blackman-Harris window per column, and below
+//! 500 Hz a second analysis decimated 4× (6 Hz resolution) averaged over
+//! three windows; log-frequency rows;
+//! - spectrum: a 4096-point window zero-padded to 8192, log-frequency
+//! columns, fast attack and slow release, falling peaks;
+//! - waveform: min, max and RMS per column, joined into a line when zoomed
+//! past one sample per column.
+//!
+//! Columns of the scrolling views are analysed once, when everything their
+//! windows reach has arrived, into rings the views read from the oldest; so
+//! the spectrogram runs `reach` samples (about 130 ms) behind the newest.
+const std = @import("std");
+const sk = @import("sketch");
+const gpu = sk.abi.gpu;
+pub const layouts = @import("scope_layouts");
+const shaders = @import("scope_shaders");
+
+pub const min_freq = 20.0;
+pub const floor_db = -96.0;
+/// The samples kept: enough for a frame's new columns and all they read.
+pub const history = 1 << 16;
+/// How far the spectrogram reads past a column's centre (its long
+/// analysis: 2048 points decimated 4×, three windows a quarter apart).
+pub const reach = 6160;
+
+pub const Tint = layouts.Tint;
+
+/// Where and how to draw; views left null are not analysed.
+pub const Views = struct {
+ /// Seconds the waveform and spectrogram span, ending now.
+ seconds: f32 = 6,
+ /// Both scroll together, column for column: give them the same width.
+ wave: ?sk.Rect = null,
+ spectrogram: ?sk.Rect = null,
+ spectrum: ?sk.Rect = null,
+ oscilloscope: ?Oscilloscope = null,
+ gain: f32 = 1,
+ tint: Tint = .live,
+
+ pub const Oscilloscope = struct {
+ rect: sk.Rect,
+ /// The samples across it.
+ samples: u32 = 1024,
+ /// How many samples before the newest it ends: a sketch steadies a
+ /// periodic wave by ending it at the same phase each frame.
+ end: u32 = 0,
+ };
+};
+
+pub const Scope = struct {
+ samples: [history]f32 = @splat(0),
+ /// Samples pushed since the start.
+ heard: u64 = 0,
+ /// Pushed since the last `draw`, so the GPU copy is stale.
+ fresh: bool = false,
+ /// The next column to analyse, counted from the first sample; how many
+ /// have been since the view was (re)made; their count and width.
+ next: u64 = 0,
+ filled: u32 = 0,
+ columns: u32 = 0,
+ spp: u32 = 0,
+ gpu: Gpu = .{},
+
+ pub fn push(s: *Scope, heard: []const f32) void {
+ if (heard.len == 0) return;
+ const h = &s.samples;
+ if (heard.len >= h.len) {
+ @memcpy(h, heard[heard.len - h.len ..]);
+ } else {
+ std.mem.copyForwards(f32, h[0 .. h.len - heard.len], h[heard.len..]);
+ @memcpy(h[h.len - heard.len ..], heard);
+ }
+ s.heard += heard.len;
+ s.fresh = true;
+ }
+
+ /// Analyses what is new and draws the views; needs `f.host.gpu()`.
+ pub fn draw(s: *Scope, f: *sk.Frame, v: Views) void {
+ const g = &s.gpu;
+ const rate: f64 = @floatFromInt(f.in.sample_rate);
+ g.setup(f);
+ if (f.in.reloaded) g.forget();
+ if (s.fresh) f.upload(g.tape, 0, std.mem.sliceAsBytes(&s.samples));
+ defer s.fresh = false;
+
+ if (v.wave orelse v.spectrogram) |r| s.scroll(f, v, r, rate);
+ if (v.spectrum) |r| s.spectrum(f, r, rate);
+ if (v.oscilloscope) |o| s.oscilloscope(f, v, o);
+ }
+
+ /// The waveform and spectrogram, scrolling.
+ fn scroll(s: *Scope, f: *sk.Frame, v: Views, r: sk.Rect, rate: f64) void {
+ const g = &s.gpu;
+ const columns: u32 = @intFromFloat(std.math.clamp(r.w, 1, layouts.max_columns));
+ const spp: u32 = @max(1, @as(u32, @intFromFloat(@round(v.seconds * rate / @as(f64, @floatFromInt(columns))))));
+ if (columns != s.columns or spp != s.spp or f.in.reloaded) {
+ s.columns = columns;
+ s.spp = spp;
+ s.filled = 0;
+ s.next = 0;
+ }
+ const rows: u32 = if (v.spectrogram) |sr| @intFromFloat(std.math.clamp(sr.h, 1, layouts.max_rows)) else 1;
+ g.levelsFor(f, rows);
+
+ // History index of sample `i`: `i - base`. A column is ready once
+ // all it reads has arrived, and analysable while all it reads is kept.
+ const base: i64 = @as(i64, @intCast(s.heard)) - history;
+ const ready = (s.heard -| reach) / spp;
+ const kept: u64 = @intCast(@max(0, @divFloor(base + reach + spp - 1, spp)));
+ s.next = @max(s.next, kept, ready -| columns);
+ if (ready > s.next) {
+ const count: u32 = @intCast(ready - s.next);
+ const first: u32 = @intCast(@as(i64, @intCast(s.next * spp)) - base);
+ const slot: u32 = @intCast(s.next % columns);
+ if (v.wave != null) f.compute(.{
+ .label = "scope waveform.comp",
+ .kernel = waveform_kernel,
+ .readonly = &.{g.tape},
+ .readwrite = &.{g.wave.columns},
+ .uniforms = gpu.uniform(f.scratch, layouts.WaveParams{ .offset = 0, .first = first, .samples = history, .columns = count, .spp = @floatFromInt(spp), .slot = slot, .ring = columns }),
+ .groups = .{ count, 1, 1 },
+ });
+ if (v.spectrogram != null) f.compute(.{
+ .label = "scope spectrogram.comp",
+ .kernel = spectrogram_kernel,
+ .readonly = &.{g.tape},
+ .readwrite = &.{g.levels},
+ .uniforms = gpu.uniform(f.scratch, layouts.SpectrogramParams{
+ .offset = 0,
+ .first = first,
+ .samples = history,
+ .columns = count,
+ .rows = rows,
+ .spp = @floatFromInt(spp),
+ .rate = @floatCast(rate),
+ .min_freq = min_freq,
+ .max_freq = maxFreq(rate),
+ .slot = slot,
+ .ring = columns,
+ }),
+ .groups = .{ count, 1, 1 },
+ });
+ s.filled = @intCast(@min(columns, s.filled + count));
+ s.next = ready;
+ }
+
+ const scroll_at: u32 = @intCast(s.next % columns);
+ if (v.wave) |wr| {
+ const size = target(f, &g.wave, wr, layouts.max_rows);
+ const view: layouts.WaveView = .{
+ .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) },
+ .playhead = -1,
+ .gain = v.gain,
+ .tint = @intFromEnum(v.tint),
+ .scroll = scroll_at,
+ .filled = s.filled,
+ };
+ if (g.wave.redraw(std.hash.Wyhash.hash(0, std.mem.asBytes(&view)))) wavePass(f, &g.wave, size, view);
+ f.image(g.wave.image, wr, .{});
+ }
+ if (v.spectrogram) |sr| {
+ const size = target(f, &g.spectrogram, sr, rows);
+ const view: layouts.SpectrogramView = .{
+ .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) },
+ .floor_db = floor_db,
+ .ceil_db = -6,
+ .playhead = -1,
+ .scroll = scroll_at,
+ .filled = s.filled,
+ };
+ if (g.spectrogram.redraw(std.hash.Wyhash.hash(0, std.mem.asBytes(&view)))) f.gpu(.{
+ .label = "scope spectrogram.frag",
+ .image = g.spectrogram.image,
+ .width = size[0],
+ .height = size[1],
+ .clear = null,
+ .pipelines = &spectrogram_pipelines,
+ .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, view), .fragment_storage = &.{g.levels} }},
+ });
+ f.image(g.spectrogram.image, sr, .{});
+ }
+ }
+
+ /// The spectrum of the newest samples, smoothed frame to frame.
+ fn spectrum(s: *Scope, f: *sk.Frame, r: sk.Rect, rate: f64) void {
+ const g = &s.gpu;
+ const size = target(f, &g.spectrum, r, layouts.max_rows);
+ const dt: f64 = @min(@as(f64, @floatFromInt(f.in.dt)) / std.time.ns_per_s, 0.1);
+ f.compute(.{
+ .label = "scope spectrum.comp",
+ .kernel = spectrum_kernel,
+ .readonly = &.{g.tape},
+ .readwrite = &.{ g.scratch, g.spectrum.columns },
+ .uniforms = gpu.uniform(f.scratch, layouts.SpectrumParams{
+ .offset = 0,
+ .end = history,
+ .samples = history,
+ .columns = size[0],
+ .reset = @intFromBool(!g.spectrum_started),
+ .rate = @floatCast(rate),
+ .min_freq = min_freq,
+ .max_freq = maxFreq(rate),
+ .dt = @floatCast(dt),
+ }),
+ });
+ g.spectrum_started = true;
+ f.gpu(.{
+ .label = "scope spectrum.frag",
+ .image = g.spectrum.image,
+ .width = size[0],
+ .height = size[1],
+ .clear = null,
+ .pipelines = &spectrum_pipelines,
+ .draws = &.{.{
+ .pipeline = 0,
+ .vertex_count = 3,
+ .fragment_uniforms = gpu.uniform(f.scratch, layouts.SpectrumView{
+ .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) },
+ .floor_db = floor_db,
+ .ceil_db = 0,
+ .min_freq = min_freq,
+ .max_freq = maxFreq(rate),
+ }),
+ .fragment_storage = &.{g.spectrum.columns},
+ }},
+ });
+ f.image(g.spectrum.image, r, .{});
+ }
+
+ /// The newest `samples` (less `end`), as a line.
+ fn oscilloscope(s: *Scope, f: *sk.Frame, v: Views, o: Views.Oscilloscope) void {
+ const g = &s.gpu;
+ const size = target(f, &g.oscilloscope, o.rect, layouts.max_rows);
+ const n: u32 = @min(o.samples, history / 2);
+ const end: u32 = @min(o.end, history / 2);
+ const first: u32 = history - n - end;
+ f.compute(.{
+ .label = "scope oscilloscope",
+ .kernel = waveform_kernel,
+ .readonly = &.{g.tape},
+ .readwrite = &.{g.oscilloscope.columns},
+ .uniforms = gpu.uniform(f.scratch, layouts.WaveParams{
+ .offset = 0,
+ .first = first,
+ .samples = history,
+ .columns = size[0],
+ .spp = @as(f32, @floatFromInt(n)) / @as(f32, @floatFromInt(size[0])),
+ .slot = 0,
+ .ring = layouts.max_columns,
+ }),
+ .groups = .{ size[0], 1, 1 },
+ });
+ wavePass(f, &g.oscilloscope, size, .{
+ .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) },
+ .playhead = -1,
+ .gain = v.gain,
+ .tint = @intFromEnum(v.tint),
+ .scroll = 0,
+ .filled = size[0],
+ });
+ f.image(g.oscilloscope.image, o.rect, .{});
+ }
+};
+
+/// The highest frequency the views show.
+pub fn maxFreq(rate: f64) f32 {
+ return @floatCast(@min(20_000, rate / 2));
+}
+
+/// Where `hz` falls across a spectrum view (or up a spectrogram, from its
+/// bottom): 0 to 1, log frequency.
+pub fn place(hz: f64, rate: f64) f32 {
+ return @floatCast(@log(hz / min_freq) / @log(maxFreq(rate) / min_freq));
+}
+
+// ---------------------------------------------------------------------------
+// GPU objects.
+
+/// One view's image and columns, and the inputs it was last drawn from.
+const View = struct {
+ image: sk.Image = .none,
+ size: [2]u32 = .{ 0, 0 },
+ columns: sk.Buffer = .none,
+ drawn: u64 = 0,
+
+ fn redraw(v: *View, key: u64) bool {
+ if (key == v.drawn) return false;
+ v.drawn = key;
+ return true;
+ }
+};
+
+const Gpu = struct {
+ ready: bool = false,
+ tape: sk.Buffer = .none,
+ scratch: sk.Buffer = .none,
+ levels: sk.Buffer = .none,
+ levels_rows: u32 = 0,
+ wave: View = .{},
+ spectrogram: View = .{},
+ spectrum: View = .{},
+ oscilloscope: View = .{},
+ spectrum_started: bool = false,
+
+ fn setup(g: *Gpu, f: *sk.Frame) void {
+ if (g.ready) return;
+ g.ready = true;
+ g.tape = f.host.newBuffer(history * @sizeOf(f32));
+ g.scratch = f.host.newBuffer(layouts.fft_size * 2 * @sizeOf([2]f32));
+ g.wave.columns = f.host.newBuffer(layouts.max_columns * @sizeOf([4]f32));
+ g.oscilloscope.columns = f.host.newBuffer(layouts.max_columns * @sizeOf([4]f32));
+ g.spectrum.columns = f.host.newBuffer(layouts.max_columns * @sizeOf([2]f32));
+ }
+
+ /// The spectrogram's levels, `rows` of `max_columns`.
+ fn levelsFor(g: *Gpu, f: *sk.Frame, rows: u32) void {
+ if (rows <= g.levels_rows) return;
+ if (g.levels != .none) f.host.freeBuffer(g.levels);
+ g.levels = f.host.newBuffer(layouts.max_columns * rows * @sizeOf(f32));
+ g.levels_rows = rows;
+ g.forget();
+ }
+
+ /// New shaders, or new buffers: draw everything again.
+ fn forget(g: *Gpu) void {
+ g.wave.drawn = 0;
+ g.spectrogram.drawn = 0;
+ g.spectrum_started = false;
+ }
+};
+
+/// The view's image at `r`'s size, (re)made when that changes.
+fn target(f: *sk.Frame, v: *View, r: sk.Rect, max_h: u32) [2]u32 {
+ const want: [2]u32 = .{
+ @intFromFloat(std.math.clamp(r.w, 1, layouts.max_columns)),
+ @intFromFloat(std.math.clamp(r.h, 1, @as(f32, @floatFromInt(max_h)))),
+ };
+ if (v.image == .none or v.size[0] != want[0] or v.size[1] != want[1]) {
+ if (v.image != .none) f.host.freeImage(v.image);
+ v.image = f.host.newImage(want[0], want[1]);
+ v.size = want;
+ v.drawn = 0;
+ }
+ return want;
+}
+
+fn wavePass(f: *sk.Frame, v: *View, size: [2]u32, view: layouts.WaveView) void {
+ f.gpu(.{
+ .label = "scope waveform.frag",
+ .image = v.image,
+ .width = size[0],
+ .height = size[1],
+ .clear = null,
+ .pipelines = &wave_pipelines,
+ .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, view), .fragment_storage = &.{v.columns} }},
+ });
+}
+
+fn viewPipeline(fragment: []const u8) gpu.Pipeline {
+ return .{
+ .vertex = .{ .code = shaders.view_vert },
+ .fragment = .{ .code = fragment, .uniform_buffers = 1, .storage_buffers = 1 },
+ .stride = 0,
+ .attributes = &.{},
+ .blend = .none,
+ };
+}
+const wave_pipelines = [_]gpu.Pipeline{viewPipeline(shaders.waveform_frag)};
+const spectrum_pipelines = [_]gpu.Pipeline{viewPipeline(shaders.spectrum_frag)};
+const spectrogram_pipelines = [_]gpu.Pipeline{viewPipeline(shaders.spectrogram_frag)};
+
+const spectrum_kernel: gpu.Kernel = .{ .code = shaders.spectrum_comp, .readonly_buffers = 1, .readwrite_buffers = 2, .uniform_buffers = 1, .threads = .{ 256, 1, 1 } };
+const spectrogram_kernel: gpu.Kernel = .{ .code = shaders.spectrogram_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1, .threads = .{ 256, 1, 1 } };
+const waveform_kernel: gpu.Kernel = .{ .code = shaders.waveform_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1, .threads = .{ 64, 1, 1 } };
+
+test "the history keeps the newest samples in order" {
+ var s: Scope = .{};
+ s.push(&.{ 1, 2, 3 });
+ s.push(&.{4});
+ try std.testing.expectEqualSlices(f32, &.{ 1, 2, 3, 4 }, s.samples[history - 4 ..]);
+ try std.testing.expectEqual(@as(u64, 4), s.heard);
+}
+
+test "frequencies place on a log axis" {
+ try std.testing.expectEqual(@as(f32, 0), place(min_freq, 48_000));
+ try std.testing.expectApproxEqAbs(@as(f32, 1), place(20_000, 48_000), 1e-6);
+}
diff --git a/src/scope/shaders/spectrogram.comp b/src/scope/shaders/spectrogram.comp
new file mode 100644
index 0000000..b32f4c5
--- /dev/null
+++ b/src/scope/shaders/spectrogram.comp
@@ -0,0 +1,208 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// The spectrogram, one workgroup per column, the same columns as the
+// waveform (waveform.comp), so the two line up in time.
+//
+// Two resolutions per column, because one window cannot serve both ends of
+// a log axis: 2048 samples resolve 23 Hz, which is sharp in time and fine
+// in the kilohertz, but leaves 20 to 100 Hz (a quarter of the height) to
+// four bins. So the low end is analysed again from the signal low-passed
+// and decimated to a quarter of the rate: the same 2048 points then span
+// 170 ms and resolve 6 Hz. Rows take the long analysis below 250 Hz, the
+// short one above 500 Hz, and blend in between.
+//
+// A single window's spectrum of noise scatters as much as its level (each
+// bin is chi-squared with 2 degrees of freedom), which in dB shows as
+// speckle full of black holes, and finer bins only make more of them. So
+// the long analysis averages three windows a quarter window apart in time
+// (Welch): steady lines stay as sharp, noise settles. Multitaper averaging
+// was tried and measured against a reference (see test/README.md): sine
+// tapers and DPSS both widen narrow lines into slabs and leak, which a
+// 96 dB scale shows.
+//
+// Each analysis is a Blackman-Harris window zero-padded to 4096 points,
+// transformed as 2048 complex points (even and odd samples as real and
+// imaginary parts) by an in-place radix-2 FFT in shared memory, then split
+// into the real signal's bins. Zoomed out, a column averages the power of
+// up to four windows spread across its samples, so it neither skips sound
+// nor flickers as the view moves; zoomed in, neighbouring windows overlap.
+// Rows are log-spaced: a Catmull-Rom spline through the bins' levels where
+// rows are finer than bins, the loudest bin where they are coarser.
+layout(local_size_x = 256) in;
+
+#include "SpectrogramParams.glsl"
+
+layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; };
+// MAX_ROWS rows of MAX_COLUMNS levels, dBFS; row 0 is the lowest frequency.
+layout(set = 1, binding = 0) buffer Levels { float levels[]; };
+
+// Match layouts.zig.
+const uint MAX_COLUMNS = 2048;
+const uint W = 2048;
+const uint N = 4096;
+const uint HALF = N / 2;
+const uint LOG2_HALF = 11;
+const uint THREADS = 256;
+// The long analysis: every DECIMATE-th sample of the low-passed signal.
+const uint DECIMATE = 4;
+// Its bins needed below the blend: 600 Hz at 12 kHz / 4096 is bin 205.
+const uint LONG_BINS = 256;
+// Windows the long analysis averages per column, a quarter window apart.
+const uint WELCH = 3;
+const float BLEND_LO = 250.0;
+const float BLEND_HI = 500.0;
+const float PI = 3.14159265358979;
+const float SILENCE = -160.0;
+// Columns past the end of the tape.
+const float NO_TAPE = 1000.0;
+const float A0 = 0.35875;
+const float A1 = 0.48829;
+const float A2 = 0.14128;
+const float A3 = 0.01168;
+
+shared vec2 z[HALF];
+// Power per bin, summed over windows; then dBFS.
+shared float short_bins[HALF + 1];
+shared float long_bins[LONG_BINS];
+
+float sample_at(int i) {
+ return i >= 0 && i < int(u.samples) ? tape[u.offset + uint(i)] : 0.0;
+}
+
+// The signal low-passed for decimation: a triangle over 7 samples (two
+// boxes of 4), whose zeros sit on 12 and 24 kHz, exactly where anything
+// that would fold onto the low bins comes from.
+float low_passed(int i) {
+ return (sample_at(i - 3) + 2.0 * sample_at(i - 2) + 3.0 * sample_at(i - 1) + 4.0 * sample_at(i) + 3.0 * sample_at(i + 1) + 2.0 * sample_at(i + 2) + sample_at(i + 3)) / 16.0;
+}
+
+float blackman_harris(uint i) {
+ float a = 2.0 * PI * float(i) / float(W - 1);
+ return A0 - A1 * cos(a) + A2 * cos(2.0 * a) - A3 * cos(3.0 * a);
+}
+
+vec2 mul(vec2 a, vec2 b) {
+ return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
+}
+
+float windowed(int centre, uint i, bool long_window) {
+ if (long_window) return low_passed(centre + (int(i) - int(W / 2)) * int(DECIMATE)) * blackman_harris(i);
+ return sample_at(centre + int(i) - int(W / 2)) * blackman_harris(i);
+}
+
+// Adds the power spectrum of the window centred on sample `centre`.
+void analyse(int centre, bool long_window) {
+ uint t = gl_LocalInvocationID.x;
+ // Even and odd samples packed as one complex sequence, in bit-reversed
+ // order for the in-place transform. Past W it is all padding.
+ for (uint i = t; i < HALF; i += THREADS) {
+ vec2 v = vec2(0.0);
+ if (2u * i < W) v = vec2(windowed(centre, 2u * i, long_window), windowed(centre, 2u * i + 1u, long_window));
+ z[bitfieldReverse(i) >> (32u - LOG2_HALF)] = v;
+ }
+ barrier();
+ for (uint s = 1u; s <= LOG2_HALF; s++) {
+ uint half_size = 1u << (s - 1u);
+ for (uint j = t; j < HALF / 2u; j += THREADS) {
+ uint k = j & (half_size - 1u);
+ uint i0 = ((j >> (s - 1u)) << s) + k;
+ uint i1 = i0 + half_size;
+ float angle = -PI * float(k) / float(half_size);
+ vec2 b = mul(vec2(cos(angle), sin(angle)), z[i1]);
+ vec2 a = z[i0];
+ z[i0] = a + b;
+ z[i1] = a - b;
+ }
+ barrier();
+ }
+ // The real signal's bins from the packed transform:
+ // X[k] = E[k] + e^(-2 pi i k / N) O[k], with E and O the even and odd
+ // samples' spectra, recovered from Z[k] and conj(Z[HALF - k]).
+ uint count = long_window ? LONG_BINS : HALF + 1u;
+ for (uint k = t; k < count; k += THREADS) {
+ vec2 zk = z[k % HALF];
+ vec2 zc = z[(HALF - k) % HALF];
+ zc.y = -zc.y;
+ vec2 even = 0.5 * (zk + zc);
+ vec2 d = 0.5 * (zk - zc);
+ vec2 odd = vec2(d.y, -d.x);
+ float angle = -2.0 * PI * float(k) / float(N);
+ vec2 x = even + mul(vec2(cos(angle), sin(angle)), odd);
+ if (long_window) long_bins[k] += dot(x, x);
+ else short_bins[k] += dot(x, x);
+ }
+ barrier();
+}
+
+float bin_level(uint k, bool long_window) {
+ return long_window ? long_bins[min(k, LONG_BINS - 1u)] : short_bins[min(k, HALF)];
+}
+
+// The level of the row spanning bins `lo` to `hi` of one analysis.
+float row_level(float lo, float hi, bool long_window) {
+ if (hi - lo < 1.0) {
+ float mid = sqrt(lo * hi);
+ uint i = uint(floor(mid));
+ float f = mid - floor(mid);
+ float p0 = bin_level(max(i, 1u) - 1u, long_window);
+ float p1 = bin_level(i, long_window);
+ float p2 = bin_level(i + 1u, long_window);
+ float p3 = bin_level(i + 2u, long_window);
+ float v = p1 + 0.5 * f * (p2 - p0 + f * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3 + f * (3.0 * (p1 - p2) + p3 - p0)));
+ return clamp(v, SILENCE, max(max(p1, p2), max(p0, p3)));
+ }
+ float v = SILENCE;
+ for (uint i = uint(floor(lo)); i <= uint(ceil(hi)); i++) v = max(v, bin_level(i, long_window));
+ return v;
+}
+
+// Average power over `count` windows to dBFS: a full-scale sine at a bin's
+// centre gives (W * A0 / 2) squared.
+float to_db(float power, uint count) {
+ float ratio = power / float(count) / (0.25 * float(W) * A0 * float(W) * A0);
+ return ratio > 0.0 ? max(10.0 * log(ratio) / log(10.0), SILENCE) : SILENCE;
+}
+
+void main() {
+ uint c = gl_WorkGroupID.x;
+ uint t = gl_LocalInvocationID.x;
+ for (uint k = t; k <= HALF; k += THREADS) short_bins[k] = 0.0;
+ for (uint k = t; k < LONG_BINS; k += THREADS) long_bins[k] = 0.0;
+ barrier();
+
+ float start = float(c) * u.spp;
+ uint short_windows = uint(clamp(ceil(u.spp / float(W / 2u)), 1.0, 4.0));
+ for (uint w = 0u; w < short_windows; w++) {
+ analyse(int(float(u.first) + start + u.spp * (float(w) + 0.5) / float(short_windows)), false);
+ }
+ // Zoomed out, the windows spread across the column average already;
+ // zoomed in, WELCH windows a quarter window apart around its centre.
+ uint long_windows = uint(clamp(ceil(u.spp / float(W * DECIMATE / 2u)), 1.0, 4.0));
+ if (long_windows == 1u) {
+ int centre = int(float(u.first) + start + 0.5 * u.spp);
+ for (uint w = 0u; w < WELCH; w++) analyse(centre + (int(w) - int(WELCH / 2u)) * int(W * DECIMATE / 4u), true);
+ long_windows = WELCH;
+ } else {
+ for (uint w = 0u; w < long_windows; w++) {
+ analyse(int(float(u.first) + start + u.spp * (float(w) + 0.5) / float(long_windows)), true);
+ }
+ }
+
+ for (uint k = t; k <= HALF; k += THREADS) short_bins[k] = to_db(short_bins[k], short_windows);
+ for (uint k = t; k < LONG_BINS; k += THREADS) long_bins[k] = to_db(long_bins[k], long_windows);
+ barrier();
+
+ bool inside = float(u.first) + start < float(u.samples);
+ float short_hz = u.rate / float(N);
+ float long_hz = short_hz / float(DECIMATE);
+ float ratio = log(u.max_freq / u.min_freq);
+ for (uint r = t; r < u.rows; r += THREADS) {
+ float lo = u.min_freq * exp(ratio * float(r) / float(u.rows));
+ float hi = u.min_freq * exp(ratio * float(r + 1u) / float(u.rows));
+ float blend = smoothstep(BLEND_LO, BLEND_HI, sqrt(lo * hi));
+ float v = 0.0;
+ if (blend < 1.0) v += (1.0 - blend) * row_level(lo / long_hz, hi / long_hz, true);
+ if (blend > 0.0) v += blend * row_level(lo / short_hz, hi / short_hz, false);
+ levels[r * MAX_COLUMNS + (u.slot + c) % u.ring] = inside ? v : NO_TAPE;
+ }
+}
diff --git a/src/scope/shaders/spectrogram.frag b/src/scope/shaders/spectrogram.frag
new file mode 100644
index 0000000..9cb5e3d
--- /dev/null
+++ b/src/scope/shaders/spectrogram.frag
@@ -0,0 +1,45 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// The spectrogram (spectrogram.comp): time across, as the waveform above
+// it, log frequency up, each level through a heat gradient (inferno:
+// black, violet, red, orange, pale yellow), with the playhead.
+layout(location = 0) out vec4 out_color;
+
+#include "SpectrogramView.glsl"
+
+layout(set = 2, binding = 0) readonly buffer Levels { float levels[]; };
+
+// Match layouts.zig and spectrogram.comp.
+const uint MAX_COLUMNS = 2048;
+const float NO_TAPE = 1000.0;
+
+// Matplotlib's inferno, as a polynomial fit (Matt Zucker's, CC0).
+vec3 inferno(float t) {
+ const vec3 c0 = vec3(0.0002189403691192265, 0.001651004631001012, -0.01948089843709184);
+ const vec3 c1 = vec3(0.1065134194856116, 0.5639564367884091, 3.932712388889277);
+ const vec3 c2 = vec3(11.60249308247187, -3.972853965665698, -15.9423941062914);
+ const vec3 c3 = vec3(-41.70399613139459, 17.43639888205313, 44.35414519872813);
+ const vec3 c4 = vec3(77.162935699427, -33.40235894210092, -81.80730925738993);
+ const vec3 c5 = vec3(-71.31942824499214, 32.62606426397723, 73.20951985803202);
+ const vec3 c6 = vec3(25.13112622477341, -12.24266895238567, -23.07032500287172);
+ return clamp(c0 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))), 0.0, 1.0);
+}
+
+void main() {
+ vec2 p = gl_FragCoord.xy;
+ uint row = uint(u.size.y - p.y);
+ uint x = uint(p.x);
+ float db = levels[row * MAX_COLUMNS + (x + u.scroll) % uint(u.size.x)];
+ if (x + u.filled < uint(u.size.x)) db = NO_TAPE;
+ vec3 color = vec3(0.035, 0.03, 0.05);
+ if (db < NO_TAPE) {
+ float t = clamp((db - u.floor_db) / (u.ceil_db - u.floor_db), 0.0, 1.0);
+ // A touch of contrast: quiet stays dark, loud blooms.
+ color = inferno(pow(t, 1.3));
+ }
+ if (u.playhead >= 0.0) {
+ float d = abs(p.x - u.playhead);
+ color = mix(color, vec3(1.0, 0.85, 0.3), clamp(1.5 - d, 0.0, 1.0) * 0.8);
+ }
+ out_color = vec4(color, 1.0);
+}
diff --git a/src/scope/shaders/spectrum.comp b/src/scope/shaders/spectrum.comp
new file mode 100644
index 0000000..e576e30
--- /dev/null
+++ b/src/scope/shaders/spectrum.comp
@@ -0,0 +1,128 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// The spectrum at the playhead, entirely on the GPU, in one workgroup:
+//
+// 1. a Blackman-Harris window of the tape ending at `u.end`, zero-padded to
+// N. Its sidelobes sit below -92 dB, so the finely sampled spectrum shows
+// the signal rather than the window's ripples;
+// 2. a radix-2 Stockham FFT, ping-ponging between the scratch halves;
+// 3. levels in dBFS per bin;
+// 4. one value per log-spaced column. Where columns are narrower than a bin
+// (the low end), a Catmull-Rom spline through the bins' levels; where a
+// column spans several bins (the high end), their loudest. The zero
+// padding already interpolates between the window's own bins, so the
+// curve is continuous at every zoom;
+// 5. smoothing in time (fast attack, slow release) and falling peaks, kept
+// in the column buffer between frames.
+layout(local_size_x = 256) in;
+
+#include "SpectrumParams.glsl"
+
+layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; };
+// Two halves of N complex values, which the workgroup's threads read from
+// each other: coherent, for Vulkan; WGSL's storageBarrier covers it.
+#ifdef SKETCHBOOK_WEB
+layout(set = 1, binding = 0) buffer Scratch { vec2 scratch[]; };
+#else
+layout(set = 1, binding = 0) coherent buffer Scratch { vec2 scratch[]; };
+#endif
+// x: smoothed level, y: peak, both dBFS.
+layout(set = 1, binding = 1) buffer Columns { vec2 columns[]; };
+
+// Match layouts.zig.
+const uint N = 8192;
+const uint LOG2_N = 13;
+const uint W = 4096;
+const uint THREADS = 256;
+const float PI = 3.14159265358979;
+const float SILENCE = -160.0;
+// Blackman-Harris, 4 terms. A0 is also its coherent gain.
+const float A0 = 0.35875;
+const float A1 = 0.48829;
+const float A2 = 0.14128;
+const float A3 = 0.01168;
+
+void sync() {
+ memoryBarrierBuffer();
+ barrier();
+}
+
+void main() {
+ uint t = gl_LocalInvocationID.x;
+
+ // 1. Window.
+ int first = int(u.end) - int(W);
+ for (uint i = t; i < N; i += THREADS) {
+ float x = 0.0;
+ int at = first + int(i);
+ if (i < W && at >= 0 && at < int(u.samples)) {
+ float a = 2.0 * PI * float(i) / float(W - 1);
+ x = tape[u.offset + uint(at)] * (A0 - A1 * cos(a) + A2 * cos(2.0 * a) - A3 * cos(3.0 * a));
+ }
+ scratch[i] = vec2(x, 0.0);
+ }
+ sync();
+
+ // 2. FFT: after stage s, sub-transforms of 2p = 2^(s+1) points, in order.
+ for (uint s = 0; s < LOG2_N; s++) {
+ uint p = 1u << s;
+ uint from = (s & 1u) * N;
+ uint to = N - from;
+ for (uint i = t; i < N / 2; i += THREADS) {
+ uint k = i & (p - 1u);
+ vec2 a = scratch[from + i];
+ vec2 b = scratch[from + i + N / 2];
+ float angle = -PI * float(k) / float(p);
+ vec2 w = vec2(cos(angle), sin(angle));
+ b = vec2(b.x * w.x - b.y * w.y, b.x * w.y + b.y * w.x);
+ uint j = (i << 1u) - k;
+ scratch[to + j] = a + b;
+ scratch[to + j + p] = a - b;
+ }
+ sync();
+ }
+ uint result = (LOG2_N & 1u) * N;
+
+ // 3. Levels into the other half. A full-scale sine peaks at W * A0 / 2:
+ // the window sums to W * A0, and a real sine splits between ±f.
+ uint levels = N - result;
+ for (uint i = t; i <= N / 2; i += THREADS) {
+ float amplitude = length(scratch[result + i]) * 2.0 / (float(W) * A0);
+ scratch[levels + i].x = amplitude > 0.0 ? max(20.0 * log(amplitude) / log(10.0), SILENCE) : SILENCE;
+ }
+ sync();
+
+ // 4 and 5. Columns.
+ float bin_hz = u.rate / float(N);
+ float ratio = log(u.max_freq / u.min_freq);
+ for (uint c = t; c < u.columns; c += THREADS) {
+ float lo = u.min_freq * exp(ratio * float(c) / float(u.columns)) / bin_hz;
+ float hi = u.min_freq * exp(ratio * float(c + 1u) / float(u.columns)) / bin_hz;
+ float mid = sqrt(lo * hi);
+ float v;
+ if (hi - lo < 1.0) {
+ uint i = uint(floor(mid));
+ float f = mid - floor(mid);
+ float p0 = scratch[levels + min(max(i, 1u) - 1u, N / 2)].x;
+ float p1 = scratch[levels + min(i, N / 2)].x;
+ float p2 = scratch[levels + min(i + 1u, N / 2)].x;
+ float p3 = scratch[levels + min(i + 2u, N / 2)].x;
+ v = p1 + 0.5 * f * (p2 - p0 + f * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3 + f * (3.0 * (p1 - p2) + p3 - p0)));
+ v = clamp(v, SILENCE, max(max(p1, p2), max(p0, p3)));
+ } else {
+ v = SILENCE;
+ uint end = min(uint(ceil(hi)), N / 2);
+ for (uint i = uint(floor(lo)); i <= end; i++) v = max(v, scratch[levels + i].x);
+ }
+ vec2 old = columns[c];
+ vec2 now;
+ if (u.reset != 0u) {
+ now = vec2(v, v);
+ } else {
+ float rate = v > old.x ? 40.0 : 6.0;
+ now.x = mix(old.x, v, 1.0 - exp(-rate * u.dt));
+ now.y = max(now.x, old.y - 24.0 * u.dt);
+ }
+ columns[c] = now;
+ }
+}
diff --git a/src/scope/shaders/spectrum.frag b/src/scope/shaders/spectrum.frag
new file mode 100644
index 0000000..e88b091
--- /dev/null
+++ b/src/scope/shaders/spectrum.frag
@@ -0,0 +1,51 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// The spectrum from its columns (spectrum.comp): one column per pixel on a
+// log frequency axis. The curve is drawn by its distance along the normal,
+// from the neighbours' slope, so steep parts stay as thick as flat ones.
+layout(location = 0) out vec4 out_color;
+
+#include "SpectrumView.glsl"
+
+layout(set = 2, binding = 0) readonly buffer Columns { vec2 columns[]; };
+
+float y_of(float db) {
+ return u.size.y * (1.0 - clamp((db - u.floor_db) / (u.ceil_db - u.floor_db), 0.0, 1.0));
+}
+
+float x_of(float hz) {
+ return u.size.x * log(hz / u.min_freq) / log(u.max_freq / u.min_freq);
+}
+
+// How close `p` is to a grid line, 0..1.
+float grid(vec2 p) {
+ const float lines[10] = float[](20.0, 50.0, 100.0, 200.0, 500.0, 1000.0, 2000.0, 5000.0, 10000.0, 20000.0);
+ float near = 1e9;
+ for (int i = 0; i < 10; i++) near = min(near, abs(p.x - x_of(lines[i])));
+ float step = u.size.y * 12.0 / (u.ceil_db - u.floor_db);
+ float db = mod(p.y, step);
+ near = min(near, min(db, step - db));
+ return clamp(1.0 - near, 0.0, 1.0);
+}
+
+void main() {
+ vec2 p = gl_FragCoord.xy;
+ int n = int(u.size.x);
+ int x = int(p.x);
+ float y = y_of(columns[x].x);
+ float slope = (y_of(columns[min(x + 1, n - 1)].x) - y_of(columns[max(x - 1, 0)].x)) * 0.5;
+ float distance = abs(p.y - y) / sqrt(1.0 + slope * slope);
+
+ vec3 color = vec3(0.055, 0.06, 0.07);
+ color = mix(color, vec3(0.16, 0.17, 0.2), grid(p));
+
+ // Fill under the curve, by frequency, fading downwards.
+ float along = p.x / u.size.x;
+ vec3 hue = mix(vec3(0.2, 0.75, 0.9), vec3(0.85, 0.35, 0.9), along);
+ if (p.y > y) color = mix(color, hue, 0.35 * (1.0 - (p.y - y) / max(u.size.y - y, 1.0)) + 0.08);
+ // Peaks, then the curve.
+ float peak = abs(p.y - y_of(columns[x].y));
+ color = mix(color, vec3(0.9, 0.9, 0.95), clamp(1.0 - peak, 0.0, 1.0) * 0.45);
+ color = mix(color, mix(hue, vec3(1.0), 0.5), clamp(1.6 - distance, 0.0, 1.0));
+ out_color = vec4(color, 1.0);
+}
diff --git a/src/scope/shaders/view.vert b/src/scope/shaders/view.vert
new file mode 100644
index 0000000..d9461e9
--- /dev/null
+++ b/src/scope/shaders/view.vert
@@ -0,0 +1,7 @@
+#version 450
+// One triangle over the whole target, from the vertex index alone: the
+// fragment shaders work in pixels (gl_FragCoord).
+void main() {
+ vec2 p = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
+ gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);
+}
diff --git a/src/scope/shaders/waveform.comp b/src/scope/shaders/waveform.comp
new file mode 100644
index 0000000..d1d64e5
--- /dev/null
+++ b/src/scope/shaders/waveform.comp
@@ -0,0 +1,72 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// One workgroup per waveform column: the lowest and highest the signal goes
+// in it, and its RMS. Each column also includes the linearly interpolated
+// signal at its own edges, which it shares with its neighbours, so that
+// zoomed in past one sample per column the columns still join into one
+// continuous line.
+layout(local_size_x = 64) in;
+
+#include "WaveParams.glsl"
+
+layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; };
+// x: min, y: max, z: RMS, w: 1 where the column has tape under it.
+layout(set = 1, binding = 0) buffer Columns { vec4 columns[]; };
+
+const uint THREADS = 64;
+shared float lows[THREADS];
+shared float highs[THREADS];
+shared float squares[THREADS];
+
+float sample_at(int i) {
+ return i >= 0 && i < int(u.samples) ? tape[u.offset + uint(i)] : 0.0;
+}
+
+// The signal at `t` samples after `u.first`, between samples linearly.
+float signal(float t) {
+ float whole = floor(t);
+ int i = int(u.first) + int(whole);
+ return mix(sample_at(i), sample_at(i + 1), t - whole);
+}
+
+void main() {
+ uint c = gl_WorkGroupID.x;
+ uint t = gl_LocalInvocationID.x;
+ float start = float(c) * u.spp;
+ float end = start + u.spp;
+ int first = int(ceil(start));
+ int last = min(int(floor(end)), int(u.samples) - int(u.first) - 1);
+
+ float lo = 1e9;
+ float hi = -1e9;
+ float sq = 0.0;
+ for (int i = first + int(t); i <= last; i += int(THREADS)) {
+ float v = tape[u.offset + u.first + uint(i)];
+ lo = min(lo, v);
+ hi = max(hi, v);
+ sq += v * v;
+ }
+ if (t == 0u) {
+ float a = signal(start);
+ float b = signal(min(end, float(u.samples - u.first - 1u)));
+ lo = min(lo, min(a, b));
+ hi = max(hi, max(a, b));
+ }
+ lows[t] = lo;
+ highs[t] = hi;
+ squares[t] = sq;
+ barrier();
+ for (uint stride = THREADS / 2u; stride > 0u; stride >>= 1u) {
+ if (t < stride) {
+ lows[t] = min(lows[t], lows[t + stride]);
+ highs[t] = max(highs[t], highs[t + stride]);
+ squares[t] += squares[t + stride];
+ }
+ barrier();
+ }
+ if (t == 0u) {
+ float count = float(max(last - first + 1, 1));
+ float inside = float(u.first) + start < float(u.samples) ? 1.0 : 0.0;
+ columns[(u.slot + c) % u.ring] = vec4(lows[0], highs[0], sqrt(squares[0] / count), inside);
+ }
+}
diff --git a/src/scope/shaders/waveform.frag b/src/scope/shaders/waveform.frag
new file mode 100644
index 0000000..37be7ce
--- /dev/null
+++ b/src/scope/shaders/waveform.frag
@@ -0,0 +1,56 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// The waveform from its columns (waveform.comp): one column per pixel, the
+// band between its low and high filled, anti-aliased, at least a pixel
+// thick; the RMS inside it brighter; played and unplayed in two colors,
+// red while recording, green for the live microphone.
+layout(location = 0) out vec4 out_color;
+
+#include "WaveView.glsl"
+
+layout(set = 2, binding = 0) readonly buffer Columns { vec4 columns[]; };
+
+// Match layouts.Tint.
+const uint TINT_RECORDING = 1u;
+const uint TINT_LIVE = 2u;
+
+void main() {
+ vec2 p = gl_FragCoord.xy;
+ uint x = uint(p.x);
+ vec4 col = columns[(x + u.scroll) % uint(u.size.x)];
+ if (x + u.filled < uint(u.size.x)) col.w = 0.0;
+ float mid = u.size.y * 0.5;
+ float scale = u.size.y * 0.46 * u.gain;
+
+ vec3 color = vec3(0.055, 0.06, 0.07);
+ if (col.w > 0.0) color = vec3(0.075, 0.08, 0.095);
+ // Center line.
+ color = mix(color, vec3(0.2, 0.22, 0.26), clamp(1.0 - abs(p.y - mid), 0.0, 1.0) * 0.6);
+
+ if (col.w > 0.0) {
+ float top = mid - col.y * scale - 0.5;
+ float bottom = mid - col.x * scale + 0.5;
+ float outside = max(top - p.y, p.y - bottom);
+ float cover = clamp(0.5 - outside, 0.0, 1.0);
+ float rms = col.z * scale;
+ float inner = clamp(rms - abs(p.y - mid) + 0.5, 0.0, 1.0);
+ bool played = u.playhead >= 0.0 && p.x < u.playhead;
+ vec3 band = played ? vec3(0.35, 0.8, 0.95) : vec3(0.45, 0.5, 0.58);
+ vec3 core = played ? vec3(0.7, 0.95, 1.0) : vec3(0.65, 0.7, 0.78);
+ if (u.tint == TINT_RECORDING) {
+ band = vec3(0.95, 0.35, 0.35);
+ core = vec3(1.0, 0.6, 0.55);
+ } else if (u.tint == TINT_LIVE) {
+ band = vec3(0.35, 0.75, 0.5);
+ core = vec3(0.65, 1.0, 0.75);
+ }
+ color = mix(color, mix(band, core, inner), cover);
+ }
+
+ // Playhead.
+ if (u.playhead >= 0.0) {
+ float d = abs(p.x - u.playhead);
+ color = mix(color, vec3(1.0, 0.85, 0.3), clamp(1.5 - d, 0.0, 1.0));
+ }
+ out_color = vec4(color, 1.0);
+}