我在阅读WAVE文件时遇到问题。我目前正在使用本教程(http://rogerchansdigitalworld.blogspot.de/2010/05/how-to-read-wav-format-file-in-c.html)来深入了解这些内容。 所以这是我的代码的一部分:
void loadWaveFile(char *fname, WavData *ret) {
FILE* fp = fopen(fname,"rb");
if (fp) {
char id[5];
unsigned long size;
short format_tag, channels, block_align, bits_per_sample;
unsigned long format_length, sample_rate, avg_bytes_sec, data_size;
fread(id, sizeof(char),4,fp);
id[4] = '\0';
if(!strcmp(id,"RIFF")) {
fread(&size, sizeof(unsigned long),1,fp);
fread(id, sizeof(char),4,fp);
id[4] = '\0';
if(!strcmp(id,"WAVE")) {
fread(id,sizeof(char),4,fp);
fread(&format_length, sizeof(unsigned long),1,fp);
fread(&format_tag, sizeof(short),1,fp);
fread(&channels, sizeof(short),1,fp);
fread(&sample_rate, sizeof(unsigned long),1,fp);
fread(&avg_bytes_sec, sizeof(unsigned long),1,fp);
fread(&block_align, sizeof(short),1,fp);
fread(&bits_per_sample, sizeof(short),1,fp);
fread(id,sizeof(char),4,fp);
fread(&data_size, sizeof(unsigned long),1,fp);
ret->size = data_size/sizeof(short);
ret->data =(short*) malloc(data_size);
fread(ret->data, sizeof(short), ret->size,fp);
}
else {
std::cout<<"Error: RIFF Datei, aber keine wave Datei\n";
}
}
else {
std::cout<<"Error: keine RIFF Datei\n";
}
}
else {
std::cout<<"Error: keine Datei ausgewaehlt\n";
}
fclose(fp);
与链接中显示的几乎相同。 我的主要问题是第二个if语句。当我运行代码时,它会读取&#34; fmt&#34;而不是&#34; WAVE&#34;。好像它会跳过&#34; WAVE&#34;部分并直接跳入&#34; fmt&#34;部分。任何人都知道这里有什么问题吗? 这里还详细描述了WAVE文件http://mathmatrix.narod.ru/Wavefmt.html
谢谢!