WaveOut.cpp
上传用户:amei960
上传日期:2007-02-05
资源大小:143k
文件大小:3k
源码类别:

Audio

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "WaveOut.h"
  3. #pragma comment(lib,"Winmm")
  4. CWaveOut::CWaveOut()
  5. {
  6. memset(&waveCaps,0,sizeof(waveCaps));
  7. bDevOpen = FALSE;
  8. m_hWave = 0;
  9. //----------
  10. nChannels = 1;
  11. dwHertZ = HERTZ_POOR;
  12. wBits = 16;
  13. //----------
  14. m_BufferQueue = 0;
  15. }
  16. CWaveOut::~CWaveOut()
  17. {
  18. Stop();
  19. }
  20. BOOL CWaveOut::IsExistDevice()
  21. {
  22. if( waveOutGetNumDevs() <= 0 )
  23. {
  24. return FALSE;
  25. }
  26. return TRUE;
  27. }
  28. WAVEOUTCAPS* CWaveOut::GetDeviceCap()
  29. {
  30. MMRESULT mRet = waveOutGetDevCaps(WAVE_MAPPER,&waveCaps,sizeof(waveCaps));
  31. if( mRet == MMSYSERR_NOERROR )
  32. return &waveCaps;
  33. return NULL;
  34. }
  35. BOOL CWaveOut::Open()
  36. {
  37. if( bDevOpen )
  38. {
  39. return FALSE;
  40. }
  41. MMRESULT mRet;
  42. WAVEFORMATEX wfx;
  43. wfx.wFormatTag = WAVE_FORMAT_PCM;
  44. wfx.nChannels = nChannels;
  45. wfx.nSamplesPerSec = dwHertZ;
  46. wfx.nAvgBytesPerSec = nChannels * dwHertZ * wBits / 8;
  47. wfx.nBlockAlign = wBits * nChannels / 8;
  48. wfx.wBitsPerSample = wBits;
  49. wfx.cbSize = 0;
  50. mRet = waveOutOpen(0,WAVE_MAPPER,&wfx,0,0,WAVE_FORMAT_QUERY);
  51. if( mRet != MMSYSERR_NOERROR )
  52. {
  53. return FALSE;
  54. }
  55. mRet = waveOutOpen(&m_hWave,WAVE_MAPPER,&wfx,(DWORD)waveOutProc,(DWORD)this,CALLBACK_FUNCTION);
  56. if( mRet != MMSYSERR_NOERROR )
  57. {
  58. return FALSE;
  59. }
  60. bDevOpen = TRUE;
  61. return TRUE;
  62. }
  63. void CWaveOut::Close()
  64. {
  65. if (!bDevOpen)
  66. {
  67. return;
  68. }
  69. if(!m_hWave)
  70. {
  71. return;
  72. }
  73. waveOutReset(m_hWave);
  74. MMRESULT mRet = waveOutClose(m_hWave);
  75. if( mRet != MMSYSERR_NOERROR )
  76. {
  77. return;
  78. }
  79. m_hWave = 0;
  80. bDevOpen = FALSE;
  81. }
  82. void CALLBACK CWaveOut::waveOutProc(HWAVEOUT hwo,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)
  83. {
  84. CWaveOut *pWaveOut;
  85. pWaveOut = (CWaveOut *)dwInstance;
  86. WAVEHDR* pWaveHead = NULL;
  87. switch(uMsg)
  88. {
  89. case WOM_OPEN:
  90. break;
  91. case WOM_CLOSE:
  92. break;
  93. case WOM_DONE:
  94. pWaveHead = (WAVEHDR*)dwParam1;
  95. if( pWaveHead != NULL )
  96. {
  97. waveOutUnprepareHeader(hwo,pWaveHead,sizeof(WAVEHDR));
  98. pWaveOut->SubBuffer();
  99. delete []pWaveHead->lpData;
  100. delete pWaveHead;
  101. }
  102. break;
  103. }
  104. }
  105. BOOL CWaveOut::Start()
  106. {
  107. if( !Open() )
  108. {
  109. return FALSE;
  110. }
  111. return TRUE;
  112. }
  113. BOOL CWaveOut::Play(char* buf,UINT nSize)
  114. {
  115. if( !bDevOpen )
  116. {
  117. return FALSE;
  118. }
  119. if( GetBufferNum() >= 5 )
  120. {
  121. return FALSE;
  122. }
  123. MMRESULT mRet;
  124. char* lpData = NULL;
  125. WAVEHDR* pWaveHead = new WAVEHDR;
  126. ZeroMemory(pWaveHead,sizeof(WAVEHDR));
  127. lpData = new char[nSize];
  128. pWaveHead->dwBufferLength = nSize;
  129. memcpy(lpData,buf,nSize);
  130. pWaveHead->lpData = lpData;
  131. mRet = waveOutPrepareHeader(m_hWave,pWaveHead,sizeof(WAVEHDR));
  132.    if( mRet != MMSYSERR_NOERROR )
  133. {
  134. return FALSE;
  135. }
  136. mRet = waveOutWrite(m_hWave,pWaveHead,sizeof(WAVEHDR));
  137.    if( mRet != MMSYSERR_NOERROR )
  138. {
  139. return FALSE;
  140. }
  141. AddBuffer();
  142. return TRUE;
  143. }
  144. void CWaveOut::Stop()
  145. {
  146. Close();
  147. }