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

99 lines
3.3 KiB
C#
Raw Normal View History

2019-09-28 18:17:39 +00:00
using System;
2019-10-07 16:53:36 +00:00
using System.Collections.Generic;
using System.Text;
using UnityEditor.VersionControl;
2019-09-28 18:17:39 +00:00
using UnityEngine;
2019-10-27 10:21:33 +00:00
using UnityEngine.Events;
2019-10-07 16:53:36 +00:00
2019-09-28 18:17:39 +00:00
namespace cylvester
{
public interface IPdBackend
{
2019-10-07 16:53:36 +00:00
IPdArray LevelArray { get; }
2019-10-05 16:09:24 +00:00
IPdArrayContainer SpectrumArrayContainer{ get; }
IPdArrayContainer WaveformArrayContainer{ get; }
void Message(string message);
}
public class PdBackend : MonoBehaviour, IPdBackend
2019-09-28 18:17:39 +00:00
{
2019-10-27 10:21:33 +00:00
[SerializeField] UnityMidiEvent midiMessageReceived = null;
[SerializeField] UnityEvent midiClockReceived = null;
2019-10-02 16:17:08 +00:00
public int samplePlayback;
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 16:09:24 +00:00
2019-10-07 16:53:36 +00:00
public IPdArray LevelArray { get; private set; }
2019-10-05 16:09:24 +00:00
public IPdArrayContainer SpectrumArrayContainer { get; private set; }
public IPdArrayContainer WaveformArrayContainer { get; private set; }
2019-10-07 16:53:36 +00:00
private List<IUpdater> updaters_;
2019-10-05 16:09:24 +00:00
2019-10-05 12:18:52 +00:00
private Action onSamplePlaybackChanged_;
2019-10-26 20:15:30 +00:00
private Action<MidiMessage> onMidiMessageReceived_;
2019-10-27 10:21:33 +00:00
private Action onMidiClockReceived_;
2019-10-05 12:18:52 +00:00
2019-10-02 16:17:08 +00:00
private void Awake()
2019-09-28 18:17:39 +00:00
{
2019-10-05 16:09:24 +00:00
SpectrumArrayContainer = new PdArrayContainer("fft_");
WaveformArrayContainer = new PdArrayContainer("wave_");
2019-10-07 16:53:36 +00:00
LevelArray = new PdArray("level", PdConstant.NumMaxInputChannels);
updaters_ = new List<IUpdater>
{(IUpdater) LevelArray, (IUpdater) SpectrumArrayContainer, (IUpdater) WaveformArrayContainer};
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_ = () =>
{
pdSender_.Send("sample " + samplePlayback);
};
2019-10-05 12:18:52 +00:00
2019-10-27 10:21:33 +00:00
onMidiMessageReceived_ = (message) => { midiMessageReceived.Invoke(message); };
onMidiClockReceived_ = () => { midiClockReceived.Invoke(); };
2019-10-01 13:08:35 +00:00
samplePlaybackObserver_.ValueChanged += onSamplePlaybackChanged_;
2019-10-26 20:15:30 +00:00
midiParser_.MidiMessageReceived += onMidiMessageReceived_;
2019-10-27 10:21:33 +00:00
midiParser_.MidiClockReceived += onMidiClockReceived_;
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-26 20:15:30 +00:00
midiParser_.MidiMessageReceived -= onMidiMessageReceived_;
2019-10-27 10:21:33 +00:00
midiParser_.MidiClockReceived -= onMidiClockReceived_;
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-07 16:53:36 +00:00
foreach (var updater in updaters_)
updater.Update();
2019-10-01 13:08:35 +00:00
samplePlaybackObserver_.Value = samplePlayback;
2019-09-28 18:17:39 +00:00
}
public void Message(string message)
{
pdSender_.Send("message " + message);
}
2019-09-28 18:17:39 +00:00
}
}