This assignment is inispired by a Nifty Assignment introduced by Kevin Wayne (Princeton University) and refined by Stuart Reges (University of Washington).
It is to be completed individually by each student, with no help from anyone other than the instructor.
When a piano wire is struck, the wire vibrates and creates sound. The vibration can be measured by sampling the displacement of the wire at equally spaced points. These displacement measurements can be stored digitally, say in a list or queue structure, then used to recreate the sound wave over a speaker.
For this assignment, you will store the displacement values for a piano wire in a queue structure. When at rest, the wire can contain energy at any frequency. This is modeled by assigning the queue to contain random real numbers between -1/2 and +1/2. After the wire is struck, it vibrates causing a displacement that spreads wave-like over time. The Karplus-Strong algorithm simulates this vibration using a fairly simple update process: it repeatedly adds to the end of the queue the average of the first two samples, scaled by an energy decay factor of 0.996, and removes the first sample from the queue.
This simple algorithm provides an effective model of the wire vibration due to two features: the queue feedback mechanism and the averaging operation.
For the first part of this assignment, you are to implement a class that models a single piano wire. Your
PianoWire class should provide the following constructor and methods:
public PianoWire(int wireNum)
SAMPLE_RATE * 2(22 - wireNum)/12 / 440
Here, SAMPLE_RATE is a constant defined in the StdAudio class, which was developed at Princeton.
public void strike()
public double sample()
You should test your class thoroughly by creating an object with a short queue and displaying the results of each update. Once you are convinced that the class is behaving as desired, you may integrate it with the provided Piano class. This class implements a simple piano, with keyboard keys mapping to individual wires. The routines for rendering the vibrations using your computer's sound card are contained in the utility class StdAudio. The routines for processing keyboard events are contained in the utility class StdDraw, which was also developed at Princeton.
Currently, the piano can only play one octave, going from middle C to high C. You are to modify your
PianoWire classes so that three octaves are playable. If the user holds the Control key while hitting a key, the corresponding note in the lower octave should be played. For example, Control-s should play low C (one octave below middle C). Likewise, holding the Shift key should play a note in the upper octave. Note that Control-l and 's' should play the same note (middle C), as should 'l' and 'S' (high C).
As part of your modifications, you should add text to the display window that informs the user of the functionality of the Shift and Control keys.