Mirror bars

glint · Examples · Tutorials · Gallery

The classic LED spectrum analyser, bass in the middle, standing on a wet floor that reflects it with ripples.

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

Mirror bars: 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))); }

void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    float horizon = 0.28;
    float below = step(uv.y, horizon);
    // Distance from the floor line: below it we see the bars upside down, rippling.
    float y = abs(uv.y - horizon);
    float x = abs(uv.x - 0.5) * 2.0 + below * sin(y * 90.0 - time * 5.0) * 0.004;
    float n = 32.0;
    float id = floor(x * n), f = fract(x * n);
    // Squared, so the bass bins get more of the width (that's where most of the music is).
    float level = bin(0.01 + pow((id + 0.5) / n, 2.0) * 0.5);
    // Without audio, a made-up spectrum: loud bass, bouncing on the pulse.
    if (silent()) level = (0.25 + 0.5 * kick() * exp(-id * 0.15)) * (0.6 + 0.4 * sin(id * 0.9 + time * 3.0)) * (1.0 - id / n * 0.6);
    float h = level * 0.6 * (0.8 + 0.4 * kick());
    float seg = fract(y * 70.0);
    float lit = step(0.12, f) * step(f, 0.88) * step(0.25, seg) * step(y, h);
    vec3 barCol = mix(vec3(0.1, 1.0, 0.5), vec3(1.0, 0.15, 0.3), smoothstep(0.1, 0.5, y));
    barCol = mix(barCol, pal(id / n * 0.4 + time * 0.03), 0.35);
    vec3 col = mix(vec3(0.02, 0.01, 0.05), vec3(0.08, 0.02, 0.12), clamp(1.0 - y * 2.0, 0.0, 1.0));
    col += vec3(0.4, 0.1, 0.6) * exp(-y * 25.0) * (0.3 + spectrum.x * 2.0);
    col += barCol * lit * mix(1.0, 0.3 * max(0.0, 1.0 - y * 3.0), below);
    fragColor = vec4(col, 1.0);
}

More Audio-reactive examples

Neon runner · Hex pulse · Spectrum flower · Starburst · Punch zoom · Synthwave drive · Kick fireworks · Spectrogram