6

I need to know whether a ".wav" of 8bits, is signed or unsigned PCM, by only reading file. I cannot use "javax.sound.sampled.*" or AudioSystem libraries.

Oak Bytes
  • 4,649
  • 4
  • 36
  • 53
joseluisbz
  • 1,491
  • 1
  • 36
  • 58
  • I tried to find how Java detect if WAV file is PCM_Signed or PCM_Unsigned. In native file "AudioSystem.java" Line 1068. – joseluisbz May 24 '12 at 14:07
  • 1
    Go to the questions you've previously asked. Select the "check mark" next to the correct or best or most helpful answer. See also [the FAQ](http://stackoverflow.com/faq#howtoask). You can select your own answer if you solved it yourself. – Roddy of the Frozen Peas Jul 26 '12 at 01:53

2 Answers2

14

8 bit (or lower) WAV files are always unsigned. 9 bit or higher are always signed:

Each sample is contained in an integer i. The size of i is the smallest number of bytes required to contain the specified sample size. The least significant byte is stored first. The bits that represent the sample amplitude are stored in the most significant bits of i, and the remaining bits are set to zero.

For example, if the sample size (recorded in nBitsPerSample) is 12 bits, then each sample is stored in a two-byte integer. The least significant four bits of the first (least significant) byte is set to zero.

The data format and maximum and minimums values for PCM waveform samples of various sizes are as follows:

enter image description here

Multimedia Programming Interface and Data Specifications 1.0 - IBM/Microsoft, August 1991

endolith
  • 25,479
  • 34
  • 128
  • 192
  • "1-8 bits being unsigned, and 9+ bits being signed", is just a *convention* set in the WAV standards right? Or is it due to any "limitation" as such? – Sreenikethan I Dec 15 '21 at 13:27
  • @SreenikethanI That's the definition of the format. I'm not sure what you mean by "convention". If the data is 7 bits, it represents unsigned 7-bit numbers. – endolith Dec 15 '21 at 22:17
  • Thanks for the response. By "convention" I meant to ask whether it's just the WAV standard that decided 8-bit (and below) to be **un**signed, even though technically speaking a **signed** number is possible? – Sreenikethan I Dec 16 '21 at 13:29
  • @SreenikethanI The WAV format doesn't have any way to indicate the signedness, so no, 8-bit signed data is impossible in WAV format. If it's 8-bit, it's unsigned. (It's pretty clear from reading through the format specs that the format wasn't planned out carefully, and just kind of happened, with new stuff being tacked on along the way. It probably should have been signed for all bit depths, but the early implementations overlooked the distinction and now we're stuck with it.) – endolith Dec 16 '21 at 15:37
  • Right, that clarifies it for me. Thank you :) – Sreenikethan I Dec 17 '21 at 05:38
8

In the wav File, 8-bit samples are stored as unsigned bytes, ranging from 0 to 255. The 16-bit samples are stored as signed integers in 2's-complement.

joseluisbz
  • 1,491
  • 1
  • 36
  • 58