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

52 lines
1.6 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";
2019-10-02 16:17:08 +00:00
public int samplePlayback;
2019-10-01 13:08:35 +00:00
public PdArray levelMeterArray;
2019-10-04 13:35:23 +00:00
public ISpectrumArrayContainer spectrumArrayContainer;
2019-10-01 13:08:35 +00:00
private IChangeObserver<int> samplePlaybackObserver_;
private Action onSamplePlaybackChanged_;
private IPdSocket pdSocket_;
2019-10-02 16:17:08 +00:00
private void Awake()
2019-09-28 18:17:39 +00:00
{
2019-10-01 20:20:55 +00:00
PdProcess.Instance.Start(mainPatch);
2019-10-01 13:08:35 +00:00
levelMeterArray = new PdArray("levelmeters", PdConstant.NumMaxInputChannels);
2019-10-04 13:35:23 +00:00
spectrumArrayContainer = new SpectrumArrayContainer();
2019-10-01 13:08:35 +00:00
pdSocket_ = new PdSocket(PdConstant.ip, PdConstant.port);
samplePlaybackObserver_ = new ChangeObserver<int>(samplePlayback);
onSamplePlaybackChanged_ = () =>
{
2019-10-01 20:20:55 +00:00
var bytes = new[]{(byte)PdMessage.SampleSound, (byte)samplePlayback};
2019-10-01 13:08:35 +00:00
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-04 13:35:23 +00:00
spectrumArrayContainer.Update();
2019-10-01 13:08:35 +00:00
samplePlaybackObserver_.Value = samplePlayback;
2019-09-28 18:17:39 +00:00
}
}
}