public SoundEffectInstance GetNextFreeInstance(SoundID soundID, int maxInstances) { var list = instances[soundID]; foreach (var instance in list) { if (instance.State == SoundState.Playing) { maxInstances--; } if (maxInstances <= 0) { return(null); } if (instance.State == SoundState.Stopped) { return(instance); } } var newInstance = SoundBank.GetMappedKey(soundID).CreateInstance(); list.Add(newInstance); return(newInstance); }