Game Development Reference
In-Depth Information
if( FAILED( hr = m_pDS->CreateSoundBuffer( &dsbd, &pDSBPrimary, NULL ) ) )
return DXUT_ERR( L
”
CreateSoundBuffer
”
,hr);
WAVEFORMATEX wfx;
ZeroMemory( &wfx, sizeof(WAVEFORMATEX) );
wfx.wFormatTag = (WORD) WAVE_FORMAT_PCM;
wfx.nChannels = (WORD) dwPrimaryChannels;
wfx.nSamplesPerSec = (DWORD) dwPrimaryFreq;
wfx.wBitsPerSample = (WORD) dwPrimaryBitRate;
wfx.nBlockAlign = (WORD) (wfx.wBitsPerSample / 8 * wfx.nChannels);
wfx.nAvgBytesPerSec = (DWORD) (wfx.nSamplesPerSec * wfx.nBlockAlign);
if( FAILED( hr = pDSBPrimary->SetFormat(&wfx) ) )
return DXUT_ERR( L
”
SetFormat
”
,hr);
SAFE_RELEASE( pDSBPrimary );
return S_OK;
}
You have to love DirectSound. This method essentially makes two method calls, and
the rest of the code simply fills in parameters. The first call is to
CreateSound-
Buffer()
, which actually returns a pointer to the primary sound buffer where all
your sound effects are mixed into a single sound stream that is rendered by the
sound card. The second call to
SetFormat()
tells the sound driver to change the
primary buffer
s format to one that you specify.
The shutdown method, by contrast, is extremely simple:
'
void DirectSoundAudio::VShutdown()
{
if(m_Initialized)
{
Audio::VShutdown();
SAFE_RELEASE(m_pDS);
m_Initialized = false;
}
}
The base class
s
VShutdown()
is called to stop and release all the sounds still active.
The
SAFE_RELEASE
on
m_pDS
will release the
IDirectSound8
object and shut
down the sound system completely.
The last two methods of the
DirectSoundAudio
class allocate and release audio
buffers. An audio buffer is the C++ representation of an active sound effect. In our
'