1

I have the following code to open and read the RIFF header of a Wav-file; this code works for most wav files in extracting the header info but fails to work with a few wav files.

typedef struct header_file {
    //unsigned long RecordNumber;
    unsigned char   ChunkID[4];
    long    ChunkSize;
    unsigned char   Format[4];
    unsigned char   SubChunk1ID[4];
    unsigned long   SubChunk1Size;
    unsigned short  AudioFormat;
    unsigned short  NumChannels;
    unsigned long   SampleRate;
    unsigned long   BytesRate;
    unsigned short  BlockAlign;
    unsigned short  BitsPerSample;
    unsigned char   SubChunk2ID[4];
    unsigned long   SubChunk2Size;
        //int               BitsPerOutputSample;//Reconstructed format -> 8, 16
    //unsigned char CheckSum;
    //BYTE          Version;
} RiffWav;
FILE* WAVF = fopen(pParent->m_StrWavFileName, "rb");
HeaderSize = fread(&WavHdr, 1, sizeof(RiffWav), WAVF);
LONGLONG Count = WavHdr.SubChunk2Size / (WavHdr.NumChannels * (WavHdr.BitsPerSample / 8));
CHAR* LPbuffer = new CHAR[Count];
fread(LPbuffer, sizeof LPbuffer[0], Count, WAVF);

I've come to a conclusion that it could be because there are several meta-data headers in the wav file that should be parsed because its not a static structure, so I should parse each of the sub-headers on their own.

When I used the fseek() function it has worked on a few files that were returning incorrect information on the header but did not work for all wav files

Edit: I can confirm that all the wav files I opened are Riff Wave Format

  • You should mention that this is a [followup](https://stackoverflow.com/questions/69649876/how-to-read-data-of-wav-file) question. The additional context could help potential answerers. –  Oct 22 '21 at 19:38
  • Instead of using the `unsigned` types, you should use the `uint` types, such as `uint16_t`. The unsigned integer types can vary in size, *as long as they can support the ranges*. For example, `unsigned short` is guaranteed to be **at least 16-bits**, it could be more. A `uint16_t` is guaranteed to be explicitly 16bits. – Thomas Matthews Oct 22 '21 at 19:40
  • You should also validate that `ChunkID` is "RIFF" and `Format` is "WAVE". you may be dealing with files using different header structures. –  Oct 22 '21 at 19:44
  • @ThomasMatthews How would that help me with finding the right values since the numbers being returned are incorrect but similar in size to correct numbers; for example the incorrect number of channels returned for one type of file was 4 whereas it should have been 2. –  Oct 22 '21 at 19:45
  • Let's say that on your platform, `unsigned short` is 32-bits. This means that `NumChannels` is at the wrong place; it's shifted down by 16 bits. All the following members would be in the wrong place also, and when you read their values, they will be completely wrong. Draw it out (with pen and paper). – Thomas Matthews Oct 22 '21 at 19:48
  • @omargamal Your struct should be looking like this: https://gcc.godbolt.org/z/KxGebhnMY. This may or may not be the source of your problem, but we can't help you further until you've at least made your code reliable. –  Oct 22 '21 at 19:56

0 Answers0