PlaySound.cpp
上传用户:panstart
上传日期:2022-04-12
资源大小:199k
文件大小:6k
源码类别:

IP电话/视频会议

开发平台:

C++ Builder

  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. //
  4. //    Project     : VideoNet version 1.1.
  5. //    Description : Peer to Peer Video Conferencing over the LAN.
  6. //   Author      : Nagareshwar Y Talekar ( nsry2002@yahoo.co.in)
  7. //    Date        : 15-6-2004.
  8. //
  9. //
  10. //    File description : 
  11. //    Name    : PlaySound.cpp
  12. //    Details : For playing the data, received from remote host.
  13. //
  14. //
  15. /////////////////////////////////////////////////////////////////////////////
  16. #include "stdafx.h"
  17. #include "VideoNet.h"
  18. #include "VideoNetDlg.h"
  19. #include "PlaySound.h"
  20. #include <mmsystem.h>
  21. #include <mmreg.h>
  22. #ifdef _DEBUG
  23. #define new DEBUG_NEW
  24. #undef THIS_FILE
  25. static char THIS_FILE[] = __FILE__;
  26. #endif
  27. //////////////////////////////////////////////////////////////////////
  28. // Construction/Destruction
  29. //////////////////////////////////////////////////////////////////////
  30. IMPLEMENT_DYNCREATE(PlaySound1, CWinThread)
  31. BEGIN_MESSAGE_MAP(PlaySound1, CWinThread)
  32. ON_THREAD_MESSAGE(WM_PLAYSOUND_STARTPLAYING, OnStartPlaying)
  33. ON_THREAD_MESSAGE(WM_PLAYSOUND_STOPPLAYING, OnStopPlaying)
  34. ON_THREAD_MESSAGE(WM_PLAYSOUND_PLAYBLOCK, OnWriteSoundData)
  35. ON_THREAD_MESSAGE(MM_WOM_DONE, OnEndPlaySoundData)
  36. ON_THREAD_MESSAGE(WM_PLAYSOUND_ENDTHREAD,OnEndThread)
  37. END_MESSAGE_MAP()
  38. PlaySound1::PlaySound1()
  39. {
  40. }
  41. PlaySound1::PlaySound1(CDialog *dialog)
  42. {
  43. log.Open("playsound.log",CFile::modeCreate | CFile::modeWrite);
  44. dlg=dialog;
  45. //GetDevProperty();
  46. /*
  47. memset(&m_WaveFormatEx,0x00,sizeof(m_WaveFormatEx));
  48. m_WaveFormatEx.wfx.wFormatTag = WAVE_FORMAT_GSM610;
  49. m_WaveFormatEx.wfx.nChannels = 1;
  50. m_WaveFormatEx.wfx.wBitsPerSample = 0;
  51. m_WaveFormatEx.wfx.cbSize = 2;
  52. m_WaveFormatEx.wfx.nSamplesPerSec = SAMPLEPSEC;
  53. m_WaveFormatEx.wfx.nAvgBytesPerSec = 1625; //(SAMPLEPSEC/320)*65 ;
  54. m_WaveFormatEx.wfx.nBlockAlign = 65;
  55. m_WaveFormatEx.wSamplesPerBlock=320;
  56. */
  57. memset(&m_WaveFormatEx,0x00,sizeof(m_WaveFormatEx));
  58. m_WaveFormatEx.wFormatTag = WAVE_FORMAT_PCM;
  59. m_WaveFormatEx.nChannels = 1;
  60. m_WaveFormatEx.wBitsPerSample = 8;
  61. m_WaveFormatEx.cbSize = 0;
  62. m_WaveFormatEx.nSamplesPerSec = SAMPLEPSEC;
  63. m_WaveFormatEx.nAvgBytesPerSec = SAMPLEPSEC ;
  64. m_WaveFormatEx.nBlockAlign = 1;
  65.   
  66. Playing = FALSE;
  67. log.WriteString("n In the constructor of Play sound");
  68. }
  69. PlaySound1::~PlaySound1()
  70. {
  71. }
  72. void PlaySound1::GetDevProperty()
  73. {
  74. CString format;
  75. WAVEOUTCAPS wavecap;
  76. int propno[]= { 
  77. WAVECAPS_LRVOLUME ,
  78. WAVECAPS_PITCH ,
  79. WAVECAPS_PLAYBACKRATE,
  80. WAVECAPS_SYNC ,
  81. WAVECAPS_VOLUME,
  82. WAVECAPS_SAMPLEACCURATE ,
  83. };
  84. CString propstr[]={
  85. "WAVECAPS_LRVOLUME ",
  86. "WAVECAPS_PITCH ",
  87. "WAVECAPS_PLAYBACKRATE",
  88. "WAVECAPS_SYNC" ,
  89. "WAVECAPS_VOLUME",
  90. "WAVECAPS_SAMPLEACCURATE" ,
  91. };
  92. // Special property
  93. format.Empty();
  94. format="nSpecial properties... n";
  95. for(int j=0;j<6;j++)
  96. {
  97. if( (wavecap.dwSupport & (unsigned)propno[j]) ==(unsigned) propno[j])
  98. {
  99. format+=propstr[j]+"n";
  100. }
  101. }
  102. log.WriteString(format);
  103. }
  104. BOOL PlaySound1::InitInstance()
  105. {
  106. return TRUE;
  107. }
  108. int PlaySound1::ExitInstance()
  109. {
  110. return CWinThread::ExitInstance();
  111. }
  112. LRESULT PlaySound1::OnStartPlaying(WPARAM wParam, LPARAM lParam)
  113. {
  114. MMRESULT mmReturn = 0;
  115. if(Playing)
  116. return FALSE;
  117. log.WriteString("n Starting playing");
  118. // open wavein device
  119.  mmReturn = ::waveOutOpen( &m_hPlay, WAVE_MAPPER,
  120. &m_WaveFormatEx, ::GetCurrentThreadId(), 0, CALLBACK_THREAD);
  121. if(mmReturn )
  122. displayError(mmReturn,"PlayStart");
  123. else
  124. {
  125. Playing = TRUE;
  126. DWORD volume=0xffffffff;
  127. char str[100];
  128. waveOutSetVolume(m_hPlay,volume);
  129. }
  130. return TRUE;
  131. }
  132. void PlaySound1::displayError(int code,char mesg[])
  133. {
  134. char errorbuffer[MAX_PATH];
  135. char errorbuffer1[MAX_PATH];
  136. waveOutGetErrorText( code,errorbuffer,MAX_PATH);
  137. sprintf(errorbuffer1,"PLAY : %s :%x:%s",mesg,code,errorbuffer);
  138. AfxMessageBox(errorbuffer1);  
  139. }
  140. LRESULT PlaySound1::OnStopPlaying(WPARAM wParam, LPARAM lParam)
  141. {
  142. MMRESULT mmReturn = 0;
  143. if(Playing==FALSE)
  144. return FALSE;
  145.         log.WriteString("n Stopped  playing");
  146. mmReturn = ::waveOutReset(m_hPlay);
  147. if(!mmReturn)
  148. {
  149. Playing = FALSE;
  150. Sleep(500);
  151. mmReturn = ::waveOutClose(m_hPlay);
  152. }
  153. return mmReturn;
  154. }
  155. LRESULT PlaySound1::OnEndPlaySoundData(WPARAM wParam, LPARAM lParam)
  156. {
  157. LPWAVEHDR lpHdr = (LPWAVEHDR) lParam;
  158. if(lpHdr)
  159. {
  160. ::waveOutUnprepareHeader(m_hPlay, lpHdr, sizeof(WAVEHDR));
  161. }
  162. return ERROR_SUCCESS;
  163. }
  164. LRESULT PlaySound1::OnWriteSoundData(WPARAM wParam, LPARAM lParam)
  165. {
  166. MMRESULT mmResult = 0;
  167. int length=(int) wParam;
  168. if(((CVideoNetDlg*)dlg)->isAudioReceive==FALSE)
  169. {
  170. log.WriteString("nreception is disabled....");
  171. return FALSE;
  172. }
  173. if(Playing==FALSE)
  174. return FALSE;
  175. log.WriteString("nplaying sound data....");
  176. // Prepare wave header for playing 
  177. WAVEHDR *lpHdr=new WAVEHDR;
  178. memset(lpHdr,0,sizeof(WAVEHDR));
  179. lpHdr->lpData=(char *)lParam;
  180. lpHdr->dwBufferLength=length;
  181. mmResult = ::waveOutPrepareHeader(m_hPlay, lpHdr, sizeof(WAVEHDR));
  182. if(mmResult)
  183. {
  184. log.WriteString("nError while preparing header");
  185. return ERROR_SUCCESS;
  186. }
  187. mmResult = ::waveOutWrite(m_hPlay, lpHdr, sizeof(WAVEHDR));
  188. if(mmResult)
  189. {
  190. log.WriteString("nError while writing to device");
  191. return ERROR_SUCCESS;
  192. }
  193. return ERROR_SUCCESS;
  194. }
  195. /**
  196. * End this thread...
  197. *
  198. */
  199. LRESULT PlaySound1::OnEndThread(WPARAM wParam, LPARAM lParam)
  200. {
  201. // If already playing then stop it...
  202. if(Playing==TRUE)
  203. OnStopPlaying(0,0);
  204. log.WriteString("nEnding the play device");
  205. // Quit this thread...
  206. ::PostQuitMessage(0);
  207. return TRUE;
  208. }