<< Chapter < Page | Chapter >> Page > |
Listing 16 . Beginning of the getPiano method. |
---|
Hashtable getPiano(){Hashtable piano = new Hashtable();double factor = 1.05946309436;//12th root of 2//Define the name and frequency of the first note.
double freq = 110;//Frequency of A2 at 110 Hz. Start with this.String note = "A2";//Name of note at 110 Hz. Start with this.//Used to parse a note into 3 single-character substrings such
// as C, 5, and #.String sub1 = null;
String sub2 = null;String sub3 = null; |
The first half of the code in Listing 16 is completely straightforward.
The number in the center of the note names (such as A3#) changes at C and not at A. Also, the # character appears on some notes andnot on all notes. As a result, it is difficult to use a loop structure to deal with the names of the notes.
I will decompose each note into two or three substrings for processing. In other words, the note name "A2" will be decomposed into an"A" and a "2". The note name "C5#" will be decomposed into a "C", a "5", and a "#". The last three variables that are declared in Listing 16 will be used to save those strings.
The process of populating the Hashtable object with note names and frequencies is controlled by a for loop that begins in Listing 17 .
Listing 17 stores the name and frequency for the current note and computes the frequency of the next note. ( The initial name and frequency for the current note were defined in Listing 16 .) The frequency of the next note is computed by multiplying the frequency of the current note bythe twelfth root of two, which is stored in the variable named factor.
Listing 17 . Store the current note and compute the frequency of the next note. |
---|
for(int cnt = 0;cnt<61;cnt++){
//This loop counts from A1 through A7 at 3520 Hz.
//Store the name and the frequency of the note in the next element in// the hashtable.
piano.put(note,freq);//Compute the frequency of the next notefreq *= factor; |
While it may be possible to write a loop structure to deal with note names, it was not obvious to me how to do that. Therefore, I took a brute forceapproach and wrote a long and tedious block of logic code to do the job.
Listing 18 shows the logic used to decompose the name of the current note into two or three single-character strings and to use those strings to constructthe name of the next note. That note will be the current note at the top of the next iteration and will be saved, along with the frequency in the Hashtable object. I will leave it as an exercise for the student to understand and possibly improve on this logic.
Listing 18 . Construct the name of the next note. |
---|
//Use logic to construct the name of the next note in a sequence such
// as A2,A2#,B2,C3,C3#,D3,D3#,E3,F3,F3#,G3,G3#,A3, etc.//Begin by parsing the current note name into three single-character
// substrings, the third of which may be null.if(note.length() == 3){
sub1 = note.substring(0,1);sub2 = note.substring(1,2);
sub3 = note.substring(2,3);}else{
sub1 = note.substring(0,1);sub2 = note.substring(1,2);
sub3 = null;}//end if//Use the three substrings of the current note name to determine the
// name of the next note. This is long and tedious but it works.if((sub1.equals("A"))&&(sub3 == null)){
sub1 = "A";sub3 = "#";
}else if((sub1.equals("A"))&&(sub3.equals("#"))){
sub1 = "B";sub3 = null;
}else if((sub1.equals("B"))){sub1 = "C";
sub3 = null;//Increment the number
sub2 = "" + (1 + Integer.parseInt(sub2));}else if((sub1.equals("C"))&&(sub3 == null)){
sub1 = "C";sub3 = "#";
}else if((sub1.equals("C"))&&(sub3.equals("#"))){
sub1 = "D";sub3 = null;
}else if((sub1.equals("D"))&&(sub3 == null)){
sub1 = "D";sub3 = "#";
}else if((sub1.equals("D"))&&(sub3.equals("#"))){
sub1 = "E";sub3 = null;
}else if((sub1.equals("E"))){sub1 = "F";
sub3 = null;}else if((sub1.equals("F"))&&(sub3 == null)){
sub1 = "F";sub3 = "#";
}else if((sub1.equals("F"))&&(sub3.equals("#"))){
sub1 = "G";sub3 = null;
}else if((sub1.equals("G"))&&(sub3 == null)){
sub1 = "G";sub3 = "#";
}else if((sub1.equals("G"))&&(sub3.equals("#"))){
sub1 = "A";sub3 = null;
}else{System.out.println("Can't reach this point.");
}//end else//Construct the next note from the updated substrings.
if(sub3 == null){note = sub1 + sub2;
}else{note = sub1 + sub2 + sub3;
}//end if}//end for loop
return piano;}//end getPiano
}//end class PlayerPiano01 |
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?