Sign in to follow this  
cp25stack

How to load and manage multiple sound effect properly

Recommended Posts

 
Hello,
Have some questions about Game audio sound effect management.
 
1 What is the proper way to load multiple sound effects?
My game have around 100 sound effects type to play in 1 scene.
(It a scene of big forest that have many kind of monsters 
and each of them have various skill, magic they can use.)
Should i creat 100 sound buffer and load all of them into 100 sound buffer or not?
Is there another way better than this?
 
2 Normally do you have maximum of sound effects player at a time?
For example if there are 50 soldiers on screen and they fire gun at athe same time.
Do you really play 50 fire gun sfx at a same time or have some limit number?
 
Here is some psuedo code i use, is it OK or not
-----------------------------------------------------------------------------
.h
-----------------------------------------------------------------------------
// 100 sound buffer to keep 100 sfx in this scene
soundLib::SoundBuffer    m_sfxArray[100];
 
// Plan to not have more than 8 sfx plaing at same time
soundLib::Sound    m_sfxPlayer[8];
 
-----------------------------------------------------------------------------
.cpp
-----------------------------------------------------------------------------
// At loading phase
m_sfxArray[0].LoadFromFile("a.wav");
:
m_sfxArray[99].LoadFromFile("aaa.wav");
 
// Then at some event i want to play sound a.wav
m_sfxPlayer[avaiSlot].SetBuffer(m_sfxArray[0]);
m_sfxPlayer[avaiSlot].Play();
-----------------------------------------------------------------------------
 
Thanks in advance smile.png

Share this post


Link to post
Share on other sites

I typically have some sound utility functions I call to play the sounds, but I'm using SFML.  The sounds needed for a scene are loaded when that scene is loaded into a map with a sound name associated with it (using std::map).  Part of what is loaded is the number of times each sound should be play simultaneously.  For example, take a machine gun sound, if 12 machine guns are going off at the same time, you may want to limit the number of machine gun shooting sounds to 8, so the sound utility will only play at most 8 of those sounds at once.

 

When I want to play a sound I simply call PlaySound(std::string SoundToPlay, Vector2 LocationSoundOriginates);   and the sound engine will handle playing it, and where it is played from (for spatial sound usage).

 

That's just how I do it, and it seems to work fine.

Share this post


Link to post
Share on other sites


1 What is the proper way to load multiple sound effects?

Depends on the game and the engine.

 

For level-based games you may want to load all the level's resources in advance.

 

For location-based streamed resource games you may want to load only the resources for sound emitters located close to the player.

 

For something else, you may want something else.

 

Your design may suggest some other solutions.

 

 


2 Normally do you have maximum of sound effects player at a time?
For example if there are 50 soldiers on screen and they fire gun at athe same time.
Do you really play 50 fire gun sfx at a same time or have some limit number?

 

Most of the professional level audio tools allow sound designers to assign fixed priorities and dynamic priorities to sounds, assign maximum numbers of concurrent playbacks, and assign the sound clip to a pool inside the mixer.

 

For your 50 soldiers with a machine gun example, the sound designer might hook up various settings in their sound designer tools. First, it would be a 3D positional sound so the priority is modified by distance; even though the clip has a single priority the distance means some will be higher priority, others lower priority. With fifty emitters the maximum number of concurrent playbacks will come into play, perhaps the sound designer set a limit of 3 since beyond that it sounds terrible. So of the 50 sound emitters only 3 will actually be sent on to the mixer, likely the three with the highest priority (typically meaning the closest/loudest ones). Finally there is the pool of channels within the mixer, perhaps the sound designer has a pool of ten slots that all shooting sounds use. If you've got shotguns and rifles and machine guns and pistols all firing at once only the highest priority sounds within that pool would be played after all ten slots are used.

Share this post


Link to post
Share on other sites

My game uses fmod, and i allocate a virtual channel for each effect.

 

They are stored in a std::map keyed by effect name.

 

#define AUDIO_MUSIC_CREDITS 0
#define AUDIO_MUSIC_LEVEL1 1
#define AUDIO_MUSIC_LEVEL2 2
#define AUDIO_MUSIC_LEVEL3 3
#define AUDIO_MUSIC_LEVEL4 4
#define AUDIO_MUSIC_LEVEL5 5
#define AUDIO_MUSIC_TITLE 6

/* Holds details of a sound effect file and its channel,
* if it is currently playing
*/
class SoundEffect
{
public:
FMOD::Sound* sound;
FMOD::Channel* channel;

SoundEffect(FMOD::Sound* s);
};

/* All sound effects are loaded into a std::map keyed by filename */
typedef std::map<std::wstring, SoundEffect*> SFXBank;

class Audio
{
private:
FMOD::System *system;
/* Sound file and sub-sound for background music */
FMOD::Sound *sound, *sound_to_play;
/* Channel for background music */
FMOD::Channel *channel;
/* Result variable for various FMOD:: functions */
FMOD_RESULT result;
/* FMOD version number */
unsigned int version;
/* Opaque FMOD data */
void *extradriverdata;
/* Number of music tracks in the music.fsb file */ 
int numsubsounds;
public:
/* Holds all known sound effects */
static SFXBank effects;
Audio();
/* Play background music in a loop */
bool PlayMusic(int tracknum);
/* Stop current background music */
void StopMusic();
/* Pause background music */
void PauseMusic(bool pause);
/* Indicate if music is currently playing */
bool MusicIsPlaying();
/* Play a sound effect, if neccessary mixing it into the background music.
* Each sound effect has its own channel.
*/
bool PlayEffect(const std::wstring &soundfile);
/* Returns true if the given sound effect is currently playing */
bool EffectIsPlaying(const std::wstring &soundfile);
/* Used internally to indicate completion of a sound effect */
static FMOD_RESULT F_CALLBACK SoundComplete(FMOD_CHANNELCONTROL *channel, FMOD_CHANNELCONTROL_TYPE type, FMOD_CHANNELCONTROL_CALLBACK_TYPE cbtype, void *commanddata1, void *commanddata2);
/* Called in the GameWindow::Update() method to update FMOD internals */
void Update();
/* Returns the last error string */
const std::wstring GetError();
/* Frees FMOD memory and other resources */
~Audio();
};

 

There is a similar map for music, the difference being that music supports streaming and effects don't.

 

The lot is pre-loaded from an fsb (fmod sound bank) file, which takes about 100mb ram (these days not much at all) and responds with next to no latency.

 

Most sound libraries allow many hundreds of virtual channels at no real cost, so this might work for you too.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this