summaryrefslogtreecommitdiff
path: root/examples/micscope/shaders/spectrogram.comp
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/shaders/spectrogram.comp
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/shaders/spectrogram.comp')
-rw-r--r--examples/micscope/shaders/spectrogram.comp208
1 files changed, 208 insertions, 0 deletions
diff --git a/examples/micscope/shaders/spectrogram.comp b/examples/micscope/shaders/spectrogram.comp
new file mode 100644
index 0000000..b32f4c5
--- /dev/null
+++ b/examples/micscope/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;
+ }
+}