Subwoofer

by MusicBar, visualizer

A row of car-audio subwoofers with glowing rings pumps in and out with the bass, beads hop on the cones, and every beat throws a spray of droplets off them like a viral bass test.

1 downloads Download file, 8.5 KB
How it installs, its SHA-256 and its code

Click Apply: MusicBar downloads it, checks its SHA-256 and puts it on the taskbar, or just puts it on if it already has it. Needs MusicBar 1.8 or newer.

SHA-256 9d9baaea893d3dbcd2e62bbaa6d43addc923e1652deb4f7fb331da0cf299da5b

//! name: Subwoofer
//! author: MusicBar
//! kind: visualizer
//! description: A row of car-audio subwoofers with glowing rings pumps in and out with the bass, beads hop on the cones, and every beat throws a spray of droplets off them like a viral bass test.

float hash1(float n) { return frac(sin(n * 127.1 + 311.7) * 43758.5453); }

// Lays a color over what is there (premultiplied light, and how much of the pixel it covers).
void layer(inout float3 acc, inout float cover, float3 rgb, float a)
{
    a = saturate(a);
    acc = rgb * a + acc * (1.0 - a);
    cover = a + cover * (1.0 - a);
}

// How far sub j's cone is thrown out now (below 0: sucked in): kicked by the beat, buzzing with its band.
float pumpOf(float j, float sb)
{
    return 0.9 * beat * (0.6 + 0.6 * sb) + 0.4 * sb * sin(time * 27.0 + j * 2.1) + 0.15 * sb;
}

// The droplets a beat throws off sub j (centered at c): each flies out of the cone on its own arc and
// falls back under gravity, a short streak along the way it flies. Returns x: core, y: glow.
float2 spray(float2 px, float2 c, float j, float sb, float H)
{
    float u = 1.0 - beat;
    float live = smoothstep(0.0, 0.2, beat) * step(0.04, u);
    float core = 0;
    float glow = 0;
    [loop] for (int i = 0; i < 8; i++)
    {
        float h1 = hash1(j * 17.3 + i * 3.1), h2 = hash1(j * 5.7 + i * 11.3), h3 = hash1(j * 2.9 + i * 7.7);
        float a = (h1 - 0.5) * 1.9;
        float v = H * (0.95 + 0.8 * h2) * (0.7 + 0.5 * sb);
        float2 st = c + float2((h3 - 0.5) * 0.4, (hash1(i + j * 3.3) - 0.5) * 0.3) * H;
        float2 pos = st + float2(sin(a) * v * u * 0.9, cos(a) * v * u - 1.4 * H * u * u);
        float2 vel = float2(sin(a) * v * 0.9, cos(a) * v - 2.8 * H * u);
        float2 pa = px - pos, ba = -vel * 0.04;
        float d = length(pa - ba * saturate(dot(pa, ba) / max(dot(ba, ba), 0.0001)));
        float rad = H * (0.016 + 0.016 * h2);
        core += smoothstep(rad + 0.5, rad - 0.5, d);
        glow += exp(-d / (rad * 2.5));
    }
    return float2(saturate(core), saturate(glow)) * live;
}

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float H = size.y;
    float2 px = float2(uv.x * size.x, (1.0 - uv.y) * H);      // pixels, y up the screen so droplets fall down
    float P = 1.25 * H;                                        // one sub every P
    float j = floor(px.x / P);
    float jn = j + (frac(px.x / P) > 0.5 ? 1.0 : -1.0);
    float2 c = float2((j + 0.5) * P, 0.5 * H);
    float2 cn = float2((jn + 0.5) * P, 0.5 * H);
    float sb = spectrum(0.01 + 0.3 * abs(c.x / size.x - 0.5) * 2.0);
    float sbn = spectrum(0.01 + 0.3 * abs(cn.x / size.x - 0.5) * 2.0);
    float pump = pumpOf(j, sb);
    float3 acc = float3(0, 0, 0);
    float cover = 0;

    // The air pressure ring each sub punches out on the beat.
    float ringR = 0.47 * H + (1.0 - beat) * 0.6 * H;
    float rings = smoothstep(0.035 * H, 0.0, abs(length(px - c) - ringR)) + smoothstep(0.035 * H, 0.0, abs(length(px - cn) - ringR));
    layer(acc, cover, lerp(colorA.rgb, float3(1, 1, 1), 0.3), rings * beat * 0.45);

    // The sub, in bar heights from its center, light from up front.
    float2 q = (px - c) / H;
    float r = length(q);
    float2 dir = q / max(r, 0.0001);
    float aa = 1.0 / H;
    float3 L = normalize(float3(-0.35, 0.65, 0.7));
    layer(acc, cover, float3(0, 0, 0), smoothstep(0.5, 0.42, length(q + float2(-0.02, 0.035))) * 0.45);

    // The frame: brushed chrome with six bolts.
    float metal = 0.35 + 0.35 * sin(atan2(q.y, q.x) * 2.0 + 0.9) + 0.25 * saturate(dot(dir, normalize(float2(-0.4, 0.9))));
    metal += 0.3 * smoothstep(aa * 1.5, 0.0, abs(r - 0.465)) - 0.25 * smoothstep(aa * 2.0, 0.0, abs(r - 0.4));
    layer(acc, cover, lerp(float3(0.5, 0.52, 0.56), colorA.rgb, 0.15) * metal + 0.05, smoothstep(aa, -aa, r - 0.47));
    float ba = (floor(atan2(q.y, q.x) / 1.0472) + 0.5) * 1.0472;
    float2 bolt = q - 0.435 * float2(cos(ba), sin(ba));
    float bd = length(bolt);
    layer(acc, cover, float3(0.85, 0.86, 0.9) * (0.45 + 0.55 * saturate(0.5 + dot(bolt, float2(-0.5, 0.8)) / 0.02)), smoothstep(aa, -aa, bd - 0.018));

    // The LED ring around the surround, a bright spot chasing round, flaring on the beat.
    float chase = 0.5 + 0.5 * pow(0.5 + 0.5 * sin(atan2(q.y, q.x) - time * 3.0 + j), 3.0);
    float lit = (0.55 + 0.45 * level + 1.0 * beat) * chase;
    layer(acc, cover, colorA.rgb, exp(-abs(r - 0.392) / 0.035) * 0.5 * lit);
    layer(acc, cover, lerp(colorA.rgb, float3(1, 1, 1), 0.3 + 0.4 * beat), smoothstep(0.016, 0.004, abs(r - 0.392)) * saturate(lit));

    // The surround: a rubber roll that stretches as the cone moves.
    float sc = 1.0 + 0.1 * pump;
    float rc = 0.3 * sc;
    float n = clamp((r - (rc + 0.38) * 0.5) / ((0.38 - rc) * 0.5), -1.0, 1.0);
    float3 Ns = normalize(float3(dir * n, sqrt(saturate(1.0 - n * n)) + 0.05));
    float dif = saturate(dot(Ns, L));
    float3 rub = float3(0.06, 0.06, 0.07) + float3(0.5, 0.52, 0.58) * pow(dif, 3.0) * 0.6 + colorA.rgb * 0.08;
    layer(acc, cover, rub, smoothstep(aa, -aa, r - 0.38) * smoothstep(-aa, aa, r - rc));

    // The cone: a dark dish with fine ribs, deeper and darker when sucked in, flattened and lit when thrown out.
    float t = r / rc;
    float slope = 0.55 * (1.0 - 0.9 * saturate(pump));
    float3 Nc = normalize(float3(-dir * slope, 1.0));
    float cd = saturate(dot(Nc, L));
    float3 cone = colorB.rgb * 0.22 + 0.04;
    cone *= (0.3 + 1.25 * cd * cd) * (0.9 + 0.1 * sin(t * 42.0)) * lerp(0.7, 1.0, t);
    cone *= 1.0 - 0.5 * smoothstep(0.9, 1.0, t);                 // the dark seam where the cone meets the surround
    cone += lerp(colorB.rgb, float3(1, 1, 1), 0.4) * 0.35 * saturate(pump) * exp(-pow((t - 0.62) / 0.18, 2.0));
    layer(acc, cover, cone, smoothstep(aa, -aa, r - rc));

    // The dust cap: a glossy dome that swells toward you on a push.
    float rd = 0.115 * (1.0 + 0.25 * pump);
    float2 s = q / rd;
    float z = sqrt(saturate(1.0 - dot(s, s)));
    float3 Nd = normalize(float3(s, z + 0.1));
    float spec = pow(saturate(dot(reflect(-L, Nd), float3(0, 0, 1))), 24.0);
    float3 cap = (colorB.rgb * 0.3 + 0.05) * (0.35 + 0.9 * saturate(dot(Nd, L))) + spec * (0.6 + 0.6 * saturate(pump));
    layer(acc, cover, float3(0.02, 0.02, 0.03), smoothstep(aa, -aa, r - rd - 0.012) * 0.8);
    layer(acc, cover, cap, smoothstep(aa, -aa, r - rd));

    // A small tweeter between each pair: a chrome ring and a silk dome that glows with the treble.
    float2 tq = (px - float2(floor(px.x / P + 0.5) * P, 0.5 * H)) / H;
    float tr = length(tq);
    float tre = spectrum(0.55 + 0.35 * abs(px.x / size.x - 0.5) * 2.0);
    layer(acc, cover, colorC.rgb, exp(-max(tr - 0.06, 0.0) / 0.05) * smoothstep(0.25, 0.7, tre) * 0.6);
    layer(acc, cover, float3(0.75, 0.77, 0.82) * (0.5 + 0.5 * saturate(0.5 + tq.y / 0.1)), smoothstep(aa, -aa, tr - 0.1));
    float2 ts = tq / 0.072;
    float tz = sqrt(saturate(1.0 - dot(ts, ts)));
    float3 dome = lerp(float3(0.12, 0.12, 0.14), colorC.rgb, 0.25 + 0.6 * smoothstep(0.3, 0.8, tre)) * (0.4 + 0.7 * saturate(dot(normalize(float3(ts, tz)), L)));
    dome += pow(saturate(dot(reflect(-L, normalize(float3(ts, tz))), float3(0, 0, 1))), 20.0) * 0.7;
    layer(acc, cover, dome, smoothstep(aa, -aa, tr - 0.072));

    // Grains sitting on the cone hop off it with the bass, each on its own beat, higher when it is loud;
    // a soft shadow stays on the cone under each one.
    float hopA = 0.04 + 0.5 * smoothstep(0.15, 0.75, sb) + 0.15 * beat;
    [loop] for (int g = 0; g < 6; g++)
    {
        float hg = hash1(j * 9.1 + g * 4.3);
        float ga = hg * 6.283 + g * 1.05;
        float2 rest = c + float2(cos(ga), sin(ga) * 0.8 - 0.25) * H * (0.1 + 0.13 * hash1(g * 2.7 + j));
        float ph = frac(time * (1.6 + 0.9 * hg) + hg * 5.0);
        float lift = H * hopA * (0.6 + 0.5 * hg) * 4.0 * ph * (1.0 - ph);
        float2 gq = px - rest - float2(0.0, lift);
        float gr = H * 0.028;
        layer(acc, cover, float3(0, 0, 0), smoothstep(gr * 1.4, 0.0, length((px - rest) * float2(1.0, 1.8))) * 0.5 * smoothstep(H * 0.25, 0.0, lift));
        float3 bead = lerp(colorC.rgb, float3(1, 1, 1), 0.2) * (0.55 + 0.6 * saturate(0.5 + dot(gq, float2(-0.5, 0.8)) / gr));
        layer(acc, cover, bead, smoothstep(0.5, -0.5, length(gq) - gr));
    }

    // Droplets off this sub and its neighbor.
    float2 d1 = spray(px, c, j, sb, H);
    float2 d2 = spray(px, cn, jn, sbn, H);
    float2 drops = max(d1, d2);
    layer(acc, cover, colorC.rgb, drops.y * 0.55);
    layer(acc, cover, lerp(colorC.rgb, float3(1, 1, 1), 0.55), drops.x);

    return paint(acc / max(cover, 0.001), cover);
}