<< Chapter < Page | Chapter >> Page > |
Recall that the abstract class named AudioSignalGenerator02 (see Listing 7 ) declares an instance variable of type ByteBuffer named byteBuffer . This variable is inherited into the ToneMono class and is available to the getMelody method.
I encourage you to go to the Java Standard Edition documentation and read about the class named ByteBuffer . This is a very powerful class with a lot of capability.
In this module, we will use an object of the ByteBuffer class to eliminate some of the complexity involved in decomposing a short audio value into a pair of byte values and depositing the two byte values in the melody array.
Using terminology from the Java SE documentation, the statement in Listing 3 creates a ByteBuffer object and saves that object's reference in the inherited variable named byteBuffer by wrapping an existing byte array ( melody ) into a buffer .
Listing 3 . Instantiate and prepare a ByteBuffer object. |
---|
byteBuffer = ByteBuffer.wrap(melody); |
Normally the elements of a byte array can only be accessed using indexed square bracket ([index]) notation. Wrapping the array in a ByteBuffer object makes it possible to access the elements in the array by calling methods that are defined in the ByteBuffer class. For example, the array can be populated from beginning to end using successive calls to theoverloaded put methods of the ByteBuffer class without regard for the actual index of the elements (provided that you don't try to exceed the length of the array) .
Although it isn't obvious, this solves the problem of decomposing the short value into a pair of byte values and depositing them in the correct order in the melody array . One of the methods of the ByteBuffer class is named putShort . As you will see later, that method makes it possible to "put" a short value into our ByteBuffer object (and hence into our melody array) with a single statement. The putShort method will automatically decompose the short value into two byte values and deposit them in the proper order in the array.
Listing 4 begins by computing the melody length in samples as the product of the length in seconds and the sampling rate in samples per second.
Listing 4 . Compute and deposit audio samples in melody array. |
---|
//Compute the number of audio samples in the melody.
int sampLength = (int)(lengthInSeconds*audioParams.sampleRate);//Compute the audio sample values and deposit them in the output array.for(int cnt = 0; cnt<sampLength; cnt++){
//Compute the time in seconds for this sample.double time = cnt/audioParams.sampleRate;
//Deposit audio data for both channels in mono.byteBuffer.putShort((short)(8000*Math.sin(2*Math.PI*freq*time)));
}//end for loopreturn melody;}//end method getMelody
//-------------------------------------------------------------------------//}//end class ToneMono |
Then Listing 4 enters a for loop for the purpose of
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?