accept MMC start/stop

This commit is contained in:
Chikashi Miyama 2019-10-30 15:01:06 +01:00
parent c87301bf67
commit 90afb6b074
14 changed files with 175 additions and 51 deletions

View file

@ -62,6 +62,7 @@
<Compile Include="Assets\Editor\PdBackendEditor.cs" />
<Compile Include="Assets\Editor\PdSpectrumBindEditor.cs" />
<Compile Include="Assets\Editor\RectangularSelection.cs" />
<Compile Include="Assets\Editor\SequencerEditor.cs" />
<Compile Include="Assets\Editor\SpectrumGeneratorEditMode.cs" />
<Compile Include="Assets\Editor\StateManagerEditor.cs" />
<Compile Include="Assets\Editor\UnitTest\UnitTest_ChangeObserver.cs" />

View file

@ -73,9 +73,9 @@
<Compile Include="Assets\Scripts\Math\Threshold.cs" />
<Compile Include="Assets\Scripts\PdConnection\DspController.cs" />
<Compile Include="Assets\Scripts\PdConnection\ISpectrumGenerator.cs" />
<Compile Include="Assets\Scripts\PdConnection\MidiClockCounter.cs" />
<Compile Include="Assets\Scripts\PdConnection\MidiLogger.cs" />
<Compile Include="Assets\Scripts\PdConnection\MidiParser.cs" />
<Compile Include="Assets\Scripts\PdConnection\MidiSyncCounter.cs" />
<Compile Include="Assets\Scripts\PdConnection\PdArray.cs" />
<Compile Include="Assets\Scripts\PdConnection\PdArrayContainer.cs" />
<Compile Include="Assets\Scripts\PdConnection\PdArraySelector.cs" />
@ -89,6 +89,7 @@
<Compile Include="Assets\Scripts\PdConnection\PdSender.cs" />
<Compile Include="Assets\Scripts\PdConnection\PdSpectrumBind.cs" />
<Compile Include="Assets\Scripts\PdConnection\PdTimbreIdBind.cs" />
<Compile Include="Assets\Scripts\PdConnection\Sequencer.cs" />
<Compile Include="Assets\Scripts\PdConnection\Spectrogram.cs" />
<Compile Include="Assets\Scripts\PdConnection\SpectrumGeneratorPlayMode.cs" />
<Compile Include="Assets\Scripts\PdConnection\Waveform.cs" />

View file

@ -8,7 +8,7 @@ namespace cylvester
{
private PdBackend pdBackend_;
private SerializedProperty midiMessageReceivedProperty_;
private SerializedProperty midiClockReceivedProperty_;
private SerializedProperty midiSyncReceivedProperty_;
private readonly string[] samples_ =
{
@ -38,8 +38,8 @@ namespace cylvester
midiMessageReceivedProperty_ = serializedObject.FindProperty("midiMessageReceived");
EditorGUILayout.PropertyField(midiMessageReceivedProperty_);
midiClockReceivedProperty_ = serializedObject.FindProperty("midiClockReceived");
EditorGUILayout.PropertyField(midiClockReceivedProperty_);
midiSyncReceivedProperty_ = serializedObject.FindProperty("midiSyncReceived");
EditorGUILayout.PropertyField(midiSyncReceivedProperty_);
if (Application.isPlaying)
{

View file

@ -0,0 +1,34 @@
using UnityEditor;
using UnityEngine;
namespace cylvester
{
[CustomEditor(typeof(Sequencer))]
public class SequencerEditor : Editor
{
private SerializedProperty sequenceProperty_;
private void OnEnable()
{
sequenceProperty_ = serializedObject.FindProperty("sequence");
}
public override void OnInspectorGUI()
{
serializedObject.Update ();
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Trigger on:", GUILayout.Width(200));
EditorGUILayout.BeginHorizontal(GUILayout.Width(160));
for (var i = 0; i < 16; ++i)
{
sequenceProperty_.GetArrayElementAtIndex(i).boolValue
= EditorGUILayout.Toggle(sequenceProperty_.GetArrayElementAtIndex(i).boolValue);
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndHorizontal();
serializedObject.ApplyModifiedProperties ();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 271cb432726f4f145aef895a05d2a97a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -480,6 +480,7 @@ GameObject:
- component: {fileID: 1261823480}
- component: {fileID: 1261823482}
- component: {fileID: 1261823483}
- component: {fileID: 1261823484}
m_Layer: 0
m_Name: PdBackend
m_TagString: Untagged
@ -524,12 +525,12 @@ MonoBehaviour:
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
midiClockReceived:
midiSyncReceived:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1261823483}
m_MethodName: onClockReceived
m_Mode: 1
m_MethodName: OnSyncReceived
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
@ -539,8 +540,8 @@ MonoBehaviour:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 669316603}
m_MethodName: onClockReceived
m_Mode: 1
m_MethodName: onSyncReceived
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
@ -592,6 +593,19 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
counter: {fileID: 1790437805}
--- !u!114 &1261823484
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1261823479}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6ebe1124d37e04742be155475d179a67, type: 3}
m_Name:
m_EditorClassIdentifier:
sequence: 00000000000000000000000000000000
--- !u!1 &1461024797
GameObject:
m_ObjectHideFlags: 0

View file

@ -1,4 +1,5 @@
using UnityEngine;
using cylvester;
using UnityEngine;
public class CubeSync : MonoBehaviour
{
@ -8,7 +9,9 @@ public class CubeSync : MonoBehaviour
private float lastCallBack_;
private float callbackInterval_ = 0.05f;
public void onClockReceived()
public void onSyncReceived(MidiSync midiSync)
{
if (midiSync == MidiSync.Clock)
{
var now = Time.realtimeSinceStartup;
callbackInterval_ = now - lastCallBack_;
@ -18,6 +21,7 @@ public class CubeSync : MonoBehaviour
targetX_ = (counter_ - 12) * 0.2f;
counter_ %= 24;
}
}
public void Update()
{

View file

@ -1,21 +0,0 @@

using UnityEngine;
using UnityEngine.UI;
public class MidiClockCounter : MonoBehaviour
{
[SerializeField] private Text counter;
private int count_ = 0;
public void onClockReceived()
{
count_++;
var tick = count_ % 24;
var beat = count_ / 24;
var measure = beat / 4;
counter.text = (measure+1) + ":" + (beat%4+1) + ":" + tick;
}
}

View file

@ -7,6 +7,17 @@ namespace cylvester
public class UnityMidiEvent : UnityEvent<MidiMessage>
{}
[Serializable]
public class UnitySyncEvent : UnityEvent<MidiSync>
{}
public enum MidiSync
{
Start = 250,
Stop = 252,
Clock = 248,
}
public struct MidiMessage
{
public byte Status;
@ -24,8 +35,7 @@ namespace cylvester
public interface IMidiParser : IDisposable
{
event Action<MidiMessage> MidiMessageReceived;
event Action MidiClockReceived;
event Action<MidiSync> MidiSyncReceived;
}
public class MidiParser : IMidiParser
@ -41,7 +51,6 @@ namespace cylvester
private Accept accept_ = Accept.StatusByte;
private readonly IPdReceiver pdReceiver_;
private readonly Action<byte[]> onDataReceived_;
private static byte MIDI_CLOCK = 248;
public MidiParser(IPdReceiver pdReceiver)
{
@ -51,8 +60,8 @@ namespace cylvester
{
foreach (var element in bytes)
{
if (element == MIDI_CLOCK)
MidiClockReceived?.Invoke();
if (ProcessSyncByte(element))
continue;
if (element >= 128)
{
@ -83,7 +92,18 @@ namespace cylvester
pdReceiver_.DataReceived -= onDataReceived_;
}
private bool ProcessSyncByte(byte element)
{
if (element != (byte)MidiSync.Clock &&
element != (byte)MidiSync.Start &&
element != (byte)MidiSync.Stop)
return false;
MidiSyncReceived?.Invoke((MidiSync)element);
return true;
}
public event Action<MidiMessage> MidiMessageReceived;
public event Action MidiClockReceived;
public event Action<MidiSync> MidiSyncReceived;
}
}

View file

@ -0,0 +1,33 @@
using cylvester;
using UnityEngine;
using UnityEngine.UI;
public class MidiSyncCounter : MonoBehaviour
{
[SerializeField] private Text counter;
private int count_ = 0;
public void OnSyncReceived(MidiSync midiSync)
{
if (midiSync == MidiSync.Clock)
{
count_++;
var tick = count_ % 24;
var beat = count_ / 24;
var measure = beat / 4;
counter.text = (measure + 1) + ":" + (beat % 4 + 1) + ":" + tick;
}
else if (midiSync == MidiSync.Start)
{
Debug.Log("playback started");
count_ = 0;
}
else if (midiSync == MidiSync.Stop)
{
Debug.Log("playback ended");
}
}
}

View file

@ -18,7 +18,7 @@ namespace cylvester
public class PdBackend : MonoBehaviour, IPdBackend
{
[SerializeField] UnityMidiEvent midiMessageReceived = null;
[SerializeField] UnityEvent midiClockReceived = null;
[SerializeField] UnitySyncEvent midiSyncReceived = null;
public int samplePlayback;
@ -39,7 +39,7 @@ namespace cylvester
private Action onSamplePlaybackChanged_;
private Action<MidiMessage> onMidiMessageReceived_;
private Action onMidiClockReceived_;
private Action<MidiSync> onMidiSyncReceived_;
private void Awake()
{
@ -72,11 +72,11 @@ namespace cylvester
};
onMidiMessageReceived_ = (message) => { midiMessageReceived.Invoke(message); };
onMidiClockReceived_ = () => { midiClockReceived.Invoke(); };
onMidiSyncReceived_ = (sync) => { midiSyncReceived.Invoke(sync); };
samplePlaybackObserver_.ValueChanged += onSamplePlaybackChanged_;
midiParser_.MidiMessageReceived += onMidiMessageReceived_;
midiParser_.MidiClockReceived += onMidiClockReceived_;
midiParser_.MidiSyncReceived += onMidiSyncReceived_;
dspController_.State = true;
}
@ -86,7 +86,7 @@ namespace cylvester
pdSender_?.Dispose();
samplePlaybackObserver_.ValueChanged -= onSamplePlaybackChanged_;
midiParser_.MidiMessageReceived -= onMidiMessageReceived_;
midiParser_.MidiClockReceived -= onMidiClockReceived_;
midiParser_.MidiSyncReceived -= onMidiSyncReceived_;
}

View file

@ -0,0 +1,16 @@
using System;
using UnityEngine;
namespace cylvester
{
public class Sequencer : MonoBehaviour
{
[SerializeField] private bool[] sequence = new bool[16];
private void OnValidate()
{
if(sequence.Length != 16)
Array.Resize(ref sequence, 16);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6ebe1124d37e04742be155475d179a67
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: