将录取的数据保存为wav输出,需要注意wav的数据组成格式,基本需要下面三个操作过程.
a>产生标准的wav头部信息数据;
b>获得录取的声音数据;
a>数据后面连接b>数据,合并上面两个数据,得到一个总数据.
c>像操作文件读写一样将上面两个数据一次性写入.wav文件即可.
下面给出上面步骤实现函数:
int write_wav_head(WAVEFORMATEX *wf, char *in_buffer, int in_len, char **out_buffer, int *out_len)
{
char *buffer;
int *int_tmp,pos=0;
*out_len = WAVE_HEADER_SIZE + in_len;
buffer = (char*)malloc(*out_len);
if(buffer == NULL)
return -1;
memcpy(buffer,"RIFF",4);
pos = 4;
int_tmp = (int*)(buffer+pos);
*int_tmp = WAVE_HEADER_SIZE + in_len - 8;/**//*波形块的大小(档案大小减8)*/
pos += 4;
memcpy(buffer+pos,"WAVEfmt ",8);
pos += 8;
int_tmp = (int*)(buffer+pos);
*int_tmp = 16;
pos += 4;
/**//*格式信息*/
memcpy(buffer+pos,wf,16);
pos += 16;
memcpy(buffer+pos,"data",4);
pos += 4;
int_tmp = (int*)(buffer+pos);
*int_tmp = in_len;
pos += 4;
memcpy(buffer+pos,in_buffer,in_len);
*out_buffer = buffer;
//输出wav文件
//CFile f("rec.wav", CFile::modeCreate | CFile::modeWrite );
//f.Write(buffer,*int_tmp);
//f.Close();
return 0;
}
//**outbuffer为指向输出的数据信息地址