using System; using System.Runtime.InteropServices; using System.Security; using System.IO; namespace SFML { namespace Audio { //////////////////////////////////////////////////////////// /// /// SoundBuffer is the low-level class for loading and manipulating /// sound buffers. A sound buffer holds audio data (samples) /// which can then be played by a Sound or saved to a file. /// //////////////////////////////////////////////////////////// public class SoundBuffer : ObjectBase { //////////////////////////////////////////////////////////// /// /// Construct the sound buffer from a file /// /// Path of the sound file to load /// //////////////////////////////////////////////////////////// public SoundBuffer(string filename) : base(sfSoundBuffer_CreateFromFile(filename)) { if (This == IntPtr.Zero) throw new LoadingFailedException("sound buffer", filename); } //////////////////////////////////////////////////////////// /// /// Construct the sound buffer from a file in a stream /// /// Stream containing the file contents //////////////////////////////////////////////////////////// public SoundBuffer(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(sfSoundBuffer_CreateFromMemory((char*)dataPtr, Read)); } } if (This == IntPtr.Zero) throw new LoadingFailedException("sound buffer"); } //////////////////////////////////////////////////////////// /// /// Construct the sound buffer from an array of samples /// /// Array of samples /// Channels count /// Sample rate /// //////////////////////////////////////////////////////////// public SoundBuffer(short[] samples, uint channelsCount, uint sampleRate) : base(IntPtr.Zero) { unsafe { fixed (short* SamplesPtr = samples) { SetThis(sfSoundBuffer_CreateFromSamples(SamplesPtr, (uint)samples.Length, channelsCount, sampleRate)); } } if (This == IntPtr.Zero) throw new LoadingFailedException("sound buffer"); } //////////////////////////////////////////////////////////// /// /// Construct the sound buffer from another sound buffer /// /// Sound buffer to copy //////////////////////////////////////////////////////////// public SoundBuffer(SoundBuffer copy) : base(sfSoundBuffer_Copy(copy.This)) { } //////////////////////////////////////////////////////////// /// /// Save the sound buffer to an audio file /// /// Path of the sound file to write /// True if saving has been successful //////////////////////////////////////////////////////////// public bool SaveToFile(string filename) { return sfSoundBuffer_SaveToFile(This, filename); } //////////////////////////////////////////////////////////// /// /// Samples rate, in samples per second /// //////////////////////////////////////////////////////////// public uint SampleRate { get {return sfSoundBuffer_GetSampleRate(This);} } //////////////////////////////////////////////////////////// /// /// Number of channels (1 = mono, 2 = stereo) /// //////////////////////////////////////////////////////////// public uint ChannelsCount { get {return sfSoundBuffer_GetChannelsCount(This);} } //////////////////////////////////////////////////////////// /// /// Total duration of the buffer, in seconds /// //////////////////////////////////////////////////////////// public float Duration { get {return sfSoundBuffer_GetDuration(This);} } //////////////////////////////////////////////////////////// /// /// Array of samples contained in the buffer /// //////////////////////////////////////////////////////////// public short[] Samples { get { short[] SamplesArray = new short[sfSoundBuffer_GetSamplesCount(This)]; Marshal.Copy(sfSoundBuffer_GetSamples(This), SamplesArray, 0, SamplesArray.Length); return SamplesArray; } } //////////////////////////////////////////////////////////// /// /// Provide a string describing the object /// /// String description of the object //////////////////////////////////////////////////////////// public override string ToString() { return "[SoundBuffer]" + " SampleRate = " + SampleRate + " ChannelsCount = " + ChannelsCount + " Duration = " + Duration; } //////////////////////////////////////////////////////////// /// /// Handle the destruction of the object /// /// Is the GC disposing the object, or is it an explicit call ? //////////////////////////////////////////////////////////// protected override void Destroy(bool disposing) { sfSoundBuffer_Destroy(This); } #region Imports [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfSoundBuffer_CreateFromFile(string Filename); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] unsafe static extern IntPtr sfSoundBuffer_CreateFromMemory(char* Data, uint SizeInBytes); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] unsafe static extern IntPtr sfSoundBuffer_CreateFromSamples(short* Samples, uint SamplesCount, uint ChannelsCount, uint SampleRate); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfSoundBuffer_Copy(IntPtr SoundBuffer); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern void sfSoundBuffer_Destroy(IntPtr SoundBuffer); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern bool sfSoundBuffer_SaveToFile(IntPtr SoundBuffer, string Filename); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfSoundBuffer_GetSamples(IntPtr SoundBuffer); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern uint sfSoundBuffer_GetSamplesCount(IntPtr SoundBuffer); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern uint sfSoundBuffer_GetSampleRate(IntPtr SoundBuffer); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern uint sfSoundBuffer_GetChannelsCount(IntPtr SoundBuffer); [DllImport("csfml-audio"), SuppressUnmanagedCodeSecurity] static extern float sfSoundBuffer_GetDuration(IntPtr SoundBuffer); #endregion } } }