Skip to content

Building the instrument: physical modelling

Samples sound real because they are real, but they’re frozen: a recording of a string plucked once sounds the same every time. A real string responds. Pluck it nearer the bridge and it’s brighter; pluck it harder and it’s louder and brighter; let it ring and its high harmonics die first. Physical modelling gets all of that by simulating the instrument itself, its string, its body, in arithmetic, so the sound comes out of the physics.

The first attempts, around 1970, solved the wave equation for a vibrating string point by point, at great cost in computer time. The breakthrough came in 1983, when Kevin Karplus and Alex Strong found a plucked string in a few lines of code, cheap enough for the computers of the day; Julius Smith at Stanford then generalised their trick into digital waveguides, which model strings, tubes and much more. Yamaha’s VL1 (1994) brought waveguide instruments to the stage.

This page builds a guitar, step by step, then looks at other instruments.

It leans on:

Pluck a string, and two waves set off along it, one each way. At each end the string is held fixed, so each wave reflects back, upside down, and heads the other way. After going there and back once, the string is in its starting shape again, and it all repeats. So the string’s pitch is set by one number: how long a round trip takes.

A string, plucked (pulled into a triangle) and let go, drawn about 330 times slower than real. With the two travelling waves ticked, the dashed curves are the wave going right and the wave going left; the string is their sum. Move the pluck point, and pluck again.

On a guitar’s low E string, 65 cm long, a round trip takes 12.13 ms, so the shape repeats 82.4 times a second: the note E2. The waves travel at 107 m/s.

So a string is two delay lines, one per direction, joined at the ends. A computer can store a delay line as a list of numbers, a “circular buffer”, that it reads from one end and writes to the other. One loop of the right length is enough to model the whole round trip.

Here’s Karplus and Strong’s whole algorithm:

  1. Fill a delay line of NN samples with random noise: that’s the pluck, the string’s energy at the start.
  2. Each sample, take the oldest value out, average it with the one before it, put the average back in, and send it out as sound.
buf = noise(N) # the pluck
i = 0
while playing:
x = buf[i]
buf[i] = g * 0.5 * (x + prev) # a two-point average, slightly quieter
prev = x
out(x)
i = (i + 1) % N

The output repeats every NN samples, so it’s a note at fs/Nf_s / N. And the average is a gentle low-pass filter: on each trip round, it takes a little off every harmonic, and more off the higher ones. A high harmonic makes more trips’ worth of difference: so the sound starts as bright, buzzy noise and mellows into a pure, plucked tone, exactly as a real string does.

On A4: top: how fast each harmonic dies away, each a straight line in dB (the 1st blue, the 5th orange, the 10th green). Bottom: the wave just after the pluck (grey) and half a second later (orange, scaled up). Decay sets how much each trip loses overall; brightness gives the averaging filter less to do. Play the keyboard: each note is a fresh burst of noise.

With the plain average, the 1st harmonic takes 2.74 s to fall 60 dB, the 5th 0.89 s and the 10th just 0.28 s. At full brightness they take 2.95, 2.08 and 1.09 s. Just after the pluck the wave is noise that repeats every cycle; half a second later it’s a smooth, rounded tone.

Notice that the high notes die faster than the low ones: a high note makes more trips per second, and loses a little on every trip. Real strings do much the same.

There’s a catch. A delay line holds a whole number of samples, so the loop can only be 99 samples long, or 100, never 99.75. For low notes that hardly matters, but a high note’s loop is short, and one sample is a big fraction of it. At 44.1 kHz, F♯6’s loop should be 29.80 samples.

The fix (from David Jaffe and Julius Smith, the same year) is to make up the fraction with an all-pass filter. An all-pass filter passes every frequency at full strength, but delays it, and with one coefficient, C=(1−d)/(1+d)C = (1 - d)/(1 + d), it can add a delay of any fraction dd of a sample. The averaging filter adds half a sample of delay of its own, so that’s counted too.

How far each note is out of tune, in cents, if the loop is a whole number of samples (orange), or with the all-pass making up the fraction (blue). The keyboard plays high notes either way: compare them with another instrument, or play octaves.

With whole samples only, notes are up to 28.0 cents out of tune (F♯6), more than a quarter of a semitone, and the error jumps about from note to note. With the all-pass, every note from C2 to C7 is within 0.10 cents.

A real pluck isn’t random noise. The finger or pick pulls the string aside at one point, and lets go, and both the where and the how shape the sound.

  • Where: a harmonic that has a still point (a node) at the pluck point can’t be set moving there. Pluck at a fifth of the way along, and the 5th, 10th, 15th … harmonics are missing, just like the pulse wave’s missing harmonics. Plucking near the bridge leaves more high harmonics in (a thin, twangy sound); plucking near the middle takes out even ones (a round, hollow sound). In the model, it’s a comb filter on the pluck: subtract a copy of the noise delayed by that fraction of the loop.
  • How: a hard pick snaps away sharply, and sets high harmonics going; a soft fingertip rounds the pluck off. In the model, it’s a gentle low-pass on the pluck. With a MIDI keyboard, playing harder makes the pluck harder as well as louder.

A2’s first 20 harmonics, just after the pluck (an average of six plucks, since each pluck’s noise is different). Shading marks the harmonics with a node at the pluck point. Play the keyboard as you move the pluck point and the hardness.

Plucked at 20% of the way along, the 5th and 10th harmonics are 37.8 and 34.9 dB below the strongest, while their neighbours are within 10 dB. At exactly 50%, every even harmonic goes.

On its own, a guitar string is almost silent: it’s too thin to push much air. The sound comes from the body. The string rocks the bridge, the bridge shakes the top plate, and the top plate, the air inside the box (breathing in and out of the sound hole) and the back plate all vibrate, each with its own resonances. They boost some frequencies and not others, and they give every note on the guitar the same family character. This model’s body has nine resonances, loosely based on the lowest ones of an acoustic guitar: the air in the box near 100 Hz, the top plate near 200 Hz, and higher plate modes.

The obvious way to add the body is to run the string’s output through those resonators. But there’s a better way. The string and the body are both linear (doubling what goes in doubles what comes out) and they don’t change over a note, and for such systems the order doesn’t matter: string then body is exactly the same as body then string. So instead of plucking the string with noise and then filtering it through the body, you can pluck the string with the body’s own impulse response: the sound of tapping the guitar’s body. That’s commuted synthesis (Julius Smith, and Matti Karjalainen and Vesa Välimäki, 1993), and it makes even a detailed body cost nothing per note: the body is folded into the pluck, which is played only once.

Top: the body’s response: how much it boosts or cuts each frequency. Bottom: A2’s harmonics through the body. Body size scales the whole guitar (a bigger body resonates lower). The buttons play the string alone, the string then the body, and the body plucking the string. The keyboard plays with the body as a filter.

The two orders give the same sound: they differ by at most −138 dB, the computer’s rounding. The air resonance sits at 102 Hz for a body of normal size, lifting the lowest notes, as in a real acoustic guitar.

Commuted synthesis also means the body can be a real one. Tap a guitar’s bridge, with its strings damped, and record it: that recording is the body’s impulse response, and it can pluck the strings directly. The instrument in section 7 lets you do exactly that.

The same thinking reaches most acoustic instruments:

  • Wind instruments: the air in a clarinet’s tube is a waveguide, like the string, and the reed is a valve that opens and closes with the pressure in the mouthpiece. That valve isn’t linear, which is what lets the model play a steady note from steady blowing.
  • Bowed strings: the bow grips the string, drags it, then lets it slip back, again and again (stick-slip). Another nonlinear valve, at the bow.
  • The piano: its thick, stiff strings aren’t perfectly flexible, so their harmonics are slightly sharp, increasingly so as they go up (inharmonicity); and each note has two or three strings, slightly out of tune with each other, which is why a piano note dies away in two stages.

And for things that are struck, not plucked or blown, there’s an even simpler model. A struck bar, bell or drum rings in a set of modes, each a sine wave at its own frequency, dying away at its own rate. Their frequencies come from the object’s shape, and aren’t usually harmonics, which is why a bell or a drum has its own, clangorous kind of pitch. Modal synthesis just adds up decaying sines at the right ratios: it’s the first page’s additive synthesis, with the recipe supplied by physics.

Each object’s modes (at multiples of the note’s frequency; dashed lines mark the harmonics), how strongly the strike sets each ringing, and how long each one rings (written above it). A softer strike sets the high modes going less. Play the keyboard.

A plain bar’s modes are at 1, 2.756, 5.404, 8.933 and 13.34 times its lowest: nowhere near harmonics, which is why a glockenspiel sounds bright and glassy. A marimba bar has a deep arch cut underneath, which tunes its first modes to 1 : 4 : 10 (two octaves, and three octaves and a major third, above its note): the warm, woody marimba sound. A church bell’s are tuned to a chord: the “hum” an octave below the note, the “tierce” (a minor third), the “quint” (a fifth) and the “nominal” an octave up. A drum’s are the vibration patterns of a stretched circle, at 1, 1.593, 2.136, 2.296 … times its lowest.

(The decay times and strengths here are this page’s choices; the frequency ratios are the physics.)

Here’s everything on this page in one instrument: Karplus–Strong strings with the all-pass tuning, the pluck position and hardness, the body, and a damping when you let go of a key (as a guitarist’s hand stops a string). It plays up to 12 notes at once. As with the other synths, you can save sounds, or share them as a code or a link. Volume isn’t part of a sound, so it stays put when you change preset; the figure shows how loud the output has been lately, and says when it’s loud enough to be squashed by the page’s limiter (six strings struck hard add up).

It starts as an acoustic guitar, with a real body: a steel-string guitar’s, recorded by tapping its bridge with the strings damped. That recording plucks every note (the commuted synthesis of section 5); switch the Body to “the model” to hear the difference.

You can record your own body too. Press Record a tap, then tap a guitar (or a ukulele, a box, a table) once, firmly, on its bridge or top, with the strings damped by your other hand, and the microphone close to the sound hole. The figure finds the tap, trims it, and from then on every note is plucked by it: commuted synthesis, with your instrument’s own body. The top plot compares its response with the model’s. Open a recording does the same with a sound file. (The recording stays in your browser; saved sounds keep the settings, but not the recording; Download your tap saves the recording itself.)

Top: the body’s response: the recorded tap’s in use (orange), and the model’s. Below: the scope and spectrum of what you play. Start from a preset and change anything. Things to try:

  • With a MIDI keyboard, play softly and then hard: harder plucks are brighter as well as louder, because velocity sets the pick’s hardness too.
  • Move the pluck position towards 50% for a round, hollow tone, and down to a few percent for a thin, twangy one, as when a guitarist plays near the bridge.
  • Body size turns the model body into something more like a ukulele (small) or a bass (large).
  • Damping on release sets how long a note rings after you let go: short for a muted, staccato style, long for a harp.
  • Set Keys play to the chords of a key, and strum (below).

Keys play turns the keyboard from notes into a chord chooser: each key chooses a chord, voiced as a guitarist plays it on six strings, and it stays chosen after you let go. There are three ways to choose:

  • The chords of a key. Choose the song’s key, and each note of its scale plays the chord that belongs to it. In C major, that’s C, Dm, Em, F, G, Am and B° on C, D, E, F, G, A and B, so most songs need only one finger per chord. Notes outside the key play major chords (in C, the black keys give C♯, E♭, F♯, A♭ and B♭: the borrowed chords that turn up in a lot of songs). In a minor key, the fifth chord is major (E in A minor), as minor-key music nearly always plays it.
  • A kind per octave. From C2, one octave of major chords, the next of minor, then 7ths, then minor 7ths. Any chord with one finger, at the cost of some jumping about.
  • From the keys held. The lowest key is the root, and on its own makes a major chord; hold its minor third too for minor, its flat seventh for a 7th, or both for a minor 7th.

In the first two, each key is labelled with the chord it plays, and the chords of the chosen key are listed under the buttons. Minor swaps any chord between major and minor (so Dm becomes D), and 7th adds the seventh (G becomes G7); both stay on until pressed again, and work in every mode.

Then Strum ↓ (low string to high), Strum ↑ (high to low), or pick one string at a time with the string buttons. Strum speed sets the gap between strings: a few milliseconds is a fast, hard strum; 50 or more, a slow spread chord. As on a guitar, plucking a string again replaces what it was playing, and strings the chord doesn’t use are damped.

The voicings are the open shapes guitarists use where there’s a usual one (C is open E, C, E, open G, C, open E: 0-3-2-0-1-0) and barre chords elsewhere (F is 1-3-3-2-1-1). If your controller has buttons or pads, link them to the strum, string, Minor and 7th buttons the same way as knobs: press Link knobs, click a button on the page, then press the button on your controller. A pad linked this way strums as hard as you hit it.

  • A string is two travelling waves, reflecting at its ends; a round trip sets its pitch. A delay line models it.
  • Karplus–Strong: noise in a delay loop, through a two-point average. The noise becomes a plucked note, and the average makes high harmonics die first, as on a real string.
  • A loop can only be a whole number of samples; an all-pass filter adds the fraction, which keeps every note within 0.10 cents instead of up to 28 cents out.
  • Where a string is plucked removes the harmonics with a node there; how hard sets the brightness.
  • A guitar’s sound is shaped by its body’s resonances. Since string and body are linear, they can swap places: commuted synthesis plucks the string with the body’s impulse response.
  • Struck objects ring in modes, usually not harmonics; modal synthesis adds them up.
  • Kevin Karplus and Alex Strong, “Digital synthesis of plucked-string and drum timbres”, Computer Music Journal 7(2), 1983.
  • David A. Jaffe and Julius O. Smith, “Extensions of the Karplus–Strong plucked-string algorithm”, Computer Music Journal 7(2), 1983: the all-pass tuning, the pluck position and the brightness control.
  • Julius O. Smith, “Physical modeling using digital waveguides”, Computer Music Journal 16(4), 1992; and his online book Physical Audio Signal Processing.
  • Julius O. Smith, “Efficient synthesis of stringed musical instruments”, and Matti Karjalainen, Vesa Välimäki and Zoltán Jánosy, “Towards high-quality sound synthesis of the guitar and string instruments”, both Proceedings of the International Computer Music Conference, 1993: commuted synthesis.
  • Vesa Välimäki, Jyri Pakarinen, Cumhur Erkut and Matti Karjalainen, “Discrete-time modelling of musical instruments”, Reports on Progress in Physics 69, 2006: a survey of the whole field.
  • Neville H. Fletcher and Thomas D. Rossing, The Physics of Musical Instruments (Springer, 2nd edition 1998): strings, bodies, bars, bells and drums, measured.