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

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