WaveIn.cpp
上传用户:amei960
上传日期:2007-02-05
资源大小:143k
文件大小:4k
- #include "stdafx.h"
- #include "WaveIn.h"
- #pragma comment(lib,"Winmm")
- CWaveIn::CWaveIn()
- {
- memset(&waveCaps,0,sizeof(waveCaps));
- bDevOpen = FALSE;
- bStartData = FALSE;
- bBuffer = FALSE;
- m_hWave = 0;
- m_hWnd = 0;
- //----------
- nChannels = 1;
- dwHertZ = HERTZ_POOR;
- wBits = 16;
- //----------
- m_pHead = NULL;
- }
- CWaveIn::~CWaveIn()
- {
- FreeBuffer();
- }
- BOOL CWaveIn::IsExistDevice()
- {
- if( waveInGetNumDevs() <= 0 )
- {
- return FALSE;
- }
- return TRUE;
- }
- WAVEINCAPS* CWaveIn::GetDeviceCap()
- {
- MMRESULT mRet = waveInGetDevCaps(WAVE_MAPPER,&waveCaps,sizeof(waveCaps));
- if( mRet == MMSYSERR_NOERROR )
- return &waveCaps;
- return NULL;
- }
- BOOL CWaveIn::Open()
- {
- if( bDevOpen )
- {
- return FALSE;
- }
- if( m_hWnd == 0 )
- {
- return FALSE;
- }
- MMRESULT mRet;
- WAVEFORMATEX wfx;
- wfx.wFormatTag = WAVE_FORMAT_PCM;
- wfx.nChannels = nChannels;
- wfx.nSamplesPerSec = dwHertZ;
- wfx.nAvgBytesPerSec = nChannels * dwHertZ * wBits / 8;
- wfx.nBlockAlign = wBits * nChannels / 8;
- wfx.wBitsPerSample = wBits;
- wfx.cbSize = 0;
- mRet = waveInOpen(0,WAVE_MAPPER,&wfx,0,0,WAVE_FORMAT_QUERY);
- if( mRet != MMSYSERR_NOERROR )
- {
- return FALSE;
- }
- mRet = waveInOpen(&m_hWave,WAVE_MAPPER,&wfx,
- (DWORD)m_hWnd,0,CALLBACK_WINDOW);
- if( mRet != MMSYSERR_NOERROR )
- {
- return FALSE;
- }
- bDevOpen = TRUE;
- return TRUE;
- }
- void CWaveIn::Close()
- {
- if (!bDevOpen)
- {
- return;
- }
- if(!m_hWave)
- {
- return;
- }
- MMRESULT mRet = waveInClose(m_hWave);
- if( mRet != MMSYSERR_NOERROR )
- {
- return;
- }
- m_hWave = 0;
- bDevOpen = FALSE;
- }
- BOOL CWaveIn::StartData()
- {
- if (bStartData)
- {
- return FALSE;
- }
- if(!m_hWave)
- {
- return FALSE;
- }
- MMRESULT mRet = waveInStart(m_hWave);
- if( mRet != MMSYSERR_NOERROR )
- {
- return FALSE;
- }
- bStartData = TRUE;
- return TRUE;
- }
- void CWaveIn::CloseData()
- {
- if ( !bStartData )
- {
- return;
- }
- if(!m_hWave)
- {
- return;
- }
- MMRESULT mRet = waveInReset(m_hWave);
- if( mRet != MMSYSERR_NOERROR )
- {
- return;
- }
- bStartData = FALSE;
- }
- BOOL CWaveIn::PerpareBuffer()
- {
- if (bBuffer)
- {
- return FALSE;
- }
- MMRESULT mRet;
- mRet = waveInReset(m_hWave);
- if( mRet != MMSYSERR_NOERROR )
- {
- return FALSE;
- }
- if( m_pHead == NULL )
- {
- m_pHead = new WAVEHDR[NUM_BUF];
- for(int i=0 ; i < NUM_BUF ; i++ )
- {
- ZeroMemory(&m_pHead[i],sizeof(WAVEHDR));
- m_pHead[i].lpData = new char[SIZE_AUDIO_FRAME];
- m_pHead[i].dwBufferLength = SIZE_AUDIO_FRAME;
- waveInPrepareHeader(m_hWave,&m_pHead[i],sizeof(WAVEHDR));
- waveInAddBuffer(m_hWave,&m_pHead[i],sizeof(WAVEHDR));
- }
- }
- else
- {
- for(int i=0 ; i < NUM_BUF ; i++ )
- {
- waveInPrepareHeader(m_hWave,&m_pHead[i],sizeof(WAVEHDR));
- waveInAddBuffer(m_hWave,&m_pHead[i],sizeof(WAVEHDR));
- }
- }
- bBuffer = TRUE;
- return TRUE;
- }
- void CWaveIn::UnperpareBuffer()
- {
- if (!bBuffer)
- {
- return;
- }
-
- if( m_pHead == NULL )
- {
- return;
- }
- MMRESULT mRet;
- for(int i = 0 ; i < NUM_BUF ; i++ )
- {
- mRet = waveInUnprepareHeader(m_hWave,&m_pHead[i],sizeof(WAVEHDR));
- if( mRet != MMSYSERR_NOERROR )
- {
- continue;
- }
- }
- bBuffer = FALSE;
- }
- void CWaveIn::FreeBuffer()
- {
- if( m_pHead == NULL )
- return;
- for(int i = 0 ; i < NUM_BUF ; i++ )
- {
- if(m_pHead[i].lpData)
- {
- delete []m_pHead[i].lpData;
- m_pHead[i].lpData = NULL;
- }
- }
- delete []m_pHead;
- m_pHead = NULL;
- }
- BOOL CWaveIn::Start()
- {
- if( !Open() )
- {
- return FALSE;
- }
- if (!PerpareBuffer())
- {
- Close();
- return FALSE;
- }
- if (!StartData())
- {
- Close();
- UnperpareBuffer();
- return FALSE;
- }
- return TRUE;
- }
- void CWaveIn::Stop()
- {
- CloseData();
- UnperpareBuffer();
- Close();
- }