Little synth

glint · Examples · Tutorials · Gallery

A sound shader: the Sound pass works out every sample of a kick, hats, a bass line and an arpeggio (vec2 mainSound(int samp, float time)), glint plays it in time with the picture, and the picture listens to it through the usual audio uniforms (fft, spectrum, beat), so they move together. Rewind and it starts again; render a video and pick “The sound shader” as its audio.

Little synth: 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

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

Image: fragment shader

#version 300 es
precision highp float;

uniform float time;
uniform vec2 resolution;
uniform sampler2D fft;
uniform vec3 spectrum;
uniform float beat;

in vec2 v_texcoord;
out vec4 fragColor;

// The sound pass plays; this hears it like any other audio input.
void main() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
    float r = length(uv), a = atan(uv.y, uv.x);
    vec3 col = vec3(0.02, 0.02, 0.05) + vec3(0.25, 0.1, 0.35) * spectrum.x * exp(-r * 3.0);
    // A ring of the spectrum, mirrored left/right.
    float x = abs(a) / 3.1416;
    float level = texture(fft, vec2(x * 0.5, 0.25)).x;
    float ring = 0.22 + 0.05 * beat;
    float band = smoothstep(0.012, 0.0, abs(r - ring - level * 0.18) - 0.004);
    col += mix(vec3(0.2, 0.7, 1.0), vec3(1.0, 0.4, 0.8), x) * band * (0.6 + level);
    // The waveform across the middle.
    float wave = texture(fft, vec2(gl_FragCoord.x / resolution.x, 0.75)).x - 0.5;
    col += vec3(1.0, 0.85, 0.5) * smoothstep(0.006, 0.0, abs(uv.y + 0.38 - wave * 0.2)) * 0.8;
    // A flash in the middle on each kick.
    col += vec3(1.0, 0.6, 0.3) * beat * exp(-r * 18.0) * 2.0;
    fragColor = vec4(col, 1.0);
}

Sound: sound shader

// A tiny synth at 124 BPM: every sample worked out from the time.
#define BPM 124.0
const float TAU = 6.2831853;

float midi(float n) { return 440.0 * exp2((n - 69.0) / 12.0); }
float noise1(float t) { return fract(sin(t * 12.9898) * 43758.5453) * 2.0 - 1.0; }
// A sine at f Hz, kept accurate over minutes (fract before the sine).
float osc(float f, float t) { return sin(TAU * fract(f * t)); }

vec2 mainSound(int samp, float time) {
    float beat = time * BPM / 60.0;
    float sinceBeat = fract(beat) * 60.0 / BPM;          // seconds since the last beat
    int chord = int(mod(floor(beat / 4.0), 4.0));         // a chord per bar: Am F C G
    float root = chord == 0 ? 45.0 : chord == 1 ? 41.0 : chord == 2 ? 48.0 : 43.0;
    bool minor = chord == 0;

    // Kick: a sine that drops in pitch, with a short tail.
    float kick = sin(TAU * (48.0 * sinceBeat + 6.0 * (1.0 - exp(-sinceBeat * 35.0)))) * exp(-sinceBeat * 7.0);
    // Hats on the off-beats.
    float off = fract(beat + 0.5) * 60.0 / BPM;
    float hat = noise1(time * 997.0) * exp(-off * 45.0) * 0.22;
    // Bass: the root an octave down, saw-ish, ducking under the kick.
    float duck = smoothstep(0.0, 0.18, sinceBeat);
    float bass = (osc(midi(root - 12.0), time) + 0.5 * osc(midi(root), time)) * 0.22 * duck;
    // Arpeggio: sixteenths up the chord, a little FM for colour.
    float s16 = beat * 4.0;
    int k = int(mod(floor(s16), 4.0));
    float interval = k == 0 ? 0.0 : k == 1 ? (minor ? 3.0 : 4.0) : k == 2 ? 7.0 : 12.0;
    float t16 = fract(s16) * 15.0 / BPM;
    float f = midi(root + 12.0 + interval);
    float arp = sin(TAU * fract(f * time) + 1.5 * osc(f * 2.0, time) * exp(-t16 * 10.0)) * exp(-t16 * 7.0) * 0.22;

    float mono = kick * 0.55 + bass + hat;
    return clamp(vec2(mono + arp * 0.7, mono + arp), -1.0, 1.0) * 0.8;
}

More Games, sound & cube maps examples

Keyboard maze · Cube map reflections · Textured room · Stand inside (VR) · Video wall