Java GuitarHero

GuitarHero is a virtual guitar that you play using your keyboard.

By pressing certain keys within the StdDraw window, the user can tell the program to generate tones of a given pitch.

The difference between this program and the PlayThatTune program provided by the book authors is that this one implements an the Karplus-Strong Algorithm.

This algorithm allows the tones to have a "decay" or fading effect, producing more realistic sounding tones.

The Karplus-Strong Algorithm played an important role in the initial development of digital insturments. While most digital insturments that are designed to replicate real sounds now use samples (short recordings of the real insturment), this algorithm still plays an important role in the various genres of electronic music.

Shown here is how the keys on the computer keyboard correspond to those on a piano in this program:

How it works

The sounds you hear the program produce are mathematically generated. They are not samples. This means that, using the classes in this program, one could produce tones of any pitch requested.

There are four major steps in the process of generating each particular tone:
  1. Create a buffer with the number of samples per second divided by the frequency of the note being played.
  2. Fill the buffer whith "white noise" (in this case, a random value between 0 and 1).
  3. Shift the frequency of the generated white noise to the proper value for the note.
  4. Apply the Karplus-Strong algorithm to add decay to the note. This keeps the tone from going on forever.

An important difference to note is that in contrast to PlayThatTune, this program uses modified white noise, rather than a pure sine wave. This creates a more realistic sounding tone.

Improvements

Given more time, there are a few things in this program that could be improved:

Updates

27 Feb 2017 Reduced the download files to just two options: the binary as a java archive or the source. I decided there was really no point in having JARs with the source code in them anyways.
27 Feb 2017 I have corrected the problem with the guitar sounding too harsh, and now have achieved a much more realistic sound. The issue was a problem in my implementaiton of the Karplus-Strong algorithm, my code was removing the first and second samples from the buffer when calculating the new sample to add, when really only one should be removed and the second just read without removing. The fix was in the tic() method in GuitarString.java.
19 Oct 2015 Initial Release

Download

GuitarHero.jar Executable JAR file with compiled class files only.
GuitarHero.tar.gz Java source code of GuitarHero

To execute GuitarHero, you will also need to have stdlib.jar available in your classpath. You can download it from the textbook publisher's site: http://introcs.cs.princeton.edu/java/stdlib/stdlib.jar

Copyright © 2015 -2017 Joseph Tortorello. Contents of this webpage are all rights reserved. Linked source code is under the license distributed with the code contained in LICENSE.txt. Contact Me