声音的录制(VC)(保存为WAV文件) (转)

这里只录制了最原始的声音,格式PCM,未作任何处理,未压缩,(要压缩可能需要用到其它库)

本文用的是回调函数方式
waveInOpen(&m_hWaveIn,0,&m_soundFormat, (DWORD)(waveInProc),0, CALLBACK_FUNCTION);
最后一个参数就是回调类型
如果是CALLBACK_WINDOW,需传入一个窗口指针
waveInOpen(&m_hWaveIn,0,&m_soundFormat, (DWORD)(*CWnd),0, CALLBACK_WINDOW);
录制完了会向窗口发送消息MM_WIM_DATA

#include
#include
#include
#include
#include
#include
#pragma comment(lib, "winmm.lib")
DWORD MyThread(DWORD wPame);
void Init();
void Clear();
void SaveWav();//保存为WAV文件
DWORD CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,
   DWORD dwInstance,DWORD dwParam1,DWORD dwParam2);
//回调函数,当缓冲区录满就会调用该函数

static bool  IsRecord=false;//停止标志,因为这里在WIM_DATA 是不断AddBuff,要靠其它变量去控制,停止
//如果一开始申请的空间就足够大,没必要再增加了
//#define MAX_BUFF_SOUNDSIZE 4096

#define MAX_BUFF_SOUNDSIZE 176400

WAVEFORMATEX  m_soundFormat;//声音格式
HWAVEIN     m_hWaveIn;
WAVEHDR     m_pWaveHdrIn[3];
CHAR     m_cBufferIn[3][MAX_BUFF_SOUNDSIZE];
FILE*fp;  //保存为文件,只是为了给另一份代码使用,

int main()
{
 Init();
 HANDLE hd=CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)MyThread,NULL,NULL,NULL);
 WaitForSingleObject(hd,INFINITE);//用线程只是为了等上几秒,否则会直接结束,
 Clear();
//SaveWav(); //保存为WAV文件
 system("PAUSE");
 return 0;
}
DWORD MyThread(DWORD wPame)
{
 Sleep(25000);
 printf("%d\n",clock());
 return 0;
}
void Init()
{
 fp=fopen("myvoice","wb");
 if (fp==NULL)
 {
 printf("create err!\n");
 exit(0);
 }
 int n=waveInGetNumDevs(); //获取声音输入设备个数,没有就退出
 printf("Has Wave Num:%d\n",n);
 if (n<1)
 {
 printf("Not Find WaveIn\n");
 exit(0);
 }
 memset(&m_soundFormat,0,sizeof(m_soundFormat));//设置声音格式
 m_soundFormat.wFormatTag=WAVE_FORMAT_PCM;
 m_soundFormat.nChannels=2;
 m_soundFormat.nSamplesPerSec=44100 ;
 m_soundFormat.nAvgBytesPerSec=44100*2*16/8;
 m_soundFormat.nBlockAlign=2*16/8;
 m_soundFormat.wBitsPerSample=16;
 m_soundFormat.cbSize=0;

 MMRESULT m_res;

 m_res=waveInOpen(&m_hWaveIn,WAVE_MAPPER,&m_soundFormat,(DWORD)(waveInProc),0,CALLBACK_FUNCTION);//打开设备
 if (m_res!=0)
 {
  printf("open waveIn err!\n");
  exit(0);
 }
 unsigned int id;
 waveInGetID(m_hWaveIn,&id);//你所使用的输入设备ID,-1为默认
 printf("you use waveid:%d\n",id);
 int i;
 for (i=0;i<3;i++) //设置内存块格式
 {
  m_pWaveHdrIn[i].lpData=m_cBufferIn[i];
  m_pWaveHdrIn[i].dwBufferLength=MAX_BUFF_SOUNDSIZE;
  m_pWaveHdrIn[i].dwBytesRecorded=0;
  m_pWaveHdrIn[i].dwUser=i;
  m_pWaveHdrIn[i].dwFlags=0;
  waveInPrepareHeader(m_hWaveIn,&m_pWaveHdrIn[i],sizeof(WAVEHDR)); //准备内存块录音
  waveInAddBuffer(m_hWaveIn,&m_pWaveHdrIn[i],sizeof(WAVEHDR)); //增加内存块
 }
 IsRecord=true;
 waveInStart(m_hWaveIn);//开始录音
}

DWORD CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,
   DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)
{
 if (uMsg==WIM_DATA)
 {
//做语音聊天的话就可以在这里获取数据,压缩发送
  WAVEHDR*p=(WAVEHDR*)dwParam1;//dwParam1指向WAVEHDR的地址
  printf("getdate id:%d size:%d timestemp:%d\n",p->dwUser,p->dwBytesRecorded,clock());
  int i=p->dwUser;
  if (!IsRecord) //0表示停止了
  {
   waveInUnprepareHeader(m_hWaveIn,p,sizeof(WAVEHDR)); //释放
   return 0;
  }else
  {
  fwrite(&m_cBufferIn[i],1,p->dwBytesRecorded,fp);
  waveInUnprepareHeader(m_hWaveIn,p,sizeof(WAVEHDR)); //释放
  }
  p->lpData=m_cBufferIn[i];
  p->dwBufferLength=MAX_BUFF_SOUNDSIZE;
  p->dwBytesRecorded=0;
  p->dwUser=i;
  p->dwFlags=0;
  waveInPrepareHeader(m_hWaveIn,p,sizeof(WAVEHDR)); //准备内存块录音
  waveInAddBuffer(m_hWaveIn,p,sizeof(WAVEHDR)); //增加内存块
 }
 if (uMsg==WIM_OPEN)
 {
  printf("open\n");
 }
 if (uMsg==WIM_CLOSE)
 {
  printf("close\n");
 }
 return 0;
}
void Clear()
{
 IsRecord=false;
 Sleep(3000);//等上几秒,否则容易卡,界面编程时经常遇到
 printf("Start Stop\n");
 waveInStop(m_hWaveIn); 
 printf("Start Reset\n");
 waveInReset(m_hWaveIn); 
 printf("Start Close\n");
 waveInClose(m_hWaveIn);
 fclose(fp);
}


struct RIFF_HEADER
{
char szRiffID[4];//'R','I','F','F'
DWORD dwRiffSize;//总文件长度-8
char szRiffFormat[4];//'W','A','V','E'
};
struct WAVE_FORMAT//16字节
{
WORD    wFormatTag;        /* format type */
    WORD    nChannels;         /* number of channels (i.e. mono, stereo...) */
    DWORD   nSamplesPerSec;    /* sample rate */
    DWORD   nAvgBytesPerSec;   /* for buffer estimation */
    WORD    nBlockAlign;       /* block size of data */
    WORD    wBitsPerSample;    /* Number of bits per sample of mono data */
};
struct FMT_BLOCK
{
char  szFmtID[4]; // 'f','m','t',' '
DWORD  dwFmtSize;//WAVE格式所占字节16
WAVE_FORMAT wavFormat;
 };
struct DATA_BLOCK
{
char  szDataID[4]; // 'd','a','t','a'
DWORD  dwDataSize;
 };

void SaveWav()
{
FILE*fwav=fopen("myvoice.wav","wb");
fp=fopen("myvoice","rb");
if (fwav==NULL||fp==NULL)
{
printf("err\n");
return;
}
long filesize=0;
long n=0;
//获取语音数据
fseek(fp,0,SEEK_END);
filesize=ftell(fp);
fseek(fp,0,SEEK_SET);
RIFF_HEADER m_riff={0};
FMT_BLOCK m_fmt={0};
DATA_BLOCK m_data={0};
//RIFF
strncpy(m_riff.szRiffID,"RIFF",4);
m_riff.dwRiffSize=4+sizeof(FMT_BLOCK)+sizeof(DATA_BLOCK)+filesize;
strncpy(m_riff.szRiffFormat,"WAVE",4);
//fmt
strncpy(m_fmt.szFmtID,"fmt ",4);
m_fmt.dwFmtSize=sizeof(WAVE_FORMAT);
m_fmt.wavFormat=*(WAVE_FORMAT*)&m_soundFormat;
//Data
strncpy(m_data.szDataID,"data",4);
m_data.dwDataSize=filesize;
fwrite(&m_riff,1,sizeof(RIFF_HEADER),fwav);
fwrite(&m_fmt,1,sizeof(FMT_BLOCK),fwav);
fwrite(&m_data,1,sizeof(DATA_BLOCK),fwav);

while((n=fread(m_cBufferIn[0],1,MAX_BUFF_SOUNDSIZE,fp))>0)
fwrite(m_cBufferIn[0],1,n,fwav);

fclose(fwav);
fclose(fp);
}

你可能感兴趣的:(c++,VC)