<< Chapter < Page | Chapter >> Page > |
Listing 14 . Populate and return the melody array with stereo data. |
---|
//Use the shorter of the two lengths if they don't match. Note the use
// of a Java conditional operator to accomplish this.int lengthLimit = (trebleMelody.length<= bassMelody.length)
? trebleMelody.length : bassMelody.length;//Create an output array that is twice the length of the shorter of the
// bass or treble melodies to accommodate a stereo representation of the// melody. The bass melody will be put in the left-channel bytes in the
// array. Similarly, the treble melody will be put in the right-channel// bytes in the array.
melody = new byte[2*lengthLimit];
//Interlace the bass and treble melody data in the array so that the// bass will be played through the left speaker and the treble will be
// played through the right speaker.for(int cnt = 0;cnt<melody.length-4;cnt+=4){
melody[cnt]= bassMelody[cnt/2];melody[cnt+1] = bassMelody[1 + cnt/2];
melody[cnt+2]= trebleMelody[cnt/2];melody[cnt+3] = trebleMelody[1 + cnt/2];
}//end for loopreturn melody;//return the array and terminate the method
}//end if |
All of the code in Listing 17 is straightforward and shouldn't require an explanation beyond the embedded comments. The getMelody method terminates when the return statement is executed near the bottom of Listing 17 . At this point, the melody array has been populated with bass audio data for the leftspeaker and treble audio data for the right speaker. Control returns to Listing 4 , which causes the audio data in the melody array to be played or written into an output audio file of type AU.
The code in Listing 15 is executed only if the if statement at the beginning of Listing 13 returned false, meaning that the user did not specify a file containing bass note data on the command line. In that case, all of the code inthe if statement is skipped, passing control to the return statement in Listing 15.
Listing 15 . Populate and return the melody array with monaural data. |
---|
return trebleMelody;}//end method getMelody |
Listing 15 returns a reference to the monaural trebleMelody array containing only treble clef audio data.
Listing 15 also signals the end of the getMelody method.
At the end of Listing 12 , you saw a call to a method named makeMusic . I told you that the purpose of the makeMusic method is to transform the note data into an array of amplitude values that can be played orwritten into an audio output file. I also told you that the makeMusic method is the most important part of the entire program and that I would explainit in detail later.
The makeMusic method must be able to determine the frequency or pitch of every note that is specified in the text files containing the datafor a melody using musical annotation. That is no small task. I wrote a utility method named getPiano that creates a Hashtable object that ties note names to note frequencies and provides that Hashtable object to the makeMusic method. That method begins in Listing 16 .
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?