Đàn Tranh

by MusicBar, visualizer

The sixteen strings of a Vietnamese zither run along the bar over rows of movable bridges, each string shaking in a glowing standing wave with its part of the music and ringing brighter when plucked on the beat.

19 downloads Download file, 7.7 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 8a037705716300dab3f75f5f8eac73afdab657201486f126f0f0279fa6685155

//! name: Đàn Tranh
//! author: MusicBar
//! kind: visualizer
//! description: The sixteen strings of a Vietnamese zither run along the bar over rows of movable bridges, each string shaking in a glowing standing wave with its part of the music and ringing brighter when plucked on the beat.

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

// Where the bridge of string i stands inside one row (the rows repeat every P along the bar).
float bridgeX(float i, float H) { float f = i / 15.0; return H * (1.8 * f + 1.0 * f * f); }

// String i (0 is the lowest, by the screen edge) listening to s. It rings as a standing wave between
// the bridges: two loops on the low strings, three and four higher up. Each string is plucked now and
// then, more often when its band is loud and on the beat, and the swing dies away after the pluck.
// Returns x: the blur of its swing (brighter toward the turning points), y: the string, z: its shadow;
// e: how wide it swings, in string gaps; pk: how fresh the pluck is.
float3 strum(float2 px, float i, float s, float H, float P, out float e, out float pk)
{
    float g = 0.049 * H;
    float dy = px.y - (0.2 * H + i * g);
    float sg = frac((px.x - bridgeX(i, H)) / P);
    float n = 2.0 + step(5.5, i) + step(10.5, i);
    float ph = time * 1.6 + hash1(i * 7.13) * 9.0;
    float local = frac(ph);
    float chance = saturate((s - 0.2) * 2.0 + 0.7 * beat);
    pk = step(hash1(i * 3.7 + floor(ph) * 1.31), chance) * exp(-local * 2.8) * smoothstep(0.0, 0.04, local);
    float sweep = 1.0 - beat - i / 22.0;                                         // a beat sweeps a glissando up the strings
    pk = max(pk, 0.8 * sqrt(beat) * smoothstep(0.0, 0.06, sweep) * smoothstep(0.35, 0.1, sweep));
    float A = g * min(0.04 + 0.6 * s * s * s + 2.4 * pk * (1.0 + 0.4 * beat), 2.9);
    float shape = sin(n * 3.14159 * sg);
    float E = A * abs(shape);
    float d = A * shape * cos(time * (10.0 + 0.9 * i) + i * 1.7);
    float hw = max(H * lerp(0.012, 0.0065, i / 15.0), 0.42) * (1.0 + 0.6 * pk);
    float r = abs(dy) / max(E, 0.01);
    e = E / g;
    float blur = saturate(0.09 / sqrt(max(1.0 - r * r, 0.03))) * smoothstep(1.05, 0.95, r) * smoothstep(0.15, 0.6, e);
    blur += exp(-abs(dy - d) / (0.35 * g)) * smoothstep(0.1, 0.8, e) * 0.5;
    return float3(blur, smoothstep(hw + 0.5, hw - 0.5, abs(dy - d)), smoothstep(hw + 0.9, hw - 0.4, abs(dy - d + 0.018 * H)));
}

// The glow color of string i walks the palette from the low strings to the high ones.
float3 tone(float i) { return i < 7.5 ? lerp(colorA.rgb, colorB.rgb, i / 7.5) : lerp(colorB.rgb, colorC.rgb, (i - 7.5) / 7.5); }

// A bridge (con nhan) under string i in the nearest row: a small ivory diamond with a groove for the string.
float bridgeD(float2 px, float i, float H, float P, out float2 q)
{
    float g = 0.049 * H;
    float xb = bridgeX(i, H);
    q = float2(px.x - xb - floor((px.x - xb) / P + 0.5) * P, px.y - (0.2 * H + i * g));
    float2 b = float2(0.1 * H, 1.5 * g);
    return (abs(q.x) * b.y + abs(q.y) * b.x - b.x * b.y) / length(b);
}

// The strings around a pixel, far ones first so the nearest lands on top.
static const float order[5] = { -2.0, 2.0, -1.0, 1.0, 0.0 };

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float2 px = float2(uv.x * size.x, rise(uv) * size.y);   // pixels: along the bar, up from the screen edge
    float H = size.y;
    float P = 7.5 * H;                                       // one row of bridges every P
    float g = 0.049 * H;
    float3 acc = float3(0, 0, 0);
    float cover = 0;
    float k = clamp(floor((px.y - 0.2 * H) / g + 0.5), 0.0, 15.0);

    // The soundboard: a warm wood wash rising off the rail and fading out, with long grain.
    float grain = 0.8 + 0.2 * sin(px.y / H * 70.0 + sin(px.x / H * 0.7) * 4.0);
    layer(acc, cover, float3(0.45, 0.26, 0.12) * grain, smoothstep(0.55 * H, 0.12 * H, px.y) * 0.32);

    // The bridges, in a diagonal row like a skein of birds: ivory, lit on top, a groove where the string
    // sits, and a palette glint on the beat.
    float2 q0, q1, q2;
    float d0 = bridgeD(px, max(k - 1.0, 0.0), H, P, q0);
    float d1 = bridgeD(px, k, H, P, q1);
    float d2 = bridgeD(px, min(k + 1.0, 15.0), H, P, q2);
    float dB = min(d1, min(d0, d2));
    float2 qb = d1 <= dB ? q1 : (d0 <= dB ? q0 : q2);
    layer(acc, cover, float3(0.05, 0.02, 0.0), smoothstep(0.06 * H, 0.0, dB + 0.02 * H) * 0.45);
    float3 ivory = float3(0.95, 0.88, 0.72) * (0.62 + 0.38 * saturate(qb.y / g + 0.5)) - 0.3 * smoothstep(0.5 * g, 0.0, abs(qb.y));
    ivory = lerp(ivory, lerp(tone(k), float3(1, 1, 1), 0.4), 0.4 * beat);
    ivory = lerp(ivory, float3(0.35, 0.2, 0.1), smoothstep(-1.2, 0.0, dB));
    layer(acc, cover, ivory, smoothstep(0.5, -0.5, dB));

    // The strings: faint steel at rest (the bass ones a little stronger); a plucked one swings into a
    // glowing blur in its palette color, the string itself bright inside it, all brighter on the beat.
    float bright = 1.0 + 0.8 * beat;
    float pkK = 0;
    float sheen = 0.17 + 0.24 * pow(0.5 + 0.5 * sin(px.x / H * 0.45 - time * 0.35 + px.y / H * 2.0), 3.0);
    sheen *= lerp(0.55, 1.0, smoothstep(2.0, 4.0, g));      // on a thin bar the resting strings sit back further
    [unroll] for (int j = 0; j < 5; j++)
    {
        float i = k + order[j];
        float valid = step(-0.5, i) * step(i, 15.5);
        float ic = clamp(i, 0.0, 15.0);
        float s = spectrum(0.03 + 0.9 * ic / 15.0);
        float e, pk;
        float3 a = strum(px, ic, s, H, P, e, pk);
        float rr = saturate(e * 0.6 + pk);
        float3 c = tone(ic);
        layer(acc, cover, c, a.x * valid * saturate(0.3 + 0.45 * e + 0.8 * pk) * bright);
        layer(acc, cover, float3(0.04, 0.03, 0.03), a.z * valid * (0.12 + 0.4 * rr));
        layer(acc, cover, lerp(float3(0.88, 0.83, 0.74), lerp(c, float3(1, 1, 1), 0.4), rr), a.y * valid * lerp(sheen - 0.006 * ic, 1.0, rr));
        pkK = max(pkK, pk * step(abs(order[j]), 0.5));
    }

    // A spark where a fresh pluck lands, just before the next bridge, flashing on the beat.
    float xp = bridgeX(k, H) + 0.8 * P;
    float2 pq = float2(px.x - xp - floor((px.x - xp) / P + 0.5) * P, px.y - (0.2 * H + k * g));
    float spark = exp(-length(pq) / (0.03 * H)) + exp(-abs(pq.y) / 0.7 - abs(pq.x) / (0.1 * H)) * 0.6;
    layer(acc, cover, lerp(tone(k), float3(1, 1, 1), 0.6), spark * pkK * pkK * (0.4 + 0.9 * beat));

    // The lacquered rail along the screen edge: deep red lacquer, a gloss line, gold trim, and
    // mother-of-pearl flowers that shimmer as the light moves.
    float wy = px.y / (0.13 * H);
    float3 lac = lerp(float3(0.1, 0.02, 0.015), float3(0.46, 0.08, 0.05), smoothstep(0.0, 0.85, wy));
    lac += float3(1.0, 0.75, 0.6) * exp(-pow((wy - 0.68) / 0.09, 2.0)) * (0.3 + 0.15 * sin(px.x / H * 0.6 - time * 0.5));
    lac = lerp(lac, float3(0.95, 0.72, 0.32) * (0.8 + 0.4 * beat), smoothstep(0.86, 0.94, wy));
    float2 fq = float2(frac(px.x / (1.6 * H)) * 1.6 * H - 0.8 * H, px.y - 0.4 * 0.13 * H);
    float fr = length(fq);
    float petal = fr - 0.034 * H * (0.55 + 0.45 * abs(cos(atan2(fq.y, fq.x) * 2.0)));
    float pearl = smoothstep(0.5, -0.5, petal) + smoothstep(0.5, -0.5, length(float2(abs(fq.x) - 0.4 * H, fq.y)) - 0.012 * H);
    float3 nacre = 0.72 + 0.28 * cos(6.283 * (px.x / H * 0.35 + fr / H * 6.0 - time * 0.2 + float3(0.0, 0.33, 0.67)));
    lac = lerp(lac, nacre, saturate(pearl) * 0.9);
    layer(acc, cover, lac, smoothstep(0.5, -0.5, px.y - 0.13 * H));

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