using System; using System.Runtime.InteropServices; using System.Security; namespace SFML { namespace Audio { //////////////////////////////////////////////////////////// /// /// Enumeration of all possible sound states /// //////////////////////////////////////////////////////////// public enum SoundStatus { /// Sound is not playing Stopped, /// Sound is paused Paused, /// Sound is playing Playing } //////////////////////////////////////////////////////////// /// /// Sound defines the properties of a sound such as position, /// volume, pitch, etc. /// //////////////////////////////////////////////////////////// public class Sound : ObjectBase { //////////////////////////////////////////////////////////// /// /// Default constructor (invalid sound) /// //////////////////////////////////////////////////////////// public Sound() : base(sfSound_Create()) { } //////////////////////////////////////////////////////////// /// /// Construct the sound from a source buffer /// /// Sound buffer to play //////////////////////////////////////////////////////////// public Sound(SoundBuffer buffer) : base(sfSound_Create()) { SoundBuffer = buffer; } //////////////////////////////////////////////////////////// /// /// Play the sound /// //////////////////////////////////////////////////////////// public void Play() { sfSound_Play(This); } //////////////////////////////////////////////////////////// /// /// Pause the sound /// //////////////////////////////////////////////////////////// public void Pause() { sfSound_Pause(This); } //////////////////////////////////////////////////////////// /// /// Stop the sound /// //////////////////////////////////////////////////////////// public void Stop() { sfSound_Stop(This); } //////////////////////////////////////////////////////////// /// /// Buffer containing the sound data to play through the sound /// //////////////////////////////////////////////////////////// public SoundBuffer SoundBuffer { get {return myBuffer;} set {myBuffer = value; sfSound_SetBuffer(This, value != null ? value.This : IntPtr.Zero);} } //////////////////////////////////////////////////////////// /// /// Current status of the sound (see SoundStatus enum) /// //////////////////////////////////////////////////////////// public SoundStatus Status { get {return sfSound_GetStatus(This);} } //////////////////////////////////////////////////////////// /// /// Loop state of the sound. Default value is false /// //////////////////////////////////////////////////////////// public bool Loop { get {return sfSound_GetLoop(This);} set {sfSound_SetLoop(This, value);} } //////////////////////////////////////////////////////////// /// /// Pitch of the sound. Default value is 1 /// //////////////////////////////////////////////////////////// public float Pitch { get {return sfSound_GetPitch(This);} set {sfSound_SetPitch(This, value);} } //////////////////////////////////////////////////////////// /// /// Volume of the sound, in range [0, 100]. Default value is 100 /// //////////////////////////////////////////////////////////// public float Volume { get {return sfSound_GetVolume(This);} set {sfSound_SetVolume(This, value);} } //////////////////////////////////////////////////////////// /// /// Current playing position of the sound, in seconds /// //////////////////////////////////////////////////////////// public float PlayingOffset { get {return sfSound_GetPlayingOffset(This);} set {sfSound_SetPlayingOffset(This, value);} } //////////////////////////////////////////////////////////// /// /// 3D position of the sound. Default value is (0, 0, 0) /// //////////////////////////////////////////////////////////// public Vector3 Position { get {Vector3 v; sfSound_GetPosition(This, out v.X, out v.Y, out v.Z); return v;} set {sfSound_SetPosition(This, value.X, value.Y, value.Z);} } //////////////////////////////////////////////////////////// /// /// Minimum distance of the sound. Closer than this distance, /// the listener will hear the sound at its maximum volume. /// The default value is 1 /// //////////////////////////////////////////////////////////// public float MinDistance { get {return sfSound_GetMinDistance(This);} set {sfSound_SetMinDistance(This, value);} } //////////////////////////////////////////////////////////// /// /// Attenuation factor. The higher the attenuation, the /// more the sound will be attenuated with distance from listener. /// The default value is 1 /// //////////////////////////////////////////////////////////// public float Attenuation { get {return sfSound_GetAttenuation(This);} set {sfSound_SetAttenuation(This, value);} } //////////////////////////////////////////////////////////// /// /// Handle the destruction of the object /// /// Is the GC disposing the object, or is it an explicit call ? //////////////////////////////////////////////////////////// protected override void Destroy(bool disposing) { sfSound_Destroy(This); } private SoundBuffer myBuffer; #region Imports [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfSound_Create(); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_Destroy(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_Play(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_Pause(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_Stop(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_SetBuffer(IntPtr Sound, IntPtr Buffer); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfSound_GetBuffer(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_SetLoop(IntPtr Sound, bool Loop); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern bool sfSound_GetLoop(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern SoundStatus sfSound_GetStatus(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_SetPitch(IntPtr Sound, float Pitch); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_SetVolume(IntPtr Sound, float Volume); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_SetPosition(IntPtr Sound, float X, float Y, float Z); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_SetMinDistance(IntPtr Sound, float MinDistance); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_SetAttenuation(IntPtr Sound, float Attenuation); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_SetPlayingOffset(IntPtr Sound, float TimeOffset); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern float sfSound_GetPitch(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern float sfSound_GetVolume(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSound_GetPosition(IntPtr Sound, out float X, out float Y, out float Z); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern float sfSound_GetMinDistance(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern float sfSound_GetAttenuation(IntPtr Sound); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern float sfSound_GetPlayingOffset(IntPtr Sound); #endregion } } }