Sign in to follow this  
FippyDarkpaw

[XNA] limiting number of sound effects

Recommended Posts

My game has hundred of units. Units makes the typical noises when certain events happen, such as shooting a weapon, colliding with something, blowing up, etc... I already do distance checking to prevent sound effects from playing in events that happen far away from the player. But this is still not enough. There can be dozens of units in any skirmish and the game (which otherwise runs at a flawless FPS) starts to lag and get sound distortion due all the sound effects playing. Many of the sound effects are instances of the same sound. Are there any built in ways in XNA/XACT to easily limit the number of instances of the same sound effect? If not, what are some other methods for limiting number of sounds playing at the same time? Thanks!

Share this post


Link to post
Share on other sites
There are also some other performance issues related to XACT and XNA.

Specifically, there's some sort of resource leak which causes extensive use of PlayCue to start to bog down the game in an amazing fashion.

And there's also a reference leak within creating a Cue object and calling Play on it.

In order to avoid these two issues, I tried a ridiculous number of things. Finally, I settled on the following:


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Audio;

namespace Asplode
{
class CuePlayer : IDisposable
{
public void Dispose()
{
cue.Dispose();
}

SoundBank bank;
string name;
Cue cue;
public CuePlayer(SoundBank bank, string name)
{
this.bank = bank;
this.name = name;
}

public void Play()
{
//try
{
if(cue != null)
{
if(cue.IsPlaying)
cue.Stop(AudioStopOptions.Immediate);
cue.Dispose();
}

cue = bank.GetCue(name);
cue.Play();
}
//catch (Exception) { }
}
}
}




Then I would create one instance of a CuePlayer per each sound effect that I had, though you could pool them in other ways (make a set of 16 and keep them in a LRU or something, if you prefer). My goal was "one of any given sound effect playing at once", your goals may vary.

The important part is in the Play function. It would stop the existing cue and dispose of it (nicely dodging the reference leak issue), then create an entirely new cue and play it.

Not exactly the nicest way to have to do it, but the game stopped having any stuttering or slowdown after that, so whatever works.

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