www.pudn.com > VOIP_server_client.rar > peer1_sound_cap.cpp


#include "peer1_sound_cap.h" 
#include "dsound.h" 
#include "peer1_main.h" 
#include "peer1_resource.h" 
 
// Gloable Variables =========================================================== 
int save_record_pos=0;                     // Counter for the cap store buffers 
LPDIRECTSOUNDCAPTURE g_pDSCapture = NULL; 
LPDIRECTSOUNDCAPTUREBUFFER g_pDSBCapture = NULL; 
LPDIRECTSOUNDNOTIFY g_pDSNotify = NULL; 
DSBPOSITIONNOTIFY g_aPosNotify[NUM_REC_NOTIFICATIONS+1]; 
DWORD g_dwCaptureBufferSize; 
DWORD g_dwNextCaptureOffset; 
DWORD g_dwNotifySize; 
HANDLE g_hNotificationEvent_cap; 
 
// External Global Variables =================================================== 
extern int share_array[10]; 
extern BOOL thread1_stop,thread2_stop,thread3_stop,thread4_stop; 
struct cap_buffer{ 
                   char sound_data[CAP_LENGTH]; 
                 }; 
extern struct cap_buffer my_cap_buffer[10]; 
 
// Temp Gloable Veriables ====================================================== 
 
 
//------------------------------------------------------------------------------ 
//******************************** pr2 ***************************************** 
//------------------------------------------------------------------------------ 
void pr2(HWND hwndDlg, char *st) 
{ 
  Display(st); 
} 
 
//----------------------------------------------------------------------- 
//******************* InitDirectsoundCapture **************************** 
//----------------------------------------------------------------------- 
int InitCapture(HWND hwndDlg) 
{ 
	//HRESULT hr; 
        save_record_pos=0; 
	ZeroMemory( &g_aPosNotify, sizeof(DSBPOSITIONNOTIFY)); 
	ZeroMemory( my_cap_buffer, sizeof(struct cap_buffer)*10); 
	g_dwCaptureBufferSize = 0; 
	g_dwNotifySize = 0; 
 
	//Initialize COM 
	if ( FAILED (CoInitialize(NULL) ) ) 
		{ 
		pr2(hwndDlg,"CoInitialize failed!\r\n"); 
		return (0); 
		} 
 
	//Create IDirectSoundCapture using the default capture device 
	if ( FAILED( DirectSoundCaptureCreate( NULL, &g_pDSCapture, NULL))) 
		{ 
		pr2(hwndDlg,"DirectSoundCaptureCreate failed!\r\n"); 
		return (0); 
		} 
	 
	return (1); 
} 
 
//--------------------------------------------------------------------------- 
//********************** FreeDirectSoundCapture ***************************** 
//--------------------------------------------------------------------------- 
int FreeDirectSound_cap() 
{ 
 
        //Release DirectSound interfaces 
        SAFE_RELEASE( g_pDSNotify ); 
        SAFE_RELEASE( g_pDSBCapture ); 
        SAFE_RELEASE( g_pDSCapture ); 
 
        //Release COM 
        CoUninitialize(); 
 
        return (1); 
} 
 
//---------------------------------------------------------------------------- 
//***************** CreateCaptureBuffer ************************************** 
//---------------------------------------------------------------------------- 
int CreateCaptureBuffer(HWND hwndDlg) 
{ 
  //HRESULT hr; 
  DSCBUFFERDESC   dscbd; 
  WAVEFORMATEX     wfx = 
    {WAVE_FORMAT_PCM, 1, SAMPLE_RATE, SAMPLE_RATE, 1, 8, 0};       //11205,11205 or 8000,8000 
//wFormatTag, nChannels, nSamplesPerSec, mAvgBytesPerSec,nBlockAlign, wBitsPerSample, cbSize 
 
  SAFE_RELEASE( g_pDSNotify ); 
  SAFE_RELEASE( g_pDSBCapture ); 
  g_dwNotifySize =  CAP_LENGTH; 
 
  //set the buffer sizes 
  g_dwCaptureBufferSize =  g_dwNotifySize * NUM_REC_NOTIFICATIONS; 
 
  SAFE_RELEASE( g_pDSNotify ); 
  SAFE_RELEASE( g_pDSBCapture ); 
 
  // Create the capture buffer 
  ZeroMemory ( &dscbd, sizeof(dscbd)); 
  dscbd.dwSize = sizeof(dscbd); 
  dscbd.dwBufferBytes =  g_dwCaptureBufferSize; 
  dscbd.lpwfxFormat = &wfx; 
   
 
  if(FAILED(g_pDSCapture->CreateCaptureBuffer(&dscbd,&g_pDSBCapture,NULL))) 
    { 
     pr2(hwndDlg,"Buffer creation failed!\r\n"); 
     return (0); 
    } 
 
    g_dwNextCaptureOffset = 0; 
 
  return (1);  
 
} 
//---------------------------------------------------------------------------- 
//***************** InitNotification ***************************************** 
//----------------------------------------------------------------------------- 
int InitNotification(HWND hwndDlg) 
{ 
   g_hNotificationEvent_cap=CreateEvent(NULL,FALSE,FALSE,NULL); 
 
   if( NULL == g_pDSBCapture ) 
     { 
      pr2(hwndDlg,"g_pDSBCapture is NULL!\r\n"); 
      return (0); 
     } 
 
   if(FAILED( g_pDSBCapture->QueryInterface( IID_IDirectSoundNotify, 
                                                  (VOID**)&g_pDSNotify))) 
     { 
       pr2(hwndDlg,"Notification query interface failed!\r\n"); 
       return (0); 
     } 
 
     // Setup the notofication positions 
     for ( INT i=0; i< NUM_REC_NOTIFICATIONS; i++ ) 
     { 
       g_aPosNotify[i].dwOffset = (g_dwNotifySize * i)+g_dwNotifySize-1; 
       g_aPosNotify[i].hEventNotify = g_hNotificationEvent_cap; 
     } 
 
     // Tell DirectSound when to notify us. 
     if(FAILED (g_pDSNotify->SetNotificationPositions( 
                 NUM_REC_NOTIFICATIONS, g_aPosNotify))) 
       { 
        pr2(hwndDlg,"SetNotificationPositions failed!\r\n"); 
        return (0); 
       } 
     return (1); 
} 
//---------------------------------------------------------------------------- 
//***************** Start_Record *************************************** 
//---------------------------------------------------------------------------- 
int  Start_Record( HWND hwndDlg) 
{ 
  if(FAILED(g_pDSBCapture->Start( DSCBSTART_LOOPING))) 
    { 
    pr2(hwndDlg,"g_pDSBCapture->Start failed!\r\n"); 
    return (0); 
    } 
 
   return (1); 
} 
 
//---------------------------------------------------------------------------- 
//***************** Save_Record ********************************************** 
//---------------------------------------------------------------------------- 
int Save_Record(HWND hwndDlg, char* Sound_Data) 
{ 
  RecordCapturedData(hwndDlg,Sound_Data); 
 
  if (share_array[save_record_pos]== 0) 
  { 
  share_array[save_record_pos]= 1; 
  save_record_pos++; 
  if (save_record_pos==10) 
  save_record_pos=0; 
  } 
 
  else 
  { 
  MessageBox(hwndDlg,"Exception.","share_array[save_record_pos]!=0", MB_OK); 
                  thread1_stop = TRUE; 
                  Sleep(10); 
                  thread2_stop = TRUE; 
                  Sleep(10); 
                  thread3_stop = TRUE; 
                  Sleep(10); 
                  thread4_stop = TRUE; 
  } 
 
  return (1); 
} 
//---------------------------------------------------------------------------- 
//***************** Stop_Record *********************************************** 
//---------------------------------------------------------------------------- 
int  Stop_Record( HWND hwndDlg) 
{ 
   if(FAILED(g_pDSBCapture->Stop())) 
   { 
    pr2(hwndDlg,"g_pDSBCapture->Stop failed!\r\n"); 
    return (0); 
    } 
 
    return (1); 
} 
//---------------------------------------------------------------------------- 
//***************** RecordCapturedData *************************************** 
//---------------------------------------------------------------------------- 
int RecordCapturedData( HWND hwndDlg, char* Sound_Data) 
{ 
  VOID* pbCaptureData = NULL; 
  DWORD dwCaptureLength; 
  VOID* pbCaptureData2 = NULL; 
  DWORD dwCaptureLength2; 
  UINT dwDataWrote; 
  DWORD dwReadPos; 
  DWORD dwCapturePos; 
  LONG lLockSize; 
 
  if (NULL == g_pDSBCapture) 
      { 
      pr2(hwndDlg,"g_pDSBCapture is null!\r\n"); 
      return (0); 
      } 
 
   if( FAILED (g_pDSBCapture->GetCurrentPosition(&dwCapturePos,&dwReadPos))) 
      { 
      pr2(hwndDlg, "**** Error: GetCurrentPosition failed!\r\n"); 
      SendMessage(hwndDlg, WM_COMMAND, (WPARAM)IDC_ERR_STOP, (LPARAM) 0); 
      return (0); 
      } 
 
      lLockSize = dwReadPos - g_dwNextCaptureOffset; 
      if( lLockSize < 0 ) 
        lLockSize += g_dwCaptureBufferSize; 
 
      // Block align lock size so that we are always write on a boundary 
      lLockSize -= (lLockSize % g_dwNotifySize); 
 
      if(lLockSize == 0) 
      { 
      //pr2(hwndDlg,"lLockSize==0!\r\n"); 
      return (0); 
      } 
 
      // lock the capture buffer down 
      if(FAILED(g_pDSBCapture->Lock( g_dwNextCaptureOffset, lLockSize, 
                                           &pbCaptureData, &dwCaptureLength, 
                                           &pbCaptureData2, &dwCaptureLength2,0L))) 
 
      { 
      pr2(hwndDlg, "**** Error: g_pDSBCapture->Lock failed!\r\n"); 
      SendMessage(hwndDlg, WM_COMMAND, (WPARAM)IDC_ERR_STOP, (LPARAM) 0); 
      return (0); 
      } 
 
      // Write the data into the buffers 
       if(FAILED(memcpy(Sound_Data,(BYTE*)pbCaptureData, dwCaptureLength))) 
      { 
      pr2(hwndDlg, "**** Error: Write buffer-1 failed!\r\n"); 
      SendMessage(hwndDlg, WM_COMMAND, (WPARAM)IDC_ERR_STOP, (LPARAM) 0); 
      return (0); 
      } 
      else 
      { 
      if(dwCaptureLength!=CAP_LENGTH) 
      { 
      char st15[30]; 
      sprintf(st15,  "dwCaptureLength=%d\r\n", dwCaptureLength); 
      //pr2(hwndDlg,st15); 
      } 
      } 
 
      // Move the capture offset along 
      g_dwNextCaptureOffset += dwCaptureLength; 
      g_dwNextCaptureOffset %= g_dwCaptureBufferSize; // Circular buffer 
 
      if( pbCaptureData2 != NULL ) 
      { 
       //pr2(hwndDlg, "pbCaptureData2 != NULL\r\n"); 
 
           // Move the capture offset along 
           g_dwNextCaptureOffset += dwCaptureLength2; 
           g_dwNextCaptureOffset %= g_dwCaptureBufferSize; // Circular buffer */ 
             
       } 
 
       // Unlock the capture buffer 
       g_pDSBCapture->Unlock( pbCaptureData, dwCaptureLength, 
                              pbCaptureData2, dwCaptureLength2); 
      return (1); 
} 
 
//------------------------------------------------------------------------------ 
//************************* Get Current Position (not used) ******************** 
//------------------------------------------------------------------------------ 
void Get_Current_Position(HWND hwndDlg) 
{ 
      DWORD play_pos, write_pos; 
      g_pDSBCapture->GetCurrentPosition(&play_pos, &write_pos); 
 
      char st10[40]; 
      sprintf(st10, "Play_pos = %d\r\n ->Write_pos = %d\r\n",play_pos, write_pos); 
      pr2(hwndDlg, st10); 
}