简体   繁体   中英

Unexpected output when reading .wav data in c++

I'm making a very simple method but experiencing some trouble.

I want to read the data of a .wav file. I am just interested in the first 80 samples, so what I do is the next. I use fseek to go to byte 44 of the file (where the data starts) according to https://ccrma.stanford.edu/courses/422/projects/WaveFormat/

Then I read in blocks of 24 bits (I'm absolutely sure the .wav file has samples of 24 bits since I created it + checked it)

Here is my code:

void WavData::leerTodo(char *fname){
    double array[1000];
    FILE* fp = fopen(fname,"rb");
    fseek(fp, 44, SEEK_SET);
    if (fp) {
        fread(array,sizeof(double), 100, fp);
        for (int i = 0; i<100; i++) {
            cout<<"datos del .wav son es "<<array[i]<<"\n";
        }
    }
}

When I compare the results with the data I get from matlab, it's totally different by 10 to the power or 300 or so. I'm using xcode.

24-bit .wav file has integer samples, not floating point (float/double) ones; floating point IEEE samples in waves have usually 32 or 64 bits. Just read the data into 32-bit int; note that the alignment, signedness and endianess have to match, too (Wave data is usually signed in two's complement format). Alternatively, as pointed out in the comment, export it to 16-bit to allow easy packing and alignment matching on the data.

For example, Sound Foundry 6.0 allows 8, 16, 24 and 32 bit integer samples and 32 & 64 bit floating IEEE samples; most professional audio applications go along the same lines here, mostly because the hardware supports only those bit resolutions.

further reading: http://en.wikipedia.org/wiki/Audio_bit_depth

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