Homework 8, Part A: Guitar

Learning Goals

Exercise: While My Guitar Gently Weeps

In this exercise, you will learn how to simulate the plucking of a guitar string with the Karplus-Strong algorithm. Play the video below to see a visualization of the algorithm. If your browser won’t play the video below, you can right-click on it and save it to your Desktop to play it from there.

When a guitar string is plucked, the string vibrates and creates sound. The length of the string determines its fundamental frequency of vibration. We model a guitar string by sampling its displacement (a real number between -1/2 and +1/2) at N equally spaced points (in time), where N equals the sampling rate (44,100) divided by the fundamental frequency (rounding the quotient up to the nearest integer).

Plucking the string. The excitation of the string contains energy at any frequency. We simulate the excitation with white noise: set each of the N displacements to a random real number between -1/2 and +1/2.

The resulting vibrations. After the string is plucked, the string vibrates. The pluck causes a displacement which spreads wave-like over time. The Karplus-Strong algorithm simulates this vibration by maintaining a bounded-queue of the N samples: the algorithm repeatedly dequeues the first sample from the bounded queue and enqueues the average of the dequeued sample and the front sample in the queue, scaled by an energy decay factor of 0.994.

Task 0

Download this starting code that will allow you to complete the tasks below.

Task 1

Write a BoundedQueue.java class that implements a bounded queue ADT. A bounded queue is a queue with a maximum capacity: no elements can be enqueued when the queue is full to its capacity. The BoundedQueue class should inherit from the javafoundations.CircularArrayQueue class, given in the starting code.

Your BoundedQueue.java file should contain implementations for the following methods:

You should not add any more instance methods to this class implementation. But, of course, you should be providing evidence of testing your implementation in the main().

Make sure you test this class before continuing to the next task.

Task 2

Write a GuitarString class that models a vibrating guitar string according to the following contract:

Task 3

Now you should be ready to test your code from the previous tasks. Compile and run the provided GuitarHeroine application. If you have successfully completed the previous tasks, then when you run the application, a window should appear as follows:

Now, you can make sweet music. By pressing any of the keys on your computer keyboard corresponding to the notes as illustrated in the piano keyboard image, you can simulate plucking a guitar string for that note (make sure that your computer’s sound is not muted).

What to submit

In Gradescope submit the following files:

  1. The BoundedQueue.java file
  2. Your testing transcript (BoundedQueue.txt) for BoundedQueue class
  3. The GuitarString.java file


Homework 8, Part B: Queues

Learning Goals

Two Implementations of the Queue Interface

How to submit your Work

When done, submit the LinkedQueue.java, CircularArrayQueue.java and Qtest.java files along with the QtestTesting.txt transcript of your solutions. Include the PDF or PNG of your design. And remember to edit the @author and @version fields of your javadoc!


Submission Checklist