Record.cpp
资源名称:111.rar [点击查看]
上传用户:mony888
上传日期:2022-07-26
资源大小:1247k
文件大小:4k
源码类别:
Windows CE
开发平台:
Visual C++
- #include "stdafx.h"
- //#include "Record.h"
- buf_Mode ModeTab[3] = {
- { 4, 2 },
- { 8, 4 },
- { 16, 8 },
- };
- #define MMRETCHECK(ret)
- if ( ret != MMSYSERR_NOERROR ) { return ret; }
- #define ICHECK(ret)
- if ( ret < 0 ) { return MMSYSERR_ERROR; }
- Record::Record(BufMode ModeKey, DWORD dwBufSiz):m_BUFBLKSIZE( ModeTab[ModeKey].ucSize ), m_BUFBLKUSED( ModeTab[ModeKey].ucUsed ), m_BufSiz(dwBufSiz)
- {
- m_pCodecInterf = NULL;
- m_hevDone = NULL;
- m_pDataHdr = NULL;
- m_pEncData = NULL;
- m_fh = NULL;
- }
- Record::~Record()
- {
- }
- MMRESULT Record::SetBufSiz(DWORD dwBufSiz)
- {
- m_BufSiz = dwBufSiz;
- return MMSYSERR_NOERROR;
- }
- MMRESULT Record::InitRecord(WAVEFORMATEX & wfx)
- {
- int i;
- m_pDataHdr = new WAVEHDR[m_BUFBLKSIZE];
- if ( NULL == m_pDataHdr )
- {
- return MMSYSERR_NOMEM;
- }
- memset(m_pDataHdr, 0, sizeof(WAVEHDR) * m_BUFBLKSIZE);
- for ( i = 0; i < m_BUFBLKSIZE; i++ )
- {
- m_pDataHdr[i].lpData = new CHAR[m_BufSiz];
- if ( NULL == m_pDataHdr[i].lpData )
- {
- for ( i--; i < 0; i-- )
- {
- delete [] m_pDataHdr[i].lpData;
- }
- delete [] m_pDataHdr;
- return MMSYSERR_NOMEM;
- }
- m_pDataHdr[i].dwBufferLength = m_BufSiz;
- }
- m_wfx = wfx;
- return MMSYSERR_NOERROR;
- }
- MMRESULT Record::ClosRecord()
- {
- int i;
- if ( NULL == m_pDataHdr )
- {
- return MMSYSERR_ERROR;
- }
- for ( i = 0; i < m_BUFBLKSIZE; i++ )
- {
- if ( m_pDataHdr[i].lpData )
- {
- delete [] m_pDataHdr[i].lpData;
- }
- }
- delete [] m_pDataHdr;
- return MMSYSERR_NOERROR;
- }
- MMRESULT Record::OpenMic()
- {
- int i;
- int iRet;
- MMRESULT ret;
- if ( 0 == waveInGetNumDevs() )
- {
- return MMSYSERR_NOTENABLED;
- }
- m_uiDeviceId = 0;
- m_hevDone = CreateEvent(NULL, FALSE, FALSE, NULL);
- if ( NULL == m_hevDone )
- {
- return MMSYSERR_NOMEM;
- }
- ret = waveInOpen(&m_hwi, m_uiDeviceId, &m_wfx, ( DWORD )m_hevDone, NULL, CALLBACK_EVENT);
- MMRETCHECK(ret);
- for ( i = 0; i < m_BUFBLKSIZE; i++ )
- {
- ret = waveInPrepareHeader(m_hwi, m_pDataHdr + i, sizeof(WAVEHDR));
- MMRETCHECK(ret);
- }
- for ( i = 0; i < m_BUFBLKUSED; i++ )
- {
- ret = waveInAddBuffer(m_hwi, m_pDataHdr + i, sizeof(WAVEHDR));
- MMRETCHECK(ret);
- }
- iRet = m_pCodecInterf->init(&m_Data);
- ICHECK(iRet);
- m_dwWaitTime = ( m_BufSiz * 8000 ) / ( m_wfx.nSamplesPerSec * m_wfx.wBitsPerSample * 2 );
- ret = waveInStart(m_hwi);
- MMRETCHECK(ret);
- m_BufNO = 0;
- m_dwPCMSiz = 0;
- return MMSYSERR_NOERROR;
- }
- MMRESULT Record::ToRec()
- {
- int iRet;
- MMRESULT ret;
- BOOL bRet;
- DWORD dwWait;
- DWORD dwBytesWrot;
- dwWait = WaitForSingleObject(m_hevDone, m_dwWaitTime);
- if ( WAIT_OBJECT_0 == dwWait )
- {
- // Encode audio data.
- iRet = m_pCodecInterf->encode(&m_Data, (void**)&m_pEncData, (INT32*)&m_EncDataSiz, (UINT8*)m_pDataHdr[m_BufNO].lpData, (INT32*)&(m_pDataHdr[m_BufNO].dwBufferLength));
- ICHECK(iRet);
- bRet = WriteFile(m_fh, m_pEncData, m_EncDataSiz, &dwBytesWrot, NULL);
- if (!bRet || dwBytesWrot < m_EncDataSiz)
- {
- return MMSYSERR_ERROR;
- }
- // bRet = WriteFile(m_fh, (UINT8*)m_pDataHdr[m_BufNO].lpData, m_pDataHdr[m_BufNO].dwBufferLength, &dwBytesWrot, NULL);
- // if (!bRet || dwBytesWrot < m_pDataHdr[m_BufNO].dwBufferLength)
- // {
- // return MMSYSERR_ERROR;
- // }
- // m_dwPCMSiz += m_pDataHdr[m_BufNO].dwBufferLength;
- ret = waveInAddBuffer(m_hwi, m_pDataHdr + ((m_BufNO + m_BUFBLKUSED) % m_BUFBLKSIZE), sizeof(WAVEHDR));
- MMRETCHECK(ret);
- m_BufNO = (m_BufNO + 1) % m_BUFBLKSIZE;
- ResetEvent(m_hevDone);
- }
- return MMSYSERR_NOERROR;
- }
- void Record::SetCodecInterface(CodecInterfaceType& CodecInterf)
- {
- m_pCodecInterf = &CodecInterf;
- }
- MMRESULT Record::ClosMic()
- {
- int i;
- int iRet;
- MMRESULT ret;
- BOOL bret;
- ret = waveInReset(m_hwi);
- MMRETCHECK(ret);
- for (i = 0; i < m_BUFBLKSIZE; i++)
- {
- ret = waveInUnprepareHeader(m_hwi, m_pDataHdr + i, sizeof(WAVEHDR));
- MMRETCHECK(ret);
- }
- ret = waveInClose(m_hwi);
- MMRETCHECK(ret);
- iRet = m_pCodecInterf->close(&m_Data);
- ICHECK(iRet);
- bret = CloseHandle(m_hevDone);
- if (!bret)
- {
- return MMSYSERR_INVALHANDLE;
- }
- return MMSYSERR_NOERROR;
- }
- MMRESULT Record::ToPlay(LPCTSTR pszFileName)
- {
- return MMSYSERR_NOERROR;
- }
- MMRESULT Record::ToPaus()
- {
- return MMSYSERR_NOERROR;
- }
- MMRESULT Record::ToStop()
- {
- return MMSYSERR_NOERROR;
- }
- Record & Recorder(BufMode ModeKey, DWORD dwBufSiz)
- {
- static Record Apple(ModeKey, dwBufSiz);
- return Apple;
- }