summaryrefslogtreecommitdiff
path: root/examples/spheres/shaders/synth.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/spheres/shaders/synth.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/spheres/shaders/synth.comp')
-rw-r--r--examples/spheres/shaders/synth.comp112
1 files changed, 112 insertions, 0 deletions
diff --git a/examples/spheres/shaders/synth.comp b/examples/spheres/shaders/synth.comp
new file mode 100644
index 0000000..3a6c40c
--- /dev/null
+++ b/examples/spheres/shaders/synth.comp
@@ -0,0 +1,112 @@
+#version 450
+#extension GL_GOOGLE_include_directive : require
+// One audio block, one thread per sample: three voices from the bodies'
+// parameters, gliding from what the last block ended on to what sim.comp
+// set now, so blocks join without clicks.
+//
+// Each phase has a closed form, so every sample is computed alone: a
+// frequency gliding linearly from w0 to w1 (radians per sample) over N
+// samples has phase p0 + w0 n + (w1 - w0) n^2 / 2N after n of them.
+//
+// The modes, with carrier phase c, modulator phase m (at ratio times the
+// carrier's frequency) and depth d:
+// sine sin c
+// additive sum over harmonics k of r^(k-1) sin kc, r = d / (d + 2),
+// those under Nyquist, normalised
+// AM sin c (1 + a sin m) / (1 + a), a = min(d / 4, 1)
+// ring sin c sin m
+// FM sin of the integral of the carrier's frequency, pushed
+// d times the carrier's frequency either way by cos m: so the
+// index, deviation over modulator frequency, is d / ratio and
+// changes as the modulator does
+// PM sin(c + d sin m): the index is d, whatever the modulator
+// FM and PM sound alike for a steady modulator, and part when it moves.
+//
+// The closest pair (sim.comp) is one FM voice: the modulating body is
+// silent, and a sine at its frequency, times the index, pushes the other
+// body's carrier phase. Two operators, both moved by the bodies. The push
+// moves every harmonic of the additive voice alike, so its timbre bends
+// without turning harsh.
+layout(local_size_x = 64) in;
+
+#include "SynthParams.glsl"
+
+layout(set = 0, binding = 0) readonly buffer Voices { vec4 voices[]; };
+// Per voice: carrier phase, modulator phase, FM's phase.
+layout(set = 0, binding = 1) readonly buffer Phases { vec4 phases[]; };
+layout(set = 1, binding = 0) buffer Block { float block[]; };
+
+const float TAU = 6.28318530718;
+// Match layouts.Mode.
+const uint SINE = 0u;
+const uint ADDITIVE = 1u;
+const uint AM = 2u;
+const uint RING = 3u;
+const uint FM = 4u;
+
+float glide(float phase, float w0, float w1, float n, float count) {
+ return phase + w0 * n + (w1 - w0) * n * n / (2.0 * count);
+}
+
+// Voice i's signal, its carrier phase pushed by `push` radians.
+float voice(int i, float n, float count, float push) {
+ vec4 from = voices[i];
+ vec4 to = voices[3 + i];
+ vec4 phase = phases[i];
+ float t = n / count;
+ float wc0 = TAU * from.x / u.rate;
+ float wc1 = TAU * to.x / u.rate;
+ float c = glide(phase.x, wc0, wc1, n, count);
+ float m = glide(phase.y, wc0 * from.y, wc1 * to.y, n, count);
+ float depth = mix(from.z, to.z, t);
+ float ratio = mix(from.y, to.y, t);
+ float y;
+ if (u.mode == SINE) {
+ y = sin(c + push);
+ } else if (u.mode == ADDITIVE) {
+ float r = depth / (depth + 2.0);
+ float carrier = mix(from.x, to.x, t);
+ float sum = 0.0;
+ float norm = 0.0;
+ float weight = 1.0;
+ for (int k = 1; k <= 16; k++) {
+ if (float(k) * carrier >= 0.5 * u.rate) break;
+ sum += weight * sin(float(k) * c + push);
+ norm += weight;
+ weight *= r;
+ }
+ y = sum / max(norm, 1e-6);
+ } else if (u.mode == AM) {
+ float a = min(depth / 4.0, 1.0);
+ y = sin(c + push) * (1.0 + a * sin(m)) / (1.0 + a);
+ } else if (u.mode == RING) {
+ y = sin(c + push) * sin(m);
+ } else if (u.mode == FM) {
+ y = sin(phase.z + push + (c - phase.x) + depth / max(ratio, 1e-3) * (sin(m) - sin(phase.y)));
+ } else {
+ y = sin(c + push + depth * sin(m));
+ }
+ return y;
+}
+
+void main() {
+ uint n = gl_GlobalInvocationID.x;
+ if (n >= u.count) return;
+ float count = float(u.count);
+ float t = float(n) / count;
+ // Each body as a modulator: a sine at its carrier frequency.
+ vec3 sines;
+ for (int i = 0; i < 3; i++) {
+ float w0 = TAU * voices[i].x / u.rate;
+ float w1 = TAU * voices[3 + i].x / u.rate;
+ sines[i] = sin(glide(phases[i].x, w0, w1, float(n), count));
+ }
+ float sum = 0.0;
+ for (int i = 0; i < 3; i++) {
+ vec4 row = mix(voices[6 + i], voices[9 + i], t);
+ float audible = (1.0 - row.w) * mix(voices[i].w, voices[3 + i].w, t);
+ if (audible < 1e-4) continue;
+ sum += audible * voice(i, float(n), count, dot(row.xyz, sines));
+ }
+ block[n] = tanh(u.gain * sum);
+}