简体   繁体   中英

Parsing a Riff wav header

I have a program that should read the metadata of all wav files. I used the following parsed struct and fread functions.

The issue this code runs into is that with some .wav RIFF header fmt files the meta data read is incorrect even after paring the struct as shown below, could it help if I use a fseek function between every read function.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
using std::string;



typedef struct RIFF_CHUNCK_DISCRIPTOR {
    char                RIFF[4];        // RIFF Header      Magic header
    unsigned long       ChunkSize;      // RIFF Chunk Size  
    char                WAVE[4];        // WAVE Header      

};
typedef struct FMT_CHUNCK_DISCRIPTOR {
    char                fmt[4];         // FMT header       
    unsigned long       Subchunk1Size;  // Size of the fmt chunk                                
    unsigned short      AudioFormat;    // Audio format 
    unsigned short      NumOfChan;      // Number of channels 
    unsigned long       SamplesPerSec;  // Sampling Frequency in Hz                             
    unsigned long       bytesPerSec;    // bytes per second 
    unsigned short      blockAlign;     // 2=16-bit mono, 4=16-bit stereo 
    unsigned short      bitsPerSample;  // Number of bits per sample      
    
};
typedef struct  DATA_SUB_CHUNCK {
    char                Subchunk2ID[4]; // "data"  string   
    unsigned long       Subchunk2Size;  // Sampled data length    

};

int main(int argc, char* argv[]) {
    const char* fileName;
    cout << "enter f name " << fileName;
    RIFF_CHUNCK_DISCRIPTOR RCD;
    FMT_CHUNCK_DISCRIPTOR FCD;
    DATA_SUB_CHUNCK    DSC;

    FILE* InFile = fopen(fileName, "r");
    fread(&RCD, 1, sizeof(RIFF_CHUNCK_DISCRIPTOR), InFile);
    fread(&FCD, 1, sizeof(FMT_CHUNCK_DISCRIPTOR), InFile);
    fread(&DSC, 1, sizeof(DATA_SUB_CHUNCK), InFile);
    
 }

The reason I used multiple structs is to ensure that all the data of the header file is read correctly since although most wav file header data is in order this is sometimes not true.

I would open the files in binary mode, ie replace fopen(fileName, "r") with fopen(fileName, "rb")

On some systems (you did not tell which OS you are using, I assume Windows?) in text / non-binary mode certain chars get replace during reading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM