Pages

Saturday, August 24, 2019

Strung Out

This is another idea from my list, and I have absolutely no memory of the source, aside from a casual interest in computer-generated sound and music: The Karplus-Strong algorithm. This is a technique for generating the sound of a plucked string, using entirely electronics, no actual strings required! Before we get into the algorithm though, we should go over some background:

What is sound?
Sound is a wave made up of pockets of higher- and lower-pressure air (or other substances). These pockets of pressure excite your eardrums in different ways that your brain interprets as sound. A musical note has a pitch, which is a specific frequency of high/low pressure changes.

How does a computer make sound?
A computer speaker uses magnets to change electricity into sound:
By Svjo - Own work, CC BY-SA 3.0, Link
The yellow coil is a wire that the computer can run current through. This creates a magnetic field, which moves the magnet (2) in the middle of the coil. The magnet is attached to a diaphragm (4) which compresses and expands the air in front of it to make sound waves.

What do we put into the speaker to get a note?
This is where the algorithm comes in. A pitch is a specific frequency, so as long as we repeat whatever we're putting into the speaker, we'll get some kind of note. If you remember your math classes, a sine wave might come to mind:
Unfortunately, as any Physics Lab instructor can tell you, pure sine tones aren't the most melodious to listen to:

Not surprisingly, it sounds a bit like a telephone tone, another form of computer-generated sound, designed to be heard by a computer.

Musical instruments sound different even when playing the same pitch because of the harmonics they include. There's a base frequency, like the sine wave above, but then there are many others that add color to the sound. This is where the Karplus-Strong algorithm comes in. We want a way to systematically create a range of frequencies associated with a single base frequency.

I don't want to get too deep into how the algorithm works – for that you can read the Wikipedia page I linked above, or take a look at the code I used to make these sounds. I will, however, try to summarize the concept.

We start with a list of values – mostly zeros, but we initialize things with some random noise at the beginning:
We then take the first value and output it. At the same time, we combine it with the second:
We put the combined value at the back, and move everything forward:
It's a simple method, but I was stunned by the quality of the sound I got out of it:

I also plotted some frames of the buffer to see what was going on:
You can see the burst of noise at the beginning, and then a damping sinusoidal wave.

I was curious what kinds of tones I could get by playing around with the filtering pattern. First I tried simply extending from 2 bins to 5:

Then I decided to go nuts and try a sinusoid filter:

The sound is even worse than the pure sine, but the waveform is really interesting:

It's a little difficult to see, but the initial noise manages to continue propagating through, as the sine pattern gradually asserts itself.

The thing I love most about Physics is how simple models can still give realistic results, and this is a perfect example. I wrote the code above in less than an hour, with no specialized knowledge about sound synthesis, yet the result sounds exactly like a plucked string. As always, I encourage the tech-minded among you to look at the code, and come up with some interesting sounds for yourself!

No comments:

Post a Comment