Guitar Heroine

Kevin Wayne
Princeton University

The assignment, as given to students.

Simulate the plucking of a guitar string with the Karplus-Strong algorithm and transform your laptop into a musical instrument. The Karplus-Strong models the vibration of a guitar string with a remarkably simple process: Maintain a buffer of displacements; then, repeatedly delete the first displacement from the front of the buffer and append the average of the first two displacements, scaled by an energy dissipation factor, to the end of the buffer. The size of the buffer determines the frequency of the guitar string.

  Karplus-Strong update

To represent a guitar, you will create many guitar strings (of different frequencies), process keystrokes to identify which strings get plucked, superpose the resulting sound waves, and sonify the results. The key data structure needed to perform the Karplus-String simulation efficiently is a ring buffer (bounded queue).

Your browser can not display this movie.
Be sure that Javascript is enabled and that you have Flash 9.0.124 or better.

Both the students and the teaching staff love it!



Simulate the plucking of a guitar string with the Karplus-Strong algorithm and transform your laptop into a musical instrument. The assignment illustrates a basic programming construct (a data type that stores a sequence of values), takes advantage of a fundamental computer science concept (digital audio represents sound as a sequence of displacements), and shows the role of computation in an important application (physically-modeled sound synthesis).


Queues, real-time audio, and objects.


Late CS 1 or early CS 2. Especially appealing to non-majors. The version written here was used in the second half of a CS 1 course.


This is an intermediate assignment, taking 1-2 weeks. The amount of code needed is remarkably small.


Relies on the standard libraries from Introduction to Programming in Java for real-time audio and GUI interaction. We have also implemented a version of these libraries for Python (but some performance tricks are needed).


The code required involves little physics or math. However, you need to have explained that (our representation of) digital audio is a sequence of real numbers between -1 and +1, sampled 44,100 times per second.


Students are amazed that they can produce a musical instrument with such a small amount of code. There are endless opportunities for creativity, enrichment, and inspiration: perform a musical piece on your laptop, visualize the sound wave as the user plays the keyboard guitar, record the audio and save to a file, or modify the Karplus-Strong update rule to design a new instrument.


Requires a real-time audio library. Harder to auto-grade the interactive GUI component of the assignment.


In a CS 2 course, you could put more emphasis on the analysis of algorithms. Even though N is small, performance matters because you must do the computation 44,100 times per second, for each string. To put less emphasis on data structures, you could have students use a java.util.LinkedList instead of implementing a ring buffer.

Extra info about this assignment: