RecordSound.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    : RecordSound.cpp
  12. //    Details : Recording audio from the capture device such as microphone
  13. //
  14. //
  15. /////////////////////////////////////////////////////////////////////////////
  16. #include "stdafx.h"
  17. #include "VideoNet.h"
  18. #include "VideoNetDlg.h"
  19. #include "RecordSound.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. IMPLEMENT_DYNCREATE(RecordSound, CWinThread)
  28. BEGIN_MESSAGE_MAP(RecordSound,CWinThread)
  29. ON_THREAD_MESSAGE(MM_WIM_DATA, OnSoundData)
  30. ON_THREAD_MESSAGE(WM_RECORDSOUND_STARTRECORDING,OnStartRecording)
  31. ON_THREAD_MESSAGE(WM_RECORDSOUND_STOPRECORDING,OnStopRecording)
  32. ON_THREAD_MESSAGE(WM_RECORDSOUND_ENDTHREAD,OnEndThread)
  33. END_MESSAGE_MAP()
  34. RecordSound::RecordSound()
  35. {
  36. }
  37. RecordSound::RecordSound(CDialog *dialog)
  38. {
  39. dlg=dialog;
  40. log.Open("recording.log",CFile::modeCreate | CFile::modeWrite);
  41. log.WriteString("In the Recordsound Constructorn");
  42. recording=FALSE;
  43. isallocated=0;      //memory is not allocated to wavebuffer
  44. //Create Headers for buffering
  45. PreCreateHeader();
  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. //Setting WAVEFORMATEX  structure for the audio input
  58. memset(&m_WaveFormatEx,0x00,sizeof(m_WaveFormatEx));
  59. m_WaveFormatEx.wFormatTag=WAVE_FORMAT_PCM;
  60. m_WaveFormatEx.nChannels=1;
  61. m_WaveFormatEx.wBitsPerSample=8;
  62. m_WaveFormatEx.cbSize=0;
  63. m_WaveFormatEx.nSamplesPerSec=SAMPLERSEC;  //22.05 KHz
  64. m_WaveFormatEx.nBlockAlign=1; //(m_WaveFormatEx.wBitsPerSample/8)*m_WaveFormatEx.nChannels;
  65. m_WaveFormatEx.nAvgBytesPerSec=SAMPLERSEC ;  //m_WaveFormatEx.nBlockAlign;
  66. }
  67. RecordSound::~RecordSound()
  68. {
  69. if(!isallocated)
  70. return;
  71. for(int i=0;i<MAXRECBUFFER;i++)
  72. {
  73. if(rechead[i])
  74. delete rechead[i];
  75. }
  76. }
  77. BOOL RecordSound::InitInstance()
  78. {
  79. return TRUE;
  80. }
  81. int RecordSound::ExitInstance()
  82. {
  83. return CWinThread::ExitInstance();
  84. }
  85. void RecordSound::PreCreateHeader()
  86. {
  87. for(int i=0;i<MAXRECBUFFER;i++)
  88. rechead[i]=CreateWaveHeader();
  89. isallocated=1;
  90. }
  91. LRESULT RecordSound::OnStartRecording(WPARAM wp,LPARAM lp)
  92. {
  93. if(recording)
  94. return FALSE;
  95. log.WriteString("In OnStartrecordingn");
  96. MMRESULT mmReturn = ::waveInOpen( &m_hRecord, WAVE_MAPPER,
  97. &m_WaveFormatEx, ::GetCurrentThreadId(), 0, CALLBACK_THREAD);
  98. //Error has occured while opening device
  99. if(mmReturn!=MMSYSERR_NOERROR )
  100. {
  101. displayError(mmReturn,"Open");
  102. return FALSE;
  103. }
  104. if(mmReturn==MMSYSERR_NOERROR )
  105. {
  106. for(int i=0; i < MAXRECBUFFER ; i++)
  107. {
  108. mmReturn = ::waveInPrepareHeader(m_hRecord,rechead[i], sizeof(WAVEHDR));
  109. mmReturn = ::waveInAddBuffer(m_hRecord, rechead[i], sizeof(WAVEHDR));
  110. }
  111. mmReturn = ::waveInStart(m_hRecord);
  112. if(mmReturn!=MMSYSERR_NOERROR )
  113. displayError(mmReturn,"Start");
  114. else
  115. recording=TRUE;
  116. }
  117. return TRUE;
  118. }
  119. void RecordSound::displayError(int mmReturn,char errmsg[])
  120. {
  121. char errorbuffer[MAX_PATH];
  122. char errorbuffer1[MAX_PATH];
  123. waveInGetErrorText( mmReturn,errorbuffer,MAX_PATH);
  124. sprintf(errorbuffer1,"RECORD: %s : %x : %s",errmsg,mmReturn,errorbuffer);
  125. AfxMessageBox(errorbuffer1);  
  126. }
  127. LRESULT RecordSound::OnStopRecording(WPARAM wp,LPARAM lp)
  128. {
  129. MMRESULT mmReturn = 0;
  130. log.WriteString("nIn the onstop recording");
  131. if(!recording)
  132. return FALSE;
  133. mmReturn = ::waveInStop(m_hRecord);
  134. //To get the remaining sound data from buffer
  135. //Reset will call OnSoundData function
  136. if(!mmReturn)
  137. {
  138. recording = FALSE;
  139. mmReturn = ::waveInReset(m_hRecord);  
  140. }
  141. Sleep(500); 
  142. if(!mmReturn)
  143. mmReturn = ::waveInClose(m_hRecord);
  144. return mmReturn;
  145. }
  146. LRESULT RecordSound::OnSoundData(WPARAM wParam, LPARAM lParam)
  147. {
  148. log.WriteString("nIn the onsound data");
  149. if(recording==FALSE)
  150. return FALSE;
  151.     LPWAVEHDR lpHdr = (LPWAVEHDR) lParam;
  152. if(lpHdr->dwBytesRecorded==0 || lpHdr==NULL)
  153. return ERROR_SUCCESS;
  154. ::waveInUnprepareHeader(m_hRecord, lpHdr, sizeof(WAVEHDR));
  155. // Send recorded audio to remote host...
  156. if(lpHdr->lpData!=NULL )
  157. ( (CVideoNetDlg*) dlg )->daudio.SendAudioData((unsigned char *)lpHdr->lpData,lpHdr->dwBytesRecorded);
  158. if(recording)
  159. {
  160. //Reuse the old buffer
  161. ::waveInPrepareHeader(m_hRecord,lpHdr, sizeof(WAVEHDR));
  162. ::waveInAddBuffer(m_hRecord, lpHdr, sizeof(WAVEHDR));
  163. }
  164. return ERROR_SUCCESS;
  165. }
  166. LRESULT RecordSound::OnEndThread(WPARAM wp,LPARAM lp)
  167. {
  168. log.WriteString("nIN the onendthread");
  169. if(recording)
  170. OnStopRecording(0,0);
  171. ::PostQuitMessage(0);
  172. return TRUE;
  173. }
  174. LPWAVEHDR  RecordSound::CreateWaveHeader()
  175. {
  176. LPWAVEHDR lpHdr = new WAVEHDR;
  177. if(lpHdr==NULL)
  178. {
  179. log.WriteString("n Unable to allocate the memory");
  180. return NULL;
  181. }
  182. ZeroMemory(lpHdr, sizeof(WAVEHDR));
  183. char* lpByte = new char[RECBUFFER];//m_WaveFormatEx.nBlockAlign*SOUNDSAMPLES)];
  184. if(lpByte==NULL)
  185. {
  186. log.WriteString("n Unable to allocate the memory");
  187. return NULL;
  188. }
  189. lpHdr->lpData =  lpByte;
  190. lpHdr->dwBufferLength =RECBUFFER;   // (m_WaveFormatEx.nBlockAlign*SOUNDSAMPLES);
  191. return lpHdr;
  192. }