home *** CD-ROM | disk | FTP | other *** search
- /*
- Company: Sensaura
- Copyright: (C) 1999
-
- File Name: cdsnd3d.cpp
- File Description: Source file for implementation of DirectSoundObject class.
- This class supports an IDirectSound object by managing 3D sound settings.
- Author: Adam Philp
- File Version: 1.03.000
- Last Update: 19-APR-00
-
- Target Compiler: Microsoft Visual C++ Version 5.0
- */
-
- /////////////////////// Included files ////////////////////////////////////////////////////////////
-
- #include <windows.h>
-
- #include "cdsnd3d.h" // DirectSound3DObject class declarations
-
- #include "sensaura.h" // Sensaura-specific DirectSound functions
- #include "directx.h" // DirectX functions and macros
- #include "debug.h" // Debugging support macros and functions
-
- /////////////////////// DirectSound3DObject class implementation /////////////////////////////////////
-
- /*
- Constructor: Creates a DirectSound object. The capabilities of the sound hardware are tested,
- and the information used to create a DirectSound object with the best capabilities for 3D
- positioning
- */
-
- DirectSound3DObject::DirectSound3DObject(HINSTANCE hInstance)
- {
- m_pPrimary = NULL;
- m_pListener = NULL;
- m_DirectSoundType = DIRECTSOUND3D_NONE;
- m_bSensaura = false;
- m_dwMaxHw3dBuffers = 0;
-
- m_bSensaura = SensauraCreate(&m_pDS, hInstance);
- if(m_pDS == NULL)
- return;
-
- // Determine the optimum type of 3D positional audio which is available. If a Sensaura
- // DirectSound object was created, then use Sensaura 3D positional audio. If Sensaura is not
- // available, but other hardware 3D acceleration is, then use that instead. If neither is
- // available, then skip Microsoft DirectSound3D emulation which uses lots of CPU power. The
- // application will use stereo panning and volume for sound positioning if there is no 3D
- // hardware. If you want Microsoft emulation, just switch 'DIRECTSOUND3D_NONE' to
- // 'DIRECTSOUND3D_SOFTWARE' in this line
-
- m_DirectSoundType = m_bSensaura ? DIRECTSOUND3D_SENSAURA :
- m_dwMaxHw3dBuffers ? DIRECTSOUND3D_HARDWARE :
- DIRECTSOUND3D_NONE;
- }
-
- DirectSound3DObject::~DirectSound3DObject()
- {
- Release();
- RELEASE(m_pDS)
- }
-
- /*
- Function: Create
- Description: This is where DirectSound is set up. The primary buffer and listener objects
- are created here
- Parameters: hwnd - handle of the window to which to attach DirectSound
- Return value: true if a DirectSound primary buffer and listener were successfully created,
- false otherwise
- */
-
- bool DirectSound3DObject::Create(HWND hwnd)
- {
- DSCAPS dsc;
- DSBUFFERDESC dsbd;
- WAVEFORMATEX wfx;
-
- if(!m_pDS) // Check DirectSound OK
- return false;
-
- ASSERT(!m_pPrimary); // Check primary buffer and listener not created yer
- ASSERT(!m_pListener);
- // Set the cooperative level
- TRY_DS(m_pDS->SetCooperativeLevel(hwnd, DSSCL_PRIORITY))
-
- dsc.dwSize = sizeof(DSCAPS); // Get the sound hardware capabilities to see whether it
- TRY_DS(m_pDS->GetCaps(&dsc)) // has 3D positional audio hardware acceleration
- m_dwMaxHw3dBuffers = dsc.dwMaxHw3DAllBuffers;
-
- dsbd.dwSize = sizeof(DSBUFFERDESC); // Set primary buffer properties. We can request 3D caps
- dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D|DSBCAPS_CTRLVOLUME; // whether or not we
- dsbd.dwBufferBytes = 0; // have hardware acceleration as there will be no
- dsbd.dwReserved = 0; // performance hit if we don't use 3D secondary buffers
- dsbd.lpwfxFormat = NULL;
-
- wfx.wFormatTag = WAVE_FORMAT_PCM; // Set the primary buffer format to 22kHz, 16 bit,
- wfx.nChannels = 2; // 2 channels by default
- wfx.nSamplesPerSec = 22050;
- wfx.nAvgBytesPerSec = 88200;
- wfx.nBlockAlign = 4;
- wfx.wBitsPerSample = 16;
- wfx.cbSize = 0;
- // Create the primary buffer and set its format
- TRY_DS(m_pDS->CreateSoundBuffer(&dsbd, &m_pPrimary, NULL))
- TRY_DS(m_pPrimary->SetFormat(&wfx))
- // Get an interface to a 3D listener
- TRY_DS(m_pPrimary->QueryInterface(IID_IDirectSound3DListener, (void**)&m_pListener))
- return true; // We're ready for the app to create secondary buffers
-
- DS_ERROR: // Oops! An error must have occurred
- Release(); // Release all our DirectSound interfaces
- return false;
- }
-
- /*
- Function: Release
- Description: Release all DirectSound interfaces and set member pointers to NULL. Call this
- function before calling Create() if you want to create a DirectSound object
- more than once in your application
- */
-
- void DirectSound3DObject::Release()
- {
- RELEASE(m_pListener)
- RELEASE(m_pPrimary)
- }
-
- /*
- Function: SetDirectSound
- Description: After checking that the DirectSound object supports the type of 3D positional
- audio requested, set the type
- */
-
- bool DirectSound3DObject::SetDirectSound(DIRECTSOUND3DTYPE type)
- {
- ASSERT(m_pDS); // Can't set DirectSound type if we don't have an object
- // Set the type
- if((type == DIRECTSOUND3D_SENSAURA) && !m_bSensaura)
- return false;
- if((type == DIRECTSOUND3D_SENSAURA || type == DIRECTSOUND3D_HARDWARE) && !m_dwMaxHw3dBuffers)
- return false;
- m_DirectSoundType = type;
- return true;
- }
-