上位机MFC生成正弦波通过声卡输出源代码
如上图,例程运行后,可以点击开始或停止。
会在电脑喇叭上听到例程输出声音。
例程可以作为正弦波发生器使用。
例程创建了一个集成类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);
- }
- }
复制代码
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|