2013-02-19 88 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