using System; using Microsoft.Xna.Framework.Audio; using MLEM.Extensions; namespace MLEM.Sound { /// /// A sound effect info is a wrapper around that additionally stores , and information. /// Additionally, a can be applied, a can be created using , and more. /// public class SoundEffectInfo { private static readonly Random Random = new Random(); /// /// The that is played by this info. /// public readonly SoundEffect Sound; /// /// Volume, ranging from 0.0 (silence) to 1.0 (full volume). Volume during playback is scaled by SoundEffect.MasterVolume. /// public float Volume; /// /// Pitch adjustment, ranging from -1.0 (down an octave) to 0.0 (no change) to 1.0 (up an octave). /// To incorporate , you can use . /// public float Pitch; /// /// Pan value ranging from -1.0 (left speaker) to 0.0 (centered), 1.0 (right speaker). /// public float Pan; /// /// A value that allows randomly modifying every time or are used. /// The random modifier that is added onto will be between - and . /// public float RandomPitchModifier; /// /// Creates a new sound effect info with the given values. /// /// The sound to play /// The volume to play the sound with /// The pitch to play the sound with /// The pan to play the sound with public SoundEffectInfo(SoundEffect sound, float volume = 1, float pitch = 0, float pan = 0) { this.Sound = sound; this.Volume = volume; this.Pitch = pitch; this.Pan = pan; } /// /// Returns a random pitch for this sound effect info that is between - and + . /// If is 0, this method always returns . /// /// A random pitch to use to play this sound effect public float GetRandomPitch() { if (this.RandomPitchModifier == 0) return this.Pitch; return this.Pitch + ((float) Random.NextDouble() * 2 - 1) * this.RandomPitchModifier; } /// /// Plays this info's once. /// /// False if more sounds are currently playing than the platform allows public bool Play() { return this.Sound.Play(this.Volume, this.GetRandomPitch(), this.Pan); } /// /// Creates a new with this sound effect info's data. /// /// The value to set the returned instance's to. Defaults to false. /// A new sound effect instance, with this info's data applied public SoundEffectInstance CreateInstance(bool isLooped = false) { return this.Sound.CreateInstance(this.Volume, this.GetRandomPitch(), this.Pan, isLooped); } } }