Piano overlay

glint · Examples · Tutorials · Gallery

A keyboard for streaming your playing: plug in a MIDI keyboard and the keys light up as you play them, each note in its own colour, while its bar floats up for as long as you held it and sparks fly from the key. With nothing plugged in it plays a demo (turn demo off once you're set up). It reads uniform sampler2D midi, which turns MIDI on. For OBS, use Share › OBS: the player link with ?transparent=1 keeps only the keys and the notes, so it sits over your stream; turn background on for a full-screen version.

Piano overlay: a 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

3 passes, drawn in this order each frame, in GLSL.

Roll: fragment shader

#version 300 es
precision highp float;

uniform sampler2D feedback;
uniform vec2 resolution;
uniform float timeDelta;
uniform int frame;
uniform float rise;
uniform float keyHeight;
uniform float hue;
uniform float rainbow;

uniform sampler2D midi;
uniform int lowNote;
uniform int keys;
uniform float time;
uniform bool demo;

// Playing along with nothing plugged in: the demo, if it's on.
vec4 note(int n) {
    bool connected = texelFetch(midi, ivec2(0, 2), 0).w > 0.5;
    if (demo && !connected) return demoKey(n, time);
    return texelFetch(midi, ivec2(clamp(n, 0, 127), 0), 0);
}
int lowest() { return isBlack(lowNote) ? lowNote - 1 : lowNote; }
int highest() { int h = clamp(lowNote + keys - 1, lowNote, 126); return isBlack(h) ? h + 1 : h; }
float whites() { int n = whiteIndex(highest()) - whiteIndex(lowest()) + 1; return float(n); }

in vec2 v_texcoord;
out vec4 fragColor;

// The notes' roll: every frame the picture moves up by however many pixels "rise" asks for, and the row just
// above the keyboard is filled with the keys held right now. So a note's bar is as long as it was held, and
// floats away when it's let go. (rgb: its colour, brighter for a harder hit; a: how much of the pixel it covers.)
void main() {
    float top = floor(keyHeight * resolution.y);
    ivec2 px = ivec2(gl_FragCoord.xy);
    if (frame < 2 || gl_FragCoord.y < top) { fragColor = vec4(0.0); return; }

    // Whole pixels, so nothing smears: the distance moved this frame, rounded the same way every frame.
    float speed = rise * resolution.y;
    int shift = int(floor(time * speed) - floor((time - timeDelta) * speed));
    if (shift < 0) shift = 0;   // rewound
    int from = px.y - shift;
    if (float(from) >= top) { fragColor = texelFetch(feedback, ivec2(px.x, from), 0); return; }

    // A new row: which key is in this column, and is it down?
    float w = whites();
    float u = gl_FragCoord.x / resolution.x * w;
    float off;
    int n = keyAt(u, whiteIndex(lowest()), true, off);
    vec4 k = note(n);
    float hw = isBlack(n) ? BLACK_W * 0.42 : 0.4;
    float px1 = w / resolution.x;   // one pixel, in white units
    float cover = smoothstep(hw + px1, hw - px1, abs(off));
    fragColor = k.x > 0.0 ? vec4(noteColour(n, hue, rainbow) * (0.5 + 0.7 * k.x), cover) : vec4(0.0);
}

Piano: fragment shader

#version 300 es
precision highp float;

uniform sampler2D pass0;
uniform vec2 resolution;
uniform float keyHeight;
uniform float hue;
uniform float rainbow;
uniform float glow;
uniform bool background;

uniform sampler2D midi;
uniform int lowNote;
uniform int keys;
uniform float time;
uniform bool demo;

// Playing along with nothing plugged in: the demo, if it's on.
vec4 note(int n) {
    bool connected = texelFetch(midi, ivec2(0, 2), 0).w > 0.5;
    if (demo && !connected) return demoKey(n, time);
    return texelFetch(midi, ivec2(clamp(n, 0, 127), 0), 0);
}
int lowest() { return isBlack(lowNote) ? lowNote - 1 : lowNote; }
int highest() { int h = clamp(lowNote + keys - 1, lowNote, 126); return isBlack(h) ? h + 1 : h; }
float whites() { int n = whiteIndex(highest()) - whiteIndex(lowest()) + 1; return float(n); }

in vec2 v_texcoord;
out vec4 fragColor;

float hash(float n) { return fract(sin(n) * 43758.5453); }

// The keyboard: ivory and ebony, a held key lit in its note's colour, a flash as it's struck.
vec3 keyboard(vec2 frag, float top, float w, int base, out float lit) {
    float u = frag.x / resolution.x * w;
    float y = frag.y / top;                         // 0 at the bottom of the keys, 1 at their top
    bool upper = y > 0.36;
    float off;
    int n = keyAt(u, base, upper, off);
    vec4 k = note(n);
    vec3 c = noteColour(n, hue, rainbow);
    float flash = exp(-k.y * 7.0) * k.w;
    float px1 = w / resolution.x;
    lit = k.x;
    if (isBlack(n)) {
        // Rounded at the front, a highlight down the middle.
        float edge = smoothstep(BLACK_W * 0.5, BLACK_W * 0.5 - 2.0 * px1, abs(off));
        vec3 key = mix(vec3(0.03), vec3(0.16), smoothstep(0.36, 0.5, y) * (1.0 - abs(off) * 2.5));
        key = mix(key, c * 0.75, k.x * 0.8) + c * flash * 0.6;
        // Its shadow on the white keys round it.
        return mix(vec3(0.35), key, edge);
    }
    // White: a gap at each side, a little darker at the front, pressed ones sunk and tinted.
    float gap = smoothstep(0.5, 0.5 - 1.5 * px1, abs(off));
    vec3 key = vec3(0.93, 0.91, 0.86) * (0.78 + 0.22 * smoothstep(0.0, 0.25, y));
    key = mix(key, c * 0.9 + 0.1, k.x * 0.6) * (1.0 - 0.12 * k.x) + c * flash * 0.4;
    // The black keys' shadow just below them.
    float shadow = upper ? 0.0 : smoothstep(0.26, 0.36, y) * 0.25;
    return key * gap * (1.0 - shadow);
}

// Sparks thrown up from the keys as they're struck, for a second or two.
vec3 sparks(vec2 frag, float top, float w, int base) {
    float keyW = resolution.x / w;
    float s = resolution.y / 1080.0;
    vec3 col = vec3(0.0);
    float off;
    int centre = keyAt(frag.x / keyW, base, true, off);
    for (int d = -4; d <= 4; d++) {
        int n = centre + d;
        vec4 k = note(n);
        if (k.y > 1.6 || n < lowest() || n > highest()) continue;
        vec3 c = noteColour(n, hue, rainbow);
        float cx = keyCentre(n, base) * keyW;
        for (int i = 0; i < 6; i++) {
            float h1 = hash(float(n * 13 + i) + 0.17), h2 = hash(float(n * 7 + i * 31) + 3.1);
            float t = k.y;
            vec2 p = vec2(cx + (h1 - 0.5) * keyW * 0.8 + sin(t * 3.0 + h2 * 6.28) * 12.0 * s * t,
                          top + t * (90.0 + 160.0 * h2) * s * (0.6 + 0.6 * k.w));
            float r = length(frag - p) / s;
            col += c * k.w * exp(-t * 2.2) * (smoothstep(4.0, 1.0, r) + 0.3 * exp(-r * 0.3));
        }
    }
    return col;
}

void main() {
    vec2 frag = gl_FragCoord.xy;
    float top = floor(keyHeight * resolution.y);
    float w = whites();
    int base = whiteIndex(lowest());
    vec3 col = vec3(0.0);
    float alpha = 0.0;

    if (frag.y < top) {
        float lit;
        col = keyboard(frag, top, w, base, lit);
        alpha = 1.0;
    } else {
        // The roll, with a glow beside each bar (from the middle of the bars of the keys round this pixel),
        // fading out towards the top.
        vec4 r = texelFetch(pass0, ivec2(frag), 0);
        float keyW = resolution.x / w, s = resolution.y / 1080.0;
        float off;
        int here = keyAt(frag.x / keyW, base, true, off);
        vec3 halo = vec3(0.0);
        for (int d = -3; d <= 3; d++) {
            int n = here + d;
            float cx = keyCentre(n, base) * keyW;
            vec4 q = texelFetch(pass0, ivec2(int(cx), int(frag.y)), 0);
            float edge = (isBlack(n) ? BLACK_W * 0.42 : 0.4) * keyW;
            float dx = max(0.0, abs(frag.x - cx) - edge);
            halo += q.rgb * q.a * exp(-dx / (9.0 * s));
        }
        float rise = (frag.y - top) / (resolution.y - top);
        // A hot core down the middle of each bar.
        float core = isBlack(here) ? 1.0 - abs(off) / (BLACK_W * 0.42) : 1.0 - abs(off) / 0.4;
        col = (r.rgb * r.a * (1.0 + 0.5 * smoothstep(0.3, 1.0, core)) + halo * 0.35 * glow) * (1.0 - smoothstep(0.5, 1.0, rise));
        // A light along the top of the keys, over the ones held.
        col += noteColour(here, hue, rainbow) * note(here).x * exp(-(frag.y - top) / (6.0 * s)) * glow;
        col += sparks(frag, top, w, base) * glow;
    }

    if (background) {
        // Something behind the notes, for playing it full screen.
        vec3 bg = mix(vec3(0.02, 0.02, 0.04), vec3(0.07, 0.05, 0.12), frag.y / resolution.y);
        fragColor = vec4(mix(bg + col, col, alpha), 1.0);
        return;
    }
    // Over a stream (/play?transparent=1) the alpha is how much of the stream each pixel hides (rgb as it looks over
    // black): the keys hide it, the light only partly, so the glow adds to what's behind.
    fragColor = vec4(col, max(alpha, 0.7 * clamp(max(col.r, max(col.g, col.b)), 0.0, 1.0)));
}

Common: shared by every pass

// Common: the piano's layout, shared by the Roll and Piano passes.
// Layout in "white units": every white key is 1 wide; black keys sit on the line between two whites.
const int WHITE_OF[12] = int[12](0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6);   // the white key at or left of each note
const int NOTE_OF[7] = int[7](0, 2, 4, 5, 7, 9, 11);
bool isBlack(int n) { int c = n % 12; return c == 1 || c == 3 || c == 6 || c == 8 || c == 10; }
int whiteIndex(int n) { return n / 12 * 7 + WHITE_OF[n % 12]; }
int whiteNote(int w) { return w / 7 * 12 + NOTE_OF[w % 7]; }

const float BLACK_W = 0.58;   // a black key's width, in whites

// The key under u (white units from the left of the lowest white key), and how far u is from its middle (in
// white units). Black keys win where they are, when black is true (the top of the keyboard, and the notes' roll).
int keyAt(float u, int base, bool black, out float off) {
    int w = int(floor(u));
    int n = whiteNote(base + w);
    off = fract(u) - 0.5;
    if (!black) return n;
    float b = floor(u + 0.5);                 // the nearest line between two whites
    int left = whiteNote(base + int(b) - 1);
    if (abs(u - b) < BLACK_W * 0.5 && isBlack(left + 1)) { off = u - b; return left + 1; }
    return n;
}

// Where a key's middle is, in white units.
float keyCentre(int n, int base) {
    int w = isBlack(n) ? whiteIndex(n - 1) + 1 - base : whiteIndex(n) - base;
    return float(w) + (isBlack(n) ? 0.0 : 0.5);
}

// One colour per note: round the colour wheel by fifths, so a chord's notes come out different.
vec3 hue2rgb(float h) { return clamp(abs(mod(h * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0); }
vec3 noteColour(int n, float hue, float rainbow) {
    float h = hue + rainbow * float(n * 7 % 12) / 12.0;
    return mix(vec3(1.0), hue2rgb(fract(h)), 0.75);
}

// The demo, for when no MIDI input is connected: C, G, Am, F, two seconds each. The left hand holds the root and
// its fifth; the right hand plays the chord up and down in quavers. Returns what the midi texture would hold:
// (velocity while down, seconds since pressed, seconds since released, last velocity).
const int ROOTS[4] = int[4](0, 7, 9, 5);
const int UP[8] = int[8](0, 4, 7, 12, 16, 12, 7, 4);
vec4 demoKey(int n, float t) {
    float since = 1e6, vel = 0.0;
    bool down = false;
    int bar = int(floor(t / 2.0));
    int root = ROOTS[bar % 4];
    if (n == 36 + root || n == 43 + root) {
        since = mod(t, 2.0);
        down = since < 1.9;
        vel = 0.6;
    }
    for (int k = 0; k < 8; k++) {
        float s = floor(t / 0.25) - float(k);
        if (s < 0.0) break;
        int r = ROOTS[int(floor(s / 8.0)) % 4];
        int tone = UP[int(s) % 8];
        if (r == 9 && (tone == 4 || tone == 16)) tone -= 1;   // A minor
        if (n != 60 + r + tone) continue;
        float st = t - s * 0.25;
        if (st < since) { since = st; down = st < 0.22; vel = 0.55 + 0.35 * fract(sin(s * 12.9) * 437.5); }
        break;
    }
    return vec4(down ? vel : 0.0, since, down ? 0.0 : max(0.0, since - 0.22), vel);
}

More More GLSL examples

Fireflies · Newton fractal · Burning ship · Perfect loop · Listening piano