perlaudiowavriff

RIFF WAV header format 2014 update?


I'm trying to decode and play WAV files in perl for further operations, I have found some references about format, and some interesting q+a

What does a audio frame contain? error in reading a wav file with C++ Writing musical notes to a wav file

I found out the "Cannonical WAVE file format" enter image description here

But at the end I'm testing 2 different WAV files that doesn't follow the "standard". Mplayer has no problems at all at reading data, and I figured out a workaround on my perl code:

sysread WAV, $riff, 12;
sysread WAV, $fmt,  24;

do{
    sysread WAV, $wtf, 2;

}while( unpack("A4",$wtf) ne "da" );
sysread WAV, $wtf, 2;

#94
sysread WAV, $data,  4;

Still it troubles me how it really works, and what is that variable data is between the field "bits per sample" and the "data" field.

Thank you guys!

(I'm getting addicted to this forums)

test2.wav

       v--------- riff --------------v--------- 
0000000 4952 4646 685e 0931 4157 4556 6d66 2074
        -------------- fmt --------------------
0000010 0028 0000 fffe 0006 bb80 0000 ca00 0008
        ---------v-----------------------------
0000020 000c 0010 0016 0010 060f 0000 0001 0000
        ---------------------------------------
0000030 0000 0010 0080 aa00 3800 719b 494c 5453
        ---------------------------------------
0000040 001a 0000 4e49 4f46 5349 5446 000e 0000
        ----------------------------------v----
0000050 614c 6676 3535 312e 2e39 3031 0034 6164
        ----v---------v
0000060 6174 6800 0931 0000 0000 0000 0000 0000
0000070 0000 0000 0000 0000 0000 0000 0000 0000

test.wav

       v--------- riff --------------v--------- 
0000000 4952 4646 7048 095b 4157 4556 6d66 2074
        -------------- fmt --------------------
0000010 0012 0000 0001 0002 ac44 0000 b110 0002
        ---------v-----------------------------
0000020 0004 0010 0000 494c 5453 001a 0000 4e49
        ---------------------------------------
0000030 4f46 5349 5446 000e 0000 614c 6676 3535
        -------------------v---------v---------v
0000040 312e 2e39 3031 0034 6164 6174 7000 095b       
0000050 0000 0000 0000 0000 0000 0000 0000 0000

Solution

  • The AudioFormat field in test2.wav is 0xfffe which indicates that is the header is WAVEFORMATEXTENSIBLE. When this happens then you need to interpret the rest of the header differently.

    AudioFormat   : 2
    NumChannels   : 2
    SampleRate    : 4
    ByteRate      : 4
    BlockAlign    : 2
    BitsPerSample : 2
    cbSize        : 2  - size of the rest of the chunk
    ChannelMask   : 4
    SubFormat     : 16 - GUID
    

    For more information look at some docs on WAVEFORMATEX and WAVEFORMATEXTENSIBLE