home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 04 Mommersteeg / Tennis / DirectSound.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-23  |  2.6 KB  |  91 lines

  1. //----------------------------------------------------------------------------------------------
  2. // Sequential Prediction Demo: The positioning pattern
  3. // 
  4. // Author:  Fri Mommersteeg
  5. // Date:    10-09-2001
  6. // File:    DirectSound.cpp
  7. //----------------------------------------------------------------------------------------------
  8.  
  9. //----------------------------------------------------------------------------------------------
  10. // Include files
  11. //----------------------------------------------------------------------------------------------
  12.  
  13. #include "stdafx.h"
  14. #include "DirectSound.h"
  15. #include <assert.h>
  16.  
  17. //----------------------------------------------------------------------------------------------
  18. // The following functions are from some old, long forgotten personal DX library. I couldn't 
  19. // find the courage to document them properly, but take a look if you like...
  20. //----------------------------------------------------------------------------------------------
  21.  
  22.  
  23. extern HWND        g_hWnd;
  24. LPDS            lpds = NULL;
  25.  
  26. BOOL InitDS(DWORD dwSamplesPerSec, WORD nChannels, WORD nBitsPerSample) {
  27.     HRESULT    dsrval;
  28.  
  29.     // create DirectSound object
  30.     dsrval = DirectSoundCreate(NULL, &lpds, 0);
  31.     if (dsrval != DD_OK)
  32.         return FALSE;
  33.  
  34.     dsrval = lpds->SetCooperativeLevel(g_hWnd, DSSCL_PRIORITY );
  35.     if (dsrval != DD_OK)
  36.         return FALSE;
  37.  
  38.     // set format for primary sound buffer
  39.     WAVEFORMATEX wfx;
  40.     FillWaveFormat(&wfx, dwSamplesPerSec, nChannels, nBitsPerSample);
  41.     MatchPrimaryBuffer( &wfx );
  42.  
  43.     return TRUE;
  44. }
  45.  
  46. void ShutdownDS() {
  47.     if ( lpds != NULL ) {
  48.         lpds->Release();
  49.         lpds = NULL;
  50.     }
  51. }
  52.  
  53. void FillWaveFormat(WAVEFORMATEX * wfx, DWORD dwSamplesPerSec, WORD nChannels, WORD nBitsPerSample)
  54. {
  55.     assert( nBitsPerSample % 8 == 0 );
  56.     assert( nChannels == 1 || nChannels == 2 );
  57.     wfx->wFormatTag = WAVE_FORMAT_PCM;
  58.     wfx->nChannels = nChannels;
  59.     wfx->nSamplesPerSec = dwSamplesPerSec;
  60.     wfx->nBlockAlign = nChannels * nBitsPerSample / 8;
  61.     wfx->nAvgBytesPerSec = wfx->nBlockAlign * dwSamplesPerSec;
  62.     wfx->wBitsPerSample = nBitsPerSample;
  63.     wfx->cbSize = 0;
  64. }
  65.  
  66. void MatchPrimaryBuffer(WAVEFORMATEX * pwfx)
  67. {
  68.     assert( lpds != NULL );
  69.     assert( pwfx != NULL );
  70.  
  71.     LPDSB lpDSB;
  72.     DSBD dsbd;
  73.  
  74.     // set up primary buffer description
  75.     ZeroMemory(&dsbd, sizeof(dsbd));
  76.     dsbd.dwSize = sizeof(dsbd);
  77.     dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
  78.     dsbd.dwBufferBytes = 0;
  79.     dsbd.lpwfxFormat = NULL;
  80.  
  81.     // create primary buffer
  82.     lpds->CreateSoundBuffer(&dsbd, &lpDSB, NULL);
  83.  
  84.     // set primary format
  85.     lpDSB->SetFormat( pwfx );
  86.  
  87.     // release reference to primary format
  88.     lpDSB->Release();
  89. }
  90.  
  91.