# 0.6 Jbs2020-square wave sound  (Page 4/7)

 Page 4 / 7

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.

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

where we get a research paper on Nano chemistry....?
nanopartical of organic/inorganic / physical chemistry , pdf / thesis / review
Ali
what are the products of Nano chemistry?
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
learn
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
learn
da
no nanotechnology is also a part of physics and maths it requires angle formulas and some pressure regarding concepts
Bhagvanji
hey
Giriraj
Preparation and Applications of Nanomaterial for Drug Delivery
revolt
da
Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
ya I also want to know the raman spectra
Bhagvanji
I only see partial conversation and what's the question here!
what about nanotechnology for water purification
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
Nasa has use it in the 60's, copper as water purification in the moon travel.
Alexandre
nanocopper obvius
Alexandre
what is the stm
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?
what is a peer
What is meant by 'nano scale'?
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
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
Hafiz
what is Nano technology ?
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?
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
why we need to study biomolecules, molecular biology in nanotechnology?
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
why?
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
how did you get the value of 2000N.What calculations are needed to arrive at it
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!