home *** CD-ROM | disk | FTP | other *** search
/ Game Audio Programming / GameAudioProgramming.iso / Extras / Sensaura / SDK1.0 / data1.cab / Example_Files / DS3DDemo / Source / cdsnd3d.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-13  |  5.2 KB  |  146 lines

  1. /*
  2.     Company:            Sensaura
  3.     Copyright:          (C) 1999
  4.  
  5.     File Name:            cdsnd3d.cpp
  6.     File Description:    Source file for implementation of DirectSoundObject class.
  7.                         This class supports an IDirectSound object by managing 3D sound settings.
  8.     Author:                Adam Philp
  9.     File Version:        1.03.000
  10.     Last Update:        19-APR-00
  11.  
  12.     Target Compiler:    Microsoft Visual C++ Version 5.0
  13. */
  14.  
  15. ///////////////////////    Included files ////////////////////////////////////////////////////////////
  16.  
  17. #include <windows.h>
  18.  
  19. #include "cdsnd3d.h"                    // DirectSound3DObject class declarations
  20.  
  21. #include "sensaura.h"                    // Sensaura-specific DirectSound functions
  22. #include "directx.h"                    // DirectX functions and macros
  23. #include "debug.h"                        // Debugging support macros and functions
  24.  
  25. /////////////////////// DirectSound3DObject class implementation /////////////////////////////////////
  26.  
  27. /*
  28.     Constructor: Creates a DirectSound object.  The capabilities of the sound hardware are tested,
  29.     and the information used to create a DirectSound object with the best capabilities for 3D 
  30.     positioning
  31. */
  32.  
  33. DirectSound3DObject::DirectSound3DObject(HINSTANCE hInstance)
  34. {                                            
  35.     m_pPrimary = NULL;
  36.     m_pListener = NULL;
  37.     m_DirectSoundType = DIRECTSOUND3D_NONE;
  38.     m_bSensaura = false;
  39.     m_dwMaxHw3dBuffers = 0;
  40.  
  41.     m_bSensaura = SensauraCreate(&m_pDS, hInstance);
  42.     if(m_pDS == NULL)                    
  43.         return;
  44.  
  45.     //    Determine the optimum type of 3D positional audio which is available.  If a Sensaura 
  46.     //    DirectSound object was created, then use Sensaura 3D positional audio.  If Sensaura is not 
  47.     //    available, but other hardware 3D acceleration is, then use that instead.  If neither is 
  48.     //    available, then skip Microsoft DirectSound3D emulation which uses lots of CPU power.  The
  49.     //    application will use stereo panning and volume for sound positioning if there is no 3D
  50.     //    hardware.  If you want Microsoft emulation, just switch    'DIRECTSOUND3D_NONE' to 
  51.     //    'DIRECTSOUND3D_SOFTWARE' in this line
  52.  
  53.     m_DirectSoundType = m_bSensaura ? DIRECTSOUND3D_SENSAURA : 
  54.                         m_dwMaxHw3dBuffers ? DIRECTSOUND3D_HARDWARE : 
  55.                         DIRECTSOUND3D_NONE;
  56. }
  57.  
  58. DirectSound3DObject::~DirectSound3DObject()
  59. {
  60.     Release();
  61.     RELEASE(m_pDS)
  62. }
  63.  
  64. /*
  65.     Function:        Create
  66.     Description:    This is where DirectSound is set up.  The primary buffer and listener objects
  67.                     are created here
  68.     Parameters:        hwnd - handle of the window to which to attach DirectSound
  69.     Return value:    true if a DirectSound primary buffer and listener were successfully created, 
  70.                     false otherwise
  71. */
  72.  
  73. bool DirectSound3DObject::Create(HWND hwnd)
  74. {
  75.     DSCAPS            dsc;
  76.     DSBUFFERDESC    dsbd;
  77.     WAVEFORMATEX    wfx;
  78.  
  79.     if(!m_pDS)                            // Check DirectSound OK
  80.         return false;
  81.  
  82.     ASSERT(!m_pPrimary);                // Check primary buffer and listener not created yer
  83.     ASSERT(!m_pListener);
  84.                                         // Set the cooperative level 
  85.     TRY_DS(m_pDS->SetCooperativeLevel(hwnd, DSSCL_PRIORITY))
  86.                                     
  87.     dsc.dwSize = sizeof(DSCAPS);        // Get the sound hardware capabilities to see whether it
  88.     TRY_DS(m_pDS->GetCaps(&dsc))        // has 3D positional audio hardware acceleration
  89.     m_dwMaxHw3dBuffers = dsc.dwMaxHw3DAllBuffers;
  90.     
  91.     dsbd.dwSize = sizeof(DSBUFFERDESC);    // Set primary buffer properties.  We can request 3D caps
  92.     dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D|DSBCAPS_CTRLVOLUME;    // whether or not we 
  93.     dsbd.dwBufferBytes = 0;                // have hardware acceleration as there will be no  
  94.     dsbd.dwReserved = 0;                // performance hit if we don't use 3D secondary buffers
  95.     dsbd.lpwfxFormat = NULL;
  96.     
  97.     wfx.wFormatTag = WAVE_FORMAT_PCM;    // Set the primary buffer format to 22kHz, 16 bit,
  98.     wfx.nChannels = 2;                    // 2 channels by default
  99.     wfx.nSamplesPerSec = 22050;
  100.     wfx.nAvgBytesPerSec = 88200; 
  101.     wfx.nBlockAlign = 4;     
  102.     wfx.wBitsPerSample = 16;     
  103.     wfx.cbSize = 0; 
  104.                                         // Create the primary buffer and set its format
  105.     TRY_DS(m_pDS->CreateSoundBuffer(&dsbd, &m_pPrimary, NULL))
  106.     TRY_DS(m_pPrimary->SetFormat(&wfx))
  107.                                         // Get an interface to a 3D listener
  108.     TRY_DS(m_pPrimary->QueryInterface(IID_IDirectSound3DListener, (void**)&m_pListener))
  109.     return true;                        // We're ready for the app to create secondary buffers
  110.  
  111. DS_ERROR:                                // Oops! An error must have occurred
  112.     Release();                            // Release all our DirectSound interfaces
  113.     return false;
  114. }
  115.  
  116. /*
  117.     Function:        Release
  118.     Description:    Release all DirectSound interfaces and set member pointers to NULL.  Call this
  119.                     function before calling Create() if you want to create a DirectSound object
  120.                     more than once in your application
  121. */
  122.  
  123. void DirectSound3DObject::Release()
  124. {
  125.     RELEASE(m_pListener)
  126.     RELEASE(m_pPrimary)
  127. }
  128.  
  129. /*
  130.     Function:        SetDirectSound
  131.     Description:    After checking that the DirectSound object supports the type of 3D positional
  132.                     audio requested, set the type
  133. */
  134.  
  135. bool DirectSound3DObject::SetDirectSound(DIRECTSOUND3DTYPE type)
  136. {
  137.     ASSERT(m_pDS);                        // Can't set DirectSound type if we don't have an object
  138.                                         // Set the type
  139.     if((type == DIRECTSOUND3D_SENSAURA) && !m_bSensaura)
  140.         return false;                    
  141.     if((type == DIRECTSOUND3D_SENSAURA || type == DIRECTSOUND3D_HARDWARE) && !m_dwMaxHw3dBuffers)
  142.         return false;
  143.     m_DirectSoundType = type;
  144.     return true;
  145. }
  146.