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

36 lines
923 B
C#
Raw Normal View History

2019-07-06 14:22:52 +00:00
using System;
using System.IO.MemoryMappedFiles;
namespace PdConnection
{
public class PdBuffer : IDisposable
{
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 PdBuffer(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()
{
viewAccessor_?.Dispose();
memoryMappedFile_?.Dispose();
}
}
}