2019-09-28 18:17:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
2019-10-05 12:18:52 +00:00
|
|
|
|
using UnityEngine.Events;
|
2019-09-28 18:17:39 +00:00
|
|
|
|
|
|
|
|
|
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-05 12:18:52 +00:00
|
|
|
|
[SerializeField] UnityControlEvent onControlMessageReceived = null;
|
|
|
|
|
|
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_;
|
2019-10-05 12:18:52 +00:00
|
|
|
|
|
|
|
|
|
private IPdSender pdSender_;
|
|
|
|
|
private IPdReceiver pdReceiver_;
|
|
|
|
|
private IMidiParser midiParser_;
|
2019-10-05 04:15:15 +00:00
|
|
|
|
private IDspController dspController_;
|
2019-10-05 09:38:46 +00:00
|
|
|
|
public ISpectrumArrayContainer SpectrumArrayContainer { get; private set; }
|
|
|
|
|
|
2019-10-05 12:18:52 +00:00
|
|
|
|
private Action onSamplePlaybackChanged_;
|
|
|
|
|
private Action<ControlMessage> onControlMessageReceived_;
|
|
|
|
|
|
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-05 12:18:52 +00:00
|
|
|
|
|
|
|
|
|
pdSender_ = new PdSender(PdConstant.ip, PdConstant.sendPort);
|
|
|
|
|
pdReceiver_ = new PdReceiver(PdConstant.receivedPort);
|
|
|
|
|
midiParser_ = new MidiParser(pdReceiver_);
|
|
|
|
|
|
|
|
|
|
dspController_ = new DspController(pdSender_);
|
2019-10-05 04:15:15 +00:00
|
|
|
|
|
2019-10-01 13:08:35 +00:00
|
|
|
|
samplePlaybackObserver_ = new ChangeObserver<int>(samplePlayback);
|
|
|
|
|
|
|
|
|
|
onSamplePlaybackChanged_ = () =>
|
|
|
|
|
{
|
2019-10-05 12:18:52 +00:00
|
|
|
|
pdSender_.Send(new[]{(byte)PdMessage.SampleSound, (byte)samplePlayback});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onControlMessageReceived_ = (message) =>
|
|
|
|
|
{
|
|
|
|
|
onControlMessageReceived.Invoke(message);
|
2019-10-01 13:08:35 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
samplePlaybackObserver_.ValueChanged += onSamplePlaybackChanged_;
|
2019-10-05 12:18:52 +00:00
|
|
|
|
midiParser_.ControlMessageReceived += onControlMessageReceived_;
|
2019-09-28 18:17:39 +00:00
|
|
|
|
|
2019-10-05 12:18:52 +00:00
|
|
|
|
dspController_.State = true;
|
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-05 12:18:52 +00:00
|
|
|
|
pdSender_?.Dispose();
|
2019-10-01 13:08:35 +00:00
|
|
|
|
samplePlaybackObserver_.ValueChanged -= onSamplePlaybackChanged_;
|
2019-10-05 12:18:52 +00:00
|
|
|
|
midiParser_.ControlMessageReceived -= onControlMessageReceived_;
|
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 12:18:52 +00:00
|
|
|
|
pdReceiver_.Update();
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|