![]() |
The LARC Game Engine
A Simple Game Engine from the Laboratory for Recreational Computing
|
The Audio Player. More...
#include <Sound.h>
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... | |
![]() | |
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... | |
![]() | |
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 HWND | m_Hwnd = 0 |
Window handle. | |
static HINSTANCE | m_hInst = 0 |
Instance handle. | |
static bool | m_bExitSizeMove |
User just finished moving/resizing window. | |
![]() | |
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 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 CStepTimer * | m_pStepTimer = &cStepTimer |
Pointer to a step timer. More... | |
static CRandom * | m_pRandom = &cRandom |
Pointer to a PRNG. More... | |
static CKeyboard * | m_pKeyboard = &cKeyboard |
Pointer to a keyboard handler. More... | |
static CXBoxController * | m_pController = &cController |
Pointer to a XBox controller. More... | |
static CAudio * | m_pAudio = &cAudio |
Pointer to an audio player. More... | |
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.
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 | ( | ) |
Reclaim all dynamic memory and shut down the audio engine.
void CAudio::BeginFrame | ( | ) |
Get ready for new animation frame by flagging all sounds as unplayed.
|
protected |
Create sound instances.
index | Index of sound. |
n | Number of instances of sound wanted. |
flags | Flags. |
|
protected |
Get the index into the instance array of the next unplayed instance of a sound.
index | Index of sound in the sound array. |
|
protected |
Load a sound from a file.
filename | name of file to be loaded |
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.
CSoundDesc CAudio::loop | ( | int | i | ) |
Loop a sound effect in mono.
i | Index of sound to be looped. |
CSoundDesc CAudio::loop | ( | int | i, |
const Vector2 & | s | ||
) |
Loop a sound effect in stereo.
i | Index of sound to be looped. |
s | Position of emitter in 2D. |
CSoundDesc CAudio::loop | ( | int | i, |
const Vector3 & | s | ||
) |
Loop a sound effect in stereo.
i | Index of sound to be looped. |
s | Position of emitter in 3D. |
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.
CSoundDesc CAudio::play | ( | int | i, |
const Vector2 & | s, | ||
float | vol = 1.0f , |
||
float | p = 0.0f |
||
) |
Play a sound effect in stereo.
i | Index of sound to be played. |
s | Position of emitter in 2D. |
vol | Volume (defaults to 1.0f). |
p | Pitch (defaults to zero). |
CSoundDesc CAudio::play | ( | int | i, |
const Vector3 & | s, | ||
float | vol = 1.0f , |
||
float | p = 0.0f |
||
) |
Play a sound effect in stereo.
i | Index of sound to be played. |
s | Position of emitter in 3D. |
vol | Volume (defaults to 1.0f). |
p | Pitch (defaults to zero). |
CSoundDesc CAudio::play | ( | int | i, |
float | vol = 1.0f |
||
) |
Play a sound effect in mono.
i | Index of sound to be played. |
vol | Volume (defaults to 1.0f). |
void CAudio::SetListenerOrient | ( | const Vector3 & | vLook, |
const Vector3 & | vUp | ||
) |
Set the listener orientation from a pair of vectors.
vLook | Look-at vector. |
vUp | Up vector. |
void CAudio::SetListenerPos | ( | const Vector3 & | pos | ) |
Move the listener to a new position.
pos | New position of listener. |
void CAudio::SetPitch | ( | int | i, |
float | f | ||
) |
Set the pitch of the first instance of a sound.
i | Index of sound to be played. |
f | New pitch. |
void CAudio::SetScale | ( | const float | scale | ) |
Set the scale factor for converting from render coordinates to sound coordinates.
scale | Scale factor. |
void CAudio::stop | ( | ) |
Stop all instances of all sound effects.
void CAudio::stop | ( | int | i | ) |
Stop all instances of a sound effect.
i | Index of sound to be stopped. |
void CAudio::stop | ( | const CSoundDesc & | d | ) |
Stop an instance of a sound effects.
d | Descriptor for a sound instance. |
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.
i | Index of sound to be played. |
var | Coefficient of variability (defaults to 0.25). |
vol | Volume (defaults to 1.0). |
p | Pitch (defaults to 0). |
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.
i | Index of sound to be played. |
s | Position of emitter in 2D. |
var | Coefficient of variability (defaults to 0.25). |
vol | Volume (defaults to 1.0). |
p | Pitch (defaults to 0). |
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.
i | Index of sound to be played. |
s | Position of emitter in 3D. |
var | Coefficient of variability (defaults to 0.25). |
vol | Volume (defaults to 1.0). |
p | Pitch (defaults to 0). |