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

55 lines
1.7 KiB
C#
Raw Normal View History

2019-09-28 18:17:39 +00:00
using System;
using UnityEngine;
namespace cylvester
{
2019-10-01 13:08:35 +00:00
public class PdBackend : MonoBehaviour
2019-09-28 18:17:39 +00:00
{
2019-09-29 16:04:26 +00:00
public string mainPatch = "analyzer.pd";
public int inchannels = 2;
2019-10-01 13:08:35 +00:00
public int samplePlayback = 0;
public PdArray levelMeterArray;
public FftArrayContainer fftArrayContainer;
private IChangeObserver<int> samplePlaybackObserver_;
private Action onSamplePlaybackChanged_;
private IPdSocket pdSocket_;
2019-09-29 16:04:26 +00:00
2019-09-29 19:53:47 +00:00
private void Start()
2019-09-28 18:17:39 +00:00
{
PdProcess.Instance.Start(mainPatch, inchannels);
2019-10-01 13:08:35 +00:00
levelMeterArray = new PdArray("levelmeters", PdConstant.NumMaxInputChannels);
fftArrayContainer = new FftArrayContainer();
pdSocket_ = new PdSocket(PdConstant.ip, PdConstant.port);
samplePlaybackObserver_ = new ChangeObserver<int>(samplePlayback);
onSamplePlaybackChanged_ = () =>
{
var bytes = new byte[]{(byte)PdMessage.SampleSound, (byte)samplePlayback};
pdSocket_.Send(bytes);
};
samplePlaybackObserver_.ValueChanged += onSamplePlaybackChanged_;
2019-09-28 18:17:39 +00:00
}
2019-09-29 19:53:47 +00:00
private void OnDestroy()
2019-09-28 18:17:39 +00:00
{
PdProcess.Instance.Stop();
2019-10-01 13:08:35 +00:00
levelMeterArray?.Dispose();
pdSocket_?.Dispose();
samplePlaybackObserver_.ValueChanged -= onSamplePlaybackChanged_;
2019-09-28 18:17:39 +00:00
}
2019-09-29 19:53:47 +00:00
public void Update()
2019-09-28 18:17:39 +00:00
{
2019-09-29 17:47:17 +00:00
if(PdProcess.Instance.Running)
2019-10-01 13:08:35 +00:00
levelMeterArray.Update();
2019-09-29 19:53:47 +00:00
2019-10-01 13:08:35 +00:00
fftArrayContainer.Update();
samplePlaybackObserver_.Value = samplePlayback;
2019-09-28 18:17:39 +00:00
}
}
}