<< Chapter < Page Chapter >> Page >

You will find listings of the source code used to create these melodies later in this module.

Play or file the melody

The code in Listing 4 uses the standard procedure to either play or file the melody that was created in Listing 3 .

Listing 4 . Play or file the melody.
new AudioPlayOrFile01(audioParams,melody,this.args[0]).playOrFileData();}//end constructor //-------------------------------------------------------------------------//}//end class MusicComposer08.java

An extremely important concept

While this may seem like a rather innocuous result, runtime polymorphism is an extremely important concept in object-oriented programming. One of theimportant applications of runtime polymorphism is the Java Collections Framework . A series of accessible modules that explain the framework begins with Java4010 : Getting Started with Java Collections .

Run the program

I encourage you to copy the code from Listing 5 through Listing 16 . Compile the code and execute it. Experiment with the code,making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Jbs2050-Runtime Polymorphism with Java Sound
  • File: Jbs2050.htm
  • Published: 08/27/14
  • Revised: 09/29/15
Disclaimers:

Financial : Although the OpenStax CNX site makes it possible for you to download a PDF file for the collection thatcontains this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, youshould be aware that some of the HTML elements in this module may not translate well into PDF.

You also need to know that Prof. Baldwin receives no financial compensation from OpenStax CNX even if you purchase the PDF version of the collection.

In the past, unknown individuals have copied Prof. Baldwin's modules from cnx.org, converted them to Kindle books, and placedthem for sale on Amazon.com showing Prof. Baldwin as the author. Prof. Baldwin neither receives compensation for those sales nordoes he know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a collection thatis freely available on OpenStax CNX and that it was made and published without the prior knowledge of Prof.Baldwin.

Affiliation : Prof. Baldwin is a professor of Computer Information Technology at Austin Community College inAustin, TX.

Complete program listings

Complete listing of the twelve classes required by the program discussed in this module are provided below.

Listing 5 . The class named AudioGraphSinusoidal.
/*File AudioGraphSinusoidal.java Copyright 2014, R.G.BaldwinRevised 08/23/14 This class can be used to create an 8-second melody consisting of 32 pulsesat different frequencies. ******************************************************************************/import java.io.*; import java.nio.*;import java.util.*; public class AudioGraphSinusoidal extends AudioSignalGenerator02{public AudioGraphSinusoidal(AudioFormatParameters01 audioParams,String[] args,byte[] melody){super(audioParams,args,melody); }//end constructor//-------------------------------------------------------------------------////This method returns a melody array that will play an 8-second melody // consisting of 32 pulses at different frequencies. The frequencies of the// pulses are centered on middle-C (261.63 Hz).//The frequency deviation from middle-C versus time is based on a sinusoidal // function with a frequency of 0.5 Hz. Each pulse represents one point on// a graph of the sinusoid. Pulses with frequencies at or above middle-C are // delivered to the left speaker. Pulses with frequencies below middle-C are// delivered to the right speaker. //The audio output can be thought of as an audio representation of a graph// of a sinusoid. Pulses with frequencies above middle-C represent points // on the positive lobe of the sinusoid. Increasing pitch represents// increasing amplitude on the graph of the sinusoid. Pulses with // frequencies below middle-C can be thought of as representing points on// the negative lobe of the sinusoid. In this case, decreasing pitch // represents points on the sinusoid that are further from the horizontal// axis in the negative direction. Pulses with a frequency of middle-C can // be thought of as representing points on the horizontal axis with a value// of zero. //In order to eliminate pops and clicks caused by abrupt frequency changes// in the audio signal, the amplitude of each pulse is scaled by a // triangular (rooftop) function that has a value that is zero at both ends// and 1.0 in the center with a linear progression from the center to the // ends in both directions.//Four complete cycles of the 0.5 Hz sinusoid are represented by the 32 // pulses in the 8-second melody.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 = 8.0;//Set the center frequency. Audio pulses will be generated above and // below this frequency to represent points on the graph of a// sinusoidal function. double centerFreq = 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. These values will be used to cause pulses// representing negative values of the sinusoidal function to emit from // one speaker and pulses representing positive values to emit from// the other speaker. double gain = 0.0;double leftGain = 0.0; double rightGain = 0.0;//Declare a variable that is used to control the frequency of each pulse. double freq = 0.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){ //Time to create a new pulse at a different pitch. Compute the// frequency for the next pulse to represent a point on a sinusoidal // function of time. This section of code could easily be modified// to create audio graphs of many different functions.//Evaluate and scale the function double val = 0.35 * Math.sin(2*Math.PI*0.5*time);//Compute the frequency for the next pulse as a deviation from the// center frequency. For this scaled sinusoidal function, the Range // is from 0.65*centerFreq to 1.35*centerFreq or from 170.05 Hz// to 353.2 Hz. freq = (1+val)*centerFreq;}//end if //Deposit audio data in the melody for each channel. Scale the amplitude// of each pulse with a triangular scale factor (rooftop shape) to // minimize the undesirable pops and clicks that occur when there is an// abrupt change in the frequency from one pulse to the next. The // following gain factor ranges from 0.0 at the ends to 1.0 in the// center of the pulse. gain = (cnt%pulseLengthInSamples)/(double)pulseLengthInSamples;if(gain>0.5){ //Change to a negative slope.gain = (pulseLengthInSamples - cnt%pulseLengthInSamples)/(double)pulseLengthInSamples;}//end if//Set the final gain to a value that is compatible with 16-bit audio // data.gain = 8000*gain; //Switch the left and right channels on and off depending on the location// of the pulse frequency relative to the center frequency. if(freq>= centerFreq){ leftGain = gain;rightGain = 0;//switch off the right channel }else{rightGain = gain; leftGain = 0;//switch off the left channel}////Compute scaled pulse values and deposit them into the melody. byteBuffer.putShort((short)(leftGain*Math.sin(2*Math.PI*freq*time)));byteBuffer.putShort((short)(rightGain*Math.sin(2*Math.PI*freq*time))); }//end for loopreturn melody;}//end method getMelody //-------------------------------------------------------------------------//}//end class AudioGraphSinusoidal //===========================================================================//

Questions & Answers

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 to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
Do somebody tell me a best nano engineering book for beginners?
s. Reply
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
Virgil
is Bucky paper clear?
CYNTHIA
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Harper
Do you know which machine is used to that process?
s.
how to fabricate graphene ink ?
SUYASH Reply
for screen printed electrodes ?
SUYASH
What is lattice structure?
s. Reply
of graphene you mean?
Ebrahim
or in general
Ebrahim
in general
s.
Graphene has a hexagonal structure
tahir
On having this app for quite a bit time, Haven't realised there's a chat room in it.
Cied
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
China
Cied
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
Porter
many many of nanotubes
Porter
what is the k.e before it land
Yasmin
what is the function of carbon nanotubes?
Cesar
I'm interested in nanotube
Uday
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
what is nano technology
Sravani Reply
what is system testing?
AMJAD
preparation of nanomaterial
Victor Reply
Yes, Nanotechnology has a very fast field of applications and their is always something new to do with it...
Himanshu Reply
good afternoon madam
AMJAD
what is system testing
AMJAD
what is the application of nanotechnology?
Stotaw
In this morden time nanotechnology used in many field . 1-Electronics-manufacturad IC ,RAM,MRAM,solar panel etc 2-Helth and Medical-Nanomedicine,Drug Dilivery for cancer treatment etc 3- Atomobile -MEMS, Coating on car etc. and may other field for details you can check at Google
Azam
anybody can imagine what will be happen after 100 years from now in nano tech world
Prasenjit
after 100 year this will be not nanotechnology maybe this technology name will be change . maybe aftet 100 year . we work on electron lable practically about its properties and behaviour by the different instruments
Azam
name doesn't matter , whatever it will be change... I'm taking about effect on circumstances of the microscopic world
Prasenjit
how hard could it be to apply nanotechnology against viral infections such HIV or Ebola?
Damian
silver nanoparticles could handle the job?
Damian
not now but maybe in future only AgNP maybe any other nanomaterials
Azam
Hello
Uday
I'm interested in Nanotube
Uday
this technology will not going on for the long time , so I'm thinking about femtotechnology 10^-15
Prasenjit
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
Berger describes sociologists as concerned with
Mueller Reply
Got questions? Join the online conversation and get instant answers!
QuizOver.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