The LARC Game Engine
A Simple Game Engine from the Laboratory for Recreational Computing
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
CAudio Class Reference

The Audio Player. More...

#include <Sound.h>

Inheritance diagram for CAudio:
CWindowDesc CSettingsManager CComponent CSettings

Public Member Functions

 CAudio ()
 Constructor. More...
 
 ~CAudio ()
 Destructor. More...
 
void Load ()
 Load sounds from list in XML document. More...
 
CSoundDesc play (int, const Vector2 &, float=1.0f, float=0.0f)
 Play a sound. More...
 
CSoundDesc play (int, const Vector3 &, float=1.0f, float=0.0f)
 Play a sound. More...
 
CSoundDesc play (int, float v=1.0f)
 Play a sound. More...
 
CSoundDesc loop (int)
 Loop a sound. More...
 
CSoundDesc loop (int, const Vector2 &)
 Loop a sound. More...
 
CSoundDesc loop (int, const Vector3 &)
 Loop a sound. More...
 
CSoundDesc vary (int, float=0.1f, float=1, float=0)
 Play a randomly varied sound. More...
 
CSoundDesc vary (int, const Vector2 &, float=0.1f, float=1, float=0)
 Play a randomly varied sound. More...
 
CSoundDesc vary (int, const Vector3 &, float=0.1f, float=1, float=0)
 Play a randomly varied sound. More...
 
void BeginFrame ()
 Start of frame notification. More...
 
void mute ()
 Mute/unmute the sounds. More...
 
void stop ()
 Stop all sounds. More...
 
void stop (int)
 Stop a sound. More...
 
void stop (const CSoundDesc &)
 Stop a sound instance. More...
 
void SetListenerPos (const Vector3 &)
 Set listener position. More...
 
void SetListenerOrient (const Vector3 &, const Vector3 &)
 Set listener orientation. More...
 
void SetListener (CBaseCamera *)
 Set listener to camera pos and orientation.
 
void SetPitch (int, float)
 Set pitch of first instance of sound. More...
 
void SetScale (const float)
 Set scale. More...
 
- Public Member Functions inherited from CSettingsManager
void Load ()
 Load game settings. More...
 

Protected Member Functions

void createInstances (int, int, SOUND_EFFECT_INSTANCE_FLAGS)
 Create sound instances. More...
 
int Load (wchar_t *)
 Load sound from file. More...
 
int getNextInstance (int)
 Get the next instance that is not playing. More...
 
- Protected Member Functions inherited from CSettingsManager
void SetWinSize (int, int)
 Set window size information. More...
 

Protected Attributes

AudioEngine * m_pAudioEngine = nullptr
 XAudio 2.8 Engine wrapped up in DirectXTK.
 
vector< SoundEffect * > m_pSoundEffects
 A list of sound effect.
 
SoundEffectInstance *** m_pInstance = nullptr
 A list of arrays of sound effect instances.
 
AudioListener m_cListener
 Audio listener.
 
bool * m_bPlayed = false
 Whether each sound was started this frame.
 
int * m_nInstanceCount = 0
 Number of copies of each sound.
 
int m_nCount = 0
 Number of sounds loaded.
 
bool m_bMuted = false
 Whether mute is on.
 
Vector2 m_vEmitterPos
 Position of the emitter (the thing making the sound).
 
Vector3 m_vListenerPos
 Position of the listener.
 
float m_fScale = 500.0f
 Coordinate scale factor.
 

Additional Inherited Members

- Static Protected Attributes inherited from CWindowDesc
static HWND m_Hwnd = 0
 Window handle.
 
static HINSTANCE m_hInst = 0
 Instance handle.
 
static bool m_bExitSizeMove
 User just finished moving/resizing window.
 
- Static Protected Attributes inherited from CSettingsManager
static XMLElement * m_pXmlSettings = nullptr
 Pointer to the settings tag in the XML settings file.
 
static float m_fAspectRatio = 1.0f
 Aspect ratio, width/ht.
 
- Static Protected Attributes inherited from CSettings
static char m_szName [MAX_PATH]
 Name of this game.
 
static int m_nWinWidth = 0
 Window width in pixels.
 
static int m_nWinHeight = 0
 Window height in pixels.
 
static Vector2 m_vWinCenter = Vector2::Zero
 Window center.
 
- Static Protected Attributes inherited from CComponent
static CStepTimerm_pStepTimer = &cStepTimer
 Pointer to a step timer. More...
 
static CRandomm_pRandom = &cRandom
 Pointer to a PRNG. More...
 
static CKeyboardm_pKeyboard = &cKeyboard
 Pointer to a keyboard handler. More...
 
static CXBoxControllerm_pController = &cController
 Pointer to a XBox controller. More...
 
static CAudiom_pAudio = &cAudio
 Pointer to an audio player. More...
 

Detailed Description

The Audio Player allows you to play multiple overlapping copies of sounds simultaneously. It reads settings from the XML settings file, including a list of file names to be loaded. It can set the volume, pitch, and position of each sound instance played. Since it implements 3D sound, make sure your sound files are mono, not stereo. If you hear a horrible screeching sound instead of your nice sound sample, it probably means that your sound file is stereo, not mono. Use a sound editor such as Audacity to convert your sound from stereo to mono. Make sure you call BeginFrame once per frame to prevent multiple copies of a sound from playing at the same time, which only makes one sound but LOUDER.

Constructor & Destructor Documentation

◆ CAudio()

CAudio::CAudio ( )

Set member variables to sensible values and initialize the XAudio Engine using DirectXTK12. If the compiler is in debug mode, then set the XAudio engine in debug mode too.

◆ ~CAudio()

CAudio::~CAudio ( )

Reclaim all dynamic memory and shut down the audio engine.

Member Function Documentation

◆ BeginFrame()

void CAudio::BeginFrame ( )

Get ready for new animation frame by flagging all sounds as unplayed.

◆ createInstances()

void CAudio::createInstances ( int  index,
int  n,
SOUND_EFFECT_INSTANCE_FLAGS  flags 
)
protected

Create sound instances.

Parameters
indexIndex of sound.
nNumber of instances of sound wanted.
flagsFlags.

◆ getNextInstance()

int CAudio::getNextInstance ( int  index)
protected

Get the index into the instance array of the next unplayed instance of a sound.

Parameters
indexIndex of sound in the sound array.
Returns
Index of next unplayed instance in its instance array.

◆ Load() [1/2]

int CAudio::Load ( wchar_t *  filename)
protected

Load a sound from a file.

Parameters
filenamename of file to be loaded
Returns
Size of sound effect loaded

◆ Load() [2/2]

void CAudio::Load ( )

Load the sound files from the file list in g_xmlSettings. Processes sound file names in <sound> tags within a <sounds></sounds> pair. Starts by counting the number of sound files and creating arrays of the right size.

◆ loop() [1/3]

CSoundDesc CAudio::loop ( int  i)

Loop a sound effect in mono.

Parameters
iIndex of sound to be looped.
Returns
Sound descriptor for playing sound.

◆ loop() [2/3]

CSoundDesc CAudio::loop ( int  i,
const Vector2 &  s 
)

Loop a sound effect in stereo.

Parameters
iIndex of sound to be looped.
sPosition of emitter in 2D.
Returns
Sound descriptor for playing sound.

◆ loop() [3/3]

CSoundDesc CAudio::loop ( int  i,
const Vector3 &  s 
)

Loop a sound effect in stereo.

Parameters
iIndex of sound to be looped.
sPosition of emitter in 3D.
Returns
Sound descriptor for playing sound.

◆ mute()

void CAudio::mute ( )

If the sound is muted, unmute it. If not, mute it. This means stopping sounds that are currently playing and preventing future sounds from getting started.

◆ play() [1/3]

CSoundDesc CAudio::play ( int  i,
const Vector2 &  s,
float  vol = 1.0f,
float  p = 0.0f 
)

Play a sound effect in stereo.

Parameters
iIndex of sound to be played.
sPosition of emitter in 2D.
volVolume (defaults to 1.0f).
pPitch (defaults to zero).
Returns
Sound descriptor for playing sound.

◆ play() [2/3]

CSoundDesc CAudio::play ( int  i,
const Vector3 &  s,
float  vol = 1.0f,
float  p = 0.0f 
)

Play a sound effect in stereo.

Parameters
iIndex of sound to be played.
sPosition of emitter in 3D.
volVolume (defaults to 1.0f).
pPitch (defaults to zero).
Returns
Sound descriptor for playing sound.

◆ play() [3/3]

CSoundDesc CAudio::play ( int  i,
float  vol = 1.0f 
)

Play a sound effect in mono.

Parameters
iIndex of sound to be played.
volVolume (defaults to 1.0f).
Returns
Sound descriptor for playing sound.

◆ SetListenerOrient()

void CAudio::SetListenerOrient ( const Vector3 &  vLook,
const Vector3 &  vUp 
)

Set the listener orientation from a pair of vectors.

Parameters
vLookLook-at vector.
vUpUp vector.

◆ SetListenerPos()

void CAudio::SetListenerPos ( const Vector3 &  pos)

Move the listener to a new position.

Parameters
posNew position of listener.

◆ SetPitch()

void CAudio::SetPitch ( int  i,
float  f 
)

Set the pitch of the first instance of a sound.

Parameters
iIndex of sound to be played.
fNew pitch.

◆ SetScale()

void CAudio::SetScale ( const float  scale)

Set the scale factor for converting from render coordinates to sound coordinates.

Parameters
scaleScale factor.

◆ stop() [1/3]

void CAudio::stop ( )

Stop all instances of all sound effects.

◆ stop() [2/3]

void CAudio::stop ( int  i)

Stop all instances of a sound effect.

Parameters
iIndex of sound to be stopped.

◆ stop() [3/3]

void CAudio::stop ( const CSoundDesc d)

Stop an instance of a sound effects.

Parameters
dDescriptor for a sound instance.

◆ vary() [1/3]

CSoundDesc CAudio::vary ( int  i,
float  var = 0.1f,
float  vol = 1,
float  p = 0 
)

Play a sound effect in mono, but slightly differently each time it is played. The pitch is varied up and down by a random amount scaled by a coefficient of variability that is provided as a parameter.

Parameters
iIndex of sound to be played.
varCoefficient of variability (defaults to 0.25).
volVolume (defaults to 1.0).
pPitch (defaults to 0).
Returns
Sound descriptor for playing sound.

◆ vary() [2/3]

CSoundDesc CAudio::vary ( int  i,
const Vector2 &  s,
float  var = 0.1f,
float  vol = 1,
float  p = 0 
)

Play a sound effect in stereo, but slightly differently each time it is played. The pitch is varied up and down by a random amount scaled by a coefficient of variability that is provided as a parameter.

Parameters
iIndex of sound to be played.
sPosition of emitter in 2D.
varCoefficient of variability (defaults to 0.25).
volVolume (defaults to 1.0).
pPitch (defaults to 0).
Returns
Sound descriptor for playing sound.

◆ vary() [3/3]

CSoundDesc CAudio::vary ( int  i,
const Vector3 &  s,
float  var = 0.1f,
float  vol = 1,
float  p = 0 
)

Play a sound effect in stereo, but slightly differently each time it is played. The pitch is varied up and down by a random amount scaled by a coefficient of variability that is provided as a parameter.

Parameters
iIndex of sound to be played.
sPosition of emitter in 3D.
varCoefficient of variability (defaults to 0.25).
volVolume (defaults to 1.0).
pPitch (defaults to 0).
Returns
Sound descriptor for playing sound.