soundvision/UnityProject/Assets/Scripts/PdConnection/PdArray.cs

40 lines
1,008 B
C#
Raw Normal View History

2019-07-06 14:22:52 +00:00
using System;
using System.IO.MemoryMappedFiles;
2019-09-28 18:17:39 +00:00
namespace cylvester
2019-07-06 14:22:52 +00:00
{
public interface IPdArray
{
float[] Data { get; }
}
public class PdArray : IDisposable, IPdArray, IUpdater
2019-07-06 14:22:52 +00:00
{
2019-07-06 14:26:14 +00:00
public float[] Data { get; }
2019-07-06 14:22:52 +00:00
private readonly MemoryMappedFile memoryMappedFile_;
private readonly MemoryMappedViewAccessor viewAccessor_;
2019-07-06 14:26:14 +00:00
public PdArray(string name, int size)
2019-07-06 14:22:52 +00:00
{
2019-07-06 14:26:14 +00:00
memoryMappedFile_ = MemoryMappedFile.OpenExisting(name);
if (memoryMappedFile_ == null)
throw new ArgumentException("no such shared memory");
2019-07-06 14:22:52 +00:00
viewAccessor_ = memoryMappedFile_.CreateViewAccessor();
2019-07-06 14:26:14 +00:00
Data = new float[size];
}
public void Update()
{
viewAccessor_.ReadArray(0, Data, 0, Data.Length);
2019-07-06 14:22:52 +00:00
}
public void Dispose()
{
memoryMappedFile_.Dispose();
viewAccessor_.Dispose();
2019-07-06 14:22:52 +00:00
}
}
}