2019-09-28 18:17:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace cylvester
|
|
|
|
|
{
|
2019-10-05 09:38:46 +00:00
|
|
|
|
|
|
|
|
|
public interface IPdBackend
|
|
|
|
|
{
|
|
|
|
|
ISpectrumArrayContainer SpectrumArrayContainer{ get; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PdBackend : MonoBehaviour, IPdBackend
|
2019-09-28 18:17:39 +00:00
|
|
|
|
{
|
2019-10-02 16:17:08 +00:00
|
|
|
|
public int samplePlayback;
|
2019-10-05 09:38:46 +00:00
|
|
|
|
private IUpdater spectrumArrayUpdater_;
|
2019-10-01 13:08:35 +00:00
|
|
|
|
|
|
|
|
|
private IChangeObserver<int> samplePlaybackObserver_;
|
|
|
|
|
private Action onSamplePlaybackChanged_;
|
|
|
|
|
private IPdSocket pdSocket_;
|
2019-10-05 04:15:15 +00:00
|
|
|
|
private IDspController dspController_;
|
2019-10-02 16:17:08 +00:00
|
|
|
|
|
2019-10-05 09:38:46 +00:00
|
|
|
|
public ISpectrumArrayContainer SpectrumArrayContainer { get; private set; }
|
|
|
|
|
|
2019-10-02 16:17:08 +00:00
|
|
|
|
private void Awake()
|
2019-09-28 18:17:39 +00:00
|
|
|
|
{
|
2019-10-05 09:38:46 +00:00
|
|
|
|
SpectrumArrayContainer = new SpectrumArrayContainer();
|
|
|
|
|
spectrumArrayUpdater_ = (IUpdater) SpectrumArrayContainer;
|
2019-10-01 13:08:35 +00:00
|
|
|
|
pdSocket_ = new PdSocket(PdConstant.ip, PdConstant.port);
|
2019-10-05 04:15:15 +00:00
|
|
|
|
dspController_ = new DspController(pdSocket_);
|
|
|
|
|
|
2019-10-01 13:08:35 +00:00
|
|
|
|
samplePlaybackObserver_ = new ChangeObserver<int>(samplePlayback);
|
|
|
|
|
|
|
|
|
|
onSamplePlaybackChanged_ = () =>
|
|
|
|
|
{
|
2019-10-05 04:15:15 +00:00
|
|
|
|
pdSocket_.Send(new[]{(byte)PdMessage.SampleSound, (byte)samplePlayback});
|
2019-10-01 13:08:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
samplePlaybackObserver_.ValueChanged += onSamplePlaybackChanged_;
|
2019-10-05 04:15:15 +00:00
|
|
|
|
dspController_.State = true;
|
2019-09-28 18:17:39 +00:00
|
|
|
|
|
2019-10-05 04:15:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 19:53:47 +00:00
|
|
|
|
private void OnDestroy()
|
2019-09-28 18:17:39 +00:00
|
|
|
|
{
|
2019-10-05 04:15:15 +00:00
|
|
|
|
dspController_.State = false;
|
2019-10-01 13:08:35 +00:00
|
|
|
|
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-10-05 09:38:46 +00:00
|
|
|
|
spectrumArrayUpdater_.Update();
|
2019-10-01 13:08:35 +00:00
|
|
|
|
samplePlaybackObserver_.Value = samplePlayback;
|
2019-09-28 18:17:39 +00:00
|
|
|
|
}
|
2019-10-05 09:38:46 +00:00
|
|
|
|
|
|
|
|
|
|
2019-09-28 18:17:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|