[XNA] limiting number of sound effects

Started by
3 comments, last by Drilian 15 years, 9 months ago
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!
Advertisement
Are you using 3.0?

You don't need XACT in 3.0, and you just play your sound effects directly. I think the limit is 16ish at a time, so it's all done automatically.
Nope 2.0. Hmm guess I better get 3.0 then. =)
Quote:Original post by FippyDarkpaw
Nope 2.0. Hmm guess I better get 3.0 then. =)
I'm not sure there is a 3.0 redist yet. 3.0 is still in the Community Technology Preview phase.

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.

This topic is closed to new replies.

Advertisement