Microsoft DirectX 9.0

CSound Sample Class

The CSound sample class represents a sound in one or more static buffers. An object of this class can be used to play multiple instances of a sound effect. The object is usually created through a call to the Create method of the CSoundManager Sample Class.

class CSound
{
protected:
    LPDIRECTSOUNDBUFFER* m_apDSBuffer;
    DWORD                m_dwDSBufferSize;
    CWaveFile*           m_pWaveFile;
    DWORD                m_dwNumBuffers;
    DWORD                m_dwCreationFlags;

    HRESULT RestoreBuffer(LPDIRECTSOUNDBUFFER pDSB, BOOL* pbWasRestored);

public:
    CSound(LPDIRECTSOUNDBUFFER* apDSBuffer, DWORD dwDSBufferSize, 
        DWORD dwNumBuffers, CWaveFile* pWaveFile, DWORD dwCreationFlags);
    virtual ~CSound();

    HRESULT Get3DBufferInterface(DWORD dwIndex, 
        LPDIRECTSOUND3DBUFFER* ppDS3DBuffer);
    HRESULT FillBufferWithSound(LPDIRECTSOUNDBUFFER pDSB, 
        BOOL bRepeatWavIfBufferLarger);
    LPDIRECTSOUNDBUFFER GetFreeBuffer();
    LPDIRECTSOUNDBUFFER GetBuffer(DWORD dwIndex);
    HRESULT Play(DWORD dwPriority = 0, DWORD dwFlags = 0, 
        LONG lVolume = 0, LONG lFrequency = 0);
    HRESULT Play3D(LPDS3DBUFFER p3DBuffer, DWORD dwPriority = 0, 
        DWORD dwFlags = 0, LONG lFrequency = 0);
    HRESULT Stop();
    HRESULT Reset();
    BOOL    IsSoundPlaying();
};

Constructor

The constructor fills one or more buffers with the data from a specified WAV file and initializes private data members.

Public Methods

The class contains the following public methods, in alphabetical order.

Method Description
FillBufferWithSound Fills the specified buffer with the data from the WAV file that was passed to the object constructor. If the buffer is larger than the data, data can optionally be repeated; otherwise the rest of the buffer is filled with silence.
Get3DBufferInterface Retrieves the IDirectSound3DBuffer8 interface pointer for the specified buffer, if the buffer has 3-D capabilities.
GetBuffer Retrieves the IDirectSoundBuffer interface of the specified buffer. Use QueryInterface to retrieve IDirectSoundBuffer8.
GetFreeBuffer Retrieves the IDirectSoundBuffer interface of the first buffer that is not playing. If all buffers are playing, the method returns a randomly selected buffer. Use QueryInterface to retrieve IDirectSoundBuffer8.
IsSoundPlaying Returns a Boolean variable that indicates whether the specified buffer is playing.
Play Plays a buffer. The method selects the buffer to play by calling GetFreeBuffer, and restores it if necessary The application can pass in a voice management flag; the DSBPLAY_LOOPING flag to repeat the sound; a volume in hundredths of decibels (where 0 is full volume and -10,000 is silence); and a frequency in hertz that is added to the basic frequency of the sound.
Play3D Plays a 3-D buffer. This method is similar to Play, but the application also passes in a DS3DBUFFER structure describing the 3-D parameters to be set. (Note that if the CSound object has more than one buffer, there is no way to determine which one is played by this call. If you want to set 3-D parameters dynamically while the buffer is playing, add functionality to return a buffer pointer.)
Reset Sets the play cursor on all buffers to the beginning.
Stop Stops all buffers.

The class is implemented in (SDK root)\samples\C++\Common\Src\Dsutil.cpp.

See Also