vc 60 源代码.docx
- 文档编号:7137181
- 上传时间:2023-01-21
- 格式:DOCX
- 页数:70
- 大小:74.81KB
vc 60 源代码.docx
《vc 60 源代码.docx》由会员分享,可在线阅读,更多相关《vc 60 源代码.docx(70页珍藏版)》请在冰豆网上搜索。
vc60源代码
#include"stdafx.h"
#include"math.h"
#include"SignalGenerate.h"
#include"SoundOut.h"
#pragmacomment(lib,"winmm")
/////////////////////////////////////////////////////////////////////////////////////////////
//commentthislinetocompilewithathreadimplementationratherthanacallbackfunction
//#defineCALL_BACK_TEST
/////////////////////////////////////////////////////////////////////////////////////////////
#ifdef_DEBUG
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#definenewDEBUG_NEW
#endif
#definerealdouble
UINTWaveOutThreadProc(void*pParam);
voidCALLBACKWaveOutProc(HWAVEOUThwo,UINTuMsg,DWORDdwInstance,DWORDdwParam1,DWORDdwParam2);
//////////////////////////////////////////////////////////////////////
//Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSoundOut:
:
CSoundOut()
{
m_NbMaxSamples=MAX_OUTPUT_SAMPLES;
m_WaveOutSampleRate=11025;
m_Toggle=0;
}
CSoundOut:
:
~CSoundOut()
{
CloseOutput();
}
///////////////////////////////////////////////////////////////////
MMRESULTCSoundOut:
:
OpenOutput()
{
MMRESULTresult;
result=waveOutGetNumDevs();
if(result==0)
{
AfxMessageBox("NoSoundOutputDevice");
returnresult;
}
//testforMicavailable
result=waveOutGetDevCaps(0,&m_WaveOutDevCaps,sizeof(WAVEOUTCAPS));
if(result!
=MMSYSERR_NOERROR)
{
AfxMessageBox(_T("SoundoutputCannotdeterminecardcapabilities!
"));
}
m_Terminate=FALSE;
#ifndefCALL_BACK_TEST
//TheSoundOutDeviveisOKnowwecancreateanEventandstarttheThread
m_WaveOutEvent=CreateEvent(NULL,FALSE,FALSE,"WaveOutThreadEvent");
m_WaveOutThread=AfxBeginThread(WaveOutThreadProc,this,THREAD_PRIORITY_TIME_CRITICAL,0,CREATE_SUSPENDED,NULL);
m_WaveOutThread->m_bAutoDelete=TRUE;
#endif
//startthethreadattheendofthebufferinit
//initformat
WaveInitFormat(1/*mono*/,m_WaveOutSampleRate/*khz*/,16/*bits*/);
//OpenOutput
#ifdefCALL_BACK_TEST
result=waveOutOpen(&m_WaveOut,0,&m_WaveFormat,(DWORD)WaveOutProc,(ULONG)this,CALLBACK_FUNCTION);
#else
result=waveOutOpen(&m_WaveOut,0,&m_WaveFormat,(DWORD)m_WaveOutEvent,NULL,CALLBACK_EVENT);
#endif
if(result!
=MMSYSERR_NOERROR)
{
AfxMessageBox(_T("SoundoutputCannotOpenDevice!
"));
returnresult;
}
m_Toggle=0;
m_SizeRecord=m_NbMaxSamples;
m_WaveHeader[m_Toggle].lpData=(CHAR*)&OutputBuffer[m_Toggle][0];
m_WaveHeader[m_Toggle].dwBufferLength=m_SizeRecord*2;
m_WaveHeader[m_Toggle].dwFlags=0;
result=waveOutPrepareHeader(m_WaveOut,&m_WaveHeader[m_Toggle],sizeof(WAVEHDR));
//MMRESULTwaveOutPrepareHeader(HWAVEOUThwi,LPWAVEHDRpwh,UINTcbwh);
if((result!
=MMSYSERR_NOERROR)||(m_WaveHeader[m_Toggle].dwFlags!
=WHDR_PREPARED))
{
AfxMessageBox(_T("SoundOutputCannotPrepareHeader!
"));
returnresult;
}
result=waveOutWrite(m_WaveOut,&m_WaveHeader[m_Toggle],sizeof(WAVEHDR));
if(result!
=MMSYSERR_NOERROR)
{
AfxMessageBox(_T("SoundOutputCannotWriteBuffer!
"));
returnresult;
}
//registerthesecondframedon'twaitfortheendofthefirstone
//sowhenwewillbenotified,thissecondframewillbecurrentlyoutputwhenwewillreloadthefirstone
m_Toggle=1;
m_SizeRecord=m_NbMaxSamples;
m_WaveHeader[m_Toggle].lpData=(CHAR*)&OutputBuffer[m_Toggle][0];
m_WaveHeader[m_Toggle].dwBufferLength=m_SizeRecord*2;
m_WaveHeader[m_Toggle].dwFlags=0;
result=waveOutPrepareHeader(m_WaveOut,&m_WaveHeader[m_Toggle],sizeof(WAVEHDR));
//MMRESULTwaveOutPrepareHeader(HWAVEOUThwi,LPWAVEHDRpwh,UINTcbwh);
if((result!
=MMSYSERR_NOERROR)||(m_WaveHeader[m_Toggle].dwFlags!
=WHDR_PREPARED))
{
NoYes
NoYes
YesNo
YesNo
YesNo
Case1Case2Case3
AfxMessageBox(_T("SoundOutputCannotPrepareHeader!
"));
returnresult;
}
result=waveOutWrite(m_WaveOut,&m_WaveHeader[m_Toggle],sizeof(WAVEHDR));
if(result!
=MMSYSERR_NOERROR)
{
AfxMessageBox(_T("SoundOutputCannotWriteBuffer!
"));
returnresult;
}
#ifndefCALL_BACK_TEST
m_WaveOutThread->ResumeThread();
#endif
returnresult;
}
voidCSoundOut:
:
AddBuffer()
{
MMRESULTresult;
if(m_Toggle==0)
m_Toggle=1;
else
m_Toggle=0;
result=waveOutUnprepareHeader(m_WaveOut,&m_WaveHeader[m_Toggle],sizeof(WAVEHDR));
if(result!
=MMSYSERR_NOERROR)
{
AfxMessageBox(_T("SoundoutputCannotUnPrepareHeader!
"));
return;
};
m_SizeRecord=m_NbMaxSamples;
m_WaveHeader[m_Toggle].lpData=(CHAR*)&OutputBuffer[m_Toggle][0];
m_WaveHeader[m_Toggle].dwBufferLength=m_SizeRecord*2;
m_WaveHeader[m_Toggle].dwLoops=0;
m_WaveHeader[m_Toggle].dwFlags=0;//WHDR_BEGINLOOP;
result=waveOutPrepareHeader(m_WaveOut,&m_WaveHeader[m_Toggle],sizeof(WAVEHDR));
if((result!
=MMSYSERR_NOERROR)||(m_WaveHeader[m_Toggle].dwFlags!
=WHDR_PREPARED))
AfxMessageBox(_T("SoundoutputCannotPrepareHeader!
"));
result=waveOutWrite(m_WaveOut,&m_WaveHeader[m_Toggle],sizeof(WAVEHDR));
if(result!
=MMSYSERR_NOERROR)
AfxMessageBox(_T("SoundoutputCannotAddBuffer!
"));
}
/*
WAVE_FORMAT_1M0811.025kHz,mono,8-bit
WAVE_FORMAT_1M1611.025kHz,mono,16-bit
WAVE_FORMAT_1S0811.025kHz,stereo,8-bit
WAVE_FORMAT_1S1611.025kHz,stereo,16-bit
WAVE_FORMAT_2M0822.05kHz,mono,8-bit
WAVE_FORMAT_2M1622.05kHz,mono,16-bit
WAVE_FORMAT_2S0822.05kHz,stereo,8-bit
WAVE_FORMAT_2S1622.05kHz,stereo,16-bit
WAVE_FORMAT_4M0844.1kHz,mono,8-bit
WAVE_FORMAT_4M1644.1kHz,mono,16-bit
WAVE_FORMAT_4S0844.1kHz,stereo,8-bit
WAVE_FORMAT_4S1644.1kHz,stereo,16-bit
*/
voidCSoundOut:
:
WaveInitFormat(WORDnCh,//numberofchannels(mono,stereo)
DWORDnSampleRate,//samplerate
WORDBitsPerSample)
{
m_WaveFormat.wFormatTag=WAVE_FORMAT_PCM;
m_WaveFormat.nChannels=nCh;
m_WaveFormat.nSamplesPerSec=nSampleRate;
m_WaveFormat.nAvgBytesPerSec=nSampleRate*nCh*BitsPerSample/8;
m_WaveFormat.nBlockAlign=m_WaveFormat.nChannels*BitsPerSample/8;
m_WaveFormat.wBitsPerSample=BitsPerSample;
m_WaveFormat.cbSize=0;
}
///////////////////////////////////////////////////////////////////////////
//thecomutationfortheOutputsamplesneedtobecalibratedaccording
//totheSoundOutboardaddanOffsetandaMultcoef.
voidCSoundOut:
:
ComputeSamples(SHORT*pt)
{
}
voidCSoundOut:
:
CloseOutput()
{
if(m_WaveOut)
waveOutPause(m_WaveOut);
Sleep(50);//waitforthepause
//CloseHandle(m_WaveOut);
m_Terminate=TRUE;
if(m_WaveOutEvent)
SetEvent(m_WaveOutEvent);
Sleep(50);//waitforthethreadtoterminate
if(m_WaveOut)
{
waveOutReset(m_WaveOut);
waveOutClose(m_WaveOut);
}
}
voidCSoundOut:
:
RazBuffers()
{
for(inti=0;i { OutputBuffer[0][i]=0; OutputBuffer[1][i]=0; } } voidCSoundOut: : StopOutput() { waveOutPause(m_WaveOut); } voidCSoundOut: : StartOutput() { waveOutRestart(m_WaveOut); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //GlogalThreadprocedurefortheCSoundOutclass //ItcannotbeincludedinsidetheClass // //TheLPARAMistheClasspointeritcanbethebaseclassCSoundOut //oradrivedclasslikeCFft //Thevalueofthisparametrecanchangeaccordingbecause //OpenMic()callavirtualfuntion ///////////////////////////////////////////////////////////////////////////////////////////////////////////// #definePT_S((CSoundOut*)pParam) UINTWaveOutThreadProc(void*pParam) { //CSoundOut*SoundOut=(classCSoundOut*)pParam; UINTresult; UINTFirstPass=TRUE; if(FirstPass) result=WaitForSingleObject(PT_S->m_WaveOutEvent,INFINITE); FirstPass=FALSE; while(! PT_S->m_Terminate) { result=WaitForSingleObject(PT_S->m_WaveOutEvent,INFINITE); if((result==WAIT_OBJECT_0)&&(! PT_S->m_Terminate)) { PT_S->AddBuffer();//Toggleaschangedstatehere! Togglepointtothejustreceivedbuffer PT_S->ComputeSamples(&PT_S->m_Toggle); } else return0;// } return0; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////// //Testwithacallbackfunctioninsteadofathread // //////////////////////////////////////////////////////////////////////////////////////////////////////////// #ifdefCALL_BACK_TEST voidCALLBACKWaveOutProc(HWAVEOUThwo,UINTuMsg,DWORDpParam,DWORDdwParam1,DWORDdwParam2) { if(! PT_S->m_Terminate) switch(uMsg) { caseWOM_DONE: PT_S->AddBuffer(); PT_S->ComputeSamples(&PT_S->m_Toggle); break; caseWOM_OPEN: break; caseWOM_CLOSE: break; } } ////////////////////////////////////////////////////////////////////// /* */ ////////////////////////////////////////////////////////////////////////////////////////// #include"stdafx.h" #include"math.h" #include"SignalGenerate.h" #include"SoundOut.h" #pragmacomment(lib,"winmm") ///////////////////////////////////////////////////////////////////////////////////////////// //commentthislinetocompilewithathreadimplementationratherthanacallbackfunction //#defineCALL_BACK_TEST ///////////////////////////////////////////////////////////////////////////////////////////// #ifdef_DEBUG #undefTHIS_FILE staticcharTHIS_FILE[]=__FILE__; #definenewDEBUG_NEW #endif #definerealdouble UINTWaveOutThreadProc(void*pParam); voidCALLBACKWaveOutProc(HWAVEOUThwo,UINTuMsg,DWORDdwInstance,DWORDdwParam1,DWORDdwParam2); ////////////////////////////////////////////////////////////////////// //Construction/Destruction ////////////////////////////////////////////////////////////////////// CSoundOut: : CSou
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- vc 60 源代码