Added the trunk/branches/tags directories at repository root, and moved previous root into trunk/
git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1002 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
commit
2f524481c1
974 changed files with 295448 additions and 0 deletions
180
dotnet/src/Audio/SoundRecorder.cs
Normal file
180
dotnet/src/Audio/SoundRecorder.cs
Normal file
|
@ -0,0 +1,180 @@
|
|||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
|
||||
namespace SFML
|
||||
{
|
||||
namespace Audio
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// SoundRecorder is an interface for capturing sound data,
|
||||
/// it is meant to be used as a base class
|
||||
/// </summary>
|
||||
////////////////////////////////////////////////////////////
|
||||
public abstract class SoundRecorder : ObjectBase
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// Default constructor
|
||||
/// </summary>
|
||||
////////////////////////////////////////////////////////////
|
||||
public SoundRecorder() :
|
||||
base(IntPtr.Zero)
|
||||
{
|
||||
myStartCallback = new StartCallback(OnStart);
|
||||
myProcessCallback = new ProcessCallback(ProcessSamples);
|
||||
myStopCallback = new StopCallback(OnStop);
|
||||
|
||||
SetThis(sfSoundRecorder_Create(myStartCallback, myProcessCallback, myStopCallback, IntPtr.Zero));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// Start the capture using default sample rate (44100 Hz)
|
||||
/// Warning : only one capture can happen at the same time
|
||||
/// </summary>
|
||||
////////////////////////////////////////////////////////////
|
||||
public void Start()
|
||||
{
|
||||
Start(44100);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// Start the capture.
|
||||
/// Warning : only one capture can happen at the same time
|
||||
/// </summary>
|
||||
/// <param name="sampleRate"> Sound frequency; the more samples, the higher the quality (44100 by default = CD quality)</param>
|
||||
////////////////////////////////////////////////////////////
|
||||
public void Start(uint sampleRate)
|
||||
{
|
||||
sfSoundRecorder_Start(This, sampleRate);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// Stop the capture
|
||||
/// </summary>
|
||||
////////////////////////////////////////////////////////////
|
||||
public void Stop()
|
||||
{
|
||||
sfSoundRecorder_Stop(This);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// Sample rate of the recorder, in samples per second
|
||||
/// </summary>
|
||||
////////////////////////////////////////////////////////////
|
||||
public uint SampleRate
|
||||
{
|
||||
get {return sfSoundRecorder_GetSampleRate(This);}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// Tell if the system supports sound capture.
|
||||
/// If not, this class won't be usable
|
||||
/// </summary>
|
||||
////////////////////////////////////////////////////////////
|
||||
public static bool CanCapture
|
||||
{
|
||||
get {return sfSoundRecorder_CanCapture();}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// Called when a new capture starts
|
||||
/// </summary>
|
||||
/// <returns>False to abort recording audio data, true to continue</returns>
|
||||
////////////////////////////////////////////////////////////
|
||||
protected virtual bool OnStart()
|
||||
{
|
||||
// Does nothing by default
|
||||
return true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// Process a new chunk of recorded samples
|
||||
/// </summary>
|
||||
/// <param name="samples">Array of samples to process</param>
|
||||
/// <returns>False to stop recording audio data, true to continue</returns>
|
||||
////////////////////////////////////////////////////////////
|
||||
protected abstract bool OnProcessSamples(short[] samples);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// Called when the current capture stops
|
||||
/// </summary>
|
||||
////////////////////////////////////////////////////////////
|
||||
protected virtual void OnStop()
|
||||
{
|
||||
// Does nothing by default
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <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)
|
||||
{
|
||||
sfSoundRecorder_Destroy(This);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// <summary>
|
||||
/// Function called directly by the C library ; convert
|
||||
/// arguments and forward them to the internal virtual function
|
||||
/// </summary>
|
||||
/// <param name="samples">Pointer to the array of samples</param>
|
||||
/// <param name="nbSamples">Number of samples in the array</param>
|
||||
/// <param name="userData">User data -- unused</param>
|
||||
/// <returns>False to stop recording audio data, true to continue</returns>
|
||||
////////////////////////////////////////////////////////////
|
||||
private bool ProcessSamples(IntPtr samples, uint nbSamples, IntPtr userData)
|
||||
{
|
||||
short[] samplesArray = new short[nbSamples];
|
||||
Marshal.Copy(samples, samplesArray, 0, samplesArray.Length);
|
||||
|
||||
return OnProcessSamples(samplesArray);
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
private delegate bool StartCallback();
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
private delegate bool ProcessCallback(IntPtr samples, uint nbSamples, IntPtr userData);
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
private delegate void StopCallback();
|
||||
|
||||
private StartCallback myStartCallback;
|
||||
private ProcessCallback myProcessCallback;
|
||||
private StopCallback myStopCallback;
|
||||
|
||||
#region Imports
|
||||
[DllImport("csfml-audio")]
|
||||
static extern IntPtr sfSoundRecorder_Create(StartCallback OnStart, ProcessCallback OnProcess, StopCallback OnStop, IntPtr UserData);
|
||||
|
||||
[DllImport("csfml-audio")]
|
||||
static extern void sfSoundRecorder_Destroy(IntPtr SoundRecorder);
|
||||
|
||||
[DllImport("csfml-audio")]
|
||||
static extern void sfSoundRecorder_Start(IntPtr SoundRecorder, uint SampleRate);
|
||||
|
||||
[DllImport("csfml-audio")]
|
||||
static extern void sfSoundRecorder_Stop(IntPtr SoundRecorder);
|
||||
|
||||
[DllImport("csfml-audio")]
|
||||
static extern uint sfSoundRecorder_GetSampleRate(IntPtr SoundRecorder);
|
||||
|
||||
[DllImport("csfml-audio")]
|
||||
static extern bool sfSoundRecorder_CanCapture();
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue