SAGE
A Simple Academic Game Engine
Loading...
Searching...
No Matches
Sage::CSound Class Reference

The Sound Player. More...

#include <SageSound.h>

Inheritance diagram for Sage::CSound:
Collaboration diagram for Sage::CSound:

Public Member Functions

 CSound ()
 Constructor.
 
 ~CSound ()
 Destructor.
 
template<class t >
void LoadMT (Sage::CMediaList< t > &)
 Load sounds multithreaded.
 
template<class t >
void Load (Sage::CMediaList< t > &)
 Load sounds.
 
const bool Loaded () const
 Sound loading completed.
 
template<class t >
void Load (t, const char *)
 Load sound.
 
template<class t >
CSoundDesc Play (t, const Vector2 &, float=1.0f, float=0.0f)
 Play sound.
 
template<class t >
CSoundDesc Play (t, const Vector3 &, float=1.0f, float=0.0f)
 Play sound.
 
template<class t >
CSoundDesc Play (t, float=1.0f)
 Play sound.
 
template<class t >
CSoundDesc Play (t, float, float)
 Play sound.
 
template<class t >
CSoundDesc Loop (t)
 Loop a sound.
 
template<class t >
CSoundDesc Loop (t, const Vector2 &)
 Loop a sound.
 
template<class t >
CSoundDesc Loop (t, const Vector3 &)
 Loop a sound.
 
template<class t >
CSoundDesc Vary (t, float=0.1f, float=1, float=0)
 Play a randomly varied sound.
 
template<class t >
CSoundDesc Vary (t, const Vector2 &, float=0.1f, float=1, float=0)
 Play a randomly varied sound.
 
template<class t >
CSoundDesc Vary (t, const Vector3 &, float=0.1f, float=1, float=0)
 Play a randomly varied sound.
 
void BeginFrame ()
 Start of frame notification.
 
void Mute ()
 Mute/unmute the sounds.
 
void Stop ()
 Stop all sounds.
 
template<class t >
void Stop (t)
 Stop a sound.
 
void Stop (const CSoundDesc &)
 Stop a sound instance.
 
void SetPitch (const CSoundDesc &, float)
 Set pitch.
 
void SetListenerPos (const Vector3 &)
 Set listener position.
 
void SetListenerPos (const Vector2 &)
 Set listener position.
 
void SetListenerOrient (const Vector3 &, const Vector3 &)
 Set listener orientation.
 
void SetListener (CCamera *)
 Set listener to camera pos and orientation.
 
void SetScale (const float)
 Set scale for the sound world.
 
- Public Member Functions inherited from Sage::CSettingsManager
void Load ()
 Load game settings.
 

Protected Member Functions

void Initialize (size_t)
 Initialize.
 
void CreateInstances (size_t, size_t, SOUND_EFFECT_INSTANCE_FLAGS)
 Create sound instances.
 
void LoadByIndex (size_t, const char *)
 Load sound by index.
 
size_t GetNextInstance (size_t)
 Get the next instance that is not playing.
 
- Protected Member Functions inherited from Sage::CSettingsManager
void SetWinSize (int, int)
 Set window size information.
 

Protected Attributes

std::unique_ptr< AudioEngine > m_pAudioEngine
 Pointer to an audio engine.
 
CPlayableSoundDescm_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.
 
bool m_bLoaded = false
 Sounds loaded flag.
 

Additional Inherited Members

- Static Protected Attributes inherited from Sage::CWindowDesc
static HWND m_Hwnd = 0
 Window handle.
 
static HINSTANCE m_hInst = 0
 Instance handle.
 
static bool m_bExitSizeMove = true
 User just finished moving/resizing window.
 
- Static Protected Attributes inherited from Sage::CSettingsManager
static tinyxml2::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 Sage::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 Sage::CComponent
static std::unique_ptr< CTimerm_pTimer = std::make_unique<CTimer>()
 Pointer to a timer.
 
static std::unique_ptr< CRandomm_pRandom = std::make_unique<CRandom>()
 Pointer to a PRNG.
 
static std::unique_ptr< CSoundm_pSound = std::make_unique<CSound>()
 Pointer to sound manager.
 
static std::unique_ptr< CKeyboardm_pKeyboard
 Pointer to a keyboard handler.
 
static std::unique_ptr< CMousem_pMouse = std::make_unique<CMouse>()
 Pointer to a mouse handler.
 
static std::unique_ptr< CControllerm_pController
 Pointer to a controller.
 

Detailed Description

The Sound 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

◆ CSound()

CSound::CSound ( )

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.

◆ ~CSound()

CSound::~CSound ( )

Reclaim all dynamic memory and shut down the audio engine.

Member Function Documentation

◆ BeginFrame()

void CSound::BeginFrame ( )

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

◆ CreateInstances()

void CSound::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.
Here is the caller graph for this function:

◆ GetNextInstance()

size_t CSound::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.
Here is the caller graph for this function:

◆ Initialize()

void CSound::Initialize ( size_t n)
protected

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

Parameters
nNumber of sounds.
Here is the caller graph for this function:

◆ Load() [1/2]

template<class t >
void Sage::CSound::Load ( Sage::CMediaList< t > & media)

Load sounds synchronously from a media descriptor.

Template Parameters
tSound enumerated type.
Parameters
mediaMedia list.
Here is the call graph for this function:

◆ Load() [2/2]

template<class t >
void Sage::CSound::Load ( t 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.
Here is the call graph for this function:

◆ LoadByIndex()

void CSound::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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Loaded()

const bool CSound::Loaded ( ) const

Reader function for the loaded flag m_bLoaded.

Returns
true if loading completed.

◆ LoadMT()

template<class t >
void Sage::CSound::LoadMT ( Sage::CMediaList< t > & media)

Load sounds asynchronously from a media descriptor, that is, spawn a thread to do the loading. Member variable m_bLoaded will be set to true when loading is finished.

Template Parameters
tSound enumerated type.
Parameters
mediaMedia list.
Here is the call graph for this function:

◆ Loop() [1/3]

template<class t >
CSoundDesc Sage::CSound::Loop ( t 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.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Loop() [2/3]

template<class t >
CSoundDesc Sage::CSound::Loop ( t 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.
Here is the call graph for this function:

◆ Loop() [3/3]

template<class t >
CSoundDesc Sage::CSound::Loop ( t 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.
Here is the call graph for this function:

◆ Mute()

void CSound::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.

Here is the call graph for this function:

◆ Play() [1/4]

template<class t >
CSoundDesc Sage::CSound::Play ( t e,
const Vector2 & s,
float vol = 1.0f,
float pitch = 0.0f )

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).
pitchPitch (defaults to zero).
Returns
Sound descriptor for playing sound.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Play() [2/4]

template<class t >
CSoundDesc Sage::CSound::Play ( t e,
const Vector3 & s,
float vol = 1.0f,
float p = 0.0f )

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.
Here is the call graph for this function:

◆ Play() [3/4]

template<class t >
CSoundDesc Sage::CSound::Play ( t e,
float vol,
float pitch )

Play a sound effect in mono.

Template Parameters
tSound enumerated type.
Parameters
eType of sound to be played.
volVolume.
pitchPitch.
Returns
Sound descriptor for playing sound.
Here is the call graph for this function:

◆ Play() [4/4]

template<class t >
CSoundDesc Sage::CSound::Play ( t e,
float vol = 1.0f )

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.
Here is the call graph for this function:

◆ SetListener()

void CSound::SetListener ( CCamera * pCamera)

Set listener to camera pos and orientation.

Parameters
pCameraPointer to the camera.
Here is the call graph for this function:

◆ SetListenerOrient()

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

Set the listener orientation from a pair of vectors.

Parameters
vLookLook-at vector.
vUpUp vector.
Here is the caller graph for this function:

◆ SetListenerPos() [1/2]

void CSound::SetListenerPos ( const Vector2 & pos)

Move the listener to a new position.

Parameters
posNew position of listener.
Here is the call graph for this function:

◆ SetListenerPos() [2/2]

void CSound::SetListenerPos ( const Vector3 & pos)

Move the listener to a new position.

Parameters
posNew position of listener.
Here is the caller graph for this function:

◆ SetPitch()

void CSound::SetPitch ( const CSoundDesc & d,
float pitch )

Set the pitch of the first instance of a sound.

Parameters
dSound descriptor.
pitchNew pitch.

◆ SetScale()

void CSound::SetScale ( const float scale)

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

Parameters
scaleScale factor.

◆ Stop() [1/3]

void CSound::Stop ( )

Stop all instances of all sound effects.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Stop() [2/3]

void CSound::Stop ( const CSoundDesc & d)

Stop an instance of a sound effects.

Parameters
dDescriptor for a sound instance.

◆ Stop() [3/3]

template<class t >
void Sage::CSound::Stop ( t e)

Stop all instances of a sound effect.

Template Parameters
tSound enumerated type.
Parameters
eType of sound to be stopped.
Here is the call graph for this function:

◆ Vary() [1/3]

template<class t >
CSoundDesc Sage::CSound::Vary ( t 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.
Here is the call graph for this function:

◆ Vary() [2/3]

template<class t >
CSoundDesc Sage::CSound::Vary ( t 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 >
CSoundDesc Sage::CSound::Vary ( t 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.
Here is the call graph for this function: