简体   繁体   English

在C ++中读取.wav数据时出现意外输出

[英]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. 我想读取.wav文件的数据。 I am just interested in the first 80 samples, so what I do is the next. 我只是对前80个样本感兴趣,所以我接下来将要做。 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/ 我使用fseek根据https://ccrma.stanford.edu/courses/422/projects/WaveFormat/转到文件的第44个字节(数据从此处开始)

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) 然后,我读取了24位的块(我绝对确定.wav文件具有24位的样本,因为我创建了它并对其进行了检查)

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. 当我将结果与从matlab获得的数据进行比较时,其功效与功率或300左右完全相差10倍。 I'm using xcode. 我正在使用xcode。

24-bit .wav file has integer samples, not floating point (float/double) ones; 24位.wav文件具有整数样本,而不是浮点数(float / double); floating point IEEE samples in waves have usually 32 or 64 bits. 波中的浮点IEEE样本通常具有32或64位。 Just read the data into 32-bit int; 只需将数据读入32位int即可; note that the alignment, signedness and endianess have to match, too (Wave data is usually signed in two's complement format). 请注意,对齐方式,符号性和字节性也必须匹配(Wave数据通常以二进制补码格式签名)。 Alternatively, as pointed out in the comment, export it to 16-bit to allow easy packing and alignment matching on the data. 或者,如注释中指出的那样,将其导出为16位以允许对数据进行简单打包和对齐匹配。

For example, Sound Foundry 6.0 allows 8, 16, 24 and 32 bit integer samples and 32 & 64 bit floating IEEE samples; 例如,Sound Foundry 6.0允许8、16、24和32位整数样本以及32和64位浮点IEEE样本。 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 进一步阅读: http : //en.wikipedia.org/wiki/Audio_bit_depth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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