正在读取Wav文件,输出错误
Reading Wav Files, wrong output
我以前发布过一个模拟问题(认为最好重新开始,希望能得到更多意见)。基本上,问题是我试图从.wav文件中读取数据,然而,输出与MatLab中的不同。
在C++中,输出为:
-128
而在Matlab:中
0
输出完全不同,不仅仅是微小的差异,而且整个数据集都是错误的,我似乎不知道为什么。我认为这可能与endianness有关,但我不确定。以下是有关.wav文件的标题信息:
**** WAV FILE *****
Chunk IDRIFFN?
Chunk Size: 57934
Format: WAVEfmt
Format: IDfmt
FormatSize: 18
Format2: 1
Channel Num: 1
Sample Rate: 22050
Byte Rate: 22050
Align: 1
Bits Per Sample: 8
代码:
file.read(this->chunkId, 4);
file.read(reinterpret_cast<char*>(&this->chunkSize), 4);
file.read(this->format, 4);
file.read(this->formatId, 4);
file.read(reinterpret_cast<char*>(&this->formatSize), 4);
file.read(reinterpret_cast<char*>(&this->format2), 2);
file.read(reinterpret_cast<char*>(&this->numChannels), 2);
file.read(reinterpret_cast<char*>(&this->sampleRate), 4);
file.read(reinterpret_cast<char*>(&this->byteRate), 4);
file.read(reinterpret_cast<char*>(&this->align), 2);
file.read(reinterpret_cast<char*>(&this->bitsPerSample), 4);
char testing[4] = {0};
int testingSize = 0;
while(file.read(testing, 4) && (testing[0] != 'd' ||
testing[1] != 'a' ||
testing[2] != 't' ||
testing[3] != 'a'))
{
file.read(reinterpret_cast<char*>(&testingSize), 4);
file.seekg(testingSize, std::ios_base::cur);
}
this->dataId[0] = testing[0];
this->dataId[1] = testing[1];
this->dataId[2] = testing[2];
this->dataId[3] = testing[3];
file.read(reinterpret_cast<char*>(&this->dataSize), 4);
this->data = new char[this->dataSize];
file.read(data,this->dataSize);
cout << "**** WAV FILE *****" << endl;
cout << "Chunk ID" << this->chunkId << endl;
cout << "Chunk Size" << this->chunkSize << endl;
cout << "Format: " << this->format << endl;
cout << "Format ID" << this->formatId << endl;
cout << "FormatSize" << this->formatSize << endl;
cout << "Format2 " << this->format2 << endl;
cout << "Channel Num" << this->numChannels << endl;
cout << "Sample Rate" << this->sampleRate << endl;
cout << "Byte Rate" << this->byteRate << endl;
cout << "Align" << this->align << endl;
cout << "Bits Per Sample" << this->bitsPerSample << endl;
cout << "Size" << testingSize << endl;
for(unsigned i=0; (i < 20); i++){
cout << (float) data[i] << endl;
}
return true;
有人看到我哪里出错了吗?我试过调试它,但没有任何乐趣(我正在使用g++进行编译)。如果有任何帮助,我们将不胜感激:)对不起,我一直问这个问题,现在真的很烦人!
感谢
我认为WAV文件中的块必须与最近的WORD边界对齐。如果在你解析的格式部分之后,你的not在下一个单词边界,你不会进入while循环,你的代码会假设你在数据部分。
同样,如果您的任何块的长度是奇数字节,您可能会遇到麻烦。
好吧,看看代码,它看起来像是一个强制转换错误。很明显,您试图输出20个浮点值(每个浮点值由4个字节组成),但实际输出的只是20个字节,每个字节都转换为一个浮点值。我想你可能想要这个
for(unsigned i=0; (i < 20); i++){
cout << ((float*) data)[i] << endl;
}
换句话说,您可以将数据转换为浮点指针,然后应用索引。不要先应用索引,然后再转换为浮点值。
我认为在matlab中,值在-1到1之间进行归一化,因此为了在c++中获得类似的输出,需要对它们进行缩放。在-32768到32767范围内的16位样本的示例,并且您希望它们缩放到-1.0到1.0。这样做的方法是除以32768.0(-32768/32768.0==-1,32767/32768略小于1)。
for(unsigned i=0; (i < 20); i++){
cout << ((float) data[i])/32768.0 << endl;
}
相关文章:
- 输出错误,问题是找到总和5000位数字cpp
- 使用复制构造函数的程序输出错误
- 异或字符串加密/解密输出错误
- 在linux上使用g++输出错误,在windows上更正
- 使用递归函数 (c++) 将长字符串转换为整数时输出错误
- ECDSA 密钥对生成输出错误
- fscanf() 输出错误的值
- 输入 1024 后十进制到二进制转换的输出错误?
- 输出错误命令的条件语句
- cuSparse (cusparseDcsrgemm) 中的矩阵乘法输出错误的结果
- 为什么 du -sh 输出错误大小的内存映射文件
- VS2017 的输出错误,但 mingw 有效
- 如何使FFMPEG C 代码不输出错误消息
- 乘以时输出错误.这是我编译器中的错误吗?C
- 输出错误:两个不同编译器上的不同输出:Prime Cryptarithm USACO
- 为什么输出错误崩溃
- C++输入/输出错误
- 尝试用 c++ 制作一个简单的加法器.编译成功,但输出错误
- 当我使用此合并排序代码运行时,输出错误
- 递归功能输出错误的值