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

71 lines
1.9 KiB
C#
Raw Normal View History

2019-09-28 18:17:39 +00:00
using System;
using System.Threading;
2019-09-28 18:17:39 +00:00
using UnityEngine;
namespace cylvester
{
public interface IPdBackend
{
2019-09-29 16:04:26 +00:00
string MainPatch { get; set; }
int NumInputChannels { get; set;}
bool State { get; set; }
void UpdateShmem();
IPdArray LevelMeterArray { get; }
2019-09-28 18:17:39 +00:00
}
[ExecuteInEditMode]
public class PdBackend : MonoBehaviour, IPdBackend
{
2019-09-29 16:04:26 +00:00
public string mainPatch = "analyzer.pd";
public int inchannels = 2;
2019-09-28 18:17:39 +00:00
private Action onToggled_;
2019-09-29 16:04:26 +00:00
private PdArray levelMeterArray_;
private UdpSender udpSender_;
private bool state_;
2019-09-28 18:17:39 +00:00
2019-09-29 16:04:26 +00:00
private const int NumMaxInputChannels = 16;
public IPdArray LevelMeterArray => levelMeterArray_;
public string MainPatch { get => mainPatch; set => mainPatch = value; }
public int NumInputChannels { get => inchannels -1; set => inchannels = value + 1; }
public bool State
{
get => state_;
set
{
if (state_ == value)
return;
var bytes = new byte[1];
bytes[0] = state_ ? (byte)0 : (byte)1;
udpSender_.SendBytes(bytes);
state_ = value;
}
}
2019-09-28 18:17:39 +00:00
private void OnEnable()
{
PdProcess.Instance.Start(mainPatch, inchannels);
2019-09-29 16:04:26 +00:00
levelMeterArray_ = new PdArray("levelmeters", NumMaxInputChannels);
udpSender_ = new UdpSender("127.0.0.1", 54637);
2019-09-28 18:17:39 +00:00
}
private void OnDisable()
{
PdProcess.Instance.Stop();
2019-09-29 16:04:26 +00:00
levelMeterArray_?.Dispose();
udpSender_?.Dispose();
2019-09-28 18:17:39 +00:00
}
2019-09-29 16:04:26 +00:00
public void UpdateShmem()
2019-09-28 18:17:39 +00:00
{
2019-09-29 17:47:17 +00:00
if(PdProcess.Instance.Running)
levelMeterArray_.Update();
2019-09-28 18:17:39 +00:00
}
}
}