<< Chapter < Page Chapter >> Page >

..

Listing 7 . The class named AudioSignalGenerator02.
/*File AudioSignalGenerator02.java Copyright 2014, R.G.BaldwinRevised 08/19/14 This is an abstract class that serves as the base class for several otherclasses that can be used to create melodies of different types. ******************************************************************************/import java.io.*; import java.nio.*;import java.util.*; public abstract class AudioSignalGenerator02{//Note: This class can only be used to generate signed 16-bit data.ByteBuffer byteBuffer; String[]args; byte[]melody; AudioFormatParameters01 audioParams;//-------------------------------------------------------------------------////Constructor public AudioSignalGenerator02(AudioFormatParameters01 audioParams,String[] args,byte[] melody){this.audioParams = audioParams; this.args = args;this.melody = melody; }//end constructor//-------------------------------------------------------------------------// //The following abstract method must be overridden in a subclass for this// class to be of any value. abstract byte[]getMelody(); }//end AudioSignalGenerator02//===========================================================================//

..

Listing 8 . The class named MusicComposer06.
/*File MusicComposer06.java Copyright 2014, R.G.BaldwinRevised 08/22/14 This program works in conjunction with the following classes to create and playthree seconds of monaural audio based on a pure sinusoidal function at 1000 cycles per second.ToneMono AudioSignalGenerator02AudioPlayOrFile01 AudioFormatParameters01The sound can be played immediately or can be saved in an audio file of type AU for playback later. You should be able to play the audio file with anystandard media player that can handle the AU file type Tested using JDK 1.8 under Win 7.******************************************************************************/ public class MusicComposer06{//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 MusicComposer06(args);}//end main //-------------------------------------------------------------------------//public MusicComposer06(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 //Get a populated array containing audio data for the pure sinusoidal tone.ToneMono toneMono = new ToneMono(audioParams,this.args,melody); melody = toneMono.getMelody();//Play or file the audio data new AudioPlayOrFile01(audioParams,melody,this.args[0]).playOrFileData(); }//end constructor//-------------------------------------------------------------------------// }//end class MusicComposer06.java//===========================================================================//

..

Listing 9 . The class named ToneMono.
/*File ToneMono.java Copyright 2014, R.G.BaldwinRevised 08/22/14 This class that can be used to create a melody consisting of a single puresinusoidal tone at 1000 Hz. The class introduces the use of ByteBuffer.******************************************************************************/ import java.io.*;import java.nio.*; import java.util.*;public class ToneMono extends AudioSignalGenerator02{public ToneMono(AudioFormatParameters01 audioParams, String[]args, byte[]melody){ super(audioParams,args,melody);}//end constructor //-------------------------------------------------------------------------////This method generates a three-second pure sinusoidal tone. A 1000 Hz tone// is emitted with equal amplitude from the left and right speakers. It is // interesting to compare this sound with the sound of a square wave with// the same fundamental frequency. byte[]getMelody(){ //Recall that the default is channels=1 for monaural.System.out.println("audioParams.channels = " + audioParams.channels);//Each channel requires two 8-bit bytes per 16-bit sample. int bytesPerSampPerChan = 2;//Override the default sample rate. Allowable sample rates are 8000,11025,// 16000,22050,44100 samples per second. audioParams.sampleRate = 8000.0F;// Set the length of the melody in secondsdouble lengthInSeconds = 3.0;//Set the frequency of the tone. double freq = 1000.0;//Create an output data array sufficient to contain the tone// at "sampleRate" samples per second, "bytesPerSampPerChan" bytes per // sample per channel and "channels" channels.melody = new byte[(int)(lengthInSeconds*audioParams.sampleRate* bytesPerSampPerChan*audioParams.channels)]; System.out.println("melody.length = " + melody.length);//Prepare a ByteBuffer for usebyteBuffer = ByteBuffer.wrap(melody); //Compute the number of audio samples in the melody.int sampLength = (int)(lengthInSeconds*audioParams.sampleRate);//Compute the audio sample values and deposit them in the output array. for(int cnt = 0; cnt<sampLength; cnt++){ //Compute the time in seconds for this sample.double time = cnt/audioParams.sampleRate; //Deposit audio data for both channels in mono.byteBuffer.putShort((short)(8000*Math.sin(2*Math.PI*freq*time))); }//end for loopreturn melody;}//end method getMelody //-------------------------------------------------------------------------//}//end class ToneMono //===========================================================================//

-end-

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