工控编程吧
标题:
上位机MFC生成正弦波通过声卡输出源代码
[打印本页]
作者:
qq263946146
时间:
2019-9-18 10:07
标题:
上位机MFC生成正弦波通过声卡输出源代码
(, 下载次数: 1)
上传
点击文件名下载附件
如上图,例程运行后,可以点击开始或停止。
会在电脑喇叭上听到例程输出声音。
例程可以作为正弦波发生器使用。
例程创建了一个集成类CSoundOut。
可以在外部实例化一个对象CSinGenerator m_SoundOut;
调用
m_SoundOut.OpenOutput();
m_SoundOut.CloseOutput();
就可以声音的输出与停止。
关键代码段
MMRESULT CSoundOut::OpenOutput()
{
MMRESULT result;
result=waveOutGetNumDevs();
if (result == 0)
{
AfxMessageBox("No Sound Output Device");
return result;
}
// test for Mic available
result=waveOutGetDevCaps (0, &m_WaveOutDevCaps, sizeof(WAVEOUTCAPS));
if ( result!= MMSYSERR_NOERROR)
{
AfxMessageBox(_T("Sound output Cannot determine card capabilities !"));
}
m_Terminate = FALSE;
#ifndef CALL_BACK_TEST
// The SoundOut Devive is OK now we can create an Event and start the Thread
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
// start the thread at the end of the buffer init
// init format
WaveInitFormat(1/* mono*/,m_WaveOutSampleRate /* khz */,16 /* bits */);
// Open Output
#ifdef CALL_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("Sound output Cannot Open Device!"));
return result;
}
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) );
//MMRESULT waveOutPrepareHeader( HWAVEOUT hwi, LPWAVEHDR pwh, UINT cbwh );
if ( (result!= MMSYSERR_NOERROR) || ( m_WaveHeader[m_Toggle].dwFlags != WHDR_PREPARED) )
{
AfxMessageBox(_T(" Sound Output Cannot Prepare Header !"));
return result;
}
result = waveOutWrite( m_WaveOut, &m_WaveHeader[m_Toggle], sizeof(WAVEHDR) );
if (result!= MMSYSERR_NOERROR)
{
AfxMessageBox(_T(" Sound Output Cannot Write Buffer !"));
return result;
}
// register the second frame don't wait for the end of the first one
// so when we will be notified, this second frame will be currently output when we will reload the first one
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) );
//MMRESULT waveOutPrepareHeader( HWAVEOUT hwi, LPWAVEHDR pwh, UINT cbwh );
if ( (result!= MMSYSERR_NOERROR) || ( m_WaveHeader[m_Toggle].dwFlags != WHDR_PREPARED) )
{
AfxMessageBox(_T(" Sound Output Cannot Prepare Header !"));
return result;
}
result = waveOutWrite( m_WaveOut, &m_WaveHeader[m_Toggle], sizeof(WAVEHDR) );
if (result!= MMSYSERR_NOERROR)
{
AfxMessageBox(_T(" Sound Output Cannot Write Buffer !"));
return result;
}
#ifndef CALL_BACK_TEST
m_WaveOutThread->ResumeThread();
#endif
return result;
}
复制代码
void CSoundOut::CloseOutput()
{
if (m_WaveOut)
waveOutPause(m_WaveOut);
Sleep(50); // wait for the pause
// CloseHandle(m_WaveOut);
m_Terminate = TRUE;
if (m_WaveOutEvent )
SetEvent(m_WaveOutEvent);
Sleep(50); // wait for the thread to terminate
if (m_WaveOut)
{
waveOutReset(m_WaveOut);
waveOutClose(m_WaveOut);
}
}
复制代码
(, 下载次数: 0)
上传
点击文件名下载附件
[MFC408]1[/MFC408]
[halcon]1[/halcon]
[weixinlianxi]1[/weixinlianxi]
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4