diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 00:19:38 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | c37f1b3b1374df2a2a434d16cd772a4bb00c567e (patch) | |
| tree | 4feccf16cf1f6808bae6ba26602705d557ab2fb7 /examples/micscope/shaders/spectrum.comp | |
| parent | 9bd77e54943d954291b5faef287ccca5dcebd7a4 (diff) | |
| download | sketchbook-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/shaders/spectrum.comp')
| -rw-r--r-- | examples/micscope/shaders/spectrum.comp | 128 |
1 files changed, 0 insertions, 128 deletions
diff --git a/examples/micscope/shaders/spectrum.comp b/examples/micscope/shaders/spectrum.comp deleted file mode 100644 index e576e30..0000000 --- a/examples/micscope/shaders/spectrum.comp +++ /dev/null @@ -1,128 +0,0 @@ -#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; - } -} |
