2

我需要从波形文件中读取标题变量并显示它们是什么。我正在使用以下代码,但我的输出数字太大。我已经搜索了几个小时的解决方案。帮助将不胜感激!谢谢。我从https://ccrma.stanford.edu/courses/422/projects/WaveFormat/获得了波形声音文件格式

输出: 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;
}`
4

2 回答 2

3

因此,您的代码基本上可以工作——如果您使用文件格式规范的作者使用的相同编译器和 O/S(32 位 Windows)来编译它。您希望您的编译器完全按照您需要匹配文件字节来布置您的结构。例如,我可以在 win32 上编译和运行它,并完美地读取 WAV 文件——一直到头文件的可变部分,您未能编码其可变性。

在编写了大量代码来处理各种文件格式之后,我建议您放弃尝试读入结构,而是为“读取接下来的 4 个字节并将它们转换为 int”之类的东西制作一些简单的实用函数.

Notice things like the "extra format bytes". Parts of the file format depend on the values in previous parts of the file format. That's why you generally need to think of it as a dynamic reading process rather than one big read to grab the headers. It's not hard to keep the result highly portable C that will work between operating systems without relying on O/S specific things like stat() or adding library dependencies for things like htonl() -- should portability (even portability to a different compiler or even just different compiler options on the same O/S) be desirable.

于 2013-02-19T02:55:33.567 回答
0

您似乎注意到了字节序问题,但处理它的方法是使用htonl, ntohl, htons, and, ntohs。这是您的号码问题的一部分。

在这里阅读:

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

请注意,这里还有很多关于 WAV 文件的其他帖子。你考虑过阅读它们吗?

此外,还有一些标准方法可以通过 windows 上的 windows API 或statlinux/unix 上获取文件信息,例如大小

于 2013-02-19T02:22:54.657 回答