using LibPDBinding.Managed.Data;
using LibPDBinding.Managed.Utils;
using LibPDBinding.Native;
namespace LibPDBinding.Managed
{
///
/// Array in Pd.
///
public sealed class PdArray
{
readonly string _name;
readonly Pd _pd;
///
/// Gets the name of the array in Pd.
///
/// The name.
public string Name { get { return _name; } }
internal PdArray (Pd pd, string name)
{
_pd = pd;
_name = name;
}
///
/// Gets the size of the Pd array.
///
/// The size.
public int Size {
get {
_pd.Activate ();
return Audio.arraysize (_name);
}
}
///
/// Read values from the array.
///
/// Start position for reading.
/// Number of values to be read.
public float[] Read (int start, int length)
{
_pd.Activate ();
float[] arrayContent = new float [length];
int status = Audio.read_array (arrayContent, _name, start, length);
if (status != 0) {
throw new PdProcessException (status, "read_array");
}
return arrayContent;
}
///
/// Writes values to the array.
///
/// New content to be written.
/// Start position for writing.
/// Number of values to be written.
public void Write (float[] newContent, int start, int length)
{
_pd.Activate ();
int status = Audio.write_array (_name, start, newContent, length);
if (status != 0) {
throw new PdProcessException (status, "write_array");
}
}
///
/// Resizes the Pd array.
///
/// NB: This is an expensive method, use sparingly.
///
/// The new size of the array.
public void Resize (int length)
{
_pd.Activate ();
MessageInvocation.SendMessage (_name, "resize", new Float (length));
}
}
}