Beat cells

glint · Examples · Tutorials · Gallery

Voronoi cells whose centres jump to new places on every kick, each lit by its own part of the spectrum, with glowing borders (the gap between the nearest and second-nearest centre).

Audio-reactive: it moves with music from a microphone, line-in or a file.

Beat cells: an audio-reactive GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

One pass, in GLSL.

fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform vec3 spectrum;
uniform float beat;
uniform float beatTime;
uniform int beatCount;
uniform sampler2D fft;

in vec2 v_texcoord;
out vec4 fragColor;

// 1 on each kick, fading; without audio, a steady pulse twice a second.
float kick() { return beatCount > 0 ? beat : pow(1.0 - fract(time * 2.0), 4.0); }
// Seconds since the last kick, and which kick it was (the same fallback).
float sinceKick() { return beatCount > 0 ? beatTime : fract(time * 2.0) / 2.0; }
float kickId() { return beatCount > 0 ? float(beatCount) : floor(time * 2.0); }
// One FFT bin, 0..1 across the spectrum (bass on the left).
float bin(float x) { return texture(fft, vec2(x, 0.25)).r; }
// Is anything playing? (Examples fake their input when not.)
bool silent() { return spectrum.x + spectrum.y + spectrum.z < 0.0001; }

float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }

vec2 hash22(vec2 p) { return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453); }

// Where a cell's centre is: sliding from last kick's place to this kick's.
vec2 centre(vec2 cell) {
    float k = kickId();
    vec2 a = hash22(cell + vec2(k - 1.0) * vec2(7.1, 3.3));
    vec2 b = hash22(cell + vec2(k) * vec2(7.1, 3.3));
    return mix(a, b, smoothstep(0.0, 0.25, sinceKick()));
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec2 p = uv * 5.0;
    vec2 n = floor(p), f = fract(p);
    float d1 = 8.0, d2 = 8.0;
    vec2 id = vec2(0.0);
    for (int j = -1; j <= 1; j++) for (int i = -1; i <= 1; i++) {
        vec2 g = vec2(float(i), float(j));
        float d = length(g + centre(n + g) - f);
        if (d < d1) { d2 = d1; d1 = d; id = n + g; } else if (d < d2) d2 = d;
    }
    float h = hash(id);
    float level = silent() ? 0.3 + 0.25 * sin(h * 20.0 + time * 2.0) : bin(0.02 + h * 0.4);
    vec3 col = pal(h * 0.6 + time * 0.03) * (0.15 + 1.5 * level) * (1.0 - d1 * 0.6);
    col += vec3(1.0, 0.9, 0.8) * smoothstep(0.06, 0.0, d2 - d1) * (0.3 + kick());
    fragColor = vec4(col, 1.0);
}

More Audio-reactive examples

Speaker cone · FFT sphere · Shockwave · Pendulum wave · Spectrum tree · Particle fountain · VU meters · Ring tunnel