using System; using System.Runtime.InteropServices; using System.Security; using System.IO; namespace SFML { namespace Audio { //////////////////////////////////////////////////////////// /// /// Music defines a big sound played using streaming, /// so usually what we call a music :) /// //////////////////////////////////////////////////////////// public class Music : ObjectBase { //////////////////////////////////////////////////////////// /// /// Construct the music from a file /// /// Path of the music file to load //////////////////////////////////////////////////////////// public Music(string filename) : base(sfMusic_CreateFromFile(filename)) { if (This == IntPtr.Zero) throw new LoadingFailedException("music", filename); } //////////////////////////////////////////////////////////// /// /// Construct the music from a file in a stream /// /// Stream containing the file contents //////////////////////////////////////////////////////////// public Music(Stream stream) : base(IntPtr.Zero) { stream.Position = 0; byte[] StreamData = new byte[stream.Length]; uint Read = (uint)stream.Read(StreamData, 0, StreamData.Length); unsafe { fixed (byte* dataPtr = StreamData) { SetThis(sfMusic_CreateFromMemory((char*)dataPtr, Read)); } } if (This == IntPtr.Zero) throw new LoadingFailedException("music"); } //////////////////////////////////////////////////////////// /// /// Play the music /// //////////////////////////////////////////////////////////// public void Play() { sfMusic_Play(This); } //////////////////////////////////////////////////////////// /// /// Pause the music /// //////////////////////////////////////////////////////////// public void Pause() { sfMusic_Pause(This); } //////////////////////////////////////////////////////////// /// /// Stop the music /// //////////////////////////////////////////////////////////// public void Stop() { sfMusic_Stop(This); } //////////////////////////////////////////////////////////// /// /// Samples rate, in samples per second /// //////////////////////////////////////////////////////////// public uint SampleRate { get {return sfMusic_GetSampleRate(This);} } //////////////////////////////////////////////////////////// /// /// Number of channels (1 = mono, 2 = stereo) /// //////////////////////////////////////////////////////////// public uint ChannelsCount { get {return sfMusic_GetChannelsCount(This);} } //////////////////////////////////////////////////////////// /// /// Current status of the music (see SoundStatus enum) /// //////////////////////////////////////////////////////////// public SoundStatus Status { get {return sfMusic_GetStatus(This);} } //////////////////////////////////////////////////////////// /// /// Total duration of the music, in seconds /// //////////////////////////////////////////////////////////// public float Duration { get {return sfMusic_GetDuration(This);} } //////////////////////////////////////////////////////////// /// /// Loop state of the sound. Default value is false /// //////////////////////////////////////////////////////////// public bool Loop { get {return sfMusic_GetLoop(This);} set {sfMusic_SetLoop(This, value);} } //////////////////////////////////////////////////////////// /// /// Pitch of the music. Default value is 1 /// //////////////////////////////////////////////////////////// public float Pitch { get {return sfMusic_GetPitch(This);} set {sfMusic_SetPitch(This, value);} } //////////////////////////////////////////////////////////// /// /// Volume of the music, in range [0, 100]. Default value is 100 /// //////////////////////////////////////////////////////////// public float Volume { get {return sfMusic_GetVolume(This);} set {sfMusic_SetVolume(This, value);} } //////////////////////////////////////////////////////////// /// /// 3D position of the music. Default value is (0, 0, 0) /// //////////////////////////////////////////////////////////// public Vector3 Position { get {Vector3 v; sfMusic_GetPosition(This, out v.X, out v.Y, out v.Z); return v;} set {sfMusic_SetPosition(This, value.X, value.Y, value.Z);} } //////////////////////////////////////////////////////////// /// /// Minimum distance of the music. Closer than this distance, /// the listener will hear the sound at its maximum volume. /// The default value is 1 /// //////////////////////////////////////////////////////////// public float MinDistance { get {return sfMusic_GetMinDistance(This);} set {sfMusic_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 sfMusic_GetAttenuation(This);} set {sfMusic_SetAttenuation(This, value);} } //////////////////////////////////////////////////////////// /// /// Current playing position, in seconds /// //////////////////////////////////////////////////////////// public float PlayingOffset { get {return sfMusic_GetPlayingOffset(This);} } //////////////////////////////////////////////////////////// /// /// Handle the destruction of the object /// /// Is the GC disposing the object, or is it an explicit call ? //////////////////////////////////////////////////////////// protected override void Destroy(bool disposing) { sfMusic_Destroy(This); } #region Imports [DllImport("csfml-audio")] static extern IntPtr sfMusic_CreateFromFile(string Filename); [DllImport("csfml-audio")] unsafe static extern IntPtr sfMusic_CreateFromMemory(char* Data, uint SizeInBytes); [DllImport("csfml-audio")] static extern void sfMusic_Destroy(IntPtr MusicStream); [DllImport("csfml-audio")] static extern void sfMusic_Play(IntPtr Music); [DllImport("csfml-audio")] static extern void sfMusic_Pause(IntPtr Music); [DllImport("csfml-audio")] static extern void sfMusic_Stop(IntPtr Music); [DllImport("csfml-audio")] static extern SoundStatus sfMusic_GetStatus(IntPtr Music); [DllImport("csfml-audio")] static extern float sfMusic_GetDuration(IntPtr Music); [DllImport("csfml-audio")] static extern uint sfMusic_GetChannelsCount(IntPtr Music); [DllImport("csfml-audio")] static extern uint sfMusic_GetSampleRate(IntPtr Music); [DllImport("csfml-audio")] static extern void sfMusic_SetPitch(IntPtr Music, float Pitch); [DllImport("csfml-audio")] static extern void sfMusic_SetLoop(IntPtr Music, bool Loop); [DllImport("csfml-audio")] static extern void sfMusic_SetVolume(IntPtr Music, float Volume); [DllImport("csfml-audio")] static extern void sfMusic_SetPosition(IntPtr Music, float X, float Y, float Z); [DllImport("csfml-audio")] static extern void sfMusic_SetMinDistance(IntPtr Music, float MinDistance); [DllImport("csfml-audio")] static extern void sfMusic_SetAttenuation(IntPtr Music, float Attenuation); [DllImport("csfml-audio")] static extern bool sfMusic_GetLoop(IntPtr Music); [DllImport("csfml-audio")] static extern float sfMusic_GetPitch(IntPtr Music); [DllImport("csfml-audio")] static extern float sfMusic_GetVolume(IntPtr Music); [DllImport("csfml-audio")] static extern void sfMusic_GetPosition(IntPtr Music, out float X, out float Y, out float Z); [DllImport("csfml-audio")] static extern float sfMusic_GetMinDistance(IntPtr Music); [DllImport("csfml-audio")] static extern float sfMusic_GetAttenuation(IntPtr Music); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern float sfMusic_GetPlayingOffset(IntPtr Music); #endregion } } }