// 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; }