我試圖讀取RIFF文件的標頭文件。一切看起來都很好。但是當我編寫並運行時,我讀取的所有文件都具有相同的數據大小。我不知道爲什麼?我曾經跑過一些關於閱讀RIFF文件的header_file的代碼,但並沒有什麼不同。讀取WAVE文件的標頭文件
#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;
}
子塊的大小是存儲在標題爲little- endian,而不是big-endian。 – Michael
什麼是你總是得到什麼神奇的大小?也許你的頭結構的對齊關閉了一些字節,並且你讀了其他的東西然後你認爲(所有東西都是傾斜的)? –
那個號碼是2147418112,我交換字節後,是65407. – user2530847