Wind Chimes
Five metal tubes in the wind. A struck tube rings in several modes at once, at 1, 2.76, 5.40 and 8.93 times its lowest pitch, each dying away at its own rate, and the sound adds those sines for every strike. The picture lights each tube along the shapes of its ringing modes, dark at their nodes, and draws all twenty partials as lines along the bottom, as tall as they ring.
· Sound & Rhythm · Fragment shader
Teaches Modal synthesis: struck tubes ring with their own partials
Builds on A Shader That Keeps the Beat (whole-number hash), Pulse Synth (line from a distance)
Watch
The idea
A plucked string rings at whole multiples of its pitch, its harmonics, and that is why it sounds like a note. A metal tube hanging on a string doesn't. Struck, it bends in several ways at once, its modes, and each mode has a frequency and a shape of its own. For a tube free at both ends, a free-free beam, the modes sit at 1, 2.756, 5.404 and 8.933 times the lowest. None of those is a whole multiple, which is why a chime sounds like a bell.
That gives a way to make the sound that needs no physics simulation at all: modal synthesis. Every strike starts one sine per mode, at the mode's frequency, and each sine dies away exponentially at its own rate. Four sines per strike are enough for the ear to hear a tube. In Pulse Synth the picture drew the sound's samples; here it draws what the samples are made of. Each tube is lit along the shapes of the modes that are ringing, and each of the twenty partials (five tubes, four modes) is a line along the bottom.
It is built in four steps, and each step is a shader you can run on its own.
Stage 1 · One tube, one sine
One tube, A4 (440 Hz) to start, struck every two seconds. A strike starts a sine at the tube's pitch that falls as exp(-since / ring): after ring seconds (2.5 at the default) it is down to about a third. It rises over a millisecond rather than at once, so the strike doesn't click. A strike rings on under the next one, so chimes adds the last five, and a strike ten seconds old is faded out over its last three seconds (remembered) before it is dropped.
To keep the phase exact ten seconds into a ring, the shader takes the number of cycles, f × since, keeps only its fraction with fract, and only then multiplies by 2π. The sound is the same on both sides.
The tube in the picture is a capsule, the distance to a segment minus a radius (onSegment also says how far along the segment a point is, which the next stage needs). It glows as loud as it rings, from the same decays without the sine, so a strike lights it over a few hundredths of a second and it dims as the sound does.
stage-1-one-tube.metal
// Stage 1 · One tube, one sine
// One tube, struck every two seconds. Each strike starts a sine at the tube's pitch that dies away exponentially,
// and the tube glows as loud as it rings. A strike rings on under the next one: the sound adds them.
// @param ring 2.5 0.5 3 Seconds the ring takes to fall to about a third
// @param pitch 0 -12 7 Semitones up or down from A4
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// The tube's pitch: A4, moved by `pitch`.
float tubeFrequency(constant Params& p) {
return 440.0 * exp2(p.pitch / 12.0);
}
// A strike every two seconds, remembered for five of them.
constant float EVERY = 2.0;
constant int REMEMBER = 5;
// A strike fades from memory over its last 3 of 10 seconds: no click.
float remembered(float since) {
return 1.0 - smoothstep(7.0, 10.0, since);
}
// How hard the tube rings at t: every strike's decay since then, added.
float ringing(float t, constant Params& p) {
float level = 0.0;
int now = int(floor(t / EVERY));
for (int back = 0; back < REMEMBER; back++) {
int k = now - back;
if (k < 0) break;
float since = t - float(k) * EVERY;
float rise = 1.0 - exp(-since / 0.03);
level += exp(-since / p.ring) * rise * remembered(since);
}
return level;
}
// The sound: each strike a sine at the tube's pitch, falling with time
// constant `ring` from a millisecond's rise.
float2 chimes(float t, constant Params& p) {
float f = tubeFrequency(p);
float sum = 0.0;
int now = int(floor(t / EVERY));
for (int back = 0; back < REMEMBER; back++) {
int k = now - back;
if (k < 0) break;
float since = t - float(k) * EVERY;
// Whole cycles dropped first, so the phase stays precise.
float cycles = fract(f * since);
float fall = exp(-since / p.ring);
float rise = 1.0 - exp(-since / 0.001);
sum += fall * sin(TAU * cycles) * rise * remembered(since);
}
return float2(tanh(sum * 0.16));
}
// Distance from a to the segment from b to c, and how far along it a is.
float2 onSegment(float2 a, float2 b, float2 c) {
float2 ba = a - b, cb = c - b;
float h = clamp(dot(ba, cb) / dot(cb, cb), 0.0, 1.0);
return float2(length(ba - cb * h), h);
}
// 1 on a shape d away, fading out over w.
float lineAt(float d, float w) {
return 1.0 - smoothstep(0.0, w, d);
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float t = u.time;
float aspect = u.resolution.x / u.resolution.y;
float2 q = float2(uv.x * aspect, uv.y);
float px = 1.0 / u.resolution.y;
float cx = 0.5 * aspect;
// Narrower than square, the chime narrows with the picture.
float wide = min(aspect, 1.0);
// A dark ground, warmer at the middle.
float heat = 0.05 + 0.04 * exp(-2.5 * length(q - float2(cx, 0.55)));
// The bar and the string the tube hangs by.
float barY = 0.9;
float2 left = float2(cx - 0.44 * wide, barY);
float2 right = float2(cx + 0.44 * wide, barY);
float bar = onSegment(q, left, right).x - 0.008;
heat = max(heat, 0.32 * lineAt(bar, 1.5 * px));
float2 top = float2(cx, barY);
float tie = onSegment(q, top, top - float2(0.0, 0.12)).x;
heat = max(heat, 0.2 * lineAt(tie, 1.2 * px));
// The tube glows as loud as it rings, with a glow around it.
float loud = ringing(t, p);
float2 a = top - float2(0.0, 0.06), b = top - float2(0.0, 0.62);
float2 d = onSegment(q, a, b);
float r = 0.021;
float edge = d.x - r;
float near = exp(-max(edge, 0.0) / 0.02);
heat += 0.16 * (1.0 - exp(-loud)) * near;
float across = sqrt(max(1.0 - (d.x / r) * (d.x / r), 0.0));
float lit = 0.1 + 0.09 * across;
lit += 0.6 * (1.0 - exp(-1.3 * loud)) * (0.55 + 0.45 * across);
heat = mix(heat, lit, lineAt(edge, 1.5 * px));
return float4(kiln(heat), 1.0);
}
float2 sound(float t, constant Params& p) {
return chimes(t, p) * p.volume;
}Stage 2 · Four partials and their shapes
Now each strike rings four modes. Their wavenumbers β solve cos(β) cosh(β) = 1 (4.730, 7.853, 10.996, 14.137), their frequencies go as β², and RATIO holds the partials that gives. At the strike they start at the levels in LEVEL, 1, 0.55, 0.35 and 0.22, with bright scaling the upper three: 0 leaves the fundamental alone and 1 doubles them. Each falls with its own time constant, ring × ratio^-0.75, so at the default the four ring for 2.5, 1.17, 0.70 and 0.48 seconds. A strike starts bright and mellows into its fundamental.
modeShape is how far each point along the tube moves in mode n, written about the middle of the tube: cos(βx) / cos(β/2) + cosh(βx) / cosh(β/2) for the symmetric modes, the same with sines for the others, 2 at both ends. The tube is lit by the sum of the modes' shapes, each times how hard it rings, so the light shows where the tube moves and the dark bands are its nodes, where a mode stands still. The lowest mode's nodes are 22.4% of the way from each end. That is where a chime maker drills the hole for the string, since a string at a node hardly damps the ring, and the string here goes to that point.
Along the bottom, spectrum draws the four partials as lines on a log scale from 200 Hz to 8 kHz, each as tall as it rings: watch the high ones drop first.
stage-2-partials.metal64 new lines since stage 1
// Stage 2 · Four partials and their shapes
// A tube rings in four modes at once, at 1, 2.76, 5.40 and 8.93 times its pitch, the higher ones dying sooner. Each
// mode bends the tube in its own shape, still at its nodes: the tube is lit by those shapes, and hung at a node.
// @param ring 2.5 0.5 3 Seconds the lowest partial takes to fall to about a third; the higher ones go sooner
// @param bright 0.5 0 1 How loud the upper partials are at the strike: 0 is the fundamental alone
// @param pitch 0 -12 7 Semitones up or down from A4
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
// The tube's pitch: A4, moved by `pitch`.
float tubeFrequency(constant Params& p) {
return 440.0 * exp2(p.pitch / 12.0);
}
// A beam with free ends: its partials sit at 1, 2.756, 5.404 and 8.933.
constant float BETA[4] = {4.7300, 7.8532, 10.9956, 14.1372};
constant float RATIO[4] = {1.0, 2.7565, 5.4039, 8.9330};
constant float LEVEL[4] = {1.0, 0.55, 0.35, 0.22};
// Mode n's shape along the tube, s from 0 to 1: 2 at the ends.
float modeShape(int n, float s) {
float k = BETA[n];
float x = s - 0.5;
if (n % 2 == 0) {
return cos(k * x) / cos(0.5 * k) + cosh(k * x) / cosh(0.5 * k);
}
return sin(k * x) / sin(0.5 * k) + sinh(k * x) / sinh(0.5 * k);
}
// Partial n's level at the strike: `bright` scales the upper three.
float partialLevel(int n, constant Params& p) {
return n == 0 ? LEVEL[0] : LEVEL[n] * 2.0 * p.bright;
}
// Partial n's time constant: `ring` for the lowest, shorter higher up.
float partialTime(int n, constant Params& p) {
return p.ring * pow(RATIO[n], -0.75);
}
// A strike every two seconds, remembered for five of them.
constant float EVERY = 2.0;
constant int REMEMBER = 5;
// A strike fades from memory over its last 3 of 10 seconds: no click.
float remembered(float since) {
return 1.0 - smoothstep(7.0, 10.0, since);
}
// How hard the four partials ring at t: each strike's level times the
// partial's decay since then (their loudness, not their waves).
float4 ringing(float t, constant Params& p) {
float4 level = float4(0.0);
float4 rate = 1.0 / float4(partialTime(0, p), partialTime(1, p),
partialTime(2, p), partialTime(3, p));
float4 at = float4(partialLevel(0, p), partialLevel(1, p),
partialLevel(2, p), partialLevel(3, p));
int now = int(floor(t / EVERY));
for (int back = 0; back < REMEMBER; back++) {
int k = now - back;
if (k < 0) break;
float since = t - float(k) * EVERY;
float rise = 1.0 - exp(-since / 0.03);
level += at * exp(-since * rate) * rise * remembered(since);
}
return level;
}
// Modal synthesis: each strike rings four sines at RATIO times the tube's
// pitch, each falling at its own rate.
float2 chimes(float t, constant Params& p) {
float f = tubeFrequency(p);
float sum = 0.0;
int now = int(floor(t / EVERY));
for (int back = 0; back < REMEMBER; back++) {
int k = now - back;
if (k < 0) break;
float since = t - float(k) * EVERY;
float wave = 0.0;
for (int n = 0; n < 4; n++) {
float cycles = fract(f * RATIO[n] * since);
float fall = exp(-since / partialTime(n, p));
wave += partialLevel(n, p) * fall * sin(TAU * cycles);
}
float rise = 1.0 - exp(-since / 0.001);
sum += wave * rise * remembered(since);
}
return float2(tanh(sum * 0.15));
}
// Distance from a to the segment from b to c, and how far along it a is.
float2 onSegment(float2 a, float2 b, float2 c) {
float2 ba = a - b, cb = c - b;
float h = clamp(dot(ba, cb) / dot(cb, cb), 0.0, 1.0);
return float2(length(ba - cb * h), h);
}
// 1 on a shape d away, fading out over w.
float lineAt(float d, float w) {
return 1.0 - smoothstep(0.0, w, d);
}
// The four partials as lines along the bottom, 200 Hz to 8 kHz on a log
// scale, as tall as they ring.
float spectrum(float2 uv, float t, float aspect, float px,
constant Params& p) {
float floorY = 0.035, tall = 0.11;
if (uv.y < floorY - 0.01 || uv.y > floorY + tall + 0.01) return 0.0;
if (uv.x < 0.05 || uv.x > 0.95) return 0.0;
float inBand = step(floorY, uv.y) * step(uv.y, floorY + tall);
float heat = 0.07 * lineAt(abs(uv.y - floorY), px);
for (int n = 0; n < 4; n++) {
float f = tubeFrequency(p) * RATIO[n];
float x = 0.06 + 0.88 * log2(f / 200.0) / log2(40.0);
float w = abs(uv.x - x) * aspect;
if (w > 3.0 * px) continue;
float h = min(ringing(t, p)[n] * 0.8, 1.0);
float top = step(uv.y, floorY + tall * h);
float bar = lineAt(w, 2.0 * px) * step(floorY, uv.y) * top;
float mark = 0.08 * lineAt(w, 1.5 * px) * inBand;
heat = max(heat, bar * (0.35 + 0.5 * h) + mark);
}
return heat;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float t = u.time;
float aspect = u.resolution.x / u.resolution.y;
float2 q = float2(uv.x * aspect, uv.y);
float px = 1.0 / u.resolution.y;
float cx = 0.5 * aspect;
// Narrower than square, the chime narrows with the picture.
float wide = min(aspect, 1.0);
// A dark ground, warmer at the middle.
float heat = 0.05 + 0.04 * exp(-2.5 * length(q - float2(cx, 0.55)));
// The bar, and the string down to the node of the lowest mode, 0.224
// of the way down the tube, where that mode barely moves.
float barY = 0.9;
float2 left = float2(cx - 0.44 * wide, barY);
float2 right = float2(cx + 0.44 * wide, barY);
float bar = onSegment(q, left, right).x - 0.008;
heat = max(heat, 0.32 * lineAt(bar, 1.5 * px));
float2 top = float2(cx, barY);
float2 hole = top - float2(0.0, 0.06 + 0.224 * 0.56);
float tie = onSegment(q, top, hole).x;
heat = max(heat, 0.2 * lineAt(tie, 1.2 * px));
float4 ring = ringing(t, p);
float loud = dot(ring, float4(1.0, 0.8, 0.6, 0.5));
float2 a = top - float2(0.0, 0.06), b = top - float2(0.0, 0.62);
float2 d = onSegment(q, a, b);
float r = 0.021;
float edge = d.x - r;
float near = exp(-max(edge, 0.0) / 0.02);
heat += 0.16 * (1.0 - exp(-loud)) * near;
// Each mode lights the tube as far as it moves there.
float moving = 0.0;
for (int n = 0; n < 4; n++) {
moving += ring[n] * abs(modeShape(n, d.y)) * 0.5;
}
float across = sqrt(max(1.0 - (d.x / r) * (d.x / r), 0.0));
float lit = 0.1 + 0.09 * across;
lit += 0.6 * (1.0 - exp(-1.3 * moving)) * (0.55 + 0.45 * across);
float pin = lineAt(length(q - hole) - 0.005, 1.5 * px);
lit = mix(lit, 0.62, pin);
heat = mix(heat, lit, lineAt(edge, 1.5 * px));
heat = max(heat, spectrum(uv, t, aspect, px, p));
return float4(kiln(heat), 1.0);
}
float2 sound(float t, constant Params& p) {
return chimes(t, p) * p.volume;
}10 lines from stage 1 that are gone
float ringing(float t, constant Params& p) {
float level = 0.0;
⋯
level += exp(-since / p.ring) * rise * remembered(since);
⋯
float cycles = fract(f * since);
float fall = exp(-since / p.ring);
⋯
sum += fall * sin(TAU * cycles) * rise * remembered(since);
⋯
return float2(tanh(sum * 0.16));
⋯
float tie = onSegment(q, top, top - float2(0.0, 0.12)).x;
⋯
float loud = ringing(t, p);
⋯
lit += 0.6 * (1.0 - exp(-1.3 * loud)) * (0.55 + 0.45 * across);Stage 3 · Five tubes in the wind
Five tubes, tuned to A major pentatonic (A4, B4, C♯5, E5 and F♯5) and moved together by pitch in semitones. A tube's pitch goes as one over its length squared, so tubeLength draws each one 0.56 × √(440 / f) of the picture's height: the lowest is the longest. Each is hung at its own node.
The wind strikes them by chance. Time is cut into quarter-second slots, and a hash of the slot and the tube (strike) says whether that tube is struck in that slot, when in it, and how hard. The chance is 0.35 × wind × gust, where gust is two slow noises in time, 0 in a lull and 1 at the height of a gust, so strikes come in flurries and the flurries are harder. At the defaults that is about 140 strikes a minute. Since the hash is a pure function of the slot, chimes and the picture find the same strikes without remembering anything: each looks back over the last 40 slots. The tubes sit across the stereo field, the lowest on the left, with equal-power panning, and tanh keeps the sum under full scale.
stage-3-five-tubes.metal96 new lines since stage 2
// Stage 3 · Five tubes in the wind
// Five tubes tuned to a pentatonic scale, struck by chance: time is cut into quarter-second slots, and a hash of the
// slot and the tube says whether it is struck, more often in a gust. The tubes spread from left to right.
// @param wind 0.6 0.15 1 How hard the wind blows: more strikes, and harder
// @param ring 2.5 0.5 3 Seconds the lowest partial takes to fall to about a third; the higher ones go sooner
// @param bright 0.5 0 1 How loud the upper partials are at the strike: 0 is the fundamental alone
// @param pitch 0 -12 7 Semitones up or down for all five tubes
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
uint hashBits(uint x) {
x ^= x >> 16;
x *= 0x7feb352du;
x ^= x >> 15;
x *= 0x846ca68bu;
x ^= x >> 16;
return x;
}
// A whole number in, a random number from 0 to 1 out.
float hash01(uint x) {
return float(hashBits(x) & 0xffffu) / 65535.0;
}
// Smooth noise along a line: a random level at every whole number, eased
// between them.
float windNoise(float x) {
float i = floor(x);
float f = fract(x);
float a = hash01(uint(int(i) + 4096));
float b = hash01(uint(int(i) + 4097));
return mix(a, b, f * f * (3.0 - 2.0 * f));
}
constant int TUBES = 5;
// Five tubes tuned to A major pentatonic, A4 to F#5, moved by `pitch`.
float tubeFrequency(int i, constant Params& p) {
const float notes[5] = {440.00, 493.88, 554.37, 659.26, 739.99};
return notes[clamp(i, 0, 4)] * exp2(p.pitch / 12.0);
}
// Pitch goes as 1 / length squared: the lowest tube is the longest.
float tubeLength(int i) {
const float notes[5] = {440.00, 493.88, 554.37, 659.26, 739.99};
return 0.56 * sqrt(440.0 / notes[clamp(i, 0, 4)]);
}
// A beam with free ends: its partials sit at 1, 2.756, 5.404 and 8.933.
constant float BETA[4] = {4.7300, 7.8532, 10.9956, 14.1372};
constant float RATIO[4] = {1.0, 2.7565, 5.4039, 8.9330};
constant float LEVEL[4] = {1.0, 0.55, 0.35, 0.22};
// Mode n's shape along the tube, s from 0 to 1: 2 at the ends.
float modeShape(int n, float s) {
float k = BETA[n];
float x = s - 0.5;
if (n % 2 == 0) {
return cos(k * x) / cos(0.5 * k) + cosh(k * x) / cosh(0.5 * k);
}
return sin(k * x) / sin(0.5 * k) + sinh(k * x) / sinh(0.5 * k);
}
// Partial n's level at the strike: `bright` scales the upper three.
float partialLevel(int n, constant Params& p) {
return n == 0 ? LEVEL[0] : LEVEL[n] * 2.0 * p.bright;
}
// Partial n's time constant: `ring` for the lowest, shorter higher up.
float partialTime(int n, constant Params& p) {
return p.ring * pow(RATIO[n], -0.75);
}
// Gusts: 0 in a lull, 1 at the height of a gust.
float gust(float t) {
float n = 0.65 * windNoise(t * 0.22) + 0.35 * windNoise(t * 0.7 + 17.0);
return smoothstep(0.2, 0.8, n);
}
// Quarter-second slots; a strike is remembered for 40.
constant float SLOT = 0.25;
constant int REMEMBER = 40;
// Whether tube i is struck in slot k, drawn from a hash of the two, more
// often and harder in a gust; if so, when and how hard.
bool strike(int i, int k, constant Params& p,
thread float& when, thread float& force) {
if (k < 0) return false;
uint seed = uint(k) * 7u + uint(i) * 131071u + 9u;
float start = float(k) * SLOT;
float g = gust(start);
if (hash01(seed) >= 0.35 * p.wind * g) return false;
when = start + SLOT * hash01(seed + 3u);
force = (0.45 + 0.55 * hash01(seed + 5u)) * (0.6 + 0.4 * g);
return true;
}
// A strike fades from memory over its last 3 of 10 seconds: no click.
float remembered(float since) {
return 1.0 - smoothstep(7.0, 10.0, since);
}
// How hard tube i's four partials ring at t: each strike's force times the
// partial's decay since then (their loudness, not their waves).
float4 ringing(int i, float t, constant Params& p) {
float4 level = float4(0.0);
float4 rate = 1.0 / float4(partialTime(0, p), partialTime(1, p),
partialTime(2, p), partialTime(3, p));
float4 at = float4(partialLevel(0, p), partialLevel(1, p),
partialLevel(2, p), partialLevel(3, p));
int now = int(floor(t / SLOT));
for (int back = 0; back < REMEMBER; back++) {
float when, force;
if (!strike(i, now - back, p, when, force)) continue;
float since = t - when;
if (since < 0.0) continue;
// The tube lights over a few hundredths of a second, not one frame.
float rise = 1.0 - exp(-since / 0.03);
level += force * at * exp(-since * rate) * rise * remembered(since);
}
return level;
}
// Modal synthesis: each strike rings four sines at RATIO times the tube's
// pitch, each falling at its own rate; the tubes spread left to right.
float2 chimes(float t, constant Params& p) {
float2 sum = float2(0.0);
int now = int(floor(t / SLOT));
for (int i = 0; i < TUBES; i++) {
float f = tubeFrequency(i, p);
float tube = 0.0;
for (int back = 0; back < REMEMBER; back++) {
float when, force;
if (!strike(i, now - back, p, when, force)) continue;
float since = t - when;
if (since < 0.0) continue;
float wave = 0.0;
for (int n = 0; n < 4; n++) {
// Whole cycles dropped first, so the phase stays precise.
float cycles = fract(f * RATIO[n] * since);
float fall = exp(-since / partialTime(n, p));
wave += partialLevel(n, p) * fall * sin(TAU * cycles);
}
float rise = 1.0 - exp(-since / 0.001);
tube += force * wave * rise * remembered(since);
}
float pan = (float(i) / float(TUBES - 1) - 0.5) * 0.6 + 0.5;
sum += tube * float2(cos(pan * 1.5707963), sin(pan * 1.5707963));
}
return tanh(sum * 0.2);
}
// Distance from a to the segment from b to c, and how far along it a is.
float2 onSegment(float2 a, float2 b, float2 c) {
float2 ba = a - b, cb = c - b;
float h = clamp(dot(ba, cb) / dot(cb, cb), 0.0, 1.0);
return float2(length(ba - cb * h), h);
}
// 1 on a shape d away, fading out over w.
float lineAt(float d, float w) {
return 1.0 - smoothstep(0.0, w, d);
}
// A point hung from `pivot`, `down` below it, swung by `angle`.
float2 hung(float2 pivot, float down, float angle) {
return pivot + down * float2(sin(angle), -cos(angle));
}
// Every partial as a line along the bottom, 200 Hz to 8 kHz on a log
// scale, as tall as it rings.
float spectrum(float2 uv, float t, float aspect, float px,
constant Params& p) {
float floorY = 0.035, tall = 0.11;
if (uv.y < floorY - 0.01 || uv.y > floorY + tall + 0.01) return 0.0;
if (uv.x < 0.05 || uv.x > 0.95) return 0.0;
float inBand = step(floorY, uv.y) * step(uv.y, floorY + tall);
float heat = 0.07 * lineAt(abs(uv.y - floorY), px);
for (int i = 0; i < TUBES; i++) {
for (int n = 0; n < 4; n++) {
float f = tubeFrequency(i, p) * RATIO[n];
float x = 0.06 + 0.88 * log2(f / 200.0) / log2(40.0);
float w = abs(uv.x - x) * aspect;
if (w > 3.0 * px) continue;
float h = min(ringing(i, t, p)[n] * 0.8, 1.0);
float top = step(uv.y, floorY + tall * h);
float bar = lineAt(w, 2.0 * px) * step(floorY, uv.y) * top;
float mark = 0.08 * lineAt(w, 1.5 * px) * inBand;
heat = max(heat, bar * (0.35 + 0.5 * h) + mark);
}
}
return heat;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float t = u.time;
float aspect = u.resolution.x / u.resolution.y;
float2 q = float2(uv.x * aspect, uv.y);
float px = 1.0 / u.resolution.y;
float cx = 0.5 * aspect;
// Narrower than square, the chime narrows with the picture.
float wide = min(aspect, 1.0);
// A dark ground, warmer at the middle.
float heat = 0.05 + 0.04 * exp(-2.5 * length(q - float2(cx, 0.55)));
// The bar the tubes hang from, and the string it hangs by.
float barY = 0.9;
float2 left = float2(cx - 0.44 * wide, barY);
float2 right = float2(cx + 0.44 * wide, barY);
float bar = onSegment(q, left, right).x - 0.008;
heat = max(heat, 0.32 * lineAt(bar, 1.5 * px));
float2 above = float2(cx, 1.02);
float hook = onSegment(q, float2(cx, barY), above).x;
heat = max(heat, 0.2 * lineAt(hook, 1.5 * px));
// Each tube hangs at s = 0.224, the node of its lowest mode.
float string = 0.0;
for (int i = 0; i < TUBES; i++) {
float L = tubeLength(i);
float2 top = float2(cx + (float(i) - 2.0) * 0.18 * wide, barY);
float2 hole = hung(top, 0.06 + 0.224 * L, 0.0);
float tie = lineAt(onSegment(q, top, hole).x, 1.2 * px);
string = max(string, 0.8 * tie);
}
heat = max(heat, 0.25 * string);
for (int i = 0; i < TUBES; i++) {
float L = tubeLength(i);
float2 top = float2(cx + (float(i) - 2.0) * 0.18 * wide, barY);
float angle = 0.0;
float2 a = hung(top, 0.06, angle);
float2 b = hung(top, 0.06 + L, angle);
float2 hole = hung(top, 0.06 + 0.224 * L, angle);
float2 d = onSegment(q, a, b);
float r = 0.021;
float edge = d.x - r;
if (edge > 0.07) continue;
float4 ring = ringing(i, t, p);
float loud = dot(ring, float4(1.0, 0.8, 0.6, 0.5));
// A glow as loud as the tube rings.
float near = exp(-max(edge, 0.0) / 0.02);
heat += 0.16 * (1.0 - exp(-loud)) * near;
if (edge < 1.5 * px) {
// Each mode lights the tube as far as it moves there.
float moving = 0.0;
for (int n = 0; n < 4; n++) {
moving += ring[n] * abs(modeShape(n, d.y)) * 0.5;
}
// A round tube catches the light down its middle.
float across = sqrt(max(1.0 - (d.x / r) * (d.x / r), 0.0));
float lit = 0.1 + 0.09 * across;
lit += 0.6 * (1.0 - exp(-1.3 * moving)) * (0.55 + 0.45 * across);
float pin = lineAt(length(q - hole) - 0.005, 1.5 * px);
lit = mix(lit, 0.62, pin);
heat = mix(heat, lit, lineAt(edge, 1.5 * px));
}
}
heat = max(heat, spectrum(uv, t, aspect, px, p));
return float4(kiln(heat), 1.0);
}
// What you hear is chimes(): the same strikes the picture lights.
float2 sound(float t, constant Params& p) {
return chimes(t, p) * p.volume;
}27 lines from stage 2 that are gone
float tubeFrequency(constant Params& p) {
return 440.0 * exp2(p.pitch / 12.0);
⋯
constant float EVERY = 2.0;
constant int REMEMBER = 5;
⋯
float4 ringing(float t, constant Params& p) {
⋯
int now = int(floor(t / EVERY));
⋯
int k = now - back;
if (k < 0) break;
float since = t - float(k) * EVERY;
⋯
level += at * exp(-since * rate) * rise * remembered(since);
⋯
float f = tubeFrequency(p);
float sum = 0.0;
int now = int(floor(t / EVERY));
⋯
int k = now - back;
if (k < 0) break;
float since = t - float(k) * EVERY;
⋯
sum += wave * rise * remembered(since);
⋯
return float2(tanh(sum * 0.15));
⋯
float f = tubeFrequency(p) * RATIO[n];
⋯
float h = min(ringing(t, p)[n] * 0.8, 1.0);
⋯
float2 top = float2(cx, barY);
float2 hole = top - float2(0.0, 0.06 + 0.224 * 0.56);
float tie = onSegment(q, top, hole).x;
heat = max(heat, 0.2 * lineAt(tie, 1.2 * px));
⋯
float4 ring = ringing(t, p);
float loud = dot(ring, float4(1.0, 0.8, 0.6, 0.5));
float2 a = top - float2(0.0, 0.06), b = top - float2(0.0, 0.62);Stage 4 · Wind Chimes
The finished shader moves. Each tube swings on its string like a pendulum, slower for a longer one and wider in a gust (sway, hung). The clapper, a disc, and the sail, a rounded card, swing together on one string from the middle of the bar. Faint streaks of noise drift sideways behind it all, faster with more wind and brighter in a gust. The strikes are still the hash's: the clapper sways in front of the tubes but doesn't decide which one rings.
When the picture is narrower than it is tall, the chime narrows with it (wide), so a portrait frame still holds all five tubes.
the kiln palette and shade(), from the finished shader
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float t = u.time;
float aspect = u.resolution.x / u.resolution.y;
float2 q = float2(uv.x * aspect, uv.y);
float px = 1.0 / u.resolution.y;
float cx = 0.5 * aspect;
// Narrower than square, the chime narrows with the picture.
float wide = min(aspect, 1.0);
// A dark ground, warmer at the middle, with the wind across it.
float heat = 0.05 + 0.04 * exp(-2.5 * length(q - float2(cx, 0.55)));
heat += streaks(q, t, p);
// The bar the tubes hang from, and the string it hangs by.
float barY = 0.9;
float2 left = float2(cx - 0.44 * wide, barY);
float2 right = float2(cx + 0.44 * wide, barY);
float bar = onSegment(q, left, right).x - 0.008;
heat = max(heat, 0.32 * lineAt(bar, 1.5 * px));
float2 above = float2(cx, 1.02);
float hook = onSegment(q, float2(cx, barY), above).x;
heat = max(heat, 0.2 * lineAt(hook, 1.5 * px));
// The clapper and the sail swing on one string from the bar's middle.
float clapAngle = sway(5, t, 0.7, p) * 1.8;
float2 pivot = float2(cx, barY);
float2 clapper = hung(pivot, 0.4, clapAngle);
float2 sail = hung(pivot, 0.62, clapAngle);
float string = lineAt(onSegment(q, pivot, sail).x, 1.2 * px);
// Each tube hangs at s = 0.224, the node of its lowest mode.
for (int i = 0; i < TUBES; i++) {
float L = tubeLength(i);
float2 top = float2(cx + (float(i) - 2.0) * 0.18 * wide, barY);
float2 hole = hung(top, 0.06 + 0.224 * L, sway(i, t, L, p));
float tie = lineAt(onSegment(q, top, hole).x, 1.2 * px);
string = max(string, 0.8 * tie);
}
heat = max(heat, 0.25 * string);
for (int i = 0; i < TUBES; i++) {
float L = tubeLength(i);
float2 top = float2(cx + (float(i) - 2.0) * 0.18 * wide, barY);
float angle = sway(i, t, L, p);
float2 a = hung(top, 0.06, angle);
float2 b = hung(top, 0.06 + L, angle);
float2 hole = hung(top, 0.06 + 0.224 * L, angle);
float2 d = onSegment(q, a, b);
float r = 0.021;
float edge = d.x - r;
if (edge > 0.07) continue;
float4 ring = ringing(i, t, p);
float loud = dot(ring, float4(1.0, 0.8, 0.6, 0.5));
// A glow as loud as the tube rings.
float near = exp(-max(edge, 0.0) / 0.02);
heat += 0.16 * (1.0 - exp(-loud)) * near;
if (edge < 1.5 * px) {
// Each mode lights the tube as far as it moves there.
float moving = 0.0;
for (int n = 0; n < 4; n++) {
moving += ring[n] * abs(modeShape(n, d.y)) * 0.5;
}
// A round tube catches the light down its middle.
float across = sqrt(max(1.0 - (d.x / r) * (d.x / r), 0.0));
float lit = 0.1 + 0.09 * across;
lit += 0.6 * (1.0 - exp(-1.3 * moving)) * (0.55 + 0.45 * across);
float pin = lineAt(length(q - hole) - 0.005, 1.5 * px);
lit = mix(lit, 0.62, pin);
heat = mix(heat, lit, lineAt(edge, 1.5 * px));
}
}
// The clapper, a disc, and the sail, a rounded card, in front.
float clap = length(q - clapper) - 0.04;
float2 shineAt = clapper + float2(-0.012, 0.012);
float shine = smoothstep(0.045, 0.0, length(q - shineAt));
heat = mix(heat, 0.24 + 0.16 * shine, lineAt(clap, 1.5 * px));
float2 local = q - sail;
float c = cos(clapAngle), s = sin(clapAngle);
local = float2(c * local.x + s * local.y, -s * local.x + c * local.y);
float2 box = abs(local - float2(0.0, -0.05)) - float2(0.035, 0.05);
float card = length(max(box, 0.0)) + min(max(box.x, box.y), 0.0);
float cardHeat = 0.2 + 0.04 * local.x / 0.035 + 0.06 * p.wind * gust(t);
heat = mix(heat, cardHeat, lineAt(card - 0.008, 1.5 * px));
heat = max(heat, spectrum(uv, t, aspect, px, p));
return float4(kiln(heat), 1.0);
}wind-chimes.metal, the whole file31 new lines since stage 3
// Ray Kiln · Wind Chimes
//
// @episode Wind Chimes
// @length 60
// @variation 30
// @variations 1,3,5,6,7,8,10,11
// @still 21.5
// @teaches Modal synthesis: struck tubes ring with their own partials
// @category Fragment
// @tags modal synthesis, partials, free-free beam, mode shape, exponential decay, procedural sound
// @sweeps wind, bright, ring
// @final Wind Chimes | The tubes and the clapper sway in the gusts, and faint streaks of wind drift across behind them.
//
// Five metal tubes hang from a bar, and the wind knocks the clapper into them. A struck tube doesn't ring at one
// pitch: it bends in several ways at once, its modes, each with a frequency and a shape of its own. For a tube free at
// both ends the modes sit at 1, 2.76, 5.40 and 8.93 times the lowest, not at whole multiples, which is why a chime
// sounds like a bell and not a string. The sound adds those four sines for every strike, each dying away at its own
// rate; the picture lights each tube along the shapes of its ringing modes, dark at their still points (the nodes),
// and draws every partial as a line along the bottom, as tall as it is loud.
// Each @param line below becomes a slider in the host app and a row in the page's parameters table:
// // @param name default min max description
// @param wind 0.6 0.15 1 How hard the wind blows: more strikes, harder, and more sway
// @param ring 2.5 0.5 3 Seconds the lowest partial takes to fall to about a third; the higher ones go sooner
// @param bright 0.5 0 1 How loud the upper partials are at the strike: 0 is the fundamental alone
// @param pitch 0 -12 7 Semitones up or down for all five tubes
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
uint hashBits(uint x) {
x ^= x >> 16;
x *= 0x7feb352du;
x ^= x >> 15;
x *= 0x846ca68bu;
x ^= x >> 16;
return x;
}
// A whole number in, a random number from 0 to 1 out.
float hash01(uint x) {
return float(hashBits(x) & 0xffffu) / 65535.0;
}
// Smooth noise along a line: a random level at every whole number, eased
// between them.
float windNoise(float x) {
float i = floor(x);
float f = fract(x);
float a = hash01(uint(int(i) + 4096));
float b = hash01(uint(int(i) + 4097));
return mix(a, b, f * f * (3.0 - 2.0 * f));
}
constant int TUBES = 5;
// Five tubes tuned to A major pentatonic, A4 to F#5, moved by `pitch`.
float tubeFrequency(int i, constant Params& p) {
const float notes[5] = {440.00, 493.88, 554.37, 659.26, 739.99};
return notes[clamp(i, 0, 4)] * exp2(p.pitch / 12.0);
}
// Pitch goes as 1 / length squared: the lowest tube is the longest.
float tubeLength(int i) {
const float notes[5] = {440.00, 493.88, 554.37, 659.26, 739.99};
return 0.56 * sqrt(440.0 / notes[clamp(i, 0, 4)]);
}
// A beam with free ends: its partials sit at 1, 2.756, 5.404 and 8.933.
constant float BETA[4] = {4.7300, 7.8532, 10.9956, 14.1372};
constant float RATIO[4] = {1.0, 2.7565, 5.4039, 8.9330};
constant float LEVEL[4] = {1.0, 0.55, 0.35, 0.22};
// Mode n's shape along the tube, s from 0 to 1: 2 at the ends.
float modeShape(int n, float s) {
float k = BETA[n];
float x = s - 0.5;
if (n % 2 == 0) {
return cos(k * x) / cos(0.5 * k) + cosh(k * x) / cosh(0.5 * k);
}
return sin(k * x) / sin(0.5 * k) + sinh(k * x) / sinh(0.5 * k);
}
// Partial n's level at the strike: `bright` scales the upper three.
float partialLevel(int n, constant Params& p) {
return n == 0 ? LEVEL[0] : LEVEL[n] * 2.0 * p.bright;
}
// Partial n's time constant: `ring` for the lowest, shorter higher up.
float partialTime(int n, constant Params& p) {
return p.ring * pow(RATIO[n], -0.75);
}
// Gusts: 0 in a lull, 1 at the height of a gust.
float gust(float t) {
float n = 0.65 * windNoise(t * 0.22) + 0.35 * windNoise(t * 0.7 + 17.0);
return smoothstep(0.2, 0.8, n);
}
// Quarter-second slots; a strike is remembered for 40.
constant float SLOT = 0.25;
constant int REMEMBER = 40;
// Whether tube i is struck in slot k, drawn from a hash of the two, more
// often and harder in a gust; if so, when and how hard.
bool strike(int i, int k, constant Params& p,
thread float& when, thread float& force) {
if (k < 0) return false;
uint seed = uint(k) * 7u + uint(i) * 131071u + 9u;
float start = float(k) * SLOT;
float g = gust(start);
if (hash01(seed) >= 0.35 * p.wind * g) return false;
when = start + SLOT * hash01(seed + 3u);
force = (0.45 + 0.55 * hash01(seed + 5u)) * (0.6 + 0.4 * g);
return true;
}
// A strike fades from memory over its last 3 of 10 seconds: no click.
float remembered(float since) {
return 1.0 - smoothstep(7.0, 10.0, since);
}
// How hard tube i's four partials ring at t: each strike's force times the
// partial's decay since then (their loudness, not their waves).
float4 ringing(int i, float t, constant Params& p) {
float4 level = float4(0.0);
float4 rate = 1.0 / float4(partialTime(0, p), partialTime(1, p),
partialTime(2, p), partialTime(3, p));
float4 at = float4(partialLevel(0, p), partialLevel(1, p),
partialLevel(2, p), partialLevel(3, p));
int now = int(floor(t / SLOT));
for (int back = 0; back < REMEMBER; back++) {
float when, force;
if (!strike(i, now - back, p, when, force)) continue;
float since = t - when;
if (since < 0.0) continue;
// The tube lights over a few hundredths of a second, not one frame.
float rise = 1.0 - exp(-since / 0.03);
level += force * at * exp(-since * rate) * rise * remembered(since);
}
return level;
}
// Modal synthesis: each strike rings four sines at RATIO times the tube's
// pitch, each falling at its own rate; the tubes spread left to right.
float2 chimes(float t, constant Params& p) {
float2 sum = float2(0.0);
int now = int(floor(t / SLOT));
for (int i = 0; i < TUBES; i++) {
float f = tubeFrequency(i, p);
float tube = 0.0;
for (int back = 0; back < REMEMBER; back++) {
float when, force;
if (!strike(i, now - back, p, when, force)) continue;
float since = t - when;
if (since < 0.0) continue;
float wave = 0.0;
for (int n = 0; n < 4; n++) {
// Whole cycles dropped first, so the phase stays precise.
float cycles = fract(f * RATIO[n] * since);
float fall = exp(-since / partialTime(n, p));
wave += partialLevel(n, p) * fall * sin(TAU * cycles);
}
float rise = 1.0 - exp(-since / 0.001);
tube += force * wave * rise * remembered(since);
}
float pan = (float(i) / float(TUBES - 1) - 0.5) * 0.6 + 0.5;
sum += tube * float2(cos(pan * 1.5707963), sin(pan * 1.5707963));
}
return tanh(sum * 0.2);
}
// Distance from a to the segment from b to c, and how far along it a is.
float2 onSegment(float2 a, float2 b, float2 c) {
float2 ba = a - b, cb = c - b;
float h = clamp(dot(ba, cb) / dot(cb, cb), 0.0, 1.0);
return float2(length(ba - cb * h), h);
}
// 1 on a shape d away, fading out over w.
float lineAt(float d, float w) {
return 1.0 - smoothstep(0.0, w, d);
}
// How far tube i (the clapper is 5) has swung from straight, in radians:
// a pendulum, slower for a longer one, swinging wider in a gust.
float sway(int i, float t, float length, constant Params& p) {
float g = gust(t - 0.4 * float(i));
float speed = 3.6 / sqrt(length / 0.5);
return p.wind * (0.01 + 0.035 * g) * sin(speed * t + 1.3 * float(i));
}
// A point hung from `pivot`, `down` below it, swung by `angle`.
float2 hung(float2 pivot, float down, float angle) {
return pivot + down * float2(sin(angle), -cos(angle));
}
// The wind itself: faint streaks drifting sideways, stronger in a gust.
float streaks(float2 q, float t, constant Params& p) {
float x = q.x * 1.3 - t * (0.15 + 0.5 * p.wind);
float row = floor(q.y * 22.0);
float n = windNoise(x + row * 13.7) * windNoise(x * 0.37 + row * 5.1);
float thin = pow(sin(3.14159 * fract(q.y * 22.0)), 8.0);
return 0.06 * p.wind * gust(t) * smoothstep(0.3, 0.75, n) * thin;
}
// Every partial as a line along the bottom, 200 Hz to 8 kHz on a log
// scale, as tall as it rings.
float spectrum(float2 uv, float t, float aspect, float px,
constant Params& p) {
float floorY = 0.035, tall = 0.11;
if (uv.y < floorY - 0.01 || uv.y > floorY + tall + 0.01) return 0.0;
if (uv.x < 0.05 || uv.x > 0.95) return 0.0;
float inBand = step(floorY, uv.y) * step(uv.y, floorY + tall);
float heat = 0.07 * lineAt(abs(uv.y - floorY), px);
for (int i = 0; i < TUBES; i++) {
for (int n = 0; n < 4; n++) {
float f = tubeFrequency(i, p) * RATIO[n];
float x = 0.06 + 0.88 * log2(f / 200.0) / log2(40.0);
float w = abs(uv.x - x) * aspect;
if (w > 3.0 * px) continue;
float h = min(ringing(i, t, p)[n] * 0.8, 1.0);
float top = step(uv.y, floorY + tall * h);
float bar = lineAt(w, 2.0 * px) * step(floorY, uv.y) * top;
float mark = 0.08 * lineAt(w, 1.5 * px) * inBand;
heat = max(heat, bar * (0.35 + 0.5 * h) + mark);
}
}
return heat;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float t = u.time;
float aspect = u.resolution.x / u.resolution.y;
float2 q = float2(uv.x * aspect, uv.y);
float px = 1.0 / u.resolution.y;
float cx = 0.5 * aspect;
// Narrower than square, the chime narrows with the picture.
float wide = min(aspect, 1.0);
// A dark ground, warmer at the middle, with the wind across it.
float heat = 0.05 + 0.04 * exp(-2.5 * length(q - float2(cx, 0.55)));
heat += streaks(q, t, p);
// The bar the tubes hang from, and the string it hangs by.
float barY = 0.9;
float2 left = float2(cx - 0.44 * wide, barY);
float2 right = float2(cx + 0.44 * wide, barY);
float bar = onSegment(q, left, right).x - 0.008;
heat = max(heat, 0.32 * lineAt(bar, 1.5 * px));
float2 above = float2(cx, 1.02);
float hook = onSegment(q, float2(cx, barY), above).x;
heat = max(heat, 0.2 * lineAt(hook, 1.5 * px));
// The clapper and the sail swing on one string from the bar's middle.
float clapAngle = sway(5, t, 0.7, p) * 1.8;
float2 pivot = float2(cx, barY);
float2 clapper = hung(pivot, 0.4, clapAngle);
float2 sail = hung(pivot, 0.62, clapAngle);
float string = lineAt(onSegment(q, pivot, sail).x, 1.2 * px);
// Each tube hangs at s = 0.224, the node of its lowest mode.
for (int i = 0; i < TUBES; i++) {
float L = tubeLength(i);
float2 top = float2(cx + (float(i) - 2.0) * 0.18 * wide, barY);
float2 hole = hung(top, 0.06 + 0.224 * L, sway(i, t, L, p));
float tie = lineAt(onSegment(q, top, hole).x, 1.2 * px);
string = max(string, 0.8 * tie);
}
heat = max(heat, 0.25 * string);
for (int i = 0; i < TUBES; i++) {
float L = tubeLength(i);
float2 top = float2(cx + (float(i) - 2.0) * 0.18 * wide, barY);
float angle = sway(i, t, L, p);
float2 a = hung(top, 0.06, angle);
float2 b = hung(top, 0.06 + L, angle);
float2 hole = hung(top, 0.06 + 0.224 * L, angle);
float2 d = onSegment(q, a, b);
float r = 0.021;
float edge = d.x - r;
if (edge > 0.07) continue;
float4 ring = ringing(i, t, p);
float loud = dot(ring, float4(1.0, 0.8, 0.6, 0.5));
// A glow as loud as the tube rings.
float near = exp(-max(edge, 0.0) / 0.02);
heat += 0.16 * (1.0 - exp(-loud)) * near;
if (edge < 1.5 * px) {
// Each mode lights the tube as far as it moves there.
float moving = 0.0;
for (int n = 0; n < 4; n++) {
moving += ring[n] * abs(modeShape(n, d.y)) * 0.5;
}
// A round tube catches the light down its middle.
float across = sqrt(max(1.0 - (d.x / r) * (d.x / r), 0.0));
float lit = 0.1 + 0.09 * across;
lit += 0.6 * (1.0 - exp(-1.3 * moving)) * (0.55 + 0.45 * across);
float pin = lineAt(length(q - hole) - 0.005, 1.5 * px);
lit = mix(lit, 0.62, pin);
heat = mix(heat, lit, lineAt(edge, 1.5 * px));
}
}
// The clapper, a disc, and the sail, a rounded card, in front.
float clap = length(q - clapper) - 0.04;
float2 shineAt = clapper + float2(-0.012, 0.012);
float shine = smoothstep(0.045, 0.0, length(q - shineAt));
heat = mix(heat, 0.24 + 0.16 * shine, lineAt(clap, 1.5 * px));
float2 local = q - sail;
float c = cos(clapAngle), s = sin(clapAngle);
local = float2(c * local.x + s * local.y, -s * local.x + c * local.y);
float2 box = abs(local - float2(0.0, -0.05)) - float2(0.035, 0.05);
float card = length(max(box, 0.0)) + min(max(box.x, box.y), 0.0);
float cardHeat = 0.2 + 0.04 * local.x / 0.035 + 0.06 * p.wind * gust(t);
heat = mix(heat, cardHeat, lineAt(card - 0.008, 1.5 * px));
heat = max(heat, spectrum(uv, t, aspect, px, p));
return float4(kiln(heat), 1.0);
}
// What you hear is chimes(): the same strikes the picture lights.
float2 sound(float t, constant Params& p) {
return chimes(t, p) * p.volume;
}3 lines from stage 3 that are gone
float string = 0.0;
⋯
float2 hole = hung(top, 0.06 + 0.224 * L, 0.0);
⋯
float angle = 0.0;The sound
Every sound in this episode is sines with exponential decays: four per strike, at the free-free beam's ratios over a pentatonic scale, the higher partials shorter, panned by tube. The picture lights the same strikes the sound plays, from the same hash. At the defaults the peak is about −4.2 dBFS and the average (RMS) about −20.4 dBFS. With wind and bright at 1 and pitch at 7 the flurries are thicker and brighter, and the peak is about −2.6 dBFS and the RMS −16.5 dBFS; wind at its lowest, 0.15, leaves a strike every few seconds, at an RMS of about −25.8 dBFS. The sound is silent until the first strike, and every strike rises over a millisecond, so nothing starts with a click; the 60-second video ends while tubes still ring, on a sample of about 0.09 (−21 dBFS).
New in Ray Kiln
A loop on the clock. The playground's clock can now repeat a stretch of time. Press I where the loop should start and O where it should end, or use the Loop menu beside the speed. The clock runs to the out point and goes back to the in point, and the picture and the sound go back together: the sound wraps on the same sample, with a 3 ms fade out and in at the join to smooth the jump. The loop shows as a gold band under the scrubber, Restart goes to its in point, it works in slow motion, and the Share link carries it. No loop in the menu takes it away. In a particle episode, where I is Inspect, the in point is Shift+I. Exports ignore the loop: they start from the time on the clock, as before.
A chime is the thing to try it on, since the whole lesson is in how one strike dies away. In this episode the middle tube is struck at 47.7 seconds, and no tube is struck for five seconds after that. Open Wind Chimes looping that strike, press Sound, and hear it again and again: the bright partials gone in under a second, then the fundamental alone, with the line for each partial along the bottom falling with it. Then set bright to 1, or ring to 0.5, and hear the same strike change.
Try this
- Set
brightto 0: each strike is one sine, the tubes glow evenly with only the fundamental's nodes dark, and the chime sounds like a set of tuning forks. At 1 the upper partials are twice as loud and the strikes clang. - Set
ringto 0.5: every strike is a short tock, and the lines along the bottom never stand for long. - Set
windto 1 for a storm of strikes, then to 0.15 and wait for the gusts. - Set
pitchto −12: the tubes ring an octave lower, and the partials' lines move left together, the same distance apart, since the ratios don't change. - In
RATIO, change the partials to1.0, 2.0, 3.0, 4.0: harmonics, and the chime becomes something like an organ pipe. - In
partialTime, change-0.75to0.0: every partial rings as long as the fundamental, and each strike stays bright to the end. - In the playground, set an in point just before a strike and an out point a second after it, then press
[twice for ¼×: the loop plays two octaves down and four times as long, and you can hear each partial leave.
Parameters
Pick a parameter and drag the slider. Each frame is the same moment of the shader, with only that one setting changed.
| Parameter | Default | Range | What it does |
|---|---|---|---|
wind | 0.6 | 0.15 to 1 | How hard the wind blows: more strikes, harder, and more sway |
ring | 2.5 | 0.5 to 3 | Seconds the lowest partial takes to fall to about a third; the higher ones go sooner |
bright | 0.5 | 0 to 1 | How loud the upper partials are at the strike: 0 is the fundamental alone |
pitch | 0 | -12 to 7 | Semitones up or down for all five tubes |
volume | 0.8 | 0 to 1 | Loudness of the sound |
More from this shader
The same shader 8 more ways: each from a different moment, with different settings, one after another. Each chapter below says which settings moved most, then lists them all.
Wallpapers
A still from the shader, rendered at full size for a desktop or a phone screen. Free to use as your wallpaper.
Clips to post
Fifteen seconds of the shader with its sound, sized for posting: square for feeds, vertical for stories and short videos. Free to post, ideally with a link to this page.
Made from this episode
The full source
This is the whole episode: the picture, the parameters and the sound.
wind-chimes.metal
// Ray Kiln · Wind Chimes
//
// @episode Wind Chimes
// @length 60
// @variation 30
// @variations 1,3,5,6,7,8,10,11
// @still 21.5
// @teaches Modal synthesis: struck tubes ring with their own partials
// @category Fragment
// @tags modal synthesis, partials, free-free beam, mode shape, exponential decay, procedural sound
// @sweeps wind, bright, ring
// @final Wind Chimes | The tubes and the clapper sway in the gusts, and faint streaks of wind drift across behind them.
//
// Five metal tubes hang from a bar, and the wind knocks the clapper into them. A struck tube doesn't ring at one
// pitch: it bends in several ways at once, its modes, each with a frequency and a shape of its own. For a tube free at
// both ends the modes sit at 1, 2.76, 5.40 and 8.93 times the lowest, not at whole multiples, which is why a chime
// sounds like a bell and not a string. The sound adds those four sines for every strike, each dying away at its own
// rate; the picture lights each tube along the shapes of its ringing modes, dark at their still points (the nodes),
// and draws every partial as a line along the bottom, as tall as it is loud.
// Each @param line below becomes a slider in the host app and a row in the page's parameters table:
// // @param name default min max description
// @param wind 0.6 0.15 1 How hard the wind blows: more strikes, harder, and more sway
// @param ring 2.5 0.5 3 Seconds the lowest partial takes to fall to about a third; the higher ones go sooner
// @param bright 0.5 0 1 How loud the upper partials are at the strike: 0 is the fundamental alone
// @param pitch 0 -12 7 Semitones up or down for all five tubes
// @param volume 0.8 0 1 Loudness of the sound
constant float TAU = 6.2831853;
// The kiln palette from Flowing Color Field.
float3 kiln(float t) {
float3 c0 = float3(0.020, 0.012, 0.020);
float3 c1 = float3(0.280, 0.040, 0.030);
float3 c2 = float3(0.880, 0.260, 0.050);
float3 c3 = float3(1.000, 0.680, 0.200);
float3 c4 = float3(1.000, 0.970, 0.840);
t = clamp(t, 0.0, 1.0);
float3 c = mix(c0, c1, smoothstep(0.00, 0.22, t));
c = mix(c, c2, smoothstep(0.22, 0.50, t));
c = mix(c, c3, smoothstep(0.50, 0.76, t));
return mix(c, c4, smoothstep(0.76, 1.00, t));
}
uint hashBits(uint x) {
x ^= x >> 16;
x *= 0x7feb352du;
x ^= x >> 15;
x *= 0x846ca68bu;
x ^= x >> 16;
return x;
}
// A whole number in, a random number from 0 to 1 out.
float hash01(uint x) {
return float(hashBits(x) & 0xffffu) / 65535.0;
}
// Smooth noise along a line: a random level at every whole number, eased
// between them.
float windNoise(float x) {
float i = floor(x);
float f = fract(x);
float a = hash01(uint(int(i) + 4096));
float b = hash01(uint(int(i) + 4097));
return mix(a, b, f * f * (3.0 - 2.0 * f));
}
constant int TUBES = 5;
// Five tubes tuned to A major pentatonic, A4 to F#5, moved by `pitch`.
float tubeFrequency(int i, constant Params& p) {
const float notes[5] = {440.00, 493.88, 554.37, 659.26, 739.99};
return notes[clamp(i, 0, 4)] * exp2(p.pitch / 12.0);
}
// Pitch goes as 1 / length squared: the lowest tube is the longest.
float tubeLength(int i) {
const float notes[5] = {440.00, 493.88, 554.37, 659.26, 739.99};
return 0.56 * sqrt(440.0 / notes[clamp(i, 0, 4)]);
}
// A beam with free ends: its partials sit at 1, 2.756, 5.404 and 8.933.
constant float BETA[4] = {4.7300, 7.8532, 10.9956, 14.1372};
constant float RATIO[4] = {1.0, 2.7565, 5.4039, 8.9330};
constant float LEVEL[4] = {1.0, 0.55, 0.35, 0.22};
// Mode n's shape along the tube, s from 0 to 1: 2 at the ends.
float modeShape(int n, float s) {
float k = BETA[n];
float x = s - 0.5;
if (n % 2 == 0) {
return cos(k * x) / cos(0.5 * k) + cosh(k * x) / cosh(0.5 * k);
}
return sin(k * x) / sin(0.5 * k) + sinh(k * x) / sinh(0.5 * k);
}
// Partial n's level at the strike: `bright` scales the upper three.
float partialLevel(int n, constant Params& p) {
return n == 0 ? LEVEL[0] : LEVEL[n] * 2.0 * p.bright;
}
// Partial n's time constant: `ring` for the lowest, shorter higher up.
float partialTime(int n, constant Params& p) {
return p.ring * pow(RATIO[n], -0.75);
}
// Gusts: 0 in a lull, 1 at the height of a gust.
float gust(float t) {
float n = 0.65 * windNoise(t * 0.22) + 0.35 * windNoise(t * 0.7 + 17.0);
return smoothstep(0.2, 0.8, n);
}
// Quarter-second slots; a strike is remembered for 40.
constant float SLOT = 0.25;
constant int REMEMBER = 40;
// Whether tube i is struck in slot k, drawn from a hash of the two, more
// often and harder in a gust; if so, when and how hard.
bool strike(int i, int k, constant Params& p,
thread float& when, thread float& force) {
if (k < 0) return false;
uint seed = uint(k) * 7u + uint(i) * 131071u + 9u;
float start = float(k) * SLOT;
float g = gust(start);
if (hash01(seed) >= 0.35 * p.wind * g) return false;
when = start + SLOT * hash01(seed + 3u);
force = (0.45 + 0.55 * hash01(seed + 5u)) * (0.6 + 0.4 * g);
return true;
}
// A strike fades from memory over its last 3 of 10 seconds: no click.
float remembered(float since) {
return 1.0 - smoothstep(7.0, 10.0, since);
}
// How hard tube i's four partials ring at t: each strike's force times the
// partial's decay since then (their loudness, not their waves).
float4 ringing(int i, float t, constant Params& p) {
float4 level = float4(0.0);
float4 rate = 1.0 / float4(partialTime(0, p), partialTime(1, p),
partialTime(2, p), partialTime(3, p));
float4 at = float4(partialLevel(0, p), partialLevel(1, p),
partialLevel(2, p), partialLevel(3, p));
int now = int(floor(t / SLOT));
for (int back = 0; back < REMEMBER; back++) {
float when, force;
if (!strike(i, now - back, p, when, force)) continue;
float since = t - when;
if (since < 0.0) continue;
// The tube lights over a few hundredths of a second, not one frame.
float rise = 1.0 - exp(-since / 0.03);
level += force * at * exp(-since * rate) * rise * remembered(since);
}
return level;
}
// Modal synthesis: each strike rings four sines at RATIO times the tube's
// pitch, each falling at its own rate; the tubes spread left to right.
float2 chimes(float t, constant Params& p) {
float2 sum = float2(0.0);
int now = int(floor(t / SLOT));
for (int i = 0; i < TUBES; i++) {
float f = tubeFrequency(i, p);
float tube = 0.0;
for (int back = 0; back < REMEMBER; back++) {
float when, force;
if (!strike(i, now - back, p, when, force)) continue;
float since = t - when;
if (since < 0.0) continue;
float wave = 0.0;
for (int n = 0; n < 4; n++) {
// Whole cycles dropped first, so the phase stays precise.
float cycles = fract(f * RATIO[n] * since);
float fall = exp(-since / partialTime(n, p));
wave += partialLevel(n, p) * fall * sin(TAU * cycles);
}
float rise = 1.0 - exp(-since / 0.001);
tube += force * wave * rise * remembered(since);
}
float pan = (float(i) / float(TUBES - 1) - 0.5) * 0.6 + 0.5;
sum += tube * float2(cos(pan * 1.5707963), sin(pan * 1.5707963));
}
return tanh(sum * 0.2);
}
// Distance from a to the segment from b to c, and how far along it a is.
float2 onSegment(float2 a, float2 b, float2 c) {
float2 ba = a - b, cb = c - b;
float h = clamp(dot(ba, cb) / dot(cb, cb), 0.0, 1.0);
return float2(length(ba - cb * h), h);
}
// 1 on a shape d away, fading out over w.
float lineAt(float d, float w) {
return 1.0 - smoothstep(0.0, w, d);
}
// How far tube i (the clapper is 5) has swung from straight, in radians:
// a pendulum, slower for a longer one, swinging wider in a gust.
float sway(int i, float t, float length, constant Params& p) {
float g = gust(t - 0.4 * float(i));
float speed = 3.6 / sqrt(length / 0.5);
return p.wind * (0.01 + 0.035 * g) * sin(speed * t + 1.3 * float(i));
}
// A point hung from `pivot`, `down` below it, swung by `angle`.
float2 hung(float2 pivot, float down, float angle) {
return pivot + down * float2(sin(angle), -cos(angle));
}
// The wind itself: faint streaks drifting sideways, stronger in a gust.
float streaks(float2 q, float t, constant Params& p) {
float x = q.x * 1.3 - t * (0.15 + 0.5 * p.wind);
float row = floor(q.y * 22.0);
float n = windNoise(x + row * 13.7) * windNoise(x * 0.37 + row * 5.1);
float thin = pow(sin(3.14159 * fract(q.y * 22.0)), 8.0);
return 0.06 * p.wind * gust(t) * smoothstep(0.3, 0.75, n) * thin;
}
// Every partial as a line along the bottom, 200 Hz to 8 kHz on a log
// scale, as tall as it rings.
float spectrum(float2 uv, float t, float aspect, float px,
constant Params& p) {
float floorY = 0.035, tall = 0.11;
if (uv.y < floorY - 0.01 || uv.y > floorY + tall + 0.01) return 0.0;
if (uv.x < 0.05 || uv.x > 0.95) return 0.0;
float inBand = step(floorY, uv.y) * step(uv.y, floorY + tall);
float heat = 0.07 * lineAt(abs(uv.y - floorY), px);
for (int i = 0; i < TUBES; i++) {
for (int n = 0; n < 4; n++) {
float f = tubeFrequency(i, p) * RATIO[n];
float x = 0.06 + 0.88 * log2(f / 200.0) / log2(40.0);
float w = abs(uv.x - x) * aspect;
if (w > 3.0 * px) continue;
float h = min(ringing(i, t, p)[n] * 0.8, 1.0);
float top = step(uv.y, floorY + tall * h);
float bar = lineAt(w, 2.0 * px) * step(floorY, uv.y) * top;
float mark = 0.08 * lineAt(w, 1.5 * px) * inBand;
heat = max(heat, bar * (0.35 + 0.5 * h) + mark);
}
}
return heat;
}
float4 shade(float2 uv, constant Uniforms& u, constant Params& p) {
float t = u.time;
float aspect = u.resolution.x / u.resolution.y;
float2 q = float2(uv.x * aspect, uv.y);
float px = 1.0 / u.resolution.y;
float cx = 0.5 * aspect;
// Narrower than square, the chime narrows with the picture.
float wide = min(aspect, 1.0);
// A dark ground, warmer at the middle, with the wind across it.
float heat = 0.05 + 0.04 * exp(-2.5 * length(q - float2(cx, 0.55)));
heat += streaks(q, t, p);
// The bar the tubes hang from, and the string it hangs by.
float barY = 0.9;
float2 left = float2(cx - 0.44 * wide, barY);
float2 right = float2(cx + 0.44 * wide, barY);
float bar = onSegment(q, left, right).x - 0.008;
heat = max(heat, 0.32 * lineAt(bar, 1.5 * px));
float2 above = float2(cx, 1.02);
float hook = onSegment(q, float2(cx, barY), above).x;
heat = max(heat, 0.2 * lineAt(hook, 1.5 * px));
// The clapper and the sail swing on one string from the bar's middle.
float clapAngle = sway(5, t, 0.7, p) * 1.8;
float2 pivot = float2(cx, barY);
float2 clapper = hung(pivot, 0.4, clapAngle);
float2 sail = hung(pivot, 0.62, clapAngle);
float string = lineAt(onSegment(q, pivot, sail).x, 1.2 * px);
// Each tube hangs at s = 0.224, the node of its lowest mode.
for (int i = 0; i < TUBES; i++) {
float L = tubeLength(i);
float2 top = float2(cx + (float(i) - 2.0) * 0.18 * wide, barY);
float2 hole = hung(top, 0.06 + 0.224 * L, sway(i, t, L, p));
float tie = lineAt(onSegment(q, top, hole).x, 1.2 * px);
string = max(string, 0.8 * tie);
}
heat = max(heat, 0.25 * string);
for (int i = 0; i < TUBES; i++) {
float L = tubeLength(i);
float2 top = float2(cx + (float(i) - 2.0) * 0.18 * wide, barY);
float angle = sway(i, t, L, p);
float2 a = hung(top, 0.06, angle);
float2 b = hung(top, 0.06 + L, angle);
float2 hole = hung(top, 0.06 + 0.224 * L, angle);
float2 d = onSegment(q, a, b);
float r = 0.021;
float edge = d.x - r;
if (edge > 0.07) continue;
float4 ring = ringing(i, t, p);
float loud = dot(ring, float4(1.0, 0.8, 0.6, 0.5));
// A glow as loud as the tube rings.
float near = exp(-max(edge, 0.0) / 0.02);
heat += 0.16 * (1.0 - exp(-loud)) * near;
if (edge < 1.5 * px) {
// Each mode lights the tube as far as it moves there.
float moving = 0.0;
for (int n = 0; n < 4; n++) {
moving += ring[n] * abs(modeShape(n, d.y)) * 0.5;
}
// A round tube catches the light down its middle.
float across = sqrt(max(1.0 - (d.x / r) * (d.x / r), 0.0));
float lit = 0.1 + 0.09 * across;
lit += 0.6 * (1.0 - exp(-1.3 * moving)) * (0.55 + 0.45 * across);
float pin = lineAt(length(q - hole) - 0.005, 1.5 * px);
lit = mix(lit, 0.62, pin);
heat = mix(heat, lit, lineAt(edge, 1.5 * px));
}
}
// The clapper, a disc, and the sail, a rounded card, in front.
float clap = length(q - clapper) - 0.04;
float2 shineAt = clapper + float2(-0.012, 0.012);
float shine = smoothstep(0.045, 0.0, length(q - shineAt));
heat = mix(heat, 0.24 + 0.16 * shine, lineAt(clap, 1.5 * px));
float2 local = q - sail;
float c = cos(clapAngle), s = sin(clapAngle);
local = float2(c * local.x + s * local.y, -s * local.x + c * local.y);
float2 box = abs(local - float2(0.0, -0.05)) - float2(0.035, 0.05);
float card = length(max(box, 0.0)) + min(max(box.x, box.y), 0.0);
float cardHeat = 0.2 + 0.04 * local.x / 0.035 + 0.06 * p.wind * gust(t);
heat = mix(heat, cardHeat, lineAt(card - 0.008, 1.5 * px));
heat = max(heat, spectrum(uv, t, aspect, px, p));
return float4(kiln(heat), 1.0);
}
// What you hear is chimes(): the same strikes the picture lights.
float2 sound(float t, constant Params& p) {
return chimes(t, p) * p.volume;
}Running it in your own project
Every Ray Kiln shader is the same shape. The standalone download above is the shader with the small wrapper around it that the Ray Kiln host adds, so it compiles with the ordinary Metal compiler and runs in your own app. It defines three entry points: rk_vertex (a full-screen triangle), rk_fragment (calls the shader's shade) and, for shaders with sound, the compute kernel rk_sound.
To draw it, pass the uniforms at fragment buffer 0 and the parameter values at buffer 1, in the order they are declared:
Drawing it, in Swift
struct Uniforms {
var resolution: SIMD2<Float>
var time: Float
var timeDelta: Float
var mouse: SIMD2<Float>
var frame: UInt32
var pad: UInt32 = 0
}
let library = try device.makeLibrary(source: standaloneSource, options: nil)
let descriptor = MTLRenderPipelineDescriptor()
descriptor.vertexFunction = library.makeFunction(name: "rk_vertex")
descriptor.fragmentFunction = library.makeFunction(name: "rk_fragment")
descriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
let pipeline = try device.makeRenderPipelineState(descriptor: descriptor)
// Each frame, inside a render pass:
var uniforms = Uniforms(resolution: size, time: time, timeDelta: dt, mouse: mouse, frame: frame)
var params: [Float] = [0.6, 2.5, 0.5, 0, 0.8] // the @param defaults, in the order they are declared
encoder.setRenderPipelineState(pipeline)
encoder.setFragmentBytes(&uniforms, length: MemoryLayout<Uniforms>.stride, index: 0)
encoder.setFragmentBytes(¶ms, length: params.count * MemoryLayout<Float>.stride, index: 1)
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)Time is in seconds, mouse is in pixels from the bottom left, and uv in the shader runs 0 to 1 with (0, 0) at the bottom left. Write the output as sRGB: the shader's numbers go to the screen as they are.
More in Sound & Rhythm
Press ? for keyboard shortcuts.

