public bool TryGetSoundEffect(string assetName, out WrappedSoundEffectInstance result, bool throttle = true) { AudioBuffer buffer = soundEffectCache.GetOrAdd(assetName, LoadSoundEffect); bool available; if (available = buffer.TryGetInstance(throttle, totalTime, out result)) { result.Instance.Volume *= SoundVolume; } // return values return(available); }
public bool TryGetInstance(bool throttle, float totalTime, out WrappedSoundEffectInstance result) { result = null; if (!(totalTime - lastPlayed > definition.MinimumTimeBetween) && throttle) { return(false); } bool lockTaken = false; spinLock.Enter(ref lockTaken); try { if (instanceCount < definition.InstanceLimit || !throttle) { instanceCount++; } else { return(false); } lastPlayed = totalTime; } finally { spinLock.Exit(); } var pooledObject = instancePool.GetFree(); result = pooledObject.Value; result.PoolObject = pooledObject; result.Instance.Volume = MathHelper.Clamp(RandomExt.GetRandomFloat(definition.MinVolume, definition.MaxVolume), 0, 1); result.Instance.Pitch = MathHelper.Clamp(RandomExt.GetRandomFloat(definition.MinPitchShift, definition.MaxPitchShift), 0, 1); return(true); }
internal void Free(WrappedSoundEffectInstance instance) { instance.Dispose(); }