<< Chapter < Page | Chapter >> Page > |
.....
Listing 13 . The class named WhiteNoise. |
---|
/*File WhiteNoise.java
Copyright 2014, R.G.BaldwinRevised 08/19/14
This is a simple class that can be used to create "white noise"******************************************************************************/
import java.io.*;import java.nio.*;
import java.util.*;public class WhiteNoise extends AudioSignalGenerator02{public WhiteNoise(AudioFormatParameters01 audioParams,
String[]args,
byte[]melody){
super(audioParams,args,melody);}//end constructor
//-------------------------------------------------------------------------////This method returns an array containing three seconds of monaural// white noise.
byte[]getMelody(){
//Set the audio parameters to monoaudioParams.channels = 1;//superfluous -- default value
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 seconds
double 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);Random generator = new Random(new Date().getTime());
for(int cnt = 0;cnt<melody.length; cnt++){
melody[cnt]= (byte)generator.nextInt();
}//end for loopreturn melody;
}//end method getMelody//-------------------------------------------------------------------------//
}//end class WhiteNoise//===========================================================================// |
.....
Listing 14 . The class named AudioFormatParameters01. |
---|
/*File AudioFormatParameters01.java
Copyright 2014, R.G.BaldwinRevised 08/16/14
******************************************************************************/public class AudioFormatParameters01{
//The following are audio format parameters used by the Java audio system.// They may be modified by the signal generator at runtime. Values allowed
// by Java SDK 1.4.1 are shown in comments.public float sampleRate = 16000.0F;
//Allowable 8000,11025,16000,22050,44100 samples per secondpublic int sampleSizeInBits = 16;
//Allowable 8,16public int channels = 1;
//Allowable 1 for mono and 2 for stereopublic boolean signed = true;
//Allowable true,falsepublic boolean bigEndian = true;
//Allowable true,false}//end class AudioFormatParameters01
//===========================================================================// |
.....
Listing 15 . The class named AudioPlayOrFile01. |
---|
/*File AudioPlayOrFile01.java
Copyright 2014, R.G.BaldwinRevised 08/16/14
******************************************************************************/import javax.sound.sampled.*;
import java.io.*;import java.util.*;
public class AudioPlayOrFile01{//An object of this class is used to either play the sound in the array
// named melody or to write it into an audio file of type AU.//The following are general instance variables used to create a// SourceDataLine object.
AudioFormat audioFormat;AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;AudioFormatParameters01 audioParams;
byte[]melody;
String playOrFile;//"play" to play immediately or a fileName to write// an output file of type AU.
//-------------------------------------------------------------------------//public AudioPlayOrFile01(AudioFormatParameters01 audioParams,byte[] melody,String playOrFile){//constructor
this.audioParams = audioParams;this.melody = melody;
this.playOrFile = playOrFile;}//end constructor
//-------------------------------------------------------------------------////This method plays or files the synthetic audio data that has been generated
// and saved in an array.void playOrFileData() {
try{//Get an input stream on the byte array containing the data
InputStream byteArrayInputStream = new ByteArrayInputStream(melody);//Get the required audio format
audioFormat = new AudioFormat(audioParams.sampleRate,audioParams.sampleSizeInBits,
audioParams.channels,audioParams.signed,
audioParams.bigEndian);
//Get an audio input stream from the ByteArrayInputStreamaudioInputStream = new AudioInputStream(
byteArrayInputStream,audioFormat,
melody.length/audioFormat.getFrameSize());//Get info on the required data line
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class,audioFormat);
//Get a SourceDataLine objectsourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLineInfo);//Decide whether to play the audio data immediately, or to write it
// into an audio file of type AU based on the incoming parameter named// playOrFile.
if(playOrFile.toUpperCase().equals("PLAY")){//Create a thread to play back the data and start it running. It will
// run until all the data has been played backnew PlayAudioThread().start();
}else{//Write the data to an output file with the name provided by the
// incoming parameter named playOrFile.try{
AudioSystem.write(audioInputStream,AudioFileFormat.Type.AU,
new File(playOrFile + ".au"));}catch (Exception e) {
e.printStackTrace();System.exit(0);
}//end catch}//end else
}catch (Exception e) {e.printStackTrace();
System.exit(0);}//end catch
}//end playOrFileData//===========================================================================//
//Inner class to play back the data that was saved.class PlayAudioThread extends Thread{
//This is a working buffer used to transfer the data between the// AudioInputStream and the SourceDataLine. The size is rather arbitrary.
byte playBuffer[]= new byte[16384];public void run(){try{
//Open and start the SourceDataLinesourceDataLine.open(audioFormat);
sourceDataLine.start();int cnt;//Get beginning of elapsed time for playback
long startTime = new Date().getTime();//Transfer the audio data to the speakerswhile((cnt = audioInputStream.read(
playBuffer,0,playBuffer.length)) != -1){//Keep looping until the input read method returns -1 for empty
// stream.if(cnt>0){
//Write data to the internal buffer of the data line where it will// be delivered to the speakers in real time
sourceDataLine.write(playBuffer, 0, cnt);}//end if
}//end while//Block and wait for internal buffer of the SourceDataLine to become// empty.
sourceDataLine.drain();//Get and display the elapsed time for the previous playback.
int elapsedTime = (int)(new Date().getTime() - startTime);System.out.println("Elapsed time: " + elapsedTime);//Finish with the SourceDataLine
sourceDataLine.stop();sourceDataLine.close();
}catch (Exception e) {e.printStackTrace();
System.exit(0);}//end catch}//end run
}//end inner class PlayAudioThread//=========================================================================//
}//end AudioPlayOrFile01 class |
.....
Listing 16 . 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//===========================================================================// |
-end-
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?