<< Chapter < Page Chapter >> Page >

.....

Listing 8 . The class named MusicComposer08.
/*File MusicComposer08.java Copyright 2014, R.G.BaldwinRevised 08/24/14 This program demonstrates late binding and runtime polymorphism using an arrayof the abstract type AudioSignalGenerator02 populated with references to objects of the following classes:AudioGraphSinusoidal AudioGraphSquareWaveFMSweep SquareWaveStereoPingpong ToneMonoTonesStereoWhiteNoise Each of the classes listed above extends the abstract class namedAudioSignalGenerator02 and overrides the abstract method named getMelody. After the array is populated, a random number generator is used to get arandom index into the array. The getMelody method is called on the reference pointed to by the random index. This causes the sound created by that audioobject to be played or filed. This is an example of late binding because the compiler cannot possibly know the value of the random index when the programis compiled. The sound can be played immediately or can be saved in an audio file oftype AU for playback later. You should be able to play the audio file with any standard media player that can handle the AU file type.The program also requires access to the following classes: AudioFormatParameters01AudioPlayOrFile01 AudioSignalGenerator02Tested using JDK 1.8 under Win 7. ******************************************************************************/import java.util.Random; public class MusicComposer08{//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 audio data that will be played or filed.byte[] melody;//A place to store the incoming args array.String[] args;//-------------------------------------------------------------------------// //Command-line parameter (only one parameter is needed)//If "play", the sound will be played immediately. Otherwise, the string will // be used as a filename for an audio file of type AU. In the latter case,// it must be a string that would be valid as a file name for the operating // system in use.public static void main(String[] args){//Instantiate a new object of this class. new MusicComposer08(args);}//end main //-------------------------------------------------------------------------//public MusicComposer08(String[]args){//constructor //Save the args array.this.args = args;//Create default args data if no args data is provided on the command line. if(args.length == 0){this.args = new String[1];this.args[0] = "play";//Play the melody immediately}//end if//Create an array capable of holding references to eight audio objects // instantiated from subclasses of the AudioSignalGenerator02 class.AudioSignalGenerator02[] audioObjects = new AudioSignalGenerator02[8];//Populate the array elements with references to eight different // audio objects.audioObjects[0] = new AudioGraphSinusoidal(audioParams,this.args,melody);audioObjects[1] = new AudioGraphSquareWave(audioParams,this.args,melody);audioObjects[2] = new FMSweep(audioParams,this.args,melody);audioObjects[3] = new SquareWave(audioParams,this.args,melody);audioObjects[4] = new StereoPingpong(audioParams,this.args,melody);audioObjects[5] = new ToneMono(audioParams,this.args,melody);audioObjects[6] = new TonesStereo(audioParams,this.args,melody);audioObjects[7] = new WhiteNoise(audioParams,this.args,melody);//Select an audio object at random and call the getMelody method on// the object. Random randomGenerator = new Random();int randomIndex = randomGenerator.nextInt(8); System.out.println("randomIndex = " + randomIndex);System.out.println(audioObjects[randomIndex].getClass());melody = audioObjects[randomIndex].getMelody(); //Play or file the audio datanew AudioPlayOrFile01(audioParams,melody,this.args[0]).playOrFileData();}//end constructor //-------------------------------------------------------------------------//}//end class MusicComposer08.java //===========================================================================//

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Accessible objected-oriented programming concepts for blind students using java. OpenStax CNX. Sep 01, 2014 Download for free at https://legacy.cnx.org/content/col11349/1.17
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?

Ask