<< Chapter < Page Chapter >> Page >

Decomposing a short into two bytes using ByteBuffer

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.

Instantiate and prepare a ByteBuffer object

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

Why use a ByteBuffer object ?

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.

Compute and deposit audio samples in melody 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

  • Computing the time in seconds for the current sample.
  • Computing the value of the Math.sin function for that time.
  • Scaling that value up by 8000 to attain a reasonable audio level.
  • Casting that result to type short .
  • Using the putShort method of the ByteBuffer class discussed earlier to deposit the short value in the next two bytes in the melody array in a single statement.

Questions & Answers

summarize halerambos & holbon
David Reply
the Three stages of Auguste Comte
Clementina Reply
what are agents of socialization
Antonio Reply
sociology of education
Nuhu Reply
definition of sociology of education
Nuhu
what is culture
Abdulrahim Reply
shared beliefs, values, and practices
AI-Robot
What are the two type of scientific method
ogunniran Reply
I'm willing to join you
Aceng Reply
what are the scientific method of sociology
Man
what is socialization
ogunniran Reply
the process wherein people come to understand societal norms and expectations, to accept society's beliefs, and to be aware of societal values
AI-Robot
scientific method in doing research
ogunniran
defimition of sickness in afica
Anita
Cosmology
ogunniran
Hmmm
ogunniran
list and explain the terms that found in society
REMMY Reply
list and explain the terms that found in society
Mukhtar
what are the agents of socialization
Antonio
Family Peer group Institution
Abdulwajud
I mean the definition
Antonio
ways of perceived deviance indifferent society
Naomi Reply
reasons of joining groups
SAM
to bring development to the nation at large
Hyellafiya
entails of consultative and consensus building from others
Gadama
World first Sociologist?
Abu
What is evolutionary model
Muhammad Reply
Evolution models refer to mathematical and computational representations of the processes involved in biological evolution. These models aim to simulate and understand how species change over time through mechanisms such as natural selection, genetic drift, and mutation. Evolutionary models can be u
faruk
what are the modern trends in religious behaviours
Selekeye Reply
what are social norms
Daniel Reply
shared standards of acceptable behavior by the group or appropriate behavior in a particular institution or those behaviors that are acceptable in a society
Lucius
that is how i understood it
Lucius
examples of societal norms
Diamond
Discuss the characteristics of the research located within positivist and the interpretivist paradigm
Tariro Reply
what is Industrialisation
Selekeye Reply
industrialization
Angelo
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