<< Chapter < Page Chapter >> Page >

Within the four bytes, the pair with the lowest index is routed to the left speaker and the other pair is routed to the right speaker.

Generating the square wave

Local working variables

Listing 3 declares three working variables. The variable named val specifies the amplitude of the square wave. You will see what the other twovariable are used for later.

Listing 3 . Local working variables.
int val = 8000;//amplitude value of square wave byte byteLow = 0;byte byteHigh = 0;

Set the frequency of the square wave

Listing 4 shows the beginning of a for loop that is used to populate the audio data array. Note that this for loop traverses the array in steps of two bytes per iteration. This will be important later when weexamine the code that populates the array.

Listing 4 . Set the frequency of the square wave.
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 signval = -val; }//end if

The amplitude of the square wave switches back and forth between val and -val on a regular basis. The value used with the modulus operator in the if statement in Listing 4 determines how often that switch will take place. If it switches every eight bytes at a samplingrate of 8000 samples per second, the resulting fundamental frequency will be 1000 Hz. The comments in Listing 4 indicate other possibilities.

Do the math: (8 bytes/switch)*(2 switches/cycle)*(1 samp/2 bytes)* (1 sec/8000 samp) = 0.001 sec/cycleor 1000 cycles/sec or 1000 Hz

Decompose val into two bytes

Listing 5 decomposes the 16 least significant bits (LSB) of val into a pair of 8-bit bytes, discarding the 16 most significant bits (MSB) of the 32-bit int value.

Listing 5 . Decompose val into two bytes.
byteLow = (byte)val;//discard all but 8 lsb byteHigh = (byte)(val>>8);//shift right 8 and discard all but 8 lsb

The first statement in Listing 5 uses a (byte) cast to discard the 24 MSB of a copy of val and to store the remaining eight LSB in the variable named byteLow . This does not modify the value actually stored in the variable named val .

The second statement in Listing 5 begins by shifting a copy of val 8 bits to the right, thereby discarding the eight LSB and keeping the remaining24 MSB of the original 32 bits. This operation extends the sign bit to the right in conjunction with the shift.

Then the second statement uses a (byte) cast to discard the 24 MSB of the same copy of val and to store the remaining eight LSB in the variable named byteHigh .

There is a more elegant, but much more cryptic way to do this, which I will show you in a future module. I'm using this approach at this time because itshould be easy for you to understand what is actually happening.

Populate the audio data array

Listing 6 assigns the values of byteHigh and byteLow to the elements in the audio data array in the required order described above . Recall that the for loop iterates through the array two bytes per iteration. Thus, each iterationproduces one audio sample.

Questions & Answers

calculate molarity of NaOH solution when 25.0ml of NaOH titrated with 27.2ml of 0.2m H2SO4
Gasin Reply
what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos Reply
What is the lkenes
Da Reply
what were atoms composed of?
Moses Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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