Spectrogram

glint · Examples · Tutorials · Gallery

A scrolling waterfall of the spectrum: one pass shifts its own last frame up a pixel and writes the spectrum along the bottom, the next colours it.

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

Spectrogram: 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

2 passes, drawn in this order each frame, in GLSL.

History: 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;

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

void main() {
    vec2 p = gl_FragCoord.xy;
    if (p.y > 1.0) {
        // Everything moves up one pixel a frame...
        fragColor = texture(feedback, (p - vec2(0.0, 1.0)) / resolution);
        return;
    }
    // ...and the bottom row is the spectrum now (squared so bass gets room).
    float x = p.x / resolution.x;
    float v = bin(x * x * 0.7);
    // Without audio, fake some: a wandering tone and a kick.
    if (silent()) v = exp(-pow((x - 0.4 - 0.25 * sin(time * 0.7)) * 14.0, 2.0)) + 0.7 * kick() * (1.0 - x);
    fragColor = vec4(v, 0.0, 0.0, 1.0);
}

Colour: fragment shader

#version 300 es
precision highp float;

uniform sampler2D prevPass;
uniform vec2 resolution;

in vec2 v_texcoord;
out vec4 fragColor;

// Black -> purple -> orange -> yellow-white.
vec3 heat(float v) {
    v = clamp(v, 0.0, 1.0);
    return pow(vec3(v), vec3(0.8, 2.0, 5.0)) + vec3(0.3, 0.0, 0.45) * sin(v * 3.14159);
}

void main() {
    vec2 uv = gl_FragCoord.xy / resolution;
    vec3 col = heat(texture(prevPass, uv).r * 1.2);
    // Faint guides every tenth of the width.
    col += 0.05 * smoothstep(0.002, 0.0, abs(fract(uv.x * 10.0 + 0.5) - 0.5) / 10.0);
    fragColor = vec4(col, 1.0);
}

More Audio-reactive examples

Mirror bars · Punch zoom · Synthwave drive · Kick fireworks · Quarter turns · Waveform trails · Speaker cone · FFT sphere