Fun Infused Games  |   Smooth Operator RSS 

  Home   |    Archive   |    Subscribe   |    Search   |    About
Too Many SoundEffects Causes Slowdown for WP7
Date 5/4/2011    Tags WP7    (0)

In my ongoing quest to make Hypership for WP7 perform as fast as inhumanly possible, I discovered that playing too many SoundEffect objects actually hurt performance a lot. With no sound effects playing, the game would run at 30 fps or slightly below. When I got to areas with lots of sounds (such as going through a big section of coins or shooting lasers into a solid wall), the performance would drop to as little as 10 fps. This was not acceptable.

Whenever you play a SoundEffect (mySound.Play()), a SoundEffectInstance object is created for that SoundEffect and it is played. For Zune games, you could have a maximum of 16 concurrently playing SoundEffectInstances and for Xbox that number is around 300. For WP7 the magic number is 64. I know I was not hitting that limit but at some point above 14, in-game performance really did tank. Hypership performs well on some devices (LG Optimus 7) and badly on others (HTC HD7) so it seems not every phone handles playing sounds as well as others.

I believe there is some sort of exception thrown when you pass this maximum number of SoundEffectInstances. It doesn't cause your game to crash but it does cause it to slow down (likely because an exception is being handled somewhere). I haven't found this behavior documented anywhere, it's just what I heard. If you have a better explanation, let me know in the comments and I'll update this article to match.

In order to resolve the slowdown, I decided to setup a way to track what SoundEffectInstances had most recently been played and stop the oldest instance before playing a new one. You cannot stop a SoundEffect object from playing which means that you need to create SoundEffectInstances manually each time and play those because you can stop SoundEffectInstances.

First you need a list to hold all the SoundEffectInstances and you also need to declare whatever SoundEffects you wish to play (you should also load those SoundEffects somewhere but I'm not going to show you code for that).
// This is a global list of all soundInstances played and should be scoped as such.
List<SoundEffectInstance> soundInstances;

// This is an example SoundEffect. Use your own!
SoundEffect mySound; 
Wherever you play your sounds, you need to do the following. I play all my sounds from one SoundBank object I created with a Play method, so it was easy to centralize this logic.

// If there are too many sounds playing already, stop the oldest. 
// I used 14 just to be safe. You probably could go a couple higher.
if (this.soundInstances.Count > 14)
{
    this.soundInstances[0].Stop();
    this.soundInstances.Remove(this.soundInstances[0]);
}

// Rather than playing mySound directly, create an instance which can be played or stopped.
SoundEffectInstance newInstance = mySound.CreateInstance();
newInstance.Play();

// Add the instance to this collection so you can stop it later.
this.soundInstances.Add(newInstance); 
                                      
Simple as that. Now you should be able to easily stop old sounds and prevent slowdown that may otherwise plague your game. If you have other comments or suggestions, please leave them in the comments below.


This article has been view 2205 times.


Comments

No comments for this article.


Add Comments

Name *
Website
  Name the animal in the picture below:

*  
Comment *
Insert Cancel


Tags
ASP.net (18)  Fin (1)  Video Games (7)  Game Dev (11)  Abduction Action (1)  WP7 (8)  Visual Studio (1)  Hypership (28)  Advise (14)  C# (14)  FIN (20)  World of Chalk (2)  Absurd (2)  Abduction Action! (27)  Nasty (34)  PC (1)  Cool (2)  Sports (11)  Rant (50)  VolChaos (1)  Development (13)  Design (2)  Volchaos (11)  XNA (40)  Nastier (4)  Xbox (1)  iOS (3)  SQL (1)  XBLIG (32)  Trivia or Die (3)  Web (19)  Trivia Or Die (1)  Abdction Action! (1)