
Show HN: Karplus-Strong Guitar Synthesizer in JavaScript - fishfish
http://amid.fish/javascript-karplus-strong/
======
pierrec
KS is one of the most interesting algorithms to experiment with. You can
modify, multiply and recombine it with itself in a huge variety of ways, and
always get new and weird sounds out of it.

In fact it forms the basis of a large branch of DSP for sound effects and
synthesis, physical modeling. Of course many techniques can be used in
physical modeling, but often it's actually delay lines all the way down, and
delay lines are really a generalization of the KS algorithm with a better
interpolation. Many crucial effects in digital music such as chorus,
phaser/flanger, and algorithmic reverbs are also just delay lines fed into
each other.

Then there are masters of the genre that create jaw-dropping effects and
instruments by making massive use of delay lines - I'm looking at xoxos:

[http://www.xoxos.net/vst/vst.html#nature](http://www.xoxos.net/vst/vst.html#nature)

~~~
chipsy
Every time I delve into audio DSP I'm a little amazed at how simple a lot of
the techniques are in an algorithmic sense. The FFT is a notorious outlier but
most effects are elaborate compositions of beautifully simple pieces of code
that warrant pages of mathematical background.

It's starkly different from "core CS" because almost everything operates on
arrays and pure functions. Yet at the same time it presents an immense design
space for both software engineering and UX - most of the "secret sauce" in an
audio DSP product comes from exposing the right parameters in the right
combinations. A low level tool like Max/MSP or PD leads to a different
workflow from a "pick the presets" VST plugin, in the same way that
programming languages are targeted against certain problem domains.

------
FigBug
What's the license on this? I'm guessing since it's just a decompiled version
of André Michelle's code, the copyright still belongs to him?

------
hiphopyo
1\. Combine with [http://git.io/drummer](http://git.io/drummer) 2\. Record to
cassette tape 3\. Send to major label

~~~
Intermernet
Doing that right now. Multi-tab multitrack :-) love it. (Not doing step 2 or 3
just yet though, can't find my old tape deck.)

------
drfuchs
When Alex demoed their first hardware, he'd hold an ice cube against the
custom vlsi chip to get it to work at high enough frequency. Early
overclocker!

~~~
FigBug
Um, what?

~~~
drfuchs
"The authors have designed and tested a custom n-channel metal-oxide
semiconductor (nMOS) chip (the Digitar chip), which computes 16 independent
notes, each with a sampling rate of 20 KHz." Kevin Karplus and Alex Strong,
"Digital Synthesis of Plucked-String and Drum Timbres", Computer Music
Journal, Vol. 7, No. 2 (Summer, 1983), pp. 43-55. That was way more voices
than they could get from microprocessors of the day.

In the early 1980's, the big excitement in Stanford EE and CS was that you
could take the new VLSI course and they'd actually fab your chip for you if
you made it to the end of the course. (Actually, each student project just got
a small section of the whole mask; enough chips were manufactured so that
everyone could get a few, and they'd just wire the pins to different pads for
each particular project's ICs.) I remember Alex showing off his first working
Digitar chip to my officemate at the time, Danny Sleator (who is also credited
in the paper), using the ice cube technique.

------
timothya
> _Karplus-Strong guitar synthesizer implemented in JavaScript using asm.js
> and Web Audio._

Are the asm.js annotations required for this to work in Firefox? I thought the
primary use case for asm.js was converting low-level code into something that
could run efficiently in JS, and not so much for handwritten JS. And the demo
seems to work fine in Chrome, which has no optimizations specific to asm.js
that I know of.

~~~
fishfish
The initial version without any optimisation at all didn't run in real-time,
so I started down the asm.js route for fun. With the refactoring done to
accommodate asm.js, it does actually work just fine in Firefox even if asm.js
compilation fails.

------
mmastrac
Hand-written asm.js? That's impressive!

[https://github.com/mrahtz/javascript-karplus-
strong/blob/mas...](https://github.com/mrahtz/javascript-karplus-
strong/blob/master/karplus-strong/guitarstring_asm.js)

~~~
girvo
Despite asm.js's main aim as a compilation target, I've seen a number of
developers write it by hand to squeeze extra deterministic perf out of JS.
Emulator devs have been doing this recently, IIRC. It's really fascinating, I
genuinely want to take a look at it myself.

------
huuu
Does anyone know if there is an area of study where sounds are physically
modeled using a 3D model of the 'instrument'?

Something like:

    
    
      * model a room
      * model an 'instrument'
      * place a virtual microphone
      * hit/trigger the instrument model
      * render sound waves produced by the model and sample the incoming waves at the microphone
    

In my mind this could be doable with GPUs.

~~~
TheOtherHobbes
It's doable, but it's incredibly inefficient. It makes more sense to use
waveguide models or procedural simulations, because they produce similar
results for a tiny fraction of the cycles.

There's a lot of non-linearity in the physics, and any rendered model has to
allow for that. So it's a long way short of real time synthesis, even with a
GPU.

Karplus-Strong is the entry-level version of various complex waveguide and
resonator models, and the technology has been used in commercial products for
more than twenty years now.

The commercial problem is that for many applications, multi-sampling sounds
better. Most musicians don't want experimental sounds, and those who do prefer
other ways to make unusual noises - preferably more intuitive ways that are
easier to control.

So there's limited commercial demand for waveguide modelling, and no demand at
all for a full-fat 3D rendered synthesis engine.

------
kyle_martin1
Nice!

The setting that sounded best to my ear was:

Character variation 0.7

String damping 0.7

String damping variation 0.15

Pluck damping 0.5

Pluck damping variation 0.40

String tension 0.0

Stereo spread 0.1

String damping calculation Magic

Body resonation None

Simple Precache time 11.3 seconds

------
rsiqueira
The Wikipedia page about Karplus–Strong string synthesis says that there is a
refinement of the algorithm that could also be used to model acoustic waves in
tubes and on drum membranes. I wish a JavaScript MIDI player could use those
high quality audio synthesis techniques to play MIDI files.

------
stephenhess
Is there a professional VST version of this? Or something similar? Sounds
beautiful and love that it isn't sample based.

~~~
pierrec
There are lots of VSTs that do about the same thing. In the xoxos VST link
posted above, check out Pling. If you want the exact same algorithm in a VST
plugin, you could port it to Lua in my scriptable plugin. In fact someone made
a nice Karplus-Strong script for it a couple of days ago (I should be merging
it instead of making HN comments):

[https://github.com/pac-dev/protoplug/pull/8](https://github.com/pac-
dev/protoplug/pull/8)

------
keithwhor
Neat! I did this a couple of years ago. My version is here (choose "Acoustic
Guitar"). Your version is a little more automated in note production, but
definitely more robust and a bit catchier. ;)

[http://www.keithwhor.com/music/](http://www.keithwhor.com/music/)

------
Gravityloss
It's strange that it's so very far away from a real acoustic or electric
guitar sound. Seems something like a piano is a lot easier to simulate with a
few damped sine waves?

~~~
boomlinde
I think the main advantage of Karplus-Strong is that it is (or at least was)
cheaper and more simple to implement, than to for example an algorithm
generating a similar sound additively from sine waves. AFAIR the original
paper describes an implementation using an impulse, a simple circular delay
buffer and an averaging (addition + single right shift) filter.

Simple additive synthesis usually falls short for something like a piano, too,
since there are a lot of factors that influence the frequency content of a
single struck note. This was posted a while ago, describing Pianoteq's
approach to piano modeling:
[https://www.pianoteq.com/tutorials?play=modelling](https://www.pianoteq.com/tutorials?play=modelling)

~~~
Gravityloss
Yeah that's what I meant. The piano there sounds rather good. Yet guitar
simulations sound absolutely horrible. Or maybe it's because I don't actually
play the piano...

Edit: There's this too by the inimitable Iñigo Quilez:
[https://www.youtube.com/watch?v=ogFAHvYatWs](https://www.youtube.com/watch?v=ogFAHvYatWs)
And here's a tool which you can play with:
[http://www.iquilezles.org/apps/soundtoy/index.html](http://www.iquilezles.org/apps/soundtoy/index.html)

------
cf
I'm always fascinated by these simulated sounds. Is there any resource for
simulating other instruments like trumpets and drums?

~~~
peterfirefly
You can get a decent snare drum out of Karplus-Strong.

The description begins on p.46 of their article:

[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.144...](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.144.5585&rep=rep1&type=pdf)

It is really cheap to implement -- you can use a shift-xor random bit
generator to get the blend factor of ½ that they mention. The whole thing was
about 10 8086 instructions, as far as I recall (it's been twenty years since I
played with it).

Here's a paper with more extensions to the Karplus-Strong algorithm (+
analyses thereof):

[http://www.music.mcgill.ca/~gary/courses/papers/Jaffe-
Extens...](http://www.music.mcgill.ca/~gary/courses/papers/Jaffe-Extensions-
CMJ-1983.pdf)

Here's an anthology from 1992, which is where I learned about Karplus-Strong:

[http://www.amazon.co.uk/Music-Machine-Selected-Readings-
Comp...](http://www.amazon.co.uk/Music-Machine-Selected-Readings-
Computer/dp/0262680785/ref=sr_1_1?ie=UTF8&qid=1433163352&sr=8-1&keywords=the+music+machine+curtis+roads)

------
sova
freakin' amazing! thank you so much for sharing! :D

------
egonelbre
Nice work :)

