C语言读取wav音频例子
时间: 2023-06-05 08:08:05 浏览: 148
你好!以下是一份C语言读取wav音频的例子:
```c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct WavHeader {
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;
};
int main() {
FILE *pFile = fopen("test.wav", "rb");
if(pFile == NULL) {
printf("Open file fail.");
return -1;
}
struct WavHeader wavHeader;
fread(&wavHeader, sizeof(struct WavHeader), 1, pFile);
printf("Chunk ID: %s\n", wavHeader.chunkId);
printf("Chunk Size: %d\n", wavHeader.chunkSize);
printf("Format: %s\n", wavHeader.format);
printf("Subchunk 1 ID: %s\n", wavHeader.subchunk1Id);
printf("Subchunk 1 Size: %d\n", wavHeader.subchunk1Size);
printf("Audio Format: %d\n", wavHeader.audioFormat);
printf("Num Channels: %d\n", wavHeader.numChannels);
printf("Sample Rate: %d\n", wavHeader.sampleRate);
printf("Byte Rate: %d\n", wavHeader.byteRate);
printf("Block Align: %d\n", wavHeader.blockAlign);
printf("Bits Per Sample: %d\n", wavHeader.bitsPerSample);
printf("Subchunk 2 ID: %s\n", wavHeader.subchunk2Id);
printf("Subchunk 2 Size: %d\n", wavHeader.subchunk2Size);
int dataSize = wavHeader.subchunk2Size;
char *pDataBuf = (char*)malloc(sizeof(char)*dataSize);
fread(pDataBuf, dataSize, 1, pFile);
fclose(pFile);
return 0;
}
```
这是一个基础的读取wav格式音频的例子,首先使用FILE结构体打开音频文件,然后读取WAV头部信息并打印出来,最后读取音频数据保存到缓冲区中。当然还需要更多的处理才能对读取的音频数据进行操作。希望对你有所帮助!
阅读全文