CS 160, Spring 2014
Programming Assignment P8
Virtual Guitar Hero

Programming due Monday, May 5 at 11:59pm, no late period.


This programming assignment has one objective:
  1. Having fun with music on a computer

Description

The purpose of the assignment is to write a Java class that 1) uses the GuitarString class to play music using the computer keyboard, and 2) uses the GuitarString class to play music from a file.

Instructions

NOTE: This assignment is complex, make sure and read the instructions carefully!

DISCLAIMER: The music library has been tested on Linux, PC, and Mac, but we cannot guarantee it will work on all systems. Please plan on using a Linux system in our lab if you have problems on your PC or Mac. Please be courteous of your neighbor when playing music in the lab by using headphones. See the instructor for headphones if you do not own your own. Even with headphones, please keep the volume as quiet as possible. To control the volume on a Linux system, find one of the mixer applications or type 'pavucontrol' into a shell window. Make sure you are adjusting the output volume, not the input.

Part One

Create a project called P8. Setup the project as follows:
  1. Copy the code from P8.java.
  2. Remove the acknowledgements section and put in your own name, date, etc.
  3. Import GuitarHero.jar into the P8 project.
  4. Use "Build Path"->"Add External Archives", to do the previous step.
  5. Copy the data file music.txt, and put it in your project (not in src or bin).
  6. Copy the data file scale.txt, and put it in your project (not in src or bin).
Run P8.java and verify that typing 'A' or 'C' (upper or lower case) will play the notes A and C on the computer. You can also hold both keys down for a chord. If this does not work, get help from someone before moving on the next part of the assignment.

Part Two

Extend the program to handle an entire octave of keys, as follows:
  1. In the area for class instance variables:

    "C","C#","D","D#","E","F","F#","G","G#","A","A#","B","H"
    
  2. In the main method:

            0 - middle C ("C")
            1 - C# above middle C ("C#")
            2 - D above middle C ("D")
            3 - D# above middle C ("D#")
            4 - E above middle C ("E")
            5 - F above middle C ("F")
            6 - F# above middle C ("F#")
            7 - G above middle C ("G")
            8 - G# above middle C ("G#")
            9 - A above middle C ("A")
            10 - A# above middle C ("A#")
            11 - B above middle C ("B")
            12 - high C ("H")
        
    The above notes represent one octave of a piano from middle C to high C.
    
  3. Add code to pluckGuitar to handle all 13 notes.
  4. There are at least three ways to do this:
  5. The next two items require a loop:
  6. Modify the code in playGuitar to combine the samples for all guitar strings in the array.
  7. Modify the code in playGuitar to advance the simulation for all guitar strings in the array.
  8. The methods for GuitarString that you call (do not write these!) are as follows:
    public GuitarString (double frequency); // Constructor
    public void pluck();                    // Pluck string
    public void tic();                      // Advance simulation
    public double sample();                 // Get next sample
    
You should now be able to play all 8 notes A-H on the keyboard, corresponding to one octave on a piano (but no sharps).

Part Three

Implement file playback using the following steps:
  1. Write the following code in the playFile method.
  2. In a try/catch block, create a scanner for the file passed in.
  3. Create a loop to read tokens one at a time.
  4. Each token should be a single note matching the strings in the note array.
  5. Call pluckGuitar with each token that you read from the file.
  6. Call playGuitar 11,025 times, which plays the note for ~0.25 seconds.
  7. Add the command line argument "music.txt" to Run Configuration
You should now be able to play the contents of "music.txt" or "scale.txt", after you have quit playing the keyboard by entering a 'X'. Sharps should work correctly.

Part Four

Improve the keyboard playback to allow mapping the entire set of notes to the keyboard in an arrangement that resembles a piano keyboard:
  1. Declare the following string in playKeyboard:
        String keyboard = "Q2W3ER5T6Y7UI";
        
  2. This string corresponds to the notes on a piano, as shown below:
                Q maps to "C"
                2 maps to "C#"
                W maps to "D"
                3 maps to "D#"
                E maps to "E"
                R maps to "F"
                5 maps to "F#"
                T maps to "G"
                6 maps to "G#"
                Y maps to "A"
                7 maps to "A#"
                U maps to "B"
                I maps to "H"
            
  3. Translate the key pressed to the index within the keyboard string (1 line of java).
  4. Use the index to lookup the string in the notes array (1 line of java).
  5. Call pluckGuitar with the note corresponding to the key pressed.
  6. No other changes are necessary
You should now be able to play the keyboard like a piano, using adjacent keys instead of note names. This allows you to play notes with sharps.

Part Five

Enhance the keyboard playback to write notes to an output file, thereby allowing you to capture songs that you play on the keyboard. Here is a description of the steps to do this:
  1. Add a parameter to the playKeyboard method for the output filename.
  2. Call playKeyboard with args[1].
  3. Modify the Run Configurations to add the output file name as an argument, e.g. "output.txt".
  4. At the top of playKeyboard, open a PrintWriter for the file, in a try/catch.
  5. Each time you call pluckGuitar to play a note, also write the String to the file.
  6. Write the notes to the file, not the keyboard characters, i.e. "A", "A#", "B", etc., not "Q", "2", "W", etc.
  7. Don't forget to catch exceptions and close the PrintWriter.

Testing

NOTE: We will test your code with a different music file, so your methods should work on arbitrary music files.

Grading Rules

Please follow the usual rules for submitting Java programs.

Grading Criteria

Submission

Submit your modified source file named P8.java to the the Checkin tab on the course web site.

ACKNOWLEDGEMENTS:

This assignment was heavily copied from the wonderfully competent faculty at Princeton University. Permission to use the assignment was granted via email by Dr. Robert Sedgewick. We gratefully acknowledge the material from Princeton University used in this assignment! The original Princeton web site is: here. This assignment was developed by Andrew Appel, Jeff Bernstein, Maia Ginsburg, Ken Steiglitz, Ge Wang, and Kevin Wayne. Copyright © 2005

© 2014 CS160 Colorado State University. All Rights Reserved.