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

108 lines
4 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;
2019-09-28 18:17:39 +00:00
using UnityEngine;
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-29 17:52:09 +00:00
IPdArray NoiseArray { get; }
2019-10-29 20:47:06 +00:00
IPdArray PitchArray { get; }
2019-10-05 16:09:24 +00:00
IPdArrayContainer SpectrumArrayContainer{ get; }
IPdArrayContainer WaveformArrayContainer{ get; }
2019-10-29 13:32:55 +00:00
void SendState(IStateManager manager);
}
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;
2019-10-30 14:01:06 +00:00
[SerializeField] UnitySyncEvent midiSyncReceived = null;
2019-10-27 10:21:33 +00:00
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-29 17:52:09 +00:00
public IPdArray NoiseArray { get; private set; }
2019-10-29 20:47:06 +00:00
public IPdArray PitchArray { 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-30 15:02:20 +00:00
private Action<MidiSync, int> onMidiSyncReceived_;
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-29 17:52:09 +00:00
LevelArray = new PdArray("level", PdConstant.NumMaxInputChannels);
NoiseArray = new PdArray("noise", PdConstant.NumMaxInputChannels);
2019-10-29 20:47:06 +00:00
PitchArray = new PdArray("pitch", PdConstant.NumMaxInputChannels);
2019-10-05 16:09:24 +00:00
SpectrumArrayContainer = new PdArrayContainer("fft_");
WaveformArrayContainer = new PdArrayContainer("wave_");
2019-10-29 17:52:09 +00:00
2019-10-07 16:53:36 +00:00
updaters_ = new List<IUpdater>
2019-10-29 17:52:09 +00:00
{
(IUpdater) LevelArray,
(IUpdater) NoiseArray,
2019-10-29 20:47:06 +00:00
(IUpdater) PitchArray,
2019-10-29 17:52:09 +00:00
(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); };
2019-10-30 15:02:20 +00:00
onMidiSyncReceived_ = (sync, count) => { midiSyncReceived.Invoke(sync, count); };
2019-10-01 13:08:35 +00:00
samplePlaybackObserver_.ValueChanged += onSamplePlaybackChanged_;
2019-10-26 20:15:30 +00:00
midiParser_.MidiMessageReceived += onMidiMessageReceived_;
2019-10-30 14:01:06 +00:00
midiParser_.MidiSyncReceived += onMidiSyncReceived_;
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-30 14:01:06 +00:00
midiParser_.MidiSyncReceived -= onMidiSyncReceived_;
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
}
2019-10-29 13:32:55 +00:00
public void SendState(IStateManager stateManager)
{
2019-11-01 11:06:54 +00:00
pdSender_.Send("state previous " + (stateManager.PreviousState.HasValue ? stateManager.PreviousState.Value.Title : "---"));
pdSender_.Send("state current " + stateManager.CurrentState.Title);
pdSender_.Send("state next " + (stateManager.NextState.HasValue ? stateManager.NextState.Value.Title : "---"));
}
2019-09-28 18:17:39 +00:00
}
}