<< Chapter < Page Chapter >> Page >

..

Listing 9 . 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 10 . The class named MusicComposer05.
/*File MusicComposer05.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 square wave at 1000 cycles per second.SquareWave 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 MusicComposer05{//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 MusicComposer05(args);}//end main //-------------------------------------------------------------------------//public MusicComposer05(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 square wave.SquareWave squareWave = new SquareWave(audioParams,this.args,melody); melody = squareWave.getMelody();//Play or file the audio data new AudioPlayOrFile01(audioParams,melody,this.args[0]).playOrFileData(); }//end constructor//-------------------------------------------------------------------------// }//end class MusicComposer05.java//===========================================================================//

..

Listing 11 . The class named SquareWave.
/*File SquareWave.java Copyright 2014, R.G.BaldwinRevised 08/22/14 This class can be used to create a square wave with 1000 cycles per second******************************************************************************/ import java.io.*;import java.nio.*; import java.util.*;public class SquareWave extends AudioSignalGenerator02{public SquareWave(AudioFormatParameters01 audioParams, String[]args, byte[]melody){ super(audioParams,args,melody);}//end constructor //-------------------------------------------------------------------------////This method returns an array containing three seconds of a square wave// at 1000 cycles per second. byte[]getMelody(){ //Recall that the default for channels is 1 for mono.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;//Create an output data array sufficient to contain the melody // 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); int val = 8000;//amplitude value of square wavebyte byteLow = 0; byte byteHigh = 0;for(int cnt = 0;cnt<melody.length; cnt+=2){if(cnt % 8 == 0){ //Change signval = -val; }//end if//Create two bytes that contain a 16-bit representation of the value.byteLow = (byte)val;//discard all but 8 lsb byteHigh = (byte)(val>>8);//shift right 8 and discard all but 8 lsb//Deposit the bytes into the array melody[cnt]= byteHigh; melody[cnt + 1]= byteLow; }//end for loopreturn melody; }//end method getMelody//-------------------------------------------------------------------------// }//end class SquareWave//===========================================================================//

-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