Speaker cone

glint · Examples · Tutorials · Gallery

A raymarched loudspeaker whose cone pumps with the bass, its rim light flashing on each kick. The speaker is a height field: depth as a function of radius.

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

Speaker cone: 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))); }

vec3 cameraRay(vec3 ro, vec3 target, vec2 uv, float zoom) {
    vec3 f = normalize(target - ro);
    vec3 r = normalize(cross(vec3(0.0, 1.0, 0.0), f));
    vec3 u = cross(f, r);
    return normalize(uv.x * r + uv.y * u + zoom * f);
}

// How far the surface stands out towards us at radius r.
float height(float r) {
    float e = 0.1 * (spectrum.x * 0.7 + kick() * 0.5);                     // the cone moves in and out by e
    if (r < 0.8) return -0.4 * smoothstep(0.8, 0.25, r) + e + 0.12 * sqrt(max(0.0, 1.0 - r * r / 0.0625)); // cone + dust cap
    if (r < 0.98) return 0.07 * sin(clamp((r - 0.8) / 0.18, 0.0, 1.0) * 3.14159) + e * (1.0 - smoothstep(0.8, 0.98, r)); // rubber roll
    if (r < 1.15) return 0.03;                                               // metal rim
    return 0.0;                                                              // the box
}

// We look from -z, so the distance to the surface is how far p is in front of it.
float map(vec3 p) { return (-p.z - height(length(p.xy))) * 0.6; }

vec3 normal(vec3 p) {
    vec2 e = vec2(0.002, 0.0);
    return normalize(vec3(map(p + e.xyy) - map(p - e.xyy), map(p + e.yxy) - map(p - e.yxy), map(p + e.yyx) - map(p - e.yyx)));
}

void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    vec3 ro = vec3(1.2 * sin(time * 0.3), 0.6 + 0.3 * sin(time * 0.2), -4.2);
    vec3 rd = cameraRay(ro, vec3(0.0), uv, 1.6);
    float t = 0.0;
    for (int i = 0; i < 120; i++) {
        float d = map(ro + rd * t);
        if (abs(d) < 0.001 || t > 10.0) break;
        t += d;
    }
    vec3 p = ro + rd * t;
    vec3 n = normal(p);
    float r = length(p.xy);
    vec3 albedo; float shine;
    if (r < 0.8) { albedo = vec3(0.08) * (0.8 + 0.2 * sin(atan(p.y, p.x) * 40.0)); shine = 8.0; }   // paper cone, ribbed
    else if (r < 0.98) { albedo = vec3(0.03); shine = 60.0; }                                       // rubber
    else if (r < 1.15) { albedo = vec3(0.5); shine = 120.0; }                                        // metal
    else { albedo = vec3(0.12, 0.07, 0.04) * (0.8 + 0.2 * hash(floor(p.xy * 40.0))); shine = 4.0; } // wood
    vec3 lp = vec3(-1.5, 2.0, -2.5);
    vec3 l = normalize(lp - p);
    float diff = max(dot(n, l), 0.0);
    float spec = pow(max(dot(reflect(-l, n), -rd), 0.0), shine) * (shine > 30.0 ? 1.0 : 0.2);
    vec3 col = albedo * (0.15 + diff) + spec;
    // A light ring in the rim.
    vec3 ring = pal(time * 0.05) * smoothstep(0.03, 0.0, abs(r - 1.2)) * (0.3 + 2.0 * kick());
    col += ring + pal(time * 0.05) * 0.1 * kick() * exp(-abs(r - 1.2) * 6.0);
    fragColor = vec4(pow(col, vec3(0.4545)), 1.0);
}

More Audio-reactive examples

Kick fireworks · Spectrogram · Quarter turns · Waveform trails · FFT sphere · Shockwave · Pendulum wave · Beat cells