summaryrefslogtreecommitdiff
path: root/examples/spheres/shaders/voices.glsl
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/voices.glsl
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/voices.glsl')
-rw-r--r--examples/spheres/shaders/voices.glsl101
1 files changed, 101 insertions, 0 deletions
diff --git a/examples/spheres/shaders/voices.glsl b/examples/spheres/shaders/voices.glsl
new file mode 100644
index 0000000..c2fd56c
--- /dev/null
+++ b/examples/spheres/shaders/voices.glsl
@@ -0,0 +1,101 @@
+// From motion to sound, shared by sim.comp (what sounds) and lookahead.comp
+// (what would): each body a voice whose pitch follows its speed, whose
+// modulator ratio follows how lopsided its two distances are, and whose
+// depth follows how near its nearest neighbour is; and the closest pair one
+// FM voice, the faster body silent, modulating the other.
+
+struct Mapping {
+ float base;
+ float speed_ref;
+ float depth_scale;
+ float couple_scale;
+ // Match layouts.Scale: free, pentatonic, diatonic, chromatic.
+ uint scale;
+};
+
+// The nearest note of the scale, as a MIDI number: A minor pentatonic (A C
+// D E G), A minor / C major (the white keys), every semitone, or none.
+float snap(float midi, uint scale) {
+ if (scale == 0u) return midi;
+ if (scale == 3u) return round(midi);
+ float m = midi - 69.0;
+ float octave = floor(m / 12.0);
+ float degree = m - 12.0 * octave;
+ const float pentatonic[6] = float[](0.0, 3.0, 5.0, 7.0, 10.0, 12.0);
+ const float diatonic[8] = float[](0.0, 2.0, 3.0, 5.0, 7.0, 8.0, 10.0, 12.0);
+ float best = 0.0;
+ if (scale == 1u) {
+ for (int i = 1; i < 6; i++) if (abs(pentatonic[i] - degree) < abs(best - degree)) best = pentatonic[i];
+ } else {
+ for (int i = 1; i < 8; i++) if (abs(diatonic[i] - degree) < abs(best - degree)) best = diatonic[i];
+ }
+ return 69.0 + 12.0 * octave + best;
+}
+
+// Voices: (carrier Hz, ratio, depth, amplitude). Coupling rows: xyz the FM
+// index each voice gets from each other, w whether it is the silent
+// modulator.
+void sing(vec2 p[3], vec2 v[3], Mapping map, out vec4 voices[3], out vec4 coupling[3]) {
+ for (int i = 0; i < 3; i++) {
+ float speed = length(v[i]);
+ float octaves = clamp(log2(max(speed, 1e-4) / map.speed_ref), -2.0, 2.5);
+ float midi = 12.0 * log2(map.base / 440.0) + 69.0 + 12.0 * octaves;
+ midi = snap(midi, map.scale);
+ float d1 = distance(p[i], p[(i + 1) % 3]);
+ float d2 = distance(p[i], p[(i + 2) % 3]);
+ float near = min(d1, d2);
+ float far = max(d1, d2);
+ voices[i] = vec4(
+ 440.0 * exp2((midi - 69.0) / 12.0),
+ 0.5 + 4.0 * near / max(near + far, 1e-6),
+ clamp(map.depth_scale / max(near, 1e-3), 0.0, 12.0),
+ 0.2 + 0.15 * clamp(speed / (2.0 * map.speed_ref), 0.0, 1.0));
+ coupling[i] = vec4(0.0);
+ }
+ int first = 0;
+ int second = 1;
+ float closest = distance(p[0], p[1]);
+ if (distance(p[0], p[2]) < closest) { first = 0; second = 2; closest = distance(p[0], p[2]); }
+ if (distance(p[1], p[2]) < closest) { first = 1; second = 2; closest = distance(p[1], p[2]); }
+ int modulator = length(v[first]) > length(v[second]) ? first : second;
+ int carrier = modulator == first ? second : first;
+ coupling[carrier][modulator] = clamp(map.couple_scale / max(closest, 1e-3), 0.0, 4.0);
+ coupling[modulator].w = 1.0;
+}
+
+// The chord with its voicing, packed: bits 0-11 the pitch classes (the
+// chord), bit 12 set if it is playable, bits 16-19 the lowest note's pitch
+// class (for root position), bits 24-31 how bad it sounds, 0 to 255.
+// Playable: every note from E2 to A5, an octave and a fifth at most from
+// lowest to highest, and the FM pair's index at most 3; past those, the
+// same pitch classes are another sound, shrill, sprawling or a cloud of
+// sidebands. Within them, wider, higher and harder modulated cost more.
+uint voicing(vec4 voices[3], vec4 coupling[3]) {
+ int low = 1000;
+ int high = -1000;
+ int lowest_class = 0;
+ for (int i = 0; i < 3; i++) {
+ int midi = int(round(69.0 + 12.0 * log2(voices[i].x / 440.0)));
+ if (midi < low) {
+ low = midi;
+ lowest_class = ((midi % 12) + 12) % 12;
+ }
+ high = max(high, midi);
+ }
+ float index = 0.0;
+ for (int i = 0; i < 3; i++) index = max(index, max(coupling[i].x, max(coupling[i].y, coupling[i].z)));
+ int spread = high - low;
+ bool playable = low >= 40 && high <= 81 && spread <= 19 && index <= 3.0;
+ float bad = 6.0 * float(max(spread - 12, 0)) + 10.0 * float(max(high - 76, 0)) + 15.0 * index;
+ return (playable ? 1u << 12 : 0u) | (uint(lowest_class) << 16) | (uint(clamp(bad, 0.0, 255.0)) << 24);
+}
+
+// The pitch classes the three voices sing, as bits (C is bit 0): the chord.
+uint chordOf(vec4 voices[3]) {
+ uint mask = 0u;
+ for (int i = 0; i < 3; i++) {
+ int midi = int(round(69.0 + 12.0 * log2(voices[i].x / 440.0)));
+ mask |= 1u << uint(((midi % 12) + 12) % 12);
+ }
+ return mask;
+}