Heartbeat

glint · Examples · Tutorials · Gallery

A heart (Inigo Quilez's exact distance function) that beats lub-dub on the kick, over a scrolling monitor trace.

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

Heartbeat: 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 dot2(vec2 v) { return dot(v, v); }
float sdHeart(vec2 p) {
    p.x = abs(p.x);
    if (p.y + p.x > 1.0) return sqrt(dot2(p - vec2(0.25, 0.75))) - sqrt(2.0) / 4.0;
    return sqrt(min(dot2(p - vec2(0.0, 1.0)), dot2(p - 0.5 * max(p.x + p.y, 0.0)))) * sign(p.x - p.y);
}

// The monitor trace: flat, then the spike of a beat.
float ecg(float t) {
    float f = fract(t);
    return 0.05 * exp(-pow((f - 0.1) * 30.0, 2.0)) - 0.08 * exp(-pow((f - 0.2) * 60.0, 2.0)) + 0.35 * exp(-pow((f - 0.23) * 70.0, 2.0))
         - 0.12 * exp(-pow((f - 0.27) * 60.0, 2.0)) + 0.08 * exp(-pow((f - 0.45) * 15.0, 2.0));
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float s = sinceKick();
    // Lub, then dub a fifth of a second later.
    float pump = exp(-s * 12.0) + 0.6 * exp(-max(s - 0.2, 0.0) * 12.0) * step(0.2, s);
    float size = 0.45 * (1.0 + 0.12 * pump);
    float d = sdHeart((uv - vec2(0.0, 0.08)) / size + vec2(0.0, 0.5)) * size;
    float px = 1.5 / resolution.y;
    vec3 col = vec3(0.04, 0.01, 0.02);
    col += vec3(1.0, 0.1, 0.2) * 0.015 / (abs(d) + 0.015) * (0.4 + pump);
    col = mix(col, vec3(0.8, 0.05, 0.15) * (0.8 + 0.4 * pump) * (1.0 - 0.3 * smoothstep(-0.2, 0.0, d)), smoothstep(px, 0.0, d));
    // Trace along the bottom, drawn left to right.
    float y = -0.38 + ecg(uv.x * 1.2 - time * 1.2);
    col += vec3(0.2, 1.0, 0.4) * 0.002 / (abs(uv.y - y) + 0.002) * smoothstep(-1.0, 0.8, uv.x);
    fragColor = vec4(col, 1.0);
}

More Audio-reactive examples

Op art · Square tunnel · Waveform ring · Cube burst · Disco ball · Grid pulses · Feedback mandala · 3D bar ring