<< Chapter < Page | Chapter >> Page > |
The melody to be played is defined in one and optionally two text files. The required text file defines the notes and the duration of those notes on thetreble clef. The optional text file defines the notes and the durations of those notes on the bass clef. Those text files must exist in a foldernamed Music which is a child or immediate subfolder of the folder that contains the compiled program code.
The text file for the treble clef of a stereo rendition of Greensleeves is shown in Listing 29 . The text file for the bass clef of that same melody is shown in Listing 30 . I will explain the contents of those files later. Click here to download a zip file containing those two text files and other files as well, including Windows batch files.
The melody can be played immediately or can be saved in an audio file of type AU for playback later. Listing 31 contains a Windows batch file that does both. I will explain the contents of this batch file later. This batch file is alsocontained in the zip file mentioned above . You should be able to play the audio file with any standard media player that can handle the AU file type.
The beginning of the class named MusicComposer09 is shown in Listing 1 . You have seen code like this in several previous modules, so no explanation beyond the embedded comments should be needed.
Listing 1 . Beginning of the class named MusicComposer09. |
---|
public class MusicComposer09{
//Instantiate an object containing audio format parameters with predefined
// values. They may be modified by the signal generator at runtime. Values// allowed by Java SDK 1.4.1 are shown in comments in the class definition.
AudioFormatParameters01 audioParams = new AudioFormatParameters01();//A buffer to hold the data for the melody that will be played or filed.byte[] melody;//A place to store the incoming args array.String[] args; |
The main method is shown in Listing 2 .
Listing 2 . The main method. |
---|
public static void main(String[] args){/*Command-line parameters
0: "play" to play immediately, fileName to create an AU file. Note thatthe output filename cannot be named play.au.
1: Beats per second.2: Name of file containing treble clef data (required).
3: Name of file containing bass clef data (optional).*///Instantiate a new object of this class.
new MusicComposer09(args);}//end main |
The only thing that is new in Listing 2 is the description of the command-line parameters. The only thing new there is the second parameterreferred to as "Beats per second." As you will see when we get into the code, the value of this parameter determines how fast the melody will beplayed. Low values cause the melody to be played slowly. Higher values cause the melody to be played more rapidly.
The beginning of the constructor for the MusicComposer09 class is shown in Listing 3 .
Listing 3 . Beginning of the constructor for the MusicComposer09 class. |
---|
public MusicComposer09(String[] args){//constructor//Save the args array.
this.args = args;if(args.length == 0){this.args = new String[4];this.args[0] = "play";//Play the melody immediatelythis.args[1] = "16";//beats per secondthis.args[2] = "GreensleevesTreble.txt";this.args[3] = "GreensleevesBass.txt";}//end if |
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?