我试图读取RIFF文件的头文件。一切看起来都很好。但是当我编写并运行时,我读取的所有文件都具有相同的数据大小。我不知道为什么?我已经运行了一些关于读取RIFF文件的header_file的代码,但它并没有什么不同。
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stdint.h>
#include <cmath>
using namespace std;
#define SWAPPED_US(x) (((x) >> 8) | ((x) << 8))
#define SWAPPED_UL(x) ((((x) >> 24) & 0xff) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) | (((x) << 24) & 0xff000000))
#pragma pack(push, 1)
typedef struct {
char ID [4];
uint32_t Size;
char Format [4];
char SubID_1 [4];
uint32_t SubSize_1;
uint16_t AudioFmt;
uint16_t Channel;
uint32_t Frequency;
uint32_t bytesRate;
uint16_t BlockAlign;
uint16_t BitsPerSample;
char SubID_2 [4];
uint32_t SubSize_2;
} RIFF_header;
#pragma pack(pop)
int getFileSize(FILE *inFile){
int fileSize = 0;
fseek(inFile,0,SEEK_END);
fileSize=ftell(inFile);
fseek(inFile,0,SEEK_SET);
return fileSize;
}
int main ()
{
FILE *fpoint;
fpoint = fopen ("test.wav", "rb");
RIFF_header *wave_file; wave_file = new RIFF_header;
fread ((void*)wave_file, sizeof (RIFF_header), (size_t)1, fpoint);
wave_file->SubSize_2 = SWAPPED_UL(wave_file->SubSize_2); // I think it's big-endianess so we have to swap byte
cout << "Size of file: " << getFileSize(fpoint) << endl;
cout << "Header: " << wave_file->ID[0]
<< wave_file->ID[1]
<< wave_file->ID[2]
<< wave_file->ID[3] << endl;
cout << "Format: " << wave_file->Format[0]
<< wave_file->Format[1]
<< wave_file->Format[2]
<< wave_file->Format[3] << endl;
cout << "Sub-chunk1 ID: " << wave_file->SubID_1[0]
<< wave_file->SubID_1[1]
<< wave_file->SubID_1[2]
<< wave_file->SubID_1[3] << endl;
cout << "Sub-chunk1 Size: " << wave_file->SubSize_1 << endl;
cout << "Audio Format: " << wave_file->AudioFmt << endl;
cout << "Channel: " << wave_file->Channel << endl;
cout << "Sample Rate: " << wave_file->Frequency << endl;
cout << "Byte Rate: " << wave_file->bytesRate << endl;
cout << "BlockAlign: " << wave_file->BlockAlign << endl;
cout << "Bits per sample: " << wave_file->BitsPerSample << endl;
cout << "Sub-chunk2 ID: " << wave_file->SubID_2[0]
<< wave_file->SubID_2[1]
<< wave_file->SubID_2[2]
<< wave_file->SubID_2[3] << endl;
cout << "Sub-chunk2 Size: " << wave_file->SubSize_2 << endl;
return 0;
}
答案 0 :(得分:0)
检查你的SubSize_1
...有时它是16个字节,有时是18个。如果你错了,你的SubSize_2
也会出错......