summaryrefslogtreecommitdiff
path: root/examples/micscope/shaders/tuner.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/tuner.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/tuner.comp')
-rw-r--r--examples/micscope/shaders/tuner.comp134
1 files changed, 134 insertions, 0 deletions
diff --git a/examples/micscope/shaders/tuner.comp b/examples/micscope/shaders/tuner.comp
new file mode 100644
index 0000000..1907f7f
--- /dev/null
+++ b/examples/micscope/shaders/tuner.comp
@@ -0,0 +1,134 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// The pitch of the newest (or the playhead's) sound, by YIN (de Cheveigné
+// and Kawahara, 2002), in one workgroup. Picking the strongest FFT peak
+// finds the loudest harmonic, which is often not the note: low strings,
+// voices and brass put more energy in the 2nd or 3rd harmonic, and a
+// missing fundamental has no peak at all. YIN looks for the period instead:
+//
+// 1. the difference function d(tau) = sum over the window of
+// (x[j] - x[j + tau])^2, which dips to 0 at every multiple of the
+// period, whatever the harmonics' balance;
+// 2. normalised by its running mean, d'(tau) = d(tau) * tau / sum d(1..tau),
+// which starts at 1 and removes the dip-at-zero bias, so the dips are
+// comparable;
+// 3. the first dip below an absolute threshold, then the bottom of it: the
+// shortest period that explains the signal, which keeps it from
+// dropping an octave onto a multiple of the period;
+// 4. a parabola through that dip of d and its neighbours, for a period
+// between samples: the semitone is 6 %, the cent 0.06 %, and whole samples
+// alone would be 30 cents apart at 1 kHz.
+layout(local_size_x = 256) in;
+
+#include "TunerParams.glsl"
+
+layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; };
+// d(tau), from tuner_difference.comp, which does the bulk of the work.
+layout(set = 0, binding = 1) readonly buffer Difference { float difference[]; };
+// freq, clarity, level, period: layouts.Pitch.
+layout(set = 1, binding = 0) buffer Result { vec4 result; };
+
+// Match layouts.zig.
+const uint W = 2048;
+const uint LAGS = 1700;
+const uint THREADS = 256;
+
+// Each thread owns CHUNK consecutive lags.
+const uint CHUNK = (LAGS + THREADS - 1u) / THREADS;
+shared float d[THREADS * CHUNK];
+// d before normalising: the parabola goes through these, since the
+// normalisation's factor tau tilts a dip by more than a cent at high pitch.
+shared float raw[THREADS * CHUNK];
+shared float squares[THREADS];
+shared float sums[THREADS];
+shared uint first_dip;
+shared float deepest[THREADS];
+shared uint deepest_at[THREADS];
+
+void main() {
+ uint t = gl_LocalInvocationID.x;
+ uint tau_max = min(LAGS - 2u, uint(ceil(u.rate / u.min_freq)));
+ uint tau_min = max(2u, uint(floor(u.rate / u.max_freq)));
+
+ // 1. The difference function (tuner_difference.comp), and the window's
+ // energy.
+ int first = int(u.end) - int(W);
+ float sq = 0.0;
+ for (uint j = t; j < W; j += THREADS) {
+ int at = first + int(j);
+ float v = at >= 0 && at < int(u.samples) ? tape[u.offset + uint(at)] : 0.0;
+ sq += v * v;
+ }
+ squares[t] = sq;
+ float own = 0.0;
+ for (uint i = 0u; i < CHUNK; i++) {
+ uint tau = t * CHUNK + i;
+ float v = tau >= 1u && tau <= tau_max + 1u ? difference[tau] : 0.0;
+ raw[tau] = v;
+ own += v;
+ }
+ sums[t] = own;
+ if (t == 0u) first_dip = 0xffffffffu;
+ barrier();
+
+ // 2. Normalised by the running sum: a scan of the threads' chunk sums
+ // (Hillis-Steele), then each chunk in order.
+ for (uint stride = 1u; stride < THREADS; stride <<= 1u) {
+ float add = t >= stride ? sums[t - stride] : 0.0;
+ barrier();
+ sums[t] += add;
+ barrier();
+ }
+ float running = t > 0u ? sums[t - 1u] : 0.0;
+ float low = 2.0;
+ uint low_at = 0u;
+ for (uint i = 0u; i < CHUNK; i++) {
+ uint tau = t * CHUNK + i;
+ running += raw[tau];
+ float v = tau == 0u ? 1.0 : (running > 0.0 ? raw[tau] * float(tau) / running : 1.0);
+ d[tau] = v;
+ if (tau >= tau_min && tau <= tau_max) {
+ // 3. The first dip under the threshold, and the deepest.
+ if (v < u.threshold) atomicMin(first_dip, tau);
+ if (v < low) {
+ low = v;
+ low_at = tau;
+ }
+ }
+ }
+ deepest[t] = low;
+ deepest_at[t] = low_at;
+ barrier();
+ for (uint stride = THREADS / 2u; stride > 0u; stride >>= 1u) {
+ if (t < stride && deepest[t + stride] < deepest[t]) {
+ deepest[t] = deepest[t + stride];
+ deepest_at[t] = deepest_at[t + stride];
+ }
+ barrier();
+ }
+ if (t != 0u) return;
+
+ float energy = 0.0;
+ for (uint i = 0u; i < THREADS; i++) energy += squares[i];
+ float level = energy > 0.0 ? 10.0 * log(energy / float(W)) / log(10.0) : -160.0;
+
+ // The first dip followed to its bottom; failing one, the deepest, which
+ // the CPU may still reject as unclear.
+ uint best = first_dip;
+ if (best == 0xffffffffu) {
+ best = max(deepest_at[0], tau_min);
+ } else {
+ while (best + 1u <= tau_max && d[best + 1u] < d[best]) best++;
+ }
+
+ // 4. The parabola through the dip.
+ float a = raw[best - 1u];
+ float b = raw[best];
+ float c = raw[best + 1u];
+ float bend = a - 2.0 * b + c;
+ float shift = bend > 0.0 ? clamp(0.5 * (a - c) / bend, -0.5, 0.5) : 0.0;
+ float period = float(best) + shift;
+ float depth = d[best];
+ bool silent = energy <= 0.0;
+ result = vec4(silent ? 0.0 : u.rate / period, silent ? 0.0 : clamp(1.0 - depth, 0.0, 1.0), level, period);
+}