summaryrefslogtreecommitdiff
path: root/examples/micscope/src/layouts.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 00:15:15 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit9bd77e54943d954291b5faef287ccca5dcebd7a4 (patch)
tree3f2d628781441568620b8108fab920713f26f649 /examples/micscope/src/layouts.zig
parente997fc3e6b312b30f5af90012fc22365bab701d4 (diff)
downloadsketchbook-9bd77e54943d954291b5faef287ccca5dcebd7a4.tar.gz
sketchbook-9bd77e54943d954291b5faef287ccca5dcebd7a4.zip
examples: micscope and spheres come home, as they were, building
Both lived outside any repository, beside sketchbook, with manifests that pointed at ../01-drat-meout/lib/sketchbook, which no longer exists. They are copied into examples/micscope and examples/spheres (their sources, shaders, tests, test material and web icons; not their caches, builds, packages, state files or .gitignore, which the root's covers), their manifests depend on "../.." as the other examples do, and their READMEs link sketchbook there. Nothing else changed: both build against today's library and pass their own zig build test (micscope 62/62 steps, 11/11 tests, its transport, record, render and tuner scripts; spheres 75/75 steps, 3/3 tests, its play script). The originals are untouched. micscope's test/reference.flac (3.7 MiB, CC BY-SA 3.0, credited in its test/README.md) is the rendering reference its render script opens. The README lists both examples. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/micscope/src/layouts.zig')
-rw-r--r--examples/micscope/src/layouts.zig154
1 files changed, 154 insertions, 0 deletions
diff --git a/examples/micscope/src/layouts.zig b/examples/micscope/src/layouts.zig
new file mode 100644
index 0000000..cd5be65
--- /dev/null
+++ b/examples/micscope/src/layouts.zig
@@ -0,0 +1,154 @@
+//! 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" } };
+};
+
+/// tuner.comp: the YIN window is the `tuner_window + tuner_lags` samples
+/// ending at `end`, read like the other analyses (`offset`, `samples`).
+pub const tuner_window = 2048;
+pub const tuner_lags = 1700;
+
+pub const TunerParams = extern struct {
+ offset: u32,
+ end: u32,
+ samples: u32,
+ rate: f32,
+ /// The pitch range searched.
+ min_freq: f32,
+ max_freq: f32,
+ /// YIN's absolute threshold on the normalised difference.
+ threshold: f32,
+ pad: u32 = 0,
+
+ pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } };
+};
+
+/// What tuner.comp writes (std430, read back by the CPU).
+pub const Pitch = extern struct {
+ /// Hz; 0 when nothing periodic was found.
+ freq: f32,
+ /// 1 minus YIN's normalised difference at the period: 1 is perfectly
+ /// periodic.
+ clarity: f32,
+ /// RMS of the window, dBFS.
+ level: f32,
+ /// The period in samples, interpolated.
+ period: f32,
+};