I have been using a profiler to optimize my game for the Xbox, my aim is as with any optimization to reduce excess CPU and memory usage. I have managed to cut down a lot of the processor time and memory used by my game. However i have a lot of bullets in my game that the player fires and every-time a bullet impacts with something the soundeffect.play() method is called to play the explosion. I would like to know if there is a more efficient way of playing the explosion soundeffect?
You could possibly look into
SoundEffectInstance. You can get one with
SoundEffect.CreateInstance (and must
Dispose of it when you are finished with it).
SoundEffect.Play internally creates
SoundEffectInstance objects and plays them. It then adds them to an internal list so that, once that sound effect has finished playing, it is stopped and added back to a pool. The next time you call
SoundEffect.Play, it will try and get a spare instance from the pool, before allocating another one.
SoundEffectInstance yourself, you can pre-allocate your desired number of instances of a sound, so you don't have the cost of allocation if the pool is empty. And you save the (small) overhead involved in tracking those internal "fire and forget" sound instances.
This can also allow you to limit the number of sounds playing at once (saving CPU). For example, if you create three instances of a sound (in a circular buffer), and your game has a situation where it wants to trigger that sound four times in a row - on the fourth trigger you could either cut off the oldest sound and restart it, or skip playing the new sound.