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