2013-02-19 98 views
2

我需要從波形文件中讀取標題變量並顯示它們的內容。我正在使用下面的代碼,但我的輸出數字太大了。我已經搜索了幾個小時的解決方案。幫助將非常感謝!謝謝。我得到了波音效檔格式從https://ccrma.stanford.edu/courses/422/projects/WaveFormat/讀取Wav頭文件產生奇怪結果的代碼? C

輸出: Wav file header information: Filesize 3884 bytes RIFF header RIFF WAVE header WAVE Subchunk1ID fmt Chunk Size (based on bits used) 604962816 Subchunk1Size 268435456 Sampling Rate 288030720 Bits Per Sample 2048 AudioFormat 256 Number of channels 2048 Byte Rate 288030720 Subchunk2ID Subchunk2Size 1684108385

這裏是源:

#include <stdio.h> 
#include <stdlib.h> 

typedef struct WAV_HEADER 
{ 
char    RIFF[4];   
int     ChunkSize;  
char    WAVE[4];  
char    fmt[4];   
int     Subchunk1Size;        
short int   AudioFormat; 
short int   NumOfChan;  
int     SamplesPerSec; 
int     bytesPerSec;  
short int   blockAlign;  
short int   bitsPerSample; 
int     Subchunk2Size; 
char    Subchunk2ID[4]; 
}wav_hdr; 

int getFileSize(FILE *inFile); 
int main(int argc,char *argv[]) 
{ 
//check startup conditions 
if(argc >= 2); //we have enough arguments -- continue 
else { printf("\nUSAGE: program requires a filename as an argument -- please try again\n"); exit(0);} 

wav_hdr wavHeader; 
FILE *wavFile; 
int headerSize = sizeof(wav_hdr),filelength = 0; 
wavFile = fopen(argv[1],"r"); 
if(wavFile == NULL) 
{ 
    printf("Unable to open wave file\n"); 
    exit(EXIT_FAILURE); 
} 
fread(&wavHeader,headerSize,1,wavFile); 
filelength = getFileSize(wavFile); 
fclose(wavFile); 
printf("\nWav file header information:\n"); 
printf("Filesize\t\t\t%d bytes\n",filelength); 
printf("RIFF header\t\t\t%c%c%c%c\n",wavHeader.RIFF[0],wavHeader.RIFF[1],wavHeader.RIFF[2],wavHeader.RIFF[3]); 
printf("WAVE  header\t\t\t%c%c%c%c\n",wavHeader.WAVE[0],wavHeader.WAVE[1],wavHeader.WAVE[2],wavHeader.WAVE[3]); 
    printf("Subchunk1ID\t\t\t%c%c%c%c\n",wavHeader.fmt[0],wavHeader.fmt[1],wavHeader.fmt[2],wavHeader.fmt[3]); 
printf("Chunk Size (based on bits used)\t%d\n",wavHeader.ChunkSize); 
printf("Subchunk1Size\t\t\t%d\n",wavHeader.Subchunk1Size); 
printf("Sampling Rate\t\t\t%d\n",wavHeader.SamplesPerSec); //Sampling frequency of the wav file 
printf("Bits Per Sample\t\t\t%d\n",wavHeader.bitsPerSample); //Number of bits used per sample 
printf("AudioFormat\t\t\t%d\n",wavHeader.AudioFormat); 
printf("Number of channels\t\t%d\n",wavHeader.bitsPerSample);  //Number of channels (mono=1/sterio=2) 
printf("Byte Rate\t\t\t%d\n",wavHeader.bytesPerSec); //Number of bytes per second 
printf("Subchunk2ID\t\t\t%c%c%c%c\n",wavHeader.Subchunk2ID[0],wavHeader.Subchunk2ID[1],wavHeader.Subchunk2ID[2],wavHeader.Subchunk2ID[3]); 
printf("Subchunk2Size\t\t\t%d\n",wavHeader.Subchunk2Size); 
printf("\n"); 
return 0; 
} 

int getFileSize(FILE *inFile) 
{ 
int fileSize = 0; 
fseek(inFile,0,SEEK_END); 
fileSize=ftell(inFile); 
fseek(inFile,0,SEEK_SET); 
return fileSize; 
}` 
+0

操作系統? – 2013-02-19 02:25:32

+0

而且,如果您要依賴內存中特定的結構佈局,我們還需要知道您計劃使用哪種編譯器和哪些選項進行編譯。 – 2013-02-19 02:29:37

+0

使用gcc的UNIX操作系統,將所有內容都關閉 – 2013-02-19 02:38:19

回答

3

所以,你的代碼基本上工作 - 如果你用相同的編譯編譯器和文件格式規範的作者使用的O/S(32位Windows)。你希望你的編譯器完全按照需要匹配文件字節來佈局你的結構。例如,我可以在win32上編譯和運行它,並且完美地讀取WAV文件 - 直至頭部的可變部分,這些部分的變化性無法編碼。

已經寫了大量的代碼來操縱各種文件格式,我建議你放棄嘗試讀入結構,而是爲諸如「讀取下4個字節並將它們變成一些簡單的實用函數變成一個整數「。

注意類似「額外格式字節」的東西。文件格式的一部分取決於文件格式以前部分的值。這就是爲什麼你通常需要把它看作是一個動態的閱讀過程,而不是一個大的閱讀來抓取標題。要保持結果的高度可移植性C不會依賴於像stat()這樣的O/S特定事物,或者爲像htonl()這樣的事物添加庫依賴關係,而是要保證可移植性(即便可移植到不同的編譯器或即使在同一個O/S上只有不同的編譯器選項)也是可取的。

+0

謝謝!有很多見解 – 2013-02-19 03:51:32

0

好像你注意到端的問題,但處理方式,它是htonlntohl,htonsntohs。這是你的號碼問題的一部分。

這裏閱讀:

http://www.beej.us/guide/bgnet/output/html/multipage/htonsman.html 

注意,這裏還有很多其他職位上的WAV文件。你有沒有考慮過讀它們?

此外,還有標準的方式來獲取文件信息,如大小,無論是通過Windows或stat Linux上的窗口API/UNIX

+0

爲什麼不鼓勵使用ftell()來獲取文件大小的相當便捷的方法? – 2013-02-19 02:29:53

+0

是的,這是一種方法。理所當然的。但如果它非常方便,那麼stat()爲什麼寫?因爲文件元數據不僅僅是文件中有多少字節。 – 2013-02-19 02:33:07

+0

謝謝,我認爲這已經讓我走向了正確的方向。我會更多地考慮它。 – 2013-02-19 02:38:39