<< 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 //===========================================================================//

Questions & Answers

what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





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