Audio signal plotting code explanation
I'm trying to understand every single line of the following code , but my knowledge prevents me from being successful.
It's actually a piece of code taken form a java tutor开发者_开发知识库ial regarding the plotting of an audio signal (waveform)
In a nutshell what the code does is basically: if audio data sample size is equal to 16 and encoding type is BigEndian do this... Well.. the problem is that I'd like to be able to catch the meaning of every single code statement.. Can anyone help me ? Many thanks in advance... The original code is here: http://www.koders.com/java/fid3508156A13C80A263E7CE65C4C9D6F5D8651AF5D.aspx?s=%22David+Anderson%22 (class SamplingGraph )
The code I'd like to understand is the following:
if (format.getSampleSizeInBits() == 16) {
int nlengthInSamples = audioBytes.size() / 2;
audioData = new int[nlengthInSamples];
if (format.isBigEndian()) {
for (int i = 0; i < nlengthInSamples; i++) {
/* First byte is MSB (high order) */
int MSB = (int) audioBytes.get(2 * i);
/* Second byte is LSB (low order) */
int LSB = (int) audioBytes.get(2 * i + 1);
audioData[i] = MSB << 8 | (255 & LSB);
}
Especially how would you translate in words the following code:
audioData[i] = MSB << 8 | (255 & LSB);
...thanks again Mat
That's what is called a bitwise operation; it manipulates the numbers at the bit level.
<<
is the bitwise left shift operator (JLS 15.19 Shift Operators)|
and&
are bitwise "or" and "and" respectively (JLS 15.22.1 Integer Bitwise Operators &, ^, and |)
The snippet combines 2 byte
for 16-bit samples into an int
in big endian format.
255, which in binary is 11111111
(i.e, a sequence of 8 ones), is used to "mask" the lower byte
, to undo any sign extension (byte
in Java is a signed numeric type). It's typical to use the hexadecimal representation 0xFF
instead of the decimal 255 in these kinds of operations.
If you don't want to get down and dirty with bit level manipulation, there should be libraries that can do this for you in a robust way.
Related questions
- How do I split an integer into 2 byte binary?
- This is the reverse operation,
int
tobyte[]
- This is the reverse operation,
- Convert 4 bytes to int
- James Gosling’s explanation of why Java’s byte is signed
MSB = Most significant bits (the first half)
LSB = Least significant bits (the second half)
This code snippet is confirming that the sample size is 16 bits and in big-endian format. Then it is calculating the MSB and LSB for the sample. Finally, it is combining the MSB and LSB by shifting the MSB to the left 8 bits and then adding in the LSB with a mask that ensures only the lower 8 bits of the LSB are used.
audioData[i] = MSB << 8 | (255 & LSB);
MSB - Most Significant Bit
LSB - Least Significant Bit
MSB << 8 // shift the MSB to the left by 8 bitposition
Example: 00000000 00000010 -> 00000010 00000000
(255 & LSB) // masking the LSB, so that we have LSB if it is less then 255 or 0
Example: 00000000 00000010 -> 00000000 00000010
00000010 00000000 -> 00000000 00000000
// The OR operation '|' is putting both together
Example: 00000010 00000000 | 00000000 00000010 -> 00000010 00000010
精彩评论