4
\$\begingroup\$

Does it make sense to carry out all audio (music, sound effects, etc.) in a separate thread?

In your experience, how large would you expect the performance impact of playing some MP3 music and 10-30 sound effects to be?

Could performance be an issue with starting to play many different sound effects (e.g. in the same frame)?

\$\endgroup\$
2
  • 4
    \$\begingroup\$ I belive, internaly, the audio api creates a separate thread to let the hardware read from there. So i dont think there will be any significant performance impact on playing an mp3. the thing i can see to cuase performance drops is how you handle your audio outside of the hardware. etc, choosing what sound to play, how many channles to use etc. \$\endgroup\$
    – Tordin
    Jan 11, 2013 at 13:32
  • 3
    \$\begingroup\$ You can always test it. Create an empty XNA game, add 100-100k sound effects, and see where it becomes a performance bottleneck. \$\endgroup\$
    – ashes999
    Jan 11, 2013 at 21:52

1 Answer 1

4
\$\begingroup\$

First things first: If you're targeting XBLIG and Windows desktop, you're using XACT, right? Just making sure. Unless you have a compelling reason not to, you should be using XACT. It's got warts, but it solves too many problems for it not be used. That is, unless you're looking to also compile with MonoGame or deploy to Windows Phone. Then it's not so useful.

Does it make sense to carry out all audio (music, sound effects, etc.) in a separate thread?

Many audio playback libraries start a background thread to load/stream audio into memory and write to the output buffer. You adding another thread on top of this wouldn't do much without a lot of main loop tweaking and tuning (which is probably futile if you aren't specifically targeting XBLIG).

In your experience, how large would you expect the performance impact of playing some MP3 music and 10-30 sound effects to be?

Yes, there's an impact. How much that impact matters depends on the minimum hardware you want to target and what else is happening in the game at that time. But, before you run into performance problems, you'll run into ear fatigue problems with that many sound effects if you don't do some clever mixing.

Could performance be an issue with starting to play many different sound effects (e.g. in the same frame)?

You'll probably get a framerate dip if there's a ton of sounds starting up in one frame.

If it really becomes a performance problem, consider staggering their playback across frames. Players' ears won't notice the difference, and you give the CPU some extra milliseconds to start mixing the samples.

Rough crappy example that uses SoundEffect:

public class AudioManager
{
    private const MaxSoundTriggersPerFrame = 15;

    private struct PlayInfo
    {
        public SoundEffect sfx;
        public float volume;
        public float pitch;
        public float pan;
    }

    Queue<SoundEffect> queuedSFX = new Queue<SoundEffect>();

    PlayOneShot(SoundEffect sfx, float volume, float pitch, float pan)
    {
        queuedSFX.Add(new PlayInfo() {
            sfx = sfx,
            volume = volume,
            pitch = pitch,
            pan = pan
        });
    }

    // Assuming this is called every update frame from Game1.cs or whatever
    public void Update()
    {
        int playCount = 0;

        while (queuedSFX.Count > 0 && playCount <= MaxSoundTriggersPerFrame) {
            var playInfo = queuedSFX.Dequeue();
            playInfo.sfx.Play(playInfo.volume, playInfo.pitch, playInfo.pan);
        }
    }
}

It would be neat to pre-populate some metadata about a particular sound effect. In-memory sounds could have their own limit, and sounds that have to be streamed and decoded could have a separate limit.

This also uses a .NET queue, and if you're concerned about performance maybe look into how this is implemented (I don't know off the top of my head). You want it to behave like a List<T>, such that internally it stores an array of PlayInfo objects and not a linked list of them.

But again: If you're thinking at this level, twenty bucks says you have too many sounds playing at once to be pleasant or useful.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I'm not sure it's wise to be recommending XACT so strongly - without this disclaimer: It's not supported by XNA on WP7, or in MonoGame or ExEn on any platform. It's also deprecated in Windows 8 (and removed from the SDK). Basically you're strictly limited to XNA 4 on Windows Desktop and Xbox 360. \$\endgroup\$ Jan 12, 2013 at 12:48
  • 1
    \$\begingroup\$ @AndrewRussell I did make an assumption in my answer that it would be a XNA4 XBLIG or windows desktop game, so I revised the answer. It's news to me that windows phone doesn't support it though, thanks for the heads up on that one. I've only toyed with XNA graphics on Windows phone. \$\endgroup\$ Jan 12, 2013 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .