Reputation: 101
So I'm trying to extract the amplitudes of different samples from a .wav file to be able to reconstruct the waveform with regards to time. While researching the format of a .wav file, I found some very useful information. However, I was confused when it came to the raw data portion. The article I found stated,"In the case of 16 bit PCM, 16-bit samples are stored as 2's-complement unsigned integers, ranging from 0 to 65535". I'm confused because I don't know how the file distinguishes between positive and negative amplitudes if it's unsigned. For example, if one amplitude is 5 represented as 00000000 00000101 in binary, then using the 2's complement -5 should be represented as 11111111 11111011 which is 65531 in decimal as an integer. This makes me think an amplitude of -5 is perceived as MUCH larger than a +5 amplitude. Clearly this isn't how it works so what am I doing wrong? Also, when I save my .wav file into a binary array and output the values, I get negative numbers! Where do these negative numbers come from if the article said only values between 0 to 65535 should be stored. Just very confused, really need to have things cleared up. The link of the article I was referring to is attached down below. The quote of what the author said is found at the very bottom of the page. Thanks.
http://www.ievs.ch/projects/var/upload/Documentation%20Microsoft%20Wave%20File%20Format.pdf
Upvotes: 0
Views: 1317
Reputation: 7910
I recommend bring the .wav data into Java via AudioSystem
methods, rather than attempting to work on the raw bytes. Once brought in, making use of the appropriate AudioFormat
, there is no need to deal with header or any sort of packet-organizing info for the data.
When I've brought in data this way, the decoded PCM is stored as signed shorts. Thus it ranges from -32768 to 32767. If 16-bit encoding, the values are obtained from two bytes, assembled in an order specified by the bigEndian
flag.
https://docs.oracle.com/javase/9/docs/api/javax/sound/sampled/AudioFormat.html
A second concern is that the individual PCM values probably should not be thought of as a useful indication of volume. Instead volume is usually determined by doing a RMS (root-mean-square) analysis of the signal.
The RMS over all time of a periodic function is equal to the RMS of one period of the function. The RMS value of a continuous function or signal can be approximated by taking the RMS of a sample consisting of equally spaced observations.
Upvotes: 0