Skip to content
Adrien Hubert

Karplus-Strong, six plucked strings.

Pluck a real string and a travelling wave bounces between the two ends. Every reflection loses a little energy, especially in the high frequencies, so the timbre dulls as the note decays. Karplus and Strong noticed that a delay line plus a two-tap average reproduces almost the same behaviour with two lines of code.

Click a string, or press 1 through 6.

The algorithm

Pick a delay length N equal to the sample rate divided by the frequency you want. Fill the first N samples of a buffer with white noise, then keep writing: each new sample is the average of the two samples one period back, multiplied by a number just under one. Read the buffer out as audio.

// sr = 48000, freq = 440, damping = 0.996
const N = Math.floor(sr / freq);
for (let i = 0; i < N; i++) buf[i] = Math.random() * 2 - 1;
for (let i = N; i < buf.length; i++)
  buf[i] = damping * 0.5 * (buf[i - N] + buf[i - N - 1]);

The delay sets the pitch. The two-tap average is a tiny low-pass filter: it leaves the bass alone and trims the treble a little on every pass, which is exactly what a real string termination does. The damping factor accounts for the rest of the losses. Move the brightness slider above and the damping rises from 0.985 to 0.9998; at the top the strings ring for ten seconds.

Pluck softness

The second slider blurs the initial noise burst before it enters the delay line. A sharp burst is wide-band and sounds like a fingernail. A smoother burst rolls off the highs from the start and sounds like the pad of a thumb. The trick is a single pass of a two-tap moving average applied however many times you want.

Source

Karplus, K. and Strong, A., "Digital Synthesis of Plucked-String and Drum Timbres" (Computer Music Journal, vol. 7 no. 2, 1983). Jaffe, D. and Smith, J., "Extensions of the Karplus-Strong Plucked-String Algorithm" (Computer Music Journal, vol. 7 no. 2, 1983) introduced fractional delay and tunable decay.