VU meters

glint · Examples · Tutorials · Gallery

A pair of analogue meters, bass and treble: a printed scale with a red zone, needles with their shadows, and a warm backlight.

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

VU meters: 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))); }

float segment(vec2 p, vec2 a, vec2 b) {
    vec2 pa = p - a, ba = b - a;
    return length(pa - ba * clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0));
}

// One meter at p (its own coordinates: pivot at the origin, face above it).
vec3 meter(vec2 p, float level, vec3 col, float px) {
    vec2 face = p - vec2(0.0, 0.22);
    float box = max(abs(face.x) - 0.4, abs(face.y) - 0.26);
    if (box > 0.03) return col;
    col = mix(col, vec3(0.08), smoothstep(px, 0.0, box - 0.02));                  // bezel
    vec3 paper = vec3(1.0, 0.85, 0.55) * (1.0 - 0.3 * length(face));              // backlit
    col = mix(col, paper, smoothstep(px, 0.0, box));
    // The scale: an arc from -50 to +50 degrees, with ticks; red past 70%.
    float r = length(p), a = atan(p.x, p.y);
    float t = (a + 0.873) / 1.746;                                                  // 0..1 across the arc
    float onArc = step(0.0, t) * step(t, 1.0);
    vec3 ink = t > 0.7 ? vec3(0.8, 0.1, 0.05) : vec3(0.1);
    col = mix(col, ink, onArc * smoothstep(px, 0.0, abs(r - 0.36) - 0.003));
    float tick = abs(fract(t * 10.0 + 0.5) - 0.5) / 10.0 * 1.746 * r;
    col = mix(col, ink, onArc * step(0.36, r) * step(r, 0.4) * smoothstep(px, 0.0, tick - 0.003));
    // The needle, and its shadow on the paper.
    float na = mix(-0.873, 0.873, clamp(level, 0.0, 1.05));
    vec2 tip = 0.42 * vec2(sin(na), cos(na));
    col *= 1.0 - 0.25 * smoothstep(0.012, 0.0, segment(p - vec2(0.012, -0.01), vec2(0.0), tip));
    col = mix(col, vec3(0.05), smoothstep(px, 0.0, segment(p, vec2(0.0), tip) - 0.003));
    col = mix(col, vec3(0.1), smoothstep(px, 0.0, length(p) - 0.03));
    return col;
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float px = 1.5 / resolution.y;
    vec3 col = vec3(0.12, 0.07, 0.05) * (0.8 + 0.2 * hash(floor(uv * 200.0)));  // wood
    float bass = silent() ? 0.35 + 0.15 * sin(time * 1.7) + 0.35 * kick() : spectrum.x * 1.3;
    float treble = silent() ? 0.3 + 0.2 * sin(time * 2.9 + 1.0) : spectrum.z * 2.5;
    col = meter(uv - vec2(-0.45, -0.2), bass, col, px);
    col = meter(uv - vec2(0.45, -0.2), treble, col, px);
    fragColor = vec4(col, 1.0);
}

More Audio-reactive examples

Pendulum wave · Beat cells · Spectrum tree · Particle fountain · Ring tunnel · Light curtain · Halftone spectrum · Orbitals