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

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza 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