diff options
Diffstat (limited to 'examples/spheres/shaders/synth.comp')
| -rw-r--r-- | examples/spheres/shaders/synth.comp | 112 |
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); +} |
