c语言读取wav格式音频数据
代码如下所示,实现的是对wav格式数据的读取。我不明白的地方是:
1.读取到的数据是放在databuf中吗?
2.如果是,那我要怎么才能把数据显示出来看?或者怎样才能操作里面的数据
求大神指导一下
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <memory.h>
#include <string.h>
typedef unsigned char BYTE;
struct RIFF_HEADER
{
//RIFF_HEADER
unsigned char szRiffID[4]; // 'R','I','F','F'
unsigned long dwRiffSize;
unsigned char szRiffFormat[4]; // 'W','A','V','E'
// WAVE_FORMAT
};
struct FMT_BLOCK
{
// FMT_BLOCK
unsigned char szFmtID[4]; // 'f','m','t',' '
unsigned long dwFmtSize;
// dwFmtSize为16则其后16字节,否则为18,末尾有2个字节的附加信息
unsigned short wFormatTag;
unsigned short wChannels;
unsigned long dwSamplesPerSec;
unsigned long dwAvgBytesPerSec;
unsigned short wBlockAlign;
unsigned short wBitsPerSample;
// unsigned short ultraMessage;
};
struct FACT_BLOCK
{
unsigned char szFactID[4]; // 'f','a','c','t'
unsigned long dwFactSize;
};
struct DATA_BLOCK
{
// DATA_BLOCK
unsigned char szDataID[4]; // 'd','a','t','a'
unsigned long dwDataSize;
};
void main()
{
FILE * fp=NULL;
fp=fopen("News Item 1.wav","rb");
RIFF_HEADER rh;
fread(&rh,1,sizeof(rh),fp);
FMT_BLOCK fb;
fread(&fb,1,sizeof(fb),fp);
if(fb.dwFmtSize==18)
{
unsigned short ultraMessage;
fread(&ultraMessage,1,2,fp);
}
DATA_BLOCK db;
// 紧接着再读入4个字节,判断是data数据块,还是fact数据块
fread(&db,1,sizeof(db),fp);
// 定义一个字符串,取出标志串
char strflag[5];
memcpy(strflag,db.szDataID,4);
strflag[4]='\0';
// 读入音频数据的存储缓冲区
BYTE * databuf=NULL;
// 比较字符串
if(strcmp(strflag,"data")==0)
{
databuf=new BYTE[db.dwDataSize];
}
else if(strcmp(strflag,"fact")==0)
{ // 说明刚刚读入的是fact数据块,而非data数据块
fread(&db,1,sizeof(db),fp);
databuf=new BYTE[db.dwDataSize];
}
// 读入音频数据到指定的音频缓冲区
fread(databuf,1,db.dwDataSize,fp);
fclose(fp);
}