<< 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.
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; |
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.
(8 bytes/switch)*(2 switches/cycle)*(1 samp/2 bytes)*
(1 sec/8000 samp) = 0.001 sec/cycleor 1000 cycles/sec or 1000 Hz
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.
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.
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?