Created
December 10, 2012 12:08
-
-
Save adrianseeley/4250234 to your computer and use it in GitHub Desktop.
SoundPool :: a C# XNA 4.0 Snippet for Properly Instancing Sound Effects Using an As-Needed Pool Methodology
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a pool: | |
// SoundPool explosion = new SoundPool(Content, "explosion"); - uses explosion.wav from the content folder | |
// Play back the sound: | |
// explosion.Play(); | |
// Play back the sound with some funk: | |
// explosion.Play(Volume: 1.0f, Pan: 0.0f, Pitch 0.0f); | |
// | |
// If you're worried about performance, I promise this is the fastest and cleanest way. | |
// You may run into issues if you try to spam too many sounds on the wrong devices, from MSOFT: | |
// | |
// On Windows Phone, a game can have a maximum of 16 total playing SoundEffectInstance instances at one time, | |
// combined across all loaded SoundEffect objects. The only limit to the total number of loaded SoundEffectInstance | |
// and SoundEffect objects is available memory. However, the user can play only 16 sound effects at one time. | |
// Attempts to play a SoundEffectInstance beyond this limit will fail. On Windows, there is no hard limit. Playing too | |
// many instances can lead to performance degradation. On Xbox 360, the limit is 300 sound effect instances loaded or playing. | |
// Dispose of old instances if you need more. | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Audio; | |
using Microsoft.Xna.Framework.Content; | |
using Microsoft.Xna.Framework.GamerServices; | |
using Microsoft.Xna.Framework.Graphics; | |
using Microsoft.Xna.Framework.Input; | |
using Microsoft.Xna.Framework.Media; | |
namespace YOUR_NAMESPACE_HERE_BRO | |
{ | |
class SoundPool | |
{ | |
public SoundEffect sound; | |
public List<SoundEffectInstance> pool; | |
public SoundPool(ContentManager Content, String Filename) | |
{ | |
sound = Content.Load<SoundEffect>(Filename); | |
pool = new List<SoundEffectInstance>() { sound.CreateInstance() }; | |
} | |
public void Play(float Volume = 1f, float Pan = 0f, float Pitch = 0f) | |
{ | |
for(int p = 0; p < pool.Count; p++) | |
{ | |
if(pool[p].State == SoundState.Stopped) | |
{ | |
Play(pool[p], Volume, Pan, Pitch); | |
return; | |
} | |
} | |
pool.Add(sound.CreateInstance()); | |
Play(pool[pool.Count - 1], Volume, Pan, Pitch); | |
} | |
private void Play(SoundEffectInstance SoundEffectInstance, float Volume, float Pan, float Pitch) | |
{ | |
SoundEffectInstance.Volume = Volume; | |
SoundEffectInstance.Pan = Pan; | |
SoundEffectInstance.Pitch = Pitch; | |
SoundEffectInstance.Play(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment