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

The Audio Player. More...

#include <Sound.h>

Inheritance diagram for LSound:
LWindowDesc LSettingsManager LComponent LSettings

Public Member Functions

 LSound ()
 Constructor. More...
 
 ~LSound ()
 Destructor. More...
 
template<class t >
void Initialize (t n)
 Initialize. More...
 
template<class t >
void Load (t, const char *)
 Load sound. More...
 
template<class t >
LSoundDesc play (t, const Vector2 &, float=1, float=0)
 Play sound. More...
 
template<class t >
LSoundDesc play (t, const Vector3 &, float=1, float=0)
 Play sound. More...
 
template<class t >
LSoundDesc play (t, float=1)
 Play sound. More...
 
template<class t >
LSoundDesc loop (t)
 Loop a sound. More...
 
template<class t >
LSoundDesc loop (t, const Vector2 &)
 Loop a sound. More...
 
template<class t >
LSoundDesc loop (t, const Vector3 &)
 Loop a sound. More...
 
template<class t >
LSoundDesc vary (t, float=0.1f, float=1, float=0)
 Play a randomly varied sound. More...
 
template<class t >
LSoundDesc vary (t, const Vector2 &, float=0.1f, float=1, float=0)
 Play a randomly varied sound. More...
 
template<class t >
LSoundDesc vary (t, 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...
 
template<class t >
void stop (t)
 Stop a sound. More...
 
void stop (const LSoundDesc &)
 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 (LBaseCamera *)
 Set listener to camera pos and orientation.
 
void SetScale (const float)
 Set scale for the sound world. More...
 
- Public Member Functions inherited from LSettingsManager
void Load ()
 Load game settings. More...
 

Protected Member Functions

void createInstances (size_t, size_t, SOUND_EFFECT_INSTANCE_FLAGS)
 Create sound instances. More...
 
void LoadByIndex (size_t, const char *)
 Load sound by index. More...
 
size_t getNextInstance (size_t)
 Get the next instance that is not playing. More...
 
- Protected Member Functions inherited from LSettingsManager
void SetWinSize (int, int)
 Set window size information. More...
 

Protected Attributes

AudioEngine * m_pAudioEngine = nullptr
 XAudio 2.8 Engine wrapped up in DirectXTK.
 
LPlayableSoundDescm_pSound = nullptr
 Playable sounds.
 
size_t m_nCount = 0
 Number of sounds.
 
AudioListener m_cListener
 Audio listener.
 
Vector2 m_vEmitterPos
 Position of the emitter (the thing making the sound).
 
Vector3 m_vListenerPos
 Position of the listener.
 
bool m_bMuted = false
 Whether mute is on.
 
float m_fScale = 500.0f
 Coordinate scale factor.
 

Private Attributes

const float DEPTH = 100.0f
 Default Z depth for 2D sounds.
 

Static Private Attributes

static bool m_bSingularityViolation = false
 Enforces that this is a singular class. More...
 

Additional Inherited Members

- Static Protected Attributes inherited from LWindowDesc
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 LSettingsManager
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 LSettings
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 LComponent
static LTimerm_pTimer = &cTimer
 Pointer to a timer. More...
 
static LRandomm_pRandom = &cRandom
 Pointer to a PRNG. More...
 
static LKeyboardm_pKeyboard = &cKeyboard
 Pointer to a keyboard handler. More...
 
static LXBoxControllerm_pController = &cController
 Pointer to a XBox controller. More...
 
static LSoundm_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

◆ LSound()

LSound::LSound ( )

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.

◆ ~LSound()

LSound::~LSound ( )

Reclaim all dynamic memory and shut down the audio engine.

Member Function Documentation

◆ BeginFrame()

void LSound::BeginFrame ( )

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

◆ createInstances()

void LSound::createInstances ( size_t  index,
size_t  n,
SOUND_EFFECT_INSTANCE_FLAGS  flags 
)
protected

Create sound instances.

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

◆ getNextInstance()

size_t LSound::getNextInstance ( size_t  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.

◆ Initialize()

template<class t >
void LSound::Initialize ( e)

Delete old sound descriptors and create the required number of new ones.

Template Parameters
tSound enumerated type.
Parameters
eNumber of sounds.

◆ Load()

template<class t >
void LSound::Load ( e,
const char *  name 
)

Load information about a sound from global variable g_xmlSettings, then load the sound sample as per that information. Abort if something goes wrong. Uses LoadByIndex() to do the actual work.

Template Parameters
tSound enumerated type.
Parameters
eSound type.
nameSound name in XML file.

◆ LoadByIndex()

void LSound::LoadByIndex ( size_t  index,
const char *  name 
)
protected

Load information about a sound from global variable g_xmlSettings, then load sounds as per that information. Abort if something goes wrong.

Parameters
indexSprite index.
nameObject name in XML file.

◆ loop() [1/3]

template<class t >
LSoundDesc LSound::loop ( e)

Loop a sound effect in mono.

Template Parameters
tSound enumerated type.
Parameters
eType of sound to be looped.
Returns
Sound descriptor for playing sound.

◆ loop() [2/3]

template<class t >
LSoundDesc LSound::loop ( e,
const Vector2 &  s 
)

Loop a sound effect in stereo.

Template Parameters
tSound enumerated type.
Parameters
eType of sound to be looped.
sPosition of emitter in 2D.
Returns
Sound descriptor for playing sound.

◆ loop() [3/3]

template<class t >
LSoundDesc LSound::loop ( e,
const Vector3 &  s 
)

Loop a sound effect in stereo.

Template Parameters
tSound enumerated type.
Parameters
eType of sound to be looped.
sPosition of emitter in 3D.
Returns
Sound descriptor for playing sound.

◆ mute()

void LSound::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]

template<class t >
LSoundDesc LSound::play ( e,
const Vector2 &  s,
float  vol = 1,
float  p = 0 
)

Play a sound effect in stereo.

Template Parameters
tSound enumerated type.
Parameters
eType 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]

template<class t >
LSoundDesc LSound::play ( e,
const Vector3 &  s,
float  vol = 1,
float  p = 0 
)

Play a sound effect in stereo.

Template Parameters
tSound enumerated type.
Parameters
eType 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]

template<class t >
LSoundDesc LSound::play ( e,
float  vol = 1 
)

Play a sound effect in mono.

Template Parameters
tSound enumerated type.
Parameters
eType of sound to be played.
volVolume (defaults to 1.0f).
Returns
Sound descriptor for playing sound.

◆ SetListenerOrient()

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

Set the listener orientation from a pair of vectors.

Parameters
vLookLook-at vector.
vUpUp vector.

◆ SetListenerPos()

void LSound::SetListenerPos ( const Vector3 &  pos)

Move the listener to a new position.

Parameters
posNew position of listener.

◆ SetScale()

void LSound::SetScale ( const float  scale)

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

Parameters
scaleScale factor.

◆ stop() [1/3]

void LSound::stop ( )

Stop all instances of all sound effects.

◆ stop() [2/3]

void LSound::stop ( const LSoundDesc d)

Stop an instance of a sound effects.

Parameters
dDescriptor for a sound instance.

◆ stop() [3/3]

template<class t >
void LSound::stop ( e)

Stop all instances of a sound effect.

Template Parameters
tSound enumerated type.
Parameters
eType of sound to be stopped.

◆ vary() [1/3]

template<class t >
LSoundDesc LSound::vary ( e,
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.

Template Parameters
tSound enumerated type.
Parameters
eType 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() [2/3]

template<class t >
LSoundDesc LSound::vary ( e,
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.

Template Parameters
tSound enumerated type.
Parameters
eType 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.

◆ vary() [3/3]

template<class t >
LSoundDesc LSound::vary ( e,
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.

Template Parameters
tSound enumerated type.
Parameters
eType 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.

Member Data Documentation

◆ m_bSingularityViolation

bool LSound::m_bSingularityViolation = false
staticprivate

Singularity violation flag.