Decoding a WAV File Header


I'm trying to understand the header of a WAV file. I've opened an example file and got this:

5249 4646 e857 1400 5741 5645 666d 7420
1000 0000 0100 0200 44ac 0000 10b1 0200
0400 1000

I've been reading this data representation tutorial.

I understand that 52 is one byte and represents the ASCII letter R. I understand up to the 1000 0000. Why does that represent decimal 16? The tutorial says that the value at that position is always 0x10. How does 1000 0000 equate to 0x10.

Also, when reading the file, will a program know whether to expect a number or ASCII? Presumably it'll check against a value that is already in HEX?

Thanks


Answers:


Hope this helps a little, I'm going to mention everything just to clear:

Something to use for hex in general: I use a hex editor, dhex, which you should be able to apt-get, yum, or brew install on your favorite unix like machine. I'll be using this site as the source for most of this material:

As you alluded to, the first 4 bytes of hex represent ASCII characters. In this case, those characters area always:

52 49 46 46 - RIFF

The next 4 bytes represent the chunk size, which is little endian. In your case it is:

e8 57 14 00 - 1333224

The next 8 bytes represent ASCII characters again. In your case:

57 41 56 45 - WAVE
66 6d 74 20 - fmt.

The next 4 represent the chunk size, which is little endian:

10 00 00 00 - 268435456

This chuck does not represent decimal 16, it represents decimal 268435456 in 16bits (4 bytes with 1 bytes being 4 bits). for reasons as to "why" the chunks are 16bits, you can read more of on this Intro to Audio Programing. This describes that the chunks in the data section will be 16bit.

01 00 represents the audio format (little endian), 1 stands for PCI in this case. 02 00 represents the number of channels, which is 2 in your case.

The next 4 bytes represent the sample rate (little endian), or

The number of samples ( frames) that exist for each second of data. This field is represented in Hz. - taken from here.

44 ac 00 00 - 44100

The next 4 bytes represent the byte rate(little endian):

10 b1 02 00 - 1049265

The next 2 bytes represent the block align:

This is the number of bytes in a frame (little endian). This is calculated by multiplying the number of channels by the number of bytes (not bits) in a sample. You can find more detailed calculations here

04 00 - 4 

Next are your 2 bytes that represent bits per sample(little endian), in your case it is 16

10 00 - 16

The Data Section:

You do not currently have a data section in your .wav file, in order to start the data section you first write the ASCII values for data like so (big endian):

64 61 74 61 - data

After this you need 4 bytes representing your chunk 2 size and you should be good to go. In terms of testing, I would recommend reading this guide if you have not already. I would also recommenced reading this post about how to build raw wav files Digital Audio - Creating a WAV (RIFF) file , this should also help in understanding how to "decode" them.