'"ChunkSize" in WAV files

I am currently looking over the following information of WAV files and I came across the "ChunkSize" of a WAV file included inside its header. However, I am unable to understand how the value of the ChunkSize is calculated. According to

http://tiny.systems/software/soundProgrammer/WavFormatDocs.pdf

    36 + SubChunk2Size, or more precisely: 
                           4 + (8 + SubChunk1Size) + (8 + SubChunk2Size) 
                           This is the size of the rest of the chunk  
                           following this number.  This is the size of the 
                           entire file in bytes minus 8 bytes for the 
                           two fields not included in this count: 
                           ChunkID and ChunkSize.  

I've looked at this for roughly 40 minutes now and I do not understand where the numbers came from. For example where did the "4" come from and the "8" which are being added?



Solution 1:[1]

As per the first diagram in your WavFormatDocs link, the ChunkSize is the total number of bytes in the Format, SubChunk1ID + SubChunk1Size + SubChunk1 and SubChunk2ID + SubChunk2Size + SubChunk2 fields:

Format:                              4 bytes
SubChunk1ID + SubChunk1Size: 4 + 4 = 8 bytes
SubChunk1:                           <SubChunk1Size> bytes
SubChunk2ID + SubChunk2Size: 4 + 4 = 8 bytes
SubChunk2:                           <SubChunk2Size> bytes
                                     ---------------------------------------------------
                                     4 + (8 + SubChunk1Size) + (8 + SubChunk2Size) bytes

The fmt chunk is a fixed size:

SubChunk1ID:   4  bytes 
SubChunk1Size: 4  bytes 
SubChunk1:     16 bytes
               --------
               24 bytes

So the ChunkSize is:

Format:         4 bytes 
fmt chunk:     24 bytes 
SubChunk2ID:    4 bytes 
SubChunk2Size:  4 bytes
SubChunk2:     <SubChunk2Size> bytes
               ---------------------
               36 + <SubChunk2Size> bytes

Solution 2:[2]

It looks like name of the second field ChunkSize is design error. Or maybe it is there due historic reasons. Maybe they did not think at early point of evolution of the format that multiple chunks would be added. I did not need to use ChunkSize at all after just taking it in count by verifying it is there and checking the value is correct. And the same term "chunk size" is used later in format in meaningful manner. ChunkSize in my code is "file_length - 8 ". if not then exception throws. It could be characterized as "wav file is knowing about its own size".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tonys
Solution 2 Kurskinen