vendredi 22 octobre 2021

How to read data from a wav file header if its not in order

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

Aucun commentaire:

Enregistrer un commentaire