COS 126 Plucking a Guitar String |
Programming Assignment |
Write a program to simulate plucking a guitar string using the Karplus–Strong algorithm. This algorithm played a seminal role in the emergence of physically modeled sound synthesis (where a physical description of a musical instrument is used to synthesize sound electronically).
Digital audio. Before reading this assignment, review the material in the textbook on digital audio (pp. 155–159, 211–215).
Simulate the plucking of a guitar string. 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. The integer n equals the sampling rate (44,100 Hz) divided by the desired fundamental frequency, rounded up to the nearest integer.
Why it works? The two primary components that make the Karplus–Strong algorithm work are the ring buffer feedback mechanism and the averaging operation.
Ring buffer. Your first task is to create a data type to model the ring buffer. Write a class named RingBuffer that implements the following API:
public class RingBuffer { public RingBuffer(int capacity) // creates an empty ring buffer with the specified capacity public int capacity() // returns the capacity of this ring buffer public int size() // returns the number of items currently in this ring buffer public boolean isEmpty() // is this ring buffer empty (size equals zero)? public boolean isFull() // is this ring buffer full (size equals capacity)? public void enqueue(double x) // adds item x to the end of this ring buffer public double dequeue() // deletes and returns the item at the front of this ring buffer public double peek() // returns the item at the front of this ring buffer public static void main(String[] args) // unit tests the constructor and all methods in this class }
To insert an item, put it at index last and increment last. To remove an item, take it from index first and increment first. When either index equals capacity, make it wrap-around by changing the index to 0. To determine the size of the ring buffer (and whether it is full or empty), you will also need a third integer instance variable size that stores the number of items currently in the ring buffer.
Guitar string. Next, create a data type to model a vibrating guitar string. Write a class named GuitarString that implements the following API:
public class GuitarString { public GuitarString(double frequency) // creates a guitar string of the specified frequency, using a sampling rate of 44,100 public GuitarString(double[] init) // creates a guitar string whose length and initial values are given by the specified array public int length() // returns the number of samples in the ring buffer public void pluck() // plucks this guitar string (by replacing the ring buffer with white noise) public void tic() // advances the Karplus-Strong simulation one time step public double sample() // returns the current sample public static void main(String[] args) // unit tests both constructors and all instance methods in this class }
Interactive guitar player. GuitarHeroLite.java is a sample GuitarString client that plays the guitar in real time, using the keyboard to input notes. When the user types the lowercase letter 'a' or 'c', the program plucks the corresponding string. Since the combined result of several sound waves is the superposition of the individual sound waves, it plays the sum of the two string samples.
Write a program GuitarHero that is similar to GuitarHeroLite, but supports a total of 37 notes on the chromatic scale from 110 Hz to 880 Hz. Use the following 37 keys to represent the keyboard, from lowest note to highest note:
public class GuitarHeroLite { public static void main(String[] args) { // create two guitar strings, for concert A and concert C double CONCERT_A = 440.0; double CONCERT_C = CONCERT_A * Math.pow(2, 3.0/12.0); GuitarString stringA = new GuitarString(CONCERT_A); GuitarString stringC = new GuitarString(CONCERT_C); while (true) { // check if the user has typed a key; if so, process it if (StdDraw.hasNextKeyTyped()) { char key = StdDraw.nextKeyTyped(); if (key == 'a') stringA.pluck(); else if (key == 'c') stringC.pluck(); } // compute the superposition of samples double sample = stringA.sample() + stringC.sample(); // play the sample on standard audio StdAudio.play(sample); // advance the simulation of each guitar string by one step stringA.tic(); stringC.tic(); } } }
This keyboard arrangement imitates a piano keyboard: The "white keys" are on the qwerty and zxcv rows and the "black keys" on the 12345 and asdf rows of the keyboard.String keyboard = "q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' ";
The ith character of the string keyboard
corresponds to a frequency of 440 × 2(i − 24) /
12,
so that the character 'q' is 110 Hz, 'i' is 220 Hz,
'v' is 440 Hz, and ' ' is 880 Hz.
Don't even think of including 37 individual GuitarString variables
or a 37-way if statement!
Instead, create and initialize an array of 37 GuitarString objects
and use keyboard.indexOf(key) to figure out which key was typed.
If a keystroke does not correspond to one of the 37 possible notes, ignore it.
Files for this assignment. The file guitar.zip contains GuitarHeroLite.java; optional API templates for RingBuffer.java and GuitarString; and this week's readme.txt template.
Submission. Submit RingBuffer.java, GuitarString.java, GuitarHero.java, and a completed readme.txt. If your partner is submitting, you should submit only a completed partner readme.txt.
Extra credit. Write a program AutoGuitar.java that will automatically play music using GuitarString objects. A few ground rules:
% java-introcs AutoGuitarYour program may not accept command-line arguments. Your program may not use standard input, standard output, or standard drawing. You may, however, submit an accompanying .txt file and read it using the In data type.
You may create chords, repetition, and phrase structure using loops, conditionals, arrays, and functions. Also, feel free to incorporate randomness. You may also create a new music instrument by modifying the Karplus–Strong algorithm; consider changing the excitation of the string (from white noise to something more structured) or changing the averaging formula (from the average of the first two samples to a more complicated rule) or anything else you might imagine. See the checklist for some concrete ideas.
You may submit additional .java or .txt files to support the extra credit, (but do not modify RingBuffer.java, GuitarString.java, or GuitarHero.java). If you are working with a partner, you can do this part together or solo, but you must decide before you begin.
This assignment was developed by Andrew Appel, Jeff Bernstein, Maia Ginsburg, Ken Steiglitz, Ge Wang, and Kevin Wayne.