我正在编写一个函数来加载波形文件,并在此过程中将数据拆分为 2 个单独的缓冲区(如果它是立体声的)。程序到达 i = 18 并在左通道 fread pass 期间崩溃。(您可以忽略 couts,它们只是用于调试。)也许我应该一次性加载文件并使用 memmove 填充缓冲区?
if(params.channels == 2){
params.leftChannelData = new unsigned char[params.dataSize/2];
params.rightChannelData = new unsigned char[params.dataSize/2];
bool isLeft = true;
int offset = 0;
const int stride = sizeof(BYTE) * (params.bitsPerSample/8);
for(int i = 0; i < params.dataSize; i += stride)
{
std::cout << "i = " << i << " ";
if(isLeft){
std::cout << "Before Left Channel, ";
fread(params.leftChannelData+offset, sizeof(BYTE), stride, file + i);
std::cout << "After Left Channel, ";
}
else{
std::cout << "Before Right Channel, ";
fread(params.rightChannelData+offset, sizeof(BYTE), stride, file + i);
std::cout << "After Right Channel, ";
offset += stride;
std::cout << "After offset incr.\n";
}
isLeft != isLeft;
}
} else {
params.leftChannelData = new unsigned char[params.dataSize];
fread(params.leftChannelData, sizeof(BYTE), params.dataSize, file);
}