1

I am trying to store data of .wav file in an array. I am not able to find a way to store data from subchunk2 in an array. Can anybody help me with this?

This is the code which I have used till now

typedef struct header_file
{
    char chunk_id[4];
    int chunk_size;
    char format[4];
    char subchunk1_id[4];
    int subchunk1_size;
    short int audio_format;
    short int num_channels;
    int sample_rate;            // sample_rate denotes the sampling rate.
    int byte_rate;
    short int block_align;
    short int bits_per_sample;
    char subchunk2_id[4];
    int subchunk2_size;         // subchunk2_size denotes the number of samples.
} header;

typedef struct header_file* header_p;

Main Part

 //Load wave file
    FILE* infile = fopen("E:/fCWT-main/MATLAB/1s_speech.wav", "rb");        // Open wave file in read mode
    int BUFSIZE = 512;                  // BUFSIZE can be changed according to the frame size required (eg:512)
    int count = 0;                      // For counting number of frames in wave file.
    short int buff16[BUFSIZE];              // short int used for 16 bit as input data format is 16 bit PCM audio
    header_p meta = (header_p)malloc(sizeof(header));   // header_p points to a header struct that contains the wave file metadata fields
    int nb;                         // variable storing number of byes returned
    

   
    if (infile)
    {
        fread(meta, 1, sizeof(header), infile);
        //fwrite(meta, 1, sizeof(*meta), outfile);
        int samples = meta->subchunk2_size;
        size_t result;
        tmp = (float*)malloc(sizeof(float) * samples);
        while (!feof(infile))
        {
            nb = fread(buff16, 1, BUFSIZE, infile);     // Reading data in chunks of BUFSIZE
            cout << nb << endl;
                        
            count++;                    // Incrementing Number of frames

           
        }
                         
        cout << " Number of frames in the input wave file are " << count << endl;
    }

Basically here I want to store and display the data of wav file into an array so that I can use that array for further processing and I don't have any idea how to do it. I have tried searching it online but that solutions were limited to reading Header File.

I am new in C++ so any help regarding this will work. Thanks!

  • [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Retired Ninja Jun 23 '22 at 07:59

0 Answers0