SoundIn.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:5k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // SoundIn.cpp: implementation of the CSoundIn class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "../../stdafx.h"
- #include "SoundIn.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- #pragma comment( lib , "winmm.lib" )
- //声音队列
- #define SEQUENCE_NUM 20
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CSoundIn::CSoundIn()
- {
- this->m_hRecord = NULL;
- this->m_bRecord = false;
- this->sequenceNum = 0;
- //采集的图像格式
- this->SetWaveFormat( 1 , 8000 , 16 );
- this->OnSound = NULL;
- this->pContext = NULL;
- this->encode = new CG729aCompress ( ); // new CGsmCompress( ); // new CG729aCompress ( );
- }
- CSoundIn::~CSoundIn( )
- {
- this->Stop( );
- delete this->encode;
- }
- void CSoundIn::SetWaveFormat( int channels /*1*/, long samplerate /*8000*/, int bitspersample/*16*/ )
- {
- memset(&this->wavFormat , 0 , sizeof( this->wavFormat ) );
- this->wavFormat.cbSize = 0;
- this->wavFormat.wFormatTag = WAVE_FORMAT_PCM; // pcm
- this->wavFormat.nChannels = channels; // mono
- this->wavFormat.nSamplesPerSec = samplerate; // 8000 kHz
- this->wavFormat.wBitsPerSample = bitspersample; // 16 bit
- this->wavFormat.nBlockAlign = this->wavFormat.nChannels * this->wavFormat.wBitsPerSample / 8;
- this->wavFormat.nAvgBytesPerSec = this->wavFormat.nSamplesPerSec * this->wavFormat.nBlockAlign;
- }
- WAVEFORMATEX * CSoundIn::GetFormat( int * size )
- {
- if( size )
- * size = sizeof( this->wavFormat );
- return &this->wavFormat;
- }
- void CSoundIn::SetCompress( int mode )
- {
- if( this->encode )
- {
- if( this->encode->GetType( ) != mode )
- {
- CCompress * p = this->encode;
- if( mode == CCompress::GSM610 )
- this->encode = new CGsmCompress( );
- else if( mode == CCompress::G729a )
- this->encode = new CG729aCompress( );
- else
- this->encode = NULL;
- delete p;
- }
- }
- else if( mode == CCompress::GSM610 )
- this->encode = new CGsmCompress( );
- else if( mode == CCompress::G729a )
- this->encode = new CG729aCompress( );
- }
- bool CSoundIn::Start( void ( * OnSound )( void * pContext , char * buffer , int size ) , void * pContext )
- {
- if( this->m_bRecord )
- return true;
- if( ! OnSound )
- return false;
- this->sequenceNum = 0;
- this->OnSound = OnSound;
- this->pContext = pContext;
- if( ! this->m_hRecord )
- {
- MMRESULT mmReturn = ::waveInOpen( &this->m_hRecord , WAVE_MAPPER , &this->wavFormat , ( DWORD )waveInProc , ( DWORD )this , CALLBACK_FUNCTION );
- if( mmReturn != MMSYSERR_NOERROR )
- {
- this->Stop( ); return false;
- }
- for( int i = 0; i < SEQUENCE_NUM ; i ++ )
- {
- WAVEHDR * pHdr = new WAVEHDR;
- memset( pHdr , 0 , sizeof( WAVEHDR ) );
- pHdr->lpData = new char[ SIZE_AUDIO_FRAME ];
- pHdr->dwBufferLength = SIZE_AUDIO_FRAME;
- ::waveInPrepareHeader( this->m_hRecord , pHdr ,sizeof( WAVEHDR ) );
- ::waveInAddBuffer( this->m_hRecord , pHdr , sizeof( WAVEHDR ) );
- }
- this->m_bRecord = true;
- this->sequenceNum = SEQUENCE_NUM;
- mmReturn = ::waveInStart( this->m_hRecord );
- if( mmReturn != MMSYSERR_NOERROR )
- {
- this->Stop( ); return false;
- }
- return true;
- }
- return false;
- }
- void CSoundIn::Stop( void )
- {
- CWaitCursor cursor;
- if( this->m_bRecord )
- {
- this->m_bRecord = false;
- while( this->sequenceNum > 0 )
- ::Sleep( 100 );
- ::waveInReset( this->m_hRecord );
- ::waveInStop( this->m_hRecord );
- ::waveInClose( this->m_hRecord );
- this->m_hRecord = NULL;
- }
- }
- bool CSoundIn::Filter( char * buffer , int size )
- {
- return true;
- }
- void CALLBACK CSoundIn::waveInProc( HWAVEIN hwi , UINT uMsg , DWORD dwInstance , DWORD dwParam1 ,DWORD dwParam2 )
- {
- CSoundIn * pThis = ( CSoundIn * )dwInstance;
- if( uMsg == WIM_DATA )
- {
- WAVEHDR * pHdr = ( WAVEHDR * )dwParam1;
- MMRESULT mmReturn = ::waveInUnprepareHeader( hwi , pHdr , sizeof( WAVEHDR ) );
- if( mmReturn != MMSYSERR_NOERROR )
- return ;
- if( pThis->m_bRecord )
- {
- if( pThis->OnSound && pThis->Filter( pHdr->lpData , pHdr->dwBufferLength ) )
- {
- int size = pHdr->dwBufferLength;
- char * buf = pHdr->lpData;
- if( pThis->encode )
- buf = pThis->encode->Compress( pHdr->lpData , &size );
- pThis->OnSound( pThis->pContext , buf , size );
- }
- mmReturn = ::waveInPrepareHeader( hwi , pHdr , sizeof( WAVEHDR ) );
- if( mmReturn == MMSYSERR_NOERROR )
- {
- mmReturn = ::waveInAddBuffer( hwi , pHdr , sizeof( WAVEHDR ) );
- if( mmReturn == MMSYSERR_NOERROR )
- return;
- }
- }
- delete [ ]pHdr->lpData;
- delete [ ]pHdr;
- pThis->sequenceNum --;
- }
- }