Listening piano
glint · Examples · Tutorials · Gallery
A keyboard that listens: play a piano (or sing, or play anything) into your microphone and glint works out which notes are sounding, lighting their keys and floating them up as the piano overlay does, with a haze over every key it hears and marks along the scale of the key the music is in. It reads uniform sampler2D notes (the notes heard, laid out like the midi texture). Until you pick an input in Inputs › Audio, a sound pass plays it a tune. Single notes and chords come through well; the bottom octave and dense chords less so. For OBS, Share › OBS makes a transparent browser source that listens to your microphone.

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
4 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 notes;
uniform int lowNote;
uniform int keys;
uniform float time;
// What's sounding: (velocity, seconds since it started, seconds since it stopped, last velocity), like a MIDI key.
vec4 note(int n) { return texelFetch(notes, ivec2(clamp(n, 0, 127), 0), 0); }
// How loud the note is right now (0..1), before deciding whether it counts as played.
float level(int n) { return texelFetch(notes, ivec2(clamp(n, 0, 127), 1), 0).x; }
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 notes;
uniform int lowNote;
uniform int keys;
uniform float time;
// What's sounding: (velocity, seconds since it started, seconds since it stopped, last velocity), like a MIDI key.
vec4 note(int n) { return texelFetch(notes, ivec2(clamp(n, 0, 127), 0), 0); }
// How loud the note is right now (0..1), before deciding whether it counts as played.
float level(int n) { return texelFetch(notes, ivec2(clamp(n, 0, 127), 1), 0).x; }
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 music's key, as heard over the last few seconds: its scale's notes get a mark along the front of their keys,
// the key note itself a stronger one, as glint grows sure of it.
float scaleMark(int n) {
vec4 k = texelFetch(notes, ivec2(1, 2), 0);
int d = (n - int(k.x) + 120) % 12;
bool inScale = k.y > 0.5 ? (d == 0 || d == 2 || d == 3 || d == 5 || d == 7 || d == 8 || d == 10)
: (d == 0 || d == 2 || d == 4 || d == 5 || d == 7 || d == 9 || d == 11);
return inScale ? k.z * (d == 0 ? 1.0 : 0.45) : 0.0;
}
// 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;
key = mix(key, c, scaleMark(n) * smoothstep(0.44, 0.41, y) * smoothstep(0.36, 0.38, y));
// 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;
key = mix(key, c * 0.8, scaleMark(n) * smoothstep(0.07, 0.05, y) * gap);
// 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;
// What it hears, played or not: a haze over each key, as tall as the note is loud.
float lv = level(here);
col += noteColour(here, hue, rainbow) * lv * exp(-(frag.y - top) / ((10.0 + 60.0 * lv) * s)) * 0.45 * glow * smoothstep(0.5, 0.2, abs(off));
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);
}Sound: sound shader
// A piano tune, every sample worked out from the time: Am, F, C, G, two seconds each.
// The left hand strikes the root and its octave, the right hand rolls the chord up and down in quavers.
const float TAU = 6.2831853;
const int ROOTS[4] = int[4](45, 41, 48, 43); // A2, F2, C3, G2
const int UP[8] = int[8](0, 7, 12, 16, 19, 16, 12, 7);
float freq(int n) { return 440.0 * exp2(float(n - 69) / 12.0); }
// One struck string: six overtones, each a little sharp (stiff strings) and dying away faster than the one below.
float struck(int n, float t, float vel) {
if (t < 0.0 || t > 4.0) return 0.0;
float f = freq(n), s = 0.0;
for (int h = 1; h <= 6; h++) {
float k = float(h);
float fh = f * k * sqrt(1.0 + 0.0003 * k * k);
s += sin(TAU * fract(fh * t)) * exp(-t * (0.9 + 0.7 * k)) / pow(k, 1.1);
}
return s * vel * (1.0 - exp(-t * 300.0));
}
// A chord tone: a minor third on A, major thirds elsewhere.
int tone(int root, int step) {
int t = UP[step % 8];
if (root == 45 && (t == 16 || t == 4)) t -= 1;
return t;
}
vec2 mainSound(int samp, float time) {
float t = time - 0.5; // a moment of quiet first
if (t < 0.0) return vec2(0.0);
int bar = int(floor(t / 2.0));
float l = 0.0, r = 0.0;
// Left hand: this bar's root and the one before it, still ringing.
for (int b = 0; b < 2; b++) {
int i = bar - b;
if (i < 0) break;
int root = ROOTS[i % 4];
float since = t - float(i) * 2.0;
float v = struck(root, since, 0.5) + struck(root + 12, since - 1.0, 0.3);
l += v;
r += v * 0.7;
}
// Right hand: the last eight quavers.
float q = floor(t / 0.25);
for (int k = 0; k < 8; k++) {
float s = q - float(k);
if (s < 0.0) break;
int root = ROOTS[int(floor(s / 8.0)) % 4];
int n = root + 12 + tone(root, int(s));
float vel = 0.35 + 0.15 * fract(sin(s * 12.9) * 437.5);
float v = struck(n, t - s * 0.25, vel);
l += v * 0.6;
r += v;
}
return clamp(vec2(l, r) * 0.45, -1.0, 1.0);
}More More GLSL examples
Newton fractal · Burning ship · Perfect loop · Piano overlay