How to read the data part of a .wav file in c++?

0

So I have this assignment about convolution, and at one part I'm asked to convolute the data of two .wav audio files and see the result. I have managed to load all the header information properly. However when I try to run the code I get this error:

Exception thrown at 0x00007FFE1E5B602E (ucrtbased.dll) in project_name.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF. occurred

My code relevant to this issue follows up. I have a comment pointing out the line where my it's more likely for my problem to be:

typedef struct  WAV_HEADER {
    char                RIFFChunkID[4];
    unsigned long       RIFFChunkSize;
    char                Format[4];
    char                FormatSubchunkID[4];
    unsigned long       FormatSubchunkSize;
    unsigned short      AudioFormat;
    unsigned short      Channels;
    unsigned long       SamplesRate;
    unsigned long       ByteRate;
    unsigned short      BlockAlign;
    unsigned short      BitsPerSample;
    char                DataSubchunkID[4];
    unsigned long       DataSubchunkSize;
    double*             Data; 
}wav_head;
WAV_HEADER wav_READ(std::string filename) {
    wav_head read;
    FILE* TOREAD;

    //Construct filepath
    std::string filepath = "<<path>>\" + filename + ".wav";
    const char* path = filepath.c_str();

    //Open file
    TOREAD = fopen(path, "r");
    if (!TOREAD) { std::cout << "Error while reading file.\n"; exit(-1); }

    //READ RIIF CHUNK---------
    fread(&read.RIFFChunkID, sizeof(char), 4, TOREAD);
    fread(&read.RIFFChunkSize, sizeof(long), 1, TOREAD);
    fread(&read.Format, sizeof(char), 4, TOREAD);

    //READ FORMAT CHUNK-------
    fread(&read.FormatSubchunkID, sizeof(char), 4, TOREAD);
    fread(&read.FormatSubchunkSize, sizeof(long), 1, TOREAD);
    fread(&read.AudioFormat, sizeof(short), 1, TOREAD);
    fread(&read.Channels, sizeof(short), 1, TOREAD);
    fread(&read.SamplesRate, sizeof(long), 1, TOREAD);
    fread(&read.ByteRate, sizeof(long), 1, TOREAD);
    fread(&read.BlockAlign, sizeof(short), 1, TOREAD);
    fread(&read.BitsPerSample, sizeof(short), 1, TOREAD);

    //READ DATA CHUNK---------
    fread(&read.DataSubchunkID, sizeof(char), 4, TOREAD);
    fread(&read.DataSubchunkSize, sizeof(long), 1, TOREAD);

    int data_el = read.DataSubchunkSize / sizeof(short);   //
    read.Data = new double[data_el];                       // Here probably lies the problem.
    fread(&read.Data, sizeof(double),data_el, TOREAD);     //

    fclose(TOREAD);

    return read;
}

Now I have converted the wav file I want to analyse into text using matlab, and I've seen that there are (let's say) n elements/numbers in the data section, which is then corrected by the DataSubchunkSize variable which 2n ( n * sizeof(short) ). So these numbers are correct. Therefore I can't see what exactly is my mistake.

P.S. I have seen this post:

C++ Reading the Data part of a WAV file

and I have actually used it a lot to write my code. However It doesn't fully cover me on what to do in my case.

ALSO, I know that there is a library that can do that job for me, however I'd like to do it this way. I'll use this lib as a final resort!

Cheers!

c++
file
wav
fstream
fread
asked on Stack Overflow Oct 29, 2019 by Fjolfrin • edited Oct 29, 2019 by Fjolfrin

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0