c语言读取wav文件的文件头
时间: 2023-11-25 13:07:21 浏览: 131
可以使用以下代码读取wav文件的文件头:
```
#include <stdio.h>
typedef struct {
char chunkID[4];
int chunkSize;
char format[4];
char subchunk1ID[4];
int subchunk1Size;
short audioFormat;
short numChannels;
int sampleRate;
int byteRate;
short blockAlign;
short bitsPerSample;
char subchunk2ID[4];
int subchunk2Size;
} WavHeader;
int main() {
FILE *fp;
WavHeader header;
fp = fopen("test.wav", "rb");
if (fp == NULL) {
printf("Error opening file\n");
return -1;
}
fread(&header, sizeof(header), 1, fp);
printf("ChunkID: %c%c%c%c\n", header.chunkID[0], header.chunkID[1], header.chunkID[2], header.chunkID[3]);
printf("ChunkSize: %d\n", header.chunkSize);
printf("Format: %c%c%c%c\n", header.format[0], header.format[1], header.format[2], header.format[3]);
printf("Subchunk1ID: %c%c%c%c\n", header.subchunk1ID[0], header.subchunk1ID[1], header.subchunk1ID[2], header.subchunk1ID[3]);
printf("Subchunk1Size: %d\n", header.subchunk1Size);
printf("AudioFormat: %d\n", header.audioFormat);
printf("NumChannels: %d\n", header.numChannels);
printf("SampleRate: %d\n", header.sampleRate);
printf("ByteRate: %d\n", header.byteRate);
printf("BlockAlign: %d\n", header.blockAlign);
printf("BitsPerSample: %d\n", header.bitsPerSample);
printf("Subchunk2ID: %c%c%c%c\n", header.subchunk2ID[0], header.subchunk2ID[1], header.subchunk2ID[2], header.subchunk2ID[3]);
printf("Subchunk2Size: %d\n", header.subchunk2Size);
fclose(fp);
return 0;
}
```
阅读全文