Sonar

glint · Examples · Tutorials · Gallery

A radar screen: the sweep turns in time with the tempo and every kick puts a blip on the screen, glowing and fading in green phosphor.

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

Sonar: 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;
uniform sampler2D feedback;
uniform float bpm;

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

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

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec3 prev = texture(feedback, gl_FragCoord.xy / resolution).rgb * vec3(0.975, 0.985, 0.975);
    float r = length(uv);
    float a = atan(uv.y, uv.x);
    // One turn per bar.
    float sweep = -time * bpm / 60.0 * 6.28318 / 4.0;
    float behind = mod(sweep - a, 6.28318);
    vec3 col = prev;
    if (r < 0.46) {
        col += vec3(0.1, 1.0, 0.4) * 0.12 * exp(-behind * 6.0);
        // Blips: where each kick puts them, drawn as the sweep passes.
        for (int i = 0; i < 6; i++) {
            float id = kickId() - float(i);
            vec2 at = (hash2(id) - 0.5) * 0.75;
            float age = sinceKick() + float(i) * 0.5;
            float d = length(uv - at);
            col += vec3(0.4, 1.0, 0.6) * 0.004 / (d * d + 0.0004) * exp(-age * 1.5) * step(length(at), 0.44) * 0.05;
        }
    }
    // The screen furniture: rings, cross, rim.
    float grid = min(abs(fract(r * 10.0 + 0.5) - 0.5) / fwidth(r * 10.0), min(abs(uv.x), abs(uv.y)) / fwidth(uv.x));
    vec3 face = col + vec3(0.05, 0.3, 0.1) * (1.0 - min(grid, 1.0)) * step(r, 0.46) * 0.4;
    face *= smoothstep(0.47, 0.45, r);
    face += vec3(0.2, 0.25, 0.2) * smoothstep(0.012, 0.0, abs(r - 0.465));
    // The feedback keeps only the phosphor (not the grid), so the grid doesn't burn in.
    fragColor = vec4(r < 0.46 ? max(face, col) : face, 1.0);
}

More Audio-reactive examples

Strobe shapes · LED meter · Bass blob · Vinyl · Neon runner · Hex pulse · Spectrum flower · Starburst