分类: C/C++
2008-03-11 17:52:40
typedef struct{WORD wFormatTag; WORD nChannels; DWORD nSamplesPerSec; DWORD nAvgBytesPerSec; WORD nBlockAlign; WORD wBitsPerSample; WORD cbSize; } WAVEFORMATEX;具体参数解释如下:
typedef struct { LPSTR lpData; DWORD dwBufferLength; DWORD dwBytesRecorded; DWORD dwUser; DWORD dwFlags; DWORD dwLoops; struct wavehdr_tag * lpNext; DWORD reserved; } WAVEHDR;lpData:波形数据的缓冲区地址
pBuffer1=(PBYTE)malloc(INP_BUFFER_SIZE); pBuffer2=(PBYTE)malloc(INP_BUFFER_SIZE);对设备头结构体分配内存
pWaveHdr1=reinterpret_cast然后使用wave音频相关函数对输入数据进行操作:(malloc(sizeof(WAVEHDR))); pWaveHdr2=reinterpret_cast (malloc(sizeof(WAVEHDR)));
waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR)); waveInPrepareHeader(hWaveIn,pWaveHdr2,sizeof(WAVEHDR));②为波形输入设备添加缓冲区
waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ; waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;③启动声音输入设备,将输入数据写入内存
waveInStart (hWaveIn) ;⑵编写消息处理函数,其中,MM_WIM_DATA 函数是本程序的核心。其主要作用是将输入数据另行保存在一缓冲区内(pSaveBuffer),该缓冲区的长度将随着已录入数据的大小而增加,从而实现保存输入话音数据的功能。同时,可将缓冲区内数据保存为wav文件。其具体实现如下:
CFile m_file; CFileException fileException; CString m_csFileName= "F:\\audio.wav";//保存路径 m_file.Open(m_csFileName,CFile::modeCreate|CFile::modeReadWrite, &fileException); DWORD m_WaveHeaderSize = 38; DWORD m_WaveFormatSize = 18; m_file.SeekToBegin(); m_file.Write("RIFF",4); unsigned int Sec=(sizeof pSaveBuffer + m_WaveHeaderSize); m_file.Write(&Sec,sizeof(Sec)); m_file.Write("WAVE",4); m_file.Write("fmt ",4); m_file.Write(&m_WaveFormatSize,sizeof(m_WaveFormatSize)); m_file.Write(&waveform.wFormatTag,sizeof(waveform.wFormatTag)); m_file.Write(&waveform.nChannels,sizeof(waveform.nChannels)); m_file.Write(&waveform.nSamplesPerSec,sizeof(waveform.nSamplesPerSec)); m_file.Write(&waveform.nAvgBytesPerSec,sizeof(waveform.nAvgBytesPerSec)); m_file.Write(&waveform.nBlockAlign,sizeof(waveform.nBlockAlign)); m_file.Write(&waveform.wBitsPerSample,sizeof(waveform.wBitsPerSample)); m_file.Write(&waveform.cbSize,sizeof(waveform.cbSize)); m_file.Write("data",4); m_file.Write(&dwDataLength,sizeof(dwDataLength)); m_file.Write(pSaveBuffer,dwDataLength); m_file.Seek(dwDataLength,CFile::begin); m_file.Close();存在的问题: