Example #1
0
        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);
        }
Example #2
0
        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);
        }
Example #3
0
 internal void Free(WrappedSoundEffectInstance instance)
 {
     instance.Dispose();
 }