From c37f1b3b1374df2a2a434d16cd772a4bb00c567e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:19:38 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/micscope/shaders/spectrogram.comp | 208 ----------------------------- 1 file changed, 208 deletions(-) delete mode 100644 examples/micscope/shaders/spectrogram.comp (limited to 'examples/micscope/shaders/spectrogram.comp') diff --git a/examples/micscope/shaders/spectrogram.comp b/examples/micscope/shaders/spectrogram.comp deleted file mode 100644 index b32f4c5..0000000 --- a/examples/micscope/shaders/spectrogram.comp +++ /dev/null @@ -1,208 +0,0 @@ -#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; - } -} -- cgit v1.3