<< Chapter < Page | Chapter >> Page > |
.....
Listing 9 . 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){
//Change this value to change the fundamental frequency.//8 results in 1000 Hz
//16 results in 500 Hz//32 results in 250 Hz, etc.
if(cnt % 8 == 0){//Change sign
val = -val;}//end if//Create two bytes that contain a 16-bit representation of the value.
byteLow = (byte)val;//discard all but 8 lsbbyteHigh = (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//===========================================================================// |
.....
Listing 10 . The class named StereoPingpong. |
---|
/*File StereoPingpong.java
Copyright 2014, R.G.BaldwinRevised 08/19/14
This class can be used to create an 4-second stereo melody that ping-pongsback and forth between the left and right speakers.
******************************************************************************/import java.io.*;
import java.nio.*;import java.util.*;
public class StereoPingpong extends AudioSignalGenerator02{public StereoPingpong(AudioFormatParameters01 audioParams,String[]args,
byte[]melody){
super(audioParams,args,melody);}//end constructor
//-------------------------------------------------------------------------////This method returns an array that will play a four-second stereo sound// that ping-pongs back and forth between the left and right speakers.
// The tone emitted by the left speaker is at a frequency of middle-C. The// tone emitted by the right speaker is twice that frequency. The sound
// switches between speakers four times per second.byte[] getMelody(){//Set channels to 2 for stereo overriding the default value of 1.
audioParams.channels = 2;System.out.println("audioParams.channels = " + audioParams.channels);//Each channel requires two 8-bit bytes per 16-bit sample.
int bytesPerSampPerChan = 2;//Override the default sampleRate of 16000.0F. Allowable sample rates// are 8000,11025,16000,22050, and 44100 samples per second.
audioParams.sampleRate = 8000.0F;// Specify the length of the melody in seconds.double lengthInSeconds = 4.0;
//Set the frequency for the left speaker.double freq = 261.63;//middle C//Create an output array of sufficient size 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);//Declare variables used to control the output volume on the left and
// right speaker channels.double leftGain = 0.0;
double rightGain = 8000.0;//Prepare a ByteBuffer for use
byteBuffer = ByteBuffer.wrap(melody);//Compute the number of audio samples in the melody.
int sampLength = (int)(lengthInSeconds*audioParams.sampleRate);//Set the length of each pulse in seconds and in samples.
double pulseLengthInSec = 0.25;//in secondsint pulseLengthInSamples = (int)(pulseLengthInSec*audioParams.sampleRate);
//Compute the audio sample values and deposit them in the output melody// array.
for(int cnt = 0; cnt<sampLength; cnt++){
//Compute the time in seconds for this sample.double time = cnt/audioParams.sampleRate;if(cnt%pulseLengthInSamples == 0){
//Swap gain values between channelsdouble temp = leftGain;
leftGain = rightGain;rightGain = temp;
}//end if//Generate data for left speaker at middle-Cdouble sinValue = Math.sin(2*Math.PI*(freq)*time);
byteBuffer.putShort((short)(leftGain*sinValue));//Generate data for right speaker at twice the frequency of middle-CsinValue = Math.sin(2*Math.PI*(freq*2.0)*time);
byteBuffer.putShort((short)(rightGain*sinValue));}//end for loopreturn melody;}//end method getMelody
//-------------------------------------------------------------------------//}//end class StereoPingpong
//===========================================================================// |
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?