You are on page 1of 2

// Emily Zhang

// 20200416
// CSE 143
// Chandan Hegde
// Assessment #2: Guitar Hero pt. 2
// A Guitar37 class, a model of a guitar with 37 strings that can be played with the keyboard

public class Guitar37 implements Guitar {


public static final String KEYBOARD =
"q2we4r5ty7u8i9op-[=zxdcfvgbnjmk,.;/' "; // keyboard layout
public static final int STRING_NUM = 37; // the number of strings

private GuitarString[] strings = new GuitarString[STRING_NUM];


private int time;

// constructs a new Guitar with 37 strings


// each string is assigned a different frequency
// the strings are stored in an array of GuitarStrings
// time, the number of times tic has been called, is set to 0
public Guitar37(){
for (int i = 0; i < strings.length; i++){
double exp = (i - 24) / 12.0;
double freq = 440.0 * Math.pow(2.0, exp);
strings[i] = new GuitarString(freq);
}
time = 0;
}

// plays the note at the given pitch


// pre: 0 <= pitch < STRING_NUM
// ignores notes that cannot be played
public void playNote(int pitch){
pitch += 24;
if (pitch >= 0 && pitch < STRING_NUM){
strings[pitch].pluck();
}
}

// returns whether or not the given key can be played


public boolean hasString(char key){
return (KEYBOARD.indexOf(key) > -1);
}
// plays the string corresponding to the given key
// pre: the given key can be played
// throws an IllegalArgumentException
public void pluck(char key){
if (!hasString(key)){
throw new IllegalArgumentException("bad key");
}
strings[KEYBOARD.indexOf(key)].pluck();
}

// returns the sum of the samples from all strings


public double sample(){
double total = 0.0;
for (int i = 0; i < strings.length; i++){
total += strings[i].sample();
}
return total;
}

// advances time forward one tic, applying tic to all the strings
// increases time by 1
// post: Karplus-Strong update applied once to each string
public void tic(){
for (int i = 0; i < strings.length; i++){
strings[i].tic();
}
time++;
}

// returns the number of times tic has been called


public int time(){
return time;
}
}

You might also like