summaryrefslogtreecommitdiff
path: root/examples/micscope/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 00:19:38 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitc37f1b3b1374df2a2a434d16cd772a4bb00c567e (patch)
tree4feccf16cf1f6808bae6ba26602705d557ab2fb7 /examples/micscope/src
parent9bd77e54943d954291b5faef287ccca5dcebd7a4 (diff)
downloadsketchbook-c37f1b3b1374df2a2a434d16cd772a4bb00c567e.tar.gz
sketchbook-c37f1b3b1374df2a2a434d16cd772a4bb00c567e.zip
micscope: on the scope module, its copy of the scope code gone
micscope carried the code the scope module was lifted from: seven shaders byte for byte, its layouts but for a comment, and a private live view (Live, analyseLive, drawLive) that was Scope.push, scroll and spectrum. Now the live view is ss.live, a scope.Scope, pushed the microphone and drawn with the panel's world rects and the camera (Views.camera), every frame, clipped away while a recording stands in for it so its columns keep up as before; the tuner reads its history from ss.live.gpu.tape. The take panels run scope.kernels and scope.pipelines over the tape with scope.layouts' blocks, wavePass and spectrogramPass inlined at their one use; maxFreq, min_freq and floor_db are the scope's. src/layouts.zig keeps only the tuner's blocks, the build compiles only the tuner's two shaders, and the tuner names notes with scope.midi, scope.hz and scope.note_names. Takes, transport and tuner are as they were. 1003 lines out, 91 in. Tests: zig build test, 67/67 steps, 10/10 tests (the live history's own test went with it; the scope module tests the same). Pictures: the four scripts' 18 screenshots, captured before and after with the same scripts and compared pixel by pixel (two runs before differ by themselves wherever wall-clock playback or microphone timing shows: the playheads, how far the live view has filled). The one difference not of that kind is the live spectrum: micscope made every image at most 132 rows, so its 184-row spectrum was stretched from 132; the scope makes it at its size, so the live spectrum's lines come out crisp (at most 86 in a channel, inside its rect). The take spectra keep micscope's 132. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/micscope/src')
-rw-r--r--examples/micscope/src/layouts.zig125
-rw-r--r--examples/micscope/src/main.zig353
-rw-r--r--examples/micscope/src/tuner.zig25
3 files changed, 79 insertions, 424 deletions
diff --git a/examples/micscope/src/layouts.zig b/examples/micscope/src/layouts.zig
index cd5be65..be95799 100644
--- a/examples/micscope/src/layouts.zig
+++ b/examples/micscope/src/layouts.zig
@@ -1,124 +1,7 @@
-//! 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 the tape buffer for
-// `0 <= i < samples`: the takes share one buffer, and a take's analysis
-// never reads its neighbours.
-/// 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" } };
-};
+//! What the tuner's 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. The views' own
+//! blocks are the scope module's (`scope.layouts`).
/// tuner.comp: the YIN window is the `tuner_window + tuner_lags` samples
/// ending at `end`, read like the other analyses (`offset`, `samples`).
diff --git a/examples/micscope/src/main.zig b/examples/micscope/src/main.zig
index facc169..3b44ba0 100644
--- a/examples/micscope/src/main.zig
+++ b/examples/micscope/src/main.zig
@@ -17,7 +17,10 @@
const std = @import("std");
const sk = @import("sketch");
const gpu = sk.abi.gpu;
-const layouts = @import("layouts");
+const scope = @import("scope");
+const layouts = scope.layouts;
+/// The tuner's blocks; its shaders are `shaders`.
+const tuner_layouts = @import("layouts");
const shaders = @import("shaders");
const tuner = @import("tuner.zig");
@@ -33,18 +36,8 @@ const max_takes = 64;
const stream = 1;
/// How far ahead playback keeps the stream filled.
const lead_seconds = 0.06;
-const min_freq = 20.0;
-const floor_db = -96.0;
/// The live view's span.
const live_seconds = 6;
-/// The microphone's recent past, kept for the live view: a frame's new
-/// columns and everything their analysis windows reach.
-const live_history = 1 << 16;
-/// How far spectrogram.comp reads past a column's centre: the long
-/// analysis, 2048 points decimated 4×, three windows a quarter apart, plus
-/// its low-pass. The live view analyses a column once that much has been
-/// heard, so its columns are final; it runs this far behind.
-const live_reach = 6160;
pub const Transport = enum { stopped, recording, forward, backward };
@@ -114,7 +107,8 @@ pub const Session = struct {
cursor: usize = 0,
/// The take being recorded, by id.
recording: u32 = 0,
- live: Live = .{},
+ /// The microphone's recent past and the live view.
+ live: scope.Scope = .{},
/// Pitch, by tuner.comp: of the microphone (live, or recording), and
/// of the selected take at its playhead.
tuners: [2]tuner.Tracker = @splat(.{}),
@@ -126,33 +120,6 @@ pub const Session = struct {
/// Which tracker a readback belongs to: its index, as the download key.
const Tuned = enum(u32) { input, take };
-/// The microphone's last `live_history` samples, and how far the live
-/// view's columns have got. Column `k` covers samples `k * spp` up to the
-/// next, counted from the first sample heard, and lands in slot
-/// `k % columns`: a ring the views read from the oldest.
-const Live = struct {
- samples: [live_history]f32 = @splat(0),
- /// Samples heard since the session started.
- heard: u64 = 0,
- /// The next column to analyse.
- next: u64 = 0,
- /// Columns analysed since the view was (re)made, up to `columns`.
- filled: u32 = 0,
- columns: u32 = 0,
- spp: u32 = 0,
-
- fn push(live: *Live, heard: []const f32) void {
- const h = &live.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);
- }
- live.heard += heard.len;
- }
-};
-
/// GPU objects, made on first use.
const Gpu = struct {
ready: bool = false,
@@ -160,15 +127,12 @@ const Gpu = struct {
tape: sk.Buffer = .none,
capacity: usize = 0,
uploaded: usize = 0,
- /// `Live.samples`.
- live_tape: sk.Buffer = .none,
/// spectrum.comp's FFT, shared by every panel.
scratch: sk.Buffer = .none,
- /// tuner.comp's results, a `layouts.Pitch` each, by `Tuned`, and the
- /// difference functions it searches.
+ /// tuner.comp's results, a `tuner_layouts.Pitch` each, by `Tuned`, and
+ /// the difference functions it searches.
pitches: [2]sk.Buffer = @splat(.none),
differences: [2]sk.Buffer = @splat(.none),
- live: Panel = .{},
/// For the takes on screen, by take id; the least recently used is
/// reused.
panels: [16]Panel = @splat(.{}),
@@ -660,8 +624,8 @@ pub fn draw(s: *const State, ss: *Session, store: *Store, f: *sk.Frame) void {
ss.tuners[@intFromEnum(Tuned.take)] = .{};
};
for (in.downloads) |d| {
- if (d.key >= ss.tuners.len or d.bytes.len < @sizeOf(layouts.Pitch)) continue;
- ss.tuners[d.key].feed(std.mem.bytesToValue(layouts.Pitch, d.bytes[0..@sizeOf(layouts.Pitch)]), s.a4, @floatCast(dt));
+ if (d.key >= ss.tuners.len or d.bytes.len < @sizeOf(tuner_layouts.Pitch)) continue;
+ ss.tuners[d.key].feed(std.mem.bytesToValue(tuner_layouts.Pitch, d.bytes[0..@sizeOf(tuner_layouts.Pitch)]), s.a4, @floatCast(dt));
}
if (!f.host.gpu()) {
@@ -730,35 +694,6 @@ fn play(ss: *Session, t: *Take, tape: []const f32, f: *sk.Frame, forward: bool,
// ---------------------------------------------------------------------------
// GPU: buffers, compute, passes.
-const view_pipeline = struct {
- fn make(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{view_pipeline.make(shaders.waveform_frag)};
-const spectrum_pipelines = [_]gpu.Pipeline{view_pipeline.make(shaders.spectrum_frag)};
-const spectrogram_pipelines = [_]gpu.Pipeline{view_pipeline.make(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 tuner_difference_kernel: gpu.Kernel = .{
.code = shaders.tuner_difference_comp,
.readonly_buffers = 1,
@@ -773,21 +708,13 @@ const tuner_kernel: gpu.Kernel = .{
.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 },
-};
fn setup(g: *Gpu, f: *sk.Frame) void {
if (g.ready) return;
g.ready = true;
g.scratch = f.host.newBuffer(layouts.fft_size * 2 * @sizeOf([2]f32));
- g.live_tape = f.host.newBuffer(live_history * @sizeOf(f32));
- for (&g.pitches) |*b| b.* = f.host.newBuffer(@sizeOf(layouts.Pitch));
- for (&g.differences) |*b| b.* = f.host.newBuffer(layouts.tuner_lags * @sizeOf(f32));
+ for (&g.pitches) |*b| b.* = f.host.newBuffer(@sizeOf(tuner_layouts.Pitch));
+ for (&g.differences) |*b| b.* = f.host.newBuffer(tuner_layouts.tuner_lags * @sizeOf(f32));
}
fn makeBuffers(p: *Panel, f: *sk.Frame) void {
@@ -845,11 +772,9 @@ fn render(s: *const State, ss: *Session, store: *Store, f: *sk.Frame, rate: f64,
const g = &ss.gpu;
setup(g, f);
sync(g, f, store.tape.items);
- // New code may mean new shaders: make everything again.
- if (f.in.reloaded) {
- g.live.forget();
- for (&g.panels) |*p| p.forget();
- }
+ // New code may mean new shaders: make everything again (the live view
+ // sees to its own).
+ if (f.in.reloaded) for (&g.panels) |*p| p.forget();
const takes = store.takes.items;
const selected = if (takes.len > 0) @min(s.selected, takes.len - 1) else 0;
const recording = s.transport.value == .recording;
@@ -860,31 +785,31 @@ fn render(s: *const State, ss: *Session, store: *Store, f: *sk.Frame, rate: f64,
// Images are made at world size; the camera only scales them.
const size = l.box(0);
- // The live view keeps up even while it is hidden.
- analyseLive(ss, f, size, rate);
+ f.clip(view);
+ // The live view, in the world's last slot. It keeps up even while it is
+ // hidden: drawn clipped away while a recording stands in for it.
+ const live = l.box(world.takes);
+ if (!world.live) f.clip(.{ .x = 0, .y = 0, .w = 0, .h = 0 });
+ ss.live.draw(f, .{ .seconds = live_seconds, .wave = live.wave, .spectrogram = live.spectrogram, .spectrum = live.spectrum, .gain = s.gain, .camera = cam });
+ f.clip(view);
// The tuner: on the microphone's newest sound, and on the selected take
// around its playhead.
- const reach = layouts.tuner_window + layouts.tuner_lags;
+ const reach = tuner_layouts.tuner_window + tuner_layouts.tuner_lags;
if (recording and takes.len > 0) {
const t = takes[takes.len - 1];
tune(g, f, .input, g.tape, t.start, t.len, t.len, rate);
- } else tune(g, f, .input, g.live_tape, 0, live_history, live_history, rate);
+ } else tune(g, f, .input, ss.live.gpu.tape, 0, scope.history, scope.history, rate);
if (!recording and takes.len > 0) {
const t = takes[selected];
const end: usize = @intFromFloat(@max(t.heard, 0));
tune(g, f, .take, g.tape, t.start, t.len, end + reach / 2, rate);
}
- f.clip(view);
- for (0..world.slots()) |slot| {
+ for (0..world.takes) |slot| {
const k: u32 = @intCast(slot);
const b = l.box(k).toScreen(cam);
if (b.whole.y + b.whole.h <= view.y or b.whole.y >= view.y + view.h) continue;
- if (k == world.takes) {
- drawLive(s, ss, f, size, b, rate, dt);
- } else {
- const tint: layouts.Tint = if (recording and k + 1 == world.takes) .recording else .take;
- drawTake(s, g, f, size, b, &takes[k], s.views[k], k == selected, tint, rate, dt);
- }
+ const tint: layouts.Tint = if (recording and k + 1 == world.takes) .recording else .take;
+ drawTake(s, g, f, size, b, &takes[k], s.views[k], k == selected, tint, rate, dt);
}
f.clip(null);
labels(s, ss, store, f, world, cam, rate);
@@ -895,7 +820,7 @@ fn render(s: *const State, ss: *Session, store: *Store, f: *sk.Frame, rate: f64,
fn tune(g: *Gpu, f: *sk.Frame, which: Tuned, tape: sk.Buffer, offset: usize, samples: usize, end: usize, rate: f64) void {
const result = g.pitches[@intFromEnum(which)];
const difference = g.differences[@intFromEnum(which)];
- const params = gpu.uniform(f.scratch, layouts.TunerParams{
+ const params = gpu.uniform(f.scratch, tuner_layouts.TunerParams{
.offset = @intCast(offset),
.end = @intCast(end),
.samples = @intCast(samples),
@@ -911,7 +836,7 @@ fn tune(g: *Gpu, f: *sk.Frame, which: Tuned, tape: sk.Buffer, offset: usize, sam
.readwrite = &.{difference},
.uniforms = params,
// Match tuner_difference.comp's LAGS_PER_GROUP.
- .groups = .{ (layouts.tuner_lags + 15) / 16, 1, 1 },
+ .groups = .{ (tuner_layouts.tuner_lags + 15) / 16, 1, 1 },
});
f.compute(.{
.label = "tuner.comp",
@@ -920,7 +845,7 @@ fn tune(g: *Gpu, f: *sk.Frame, which: Tuned, tape: sk.Buffer, offset: usize, sam
.readwrite = &.{result},
.uniforms = params,
});
- f.download(result, .{ .size = @sizeOf(layouts.Pitch), .key = @intFromEnum(which) });
+ f.download(result, .{ .size = @sizeOf(tuner_layouts.Pitch), .key = @intFromEnum(which) });
}
/// The pitches the tuner looks for: a five-string bass's low B to a
@@ -928,35 +853,11 @@ fn tune(g: *Gpu, f: *sk.Frame, which: Tuned, tape: sk.Buffer, offset: usize, sam
const tuner_low = 30.0;
const tuner_high = 4200.0;
-fn wavePass(f: *sk.Frame, p: *Panel, size: [2]u32, view: layouts.WaveView) void {
- f.gpu(.{
- .label = "waveform.frag",
- .image = p.wave,
- .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 = &.{p.wave_columns} }},
- });
-}
-
-fn spectrogramPass(f: *sk.Frame, p: *Panel, size: [2]u32, view: layouts.SpectrogramView) void {
- f.gpu(.{
- .label = "spectrogram.frag",
- .image = p.spectrogram,
- .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 = &.{p.levels} }},
- });
-}
-
/// The spectrum's analysis of `tape`, then its pass.
fn spectrum(g: *Gpu, f: *sk.Frame, tape: sk.Buffer, p: *Panel, size: [2]u32, params: layouts.SpectrumParams, rate: f64) void {
f.compute(.{
.label = "spectrum.comp",
- .kernel = spectrum_kernel,
+ .kernel = scope.kernels.spectrum,
.readonly = &.{tape},
.readwrite = &.{ g.scratch, p.spectrum_columns },
.uniforms = gpu.uniform(f.scratch, params),
@@ -967,26 +868,22 @@ fn spectrum(g: *Gpu, f: *sk.Frame, tape: sk.Buffer, p: *Panel, size: [2]u32, par
.width = size[0],
.height = size[1],
.clear = null,
- .pipelines = &spectrum_pipelines,
+ .pipelines = &scope.pipelines.spectrum,
.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,
+ .floor_db = scope.floor_db,
.ceil_db = 0,
- .min_freq = min_freq,
- .max_freq = maxFreq(rate),
+ .min_freq = scope.min_freq,
+ .max_freq = scope.maxFreq(rate),
}),
.fragment_storage = &.{p.spectrum_columns},
}},
});
}
-fn maxFreq(rate: f64) f32 {
- return @floatCast(@min(20_000, rate / 2));
-}
-
fn drawTake(s: *const State, g: *Gpu, f: *sk.Frame, size: Box, b: Box, t: *const Take, view: View, selected: bool, tint: layouts.Tint, rate: f64, dt: f64) void {
const p = panelFor(g, f, t.id);
@@ -1009,7 +906,7 @@ fn drawTake(s: *const State, g: *Gpu, f: *sk.Frame, size: Box, b: Box, t: *const
p.wave_key = key;
f.compute(.{
.label = "waveform.comp",
- .kernel = waveform_kernel,
+ .kernel = scope.kernels.waveform,
.readonly = &.{g.tape},
.readwrite = &.{p.wave_columns},
.uniforms = gpu.uniform(f.scratch, params),
@@ -1027,8 +924,8 @@ fn drawTake(s: *const State, g: *Gpu, f: *sk.Frame, size: Box, b: Box, t: *const
.rows = spectrogram[1],
.spp = spp,
.rate = @floatCast(rate),
- .min_freq = min_freq,
- .max_freq = maxFreq(rate),
+ .min_freq = scope.min_freq,
+ .max_freq = scope.maxFreq(rate),
.slot = 0,
.ring = layouts.max_columns,
};
@@ -1037,7 +934,7 @@ fn drawTake(s: *const State, g: *Gpu, f: *sk.Frame, size: Box, b: Box, t: *const
p.spectrogram_key = analysis_key;
f.compute(.{
.label = "spectrogram.comp",
- .kernel = spectrogram_kernel,
+ .kernel = scope.kernels.spectrogram,
.readonly = &.{g.tape},
.readwrite = &.{p.levels},
.uniforms = gpu.uniform(f.scratch, analysis),
@@ -1059,8 +956,8 @@ fn drawTake(s: *const State, g: *Gpu, f: *sk.Frame, size: Box, b: Box, t: *const
.columns = shown[0],
.reset = 0,
.rate = @floatCast(rate),
- .min_freq = min_freq,
- .max_freq = maxFreq(rate),
+ .min_freq = scope.min_freq,
+ .max_freq = scope.maxFreq(rate),
.dt = 0,
};
const spectrum_key = std.hash.Wyhash.hash(1, std.mem.asBytes(&sparams));
@@ -1084,11 +981,19 @@ fn drawTake(s: *const State, g: *Gpu, f: *sk.Frame, size: Box, b: Box, t: *const
const wave_drawn = std.hash.Wyhash.hash(p.wave_key, std.mem.asBytes(&wave_view));
if (wave_drawn != p.wave_drawn) {
p.wave_drawn = wave_drawn;
- wavePass(f, p, wave, wave_view);
+ f.gpu(.{
+ .label = "waveform.frag",
+ .image = p.wave,
+ .width = wave[0],
+ .height = wave[1],
+ .clear = null,
+ .pipelines = &scope.pipelines.wave,
+ .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, wave_view), .fragment_storage = &.{p.wave_columns} }},
+ });
}
const spectrogram_view: layouts.SpectrogramView = .{
.size = .{ @floatFromInt(spectrogram[0]), @floatFromInt(spectrogram[1]) },
- .floor_db = floor_db,
+ .floor_db = scope.floor_db,
.ceil_db = -6,
.playhead = playhead,
.scroll = 0,
@@ -1097,127 +1002,16 @@ fn drawTake(s: *const State, g: *Gpu, f: *sk.Frame, size: Box, b: Box, t: *const
const spectrogram_drawn = std.hash.Wyhash.hash(p.spectrogram_key, std.mem.asBytes(&spectrogram_view));
if (spectrogram_drawn != p.spectrogram_drawn) {
p.spectrogram_drawn = spectrogram_drawn;
- spectrogramPass(f, p, spectrogram, spectrogram_view);
- }
- f.image(p.wave, b.wave, .{});
- f.image(p.spectrogram, b.spectrogram, .{});
- f.image(p.spectrum, b.spectrum, .{});
-}
-
-/// Analyses the live view's new columns into its rings: only those whose
-/// windows have been heard in full, so each is analysed once.
-fn analyseLive(ss: *Session, f: *sk.Frame, b: Box, rate: f64) void {
- const live = &ss.live;
- const g = &ss.gpu;
- const p = &g.live;
- makeBuffers(p, f);
- const columns: u32 = @intFromFloat(@min(b.wave.w, layouts.max_columns));
- const spp: u32 = @max(1, @as(u32, @intFromFloat(@round(live_seconds * rate / @as(f64, @floatFromInt(columns))))));
- if (columns != live.columns or spp != live.spp or f.in.reloaded) {
- live.columns = columns;
- live.spp = spp;
- live.filled = 0;
- live.next = 0;
- }
- if (f.in.microphone.len > 0) f.upload(g.live_tape, 0, std.mem.sliceAsBytes(&live.samples));
-
- // History index of heard sample `i`: `i - base`. A column is ready once
- // all it reads is heard, and analysable while all it reads is kept.
- const base: i64 = @as(i64, @intCast(live.heard)) - live_history;
- const ready = (live.heard -| live_reach) / spp;
- const kept: u64 = @intCast(@max(0, @divFloor(base + live_reach + spp - 1, spp)));
- live.next = @max(live.next, kept, ready -| columns);
- if (ready <= live.next) return;
- const count: u32 = @intCast(ready - live.next);
- const first: u32 = @intCast(@as(i64, @intCast(live.next * spp)) - base);
- const slot: u32 = @intCast(live.next % columns);
- f.compute(.{
- .label = "waveform.comp live",
- .kernel = waveform_kernel,
- .readonly = &.{g.live_tape},
- .readwrite = &.{p.wave_columns},
- .uniforms = gpu.uniform(f.scratch, layouts.WaveParams{
- .offset = 0,
- .first = first,
- .samples = live_history,
- .columns = count,
- .spp = @floatFromInt(spp),
- .slot = slot,
- .ring = columns,
- }),
- .groups = .{ count, 1, 1 },
- });
- f.compute(.{
- .label = "spectrogram.comp live",
- .kernel = spectrogram_kernel,
- .readonly = &.{g.live_tape},
- .readwrite = &.{p.levels},
- .uniforms = gpu.uniform(f.scratch, layouts.SpectrogramParams{
- .offset = 0,
- .first = first,
- .samples = live_history,
- .columns = count,
- .rows = @intFromFloat(@min(b.spectrogram.h, spectrogram_height)),
- .spp = @floatFromInt(spp),
- .rate = @floatCast(rate),
- .min_freq = min_freq,
- .max_freq = maxFreq(rate),
- .slot = slot,
- .ring = columns,
- }),
- .groups = .{ count, 1, 1 },
- });
- live.filled = @intCast(@min(columns, live.filled + count));
- live.next = ready;
-}
-
-fn drawLive(s: *const State, ss: *Session, f: *sk.Frame, size: Box, b: Box, rate: f64, dt: f64) void {
- const live = &ss.live;
- const g = &ss.gpu;
- const p = &g.live;
- const wave = target(f, &p.wave, &p.wave_size, size.wave);
- const spectrogram = target(f, &p.spectrogram, &p.spectrogram_size, size.spectrogram);
- const scroll: u32 = @intCast(live.next % @max(live.columns, 1));
- const wave_view: layouts.WaveView = .{
- .size = .{ @floatFromInt(wave[0]), @floatFromInt(wave[1]) },
- .playhead = -1,
- .gain = s.gain,
- .tint = @intFromEnum(layouts.Tint.live),
- .scroll = scroll,
- .filled = live.filled,
- };
- const wave_drawn = std.hash.Wyhash.hash(0, std.mem.asBytes(&wave_view));
- if (wave_drawn != p.wave_drawn) {
- p.wave_drawn = wave_drawn;
- wavePass(f, p, wave, wave_view);
- }
- const spectrogram_view: layouts.SpectrogramView = .{
- .size = .{ @floatFromInt(spectrogram[0]), @floatFromInt(spectrogram[1]) },
- .floor_db = floor_db,
- .ceil_db = -6,
- .playhead = -1,
- .scroll = scroll,
- .filled = live.filled,
- };
- const spectrogram_drawn = std.hash.Wyhash.hash(0, std.mem.asBytes(&spectrogram_view));
- if (spectrogram_drawn != p.spectrogram_drawn) {
- p.spectrogram_drawn = spectrogram_drawn;
- spectrogramPass(f, p, spectrogram, spectrogram_view);
+ f.gpu(.{
+ .label = "spectrogram.frag",
+ .image = p.spectrogram,
+ .width = spectrogram[0],
+ .height = spectrogram[1],
+ .clear = null,
+ .pipelines = &scope.pipelines.spectrogram,
+ .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, spectrogram_view), .fragment_storage = &.{p.levels} }},
+ });
}
- // The spectrum of the newest samples, smoothed.
- const shown = target(f, &p.spectrum, &p.spectrum_size, size.spectrum);
- spectrum(g, f, g.live_tape, p, shown, .{
- .offset = 0,
- .end = live_history,
- .samples = live_history,
- .columns = shown[0],
- .reset = @intFromBool(p.spectrum_key == 0),
- .rate = @floatCast(rate),
- .min_freq = min_freq,
- .max_freq = maxFreq(rate),
- .dt = @floatCast(dt),
- }, rate);
- p.spectrum_key = 1;
f.image(p.wave, b.wave, .{});
f.image(p.spectrogram, b.spectrogram, .{});
f.image(p.spectrum, b.spectrum, .{});
@@ -1321,19 +1115,19 @@ fn labels(s: *const State, ss: *const Session, store: *const Store, f: *sk.Frame
/// the pitch as a needle between them, and the note in cents and Hz.
fn tunerOverlay(f: *sk.Frame, r: sk.Rect, reading: ?tuner.Reading, a4: f32, rate: f64, detailed: bool) void {
const at = reading orelse return;
- const max_freq = maxFreq(rate);
+ const max_freq = scope.maxFreq(rate);
const fade: u8 = @intFromFloat(255 * std.math.clamp(1 - at.quiet / tuner.hold_seconds, 0.2, 1));
const cents = at.cents();
const color = sk.Color.hex(if (@abs(cents) <= 5) green else if (@abs(cents) <= 15) yellow else 0xff8a4d).alpha(fade);
const x = struct {
fn of(rect: sk.Rect, hz: f32, ceiling: f32) f32 {
- return rect.x + @log(hz / min_freq) / @log(ceiling / min_freq) * rect.w;
+ return rect.x + @log(hz / scope.min_freq) / @log(ceiling / scope.min_freq) * rect.w;
}
}.of;
- const freq = tuner.frequency(at.pitch, a4);
- const lo = tuner.frequency(@as(f32, @floatFromInt(at.note)) - 2.5, a4);
- const hi = tuner.frequency(@as(f32, @floatFromInt(at.note)) + 2.5, a4);
+ const freq = scope.hz(at.pitch, a4);
+ const lo = scope.hz(@as(f32, @floatFromInt(at.note)) - 2.5, a4);
+ const hi = scope.hz(@as(f32, @floatFromInt(at.note)) + 2.5, a4);
f.fill(.{ .x = x(r, lo, max_freq), .y = r.y, .w = @max(x(r, hi, max_freq) - x(r, lo, max_freq), 1), .h = r.h }, sk.Color.white.alpha(fade / 10));
f.fill(.{ .x = @round(x(r, freq, max_freq)), .y = r.y, .w = 1, .h = r.h }, color);
// The harmonics the pitch accounts for.
@@ -1407,21 +1201,21 @@ fn axes(f: *sk.Frame, b: Box, rate: f64, span: ?[2]f64) void {
f.text(b.wave.x, below, 1, left, dim);
f.text(b.wave.x + b.wave.w - @as(f32, @floatFromInt(right.len * 8)), below, 1, right, dim);
- const max_freq = maxFreq(rate);
+ const max_freq = scope.maxFreq(rate);
for ([_]f64{ 100, 1000, 10000 }) |hz| {
- const y = b.spectrogram.y + b.spectrogram.h * (1 - @as(f32, @floatCast(@log(hz / min_freq) / @log(max_freq / min_freq))));
+ const y = b.spectrogram.y + b.spectrogram.h * (1 - @as(f32, @floatCast(@log(hz / scope.min_freq) / @log(max_freq / scope.min_freq))));
f.fill(.{ .x = b.spectrogram.x, .y = y, .w = 6, .h = 1 }, sk.Color.white.alpha(140));
f.text(b.spectrogram.x + 9, y - 4, 1, hzLabel(f, hz), sk.Color.white.alpha(170));
}
for ([_]f64{ 50, 200, 1000, 5000, 20000 }) |hz| {
if (hz > max_freq) continue;
- const x = b.spectrum.x + @as(f32, @floatCast(@log(hz / min_freq) / @log(max_freq / min_freq))) * b.spectrum.w;
+ const x = b.spectrum.x + @as(f32, @floatCast(@log(hz / scope.min_freq) / @log(max_freq / scope.min_freq))) * b.spectrum.w;
f.text(@min(x + 3, b.spectrum.x + b.spectrum.w - 24), b.spectrum.y + b.spectrum.h + 4, 1, hzLabel(f, hz), dim);
}
var db: f32 = -24;
- while (db > floor_db) : (db -= 24) {
- const y = b.spectrum.y + b.spectrum.h * (db / floor_db);
+ while (db > scope.floor_db) : (db -= 24) {
+ const y = b.spectrum.y + b.spectrum.h * (db / scope.floor_db);
f.print(b.spectrum.x + 4, y + 3, 1, dim, "{d}", .{db});
}
}
@@ -1489,10 +1283,3 @@ test {
_ = tuner;
}
-test "the live history keeps the newest samples in order" {
- var live: Live = .{};
- live.push(&.{ 1, 2, 3 });
- live.push(&.{4});
- try std.testing.expectEqualSlices(f32, &.{ 1, 2, 3, 4 }, live.samples[live_history - 4 ..]);
- try std.testing.expectEqual(@as(u64, 4), live.heard);
-}
diff --git a/examples/micscope/src/tuner.zig b/examples/micscope/src/tuner.zig
index 92a8482..159798b 100644
--- a/examples/micscope/src/tuner.zig
+++ b/examples/micscope/src/tuner.zig
@@ -10,10 +10,9 @@
//! once the pitch is 60 cents from it, so a note held a quarter tone sharp
//! does not flicker between two names.
const std = @import("std");
+const scope = @import("scope");
const layouts = @import("layouts");
-pub const names = [12][]const u8{ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
-
/// Estimates clearer than this are a note (YIN's threshold is 0.12, so
/// clarity 0.88 at least when its first dip was found).
pub const min_clarity = 0.85;
@@ -22,17 +21,8 @@ pub const min_level = -60.0;
/// How long the last note stays (shown as fading) after the sound stops.
pub const hold_seconds = 1.0;
-/// Fractional MIDI number of `freq`.
-pub fn midi(freq: f32, a4: f32) f32 {
- return 69 + 12 * std.math.log2(freq / a4);
-}
-
-pub fn frequency(m: f32, a4: f32) f32 {
- return a4 * std.math.pow(f32, 2, (m - 69) / 12);
-}
-
pub fn name(note: i32) []const u8 {
- return names[@intCast(@mod(note, 12))];
+ return scope.note_names[@intCast(@mod(note, 12))];
}
pub fn octave(note: i32) i32 {
@@ -67,7 +57,7 @@ pub const Tracker = struct {
}
return;
}
- const m = midi(p.freq, a4);
+ const m = scope.midi(p.freq, a4);
if (t.count > 0 and @abs(m - t.median()) > 1) t.count = 0;
if (t.count == t.recent.len) {
std.mem.copyForwards(f32, t.recent[0 .. t.recent.len - 1], t.recent[1..]);
@@ -97,21 +87,16 @@ fn pitchOf(freq: f32) layouts.Pitch {
}
test "notes and octaves on the chromatic scale" {
- try std.testing.expectApproxEqAbs(@as(f32, 69), midi(440, 440), 1e-4);
- try std.testing.expectApproxEqAbs(@as(f32, 40), midi(82.4069, 440), 1e-3);
try std.testing.expectEqualStrings("E", name(40));
try std.testing.expectEqual(@as(i32, 2), octave(40));
try std.testing.expectEqualStrings("B", name(23));
try std.testing.expectEqual(@as(i32, 0), octave(23));
- try std.testing.expectApproxEqAbs(@as(f32, 261.626), frequency(60, 440), 1e-2);
- // Another reference pitch moves every note with it.
- try std.testing.expectApproxEqAbs(@as(f32, 69), midi(442, 442), 1e-4);
}
test "a held note settles, in cents from it" {
var t: Tracker = .{};
// A4, 12 cents sharp.
- const sharp = frequency(69.12, 440);
+ const sharp = scope.hz(69.12, 440);
for (0..20) |_| t.feed(pitchOf(sharp), 440, 1.0 / 60.0);
const r = t.reading.?;
try std.testing.expectEqual(@as(i32, 69), r.note);
@@ -132,7 +117,7 @@ test "a new note shows at once, a quarter tone does not flicker" {
for (0..3) |_| t.feed(pitchOf(146.83), 440, 1.0 / 60.0);
try std.testing.expectEqual(@as(i32, 50), t.reading.?.note);
// 55 cents sharp of D3 stays D3.
- for (0..30) |_| t.feed(pitchOf(frequency(50.55, 440)), 440, 1.0 / 60.0);
+ for (0..30) |_| t.feed(pitchOf(scope.hz(50.55, 440)), 440, 1.0 / 60.0);
try std.testing.expectEqual(@as(i32, 50), t.reading.?.note);
}