1

I wrote a program that read information from a .wav file . I received the following results.

chunkId            :RIFF
chunkSize          :7828798
format             :WAVE
Junk chunk         :JUNK
Junk size          :92
Format chunk       :fmt 
format chunk size  :65536
Audioformat        :1
numberofchanels    :48000
samplerate         :1996488704
Byterate           :131073
BlockAlign         :16
bitspersample      :0
subchunk2Id        :
subchunk2Size      :0

I don't know why wav header like that. I use sox program to look header of this wav file . And here is result

Input File     : 'AUDIO00001.wav'
Channels       : 1
Sample Rate    : 48000
Precision      : 16-bit
Duration       : 00:01:17.28 = 3709621 samples ~ 5796.28 CDDA sectors
File Size      : 7.83M
Bit Rate       : 810k
Sample Encoding: 16-bit Signed Integer PCM

Why's my program wrong??

I update my question: my wav header structure:

struct WAVHEADER
    {
        char                chunkID[4];
        unsigned int        chunkSize;
        char                format[4];

        char                junkChunk[4];
        int                 junkSize;
        char                junkData[92];
        char                bext[4];
        unsigned int        bextSize;
        char                bextData[602];
        char                subchunk1ID[4];
        unsigned int        subchunk1Size;
        unsigned short      audioFormat;
        unsigned short      numberOfChanels;
        unsigned int        sampleRate;
        unsigned int        byteRate;
        unsigned short      blockAlign;
        unsigned short      bitsPerSample;
        char                subchunk2ID[4];
        unsigned int        subchunk2Size;
};

wav class:

wav::wav(const char* filepath)
{
    std::ifstream wavFile(filepath);
    if(wavFile.is_open())
    {
        wavFile.read((char*)&wavHeader,sizeof(WAVHEADER));

    }
}

My confusing is that when subChunk1ID is correct ("fmt") , why does subchunk1Size seem wrong ??

Huongnt
  • 31
  • 5

1 Answers1

0

The compiler seems to insert 2 bytes of padding between subchunk1ID and subchunk1Size. This is my hypothesis because

  • what you are seeing is that the struct is shifted 2 bytes "to the left"
  • and when I tested this I noticed that sizeof(WAVHEADER) was 2 bytes larger than it should be.

I'm not a C++ compiler expert so I can't tell you why it does that except that certainly it's allowed to do so.

To show visually, what the struct should look like at that point is this:

...
subchunk1ID:
    01100110
    01101101
    01110100
    00100000
subchunk1Size:
    00101000
    00000000
    00000000
    00000000
audioFormat:
    00000001
    00000000
numberOfChanels:
    00000001
    00000000
...

But instead what you are getting is this:

...
subchunk1ID:
    01100110
    01101101
    01110100
    00100000
2 bytes of pad:
    00101000
    00000000
subchunk1Size:
    00000000
    00000000
    00000001
    00000000
audioFormat:
    00000001
    00000000
...

00000000 00000000 00000001 00000000 is the number 65536 in little endian byte order.

So really what you have found is that this struct casting is not a reliable way to read a file. You should read it "the hard way" instead (one number at a time). There are other pitfalls like this one such as:

Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125