From b6bee776352d7f0757ed45d876e617b0d65ae50a Mon Sep 17 00:00:00 2001 From: Chikashi Miyama Date: Wed, 30 Oct 2019 16:02:20 +0100 Subject: [PATCH] add looped trigger --- UnityProject/Assembly-CSharp.csproj | 4 +- .../Assets/Scenes/Examples/MIDI/MIDI.unity | 132 +++++++++++++++++- .../Scenes/Examples/MIDI/script/CubeSync.cs | 9 +- .../Examples/MIDI/script}/MidiSyncCounter.cs | 9 +- .../MIDI/script}/MidiSyncCounter.cs.meta | 0 .../Examples/MIDI/script/RandomPosition.cs | 14 ++ .../MIDI/script/RandomPosition.cs.meta | 11 ++ .../Assets/Scripts/PdConnection/MidiParser.cs | 16 ++- .../Scripts/PdConnection/MidiSyncedLoop.cs | 21 +++ .../PdConnection/MidiSyncedLoop.cs.meta | 11 ++ .../Assets/Scripts/PdConnection/PdBackend.cs | 5 +- 11 files changed, 210 insertions(+), 22 deletions(-) rename UnityProject/Assets/{Scripts/PdConnection => Scenes/Examples/MIDI/script}/MidiSyncCounter.cs (73%) rename UnityProject/Assets/{Scripts/PdConnection => Scenes/Examples/MIDI/script}/MidiSyncCounter.cs.meta (100%) create mode 100644 UnityProject/Assets/Scenes/Examples/MIDI/script/RandomPosition.cs create mode 100644 UnityProject/Assets/Scenes/Examples/MIDI/script/RandomPosition.cs.meta create mode 100644 UnityProject/Assets/Scripts/PdConnection/MidiSyncedLoop.cs create mode 100644 UnityProject/Assets/Scripts/PdConnection/MidiSyncedLoop.cs.meta diff --git a/UnityProject/Assembly-CSharp.csproj b/UnityProject/Assembly-CSharp.csproj index f2e29c0..0a8b84d 100644 --- a/UnityProject/Assembly-CSharp.csproj +++ b/UnityProject/Assembly-CSharp.csproj @@ -61,7 +61,9 @@ + + @@ -75,7 +77,7 @@ - + diff --git a/UnityProject/Assets/Scenes/Examples/MIDI/MIDI.unity b/UnityProject/Assets/Scenes/Examples/MIDI/MIDI.unity index 014f6b3..2bab826 100644 --- a/UnityProject/Assets/Scenes/Examples/MIDI/MIDI.unity +++ b/UnityProject/Assets/Scenes/Examples/MIDI/MIDI.unity @@ -479,6 +479,7 @@ GameObject: - component: {fileID: 1261823481} - component: {fileID: 1261823480} - component: {fileID: 1261823482} + - component: {fileID: 1261823485} - component: {fileID: 1261823483} - component: {fileID: 1261823484} m_Layer: 0 @@ -540,7 +541,18 @@ MonoBehaviour: m_BoolArgument: 0 m_CallState: 2 - m_Target: {fileID: 669316603} - m_MethodName: onSyncReceived + m_MethodName: OnSyncReceived + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 1261823485} + m_MethodName: OnSyncReceived m_Mode: 0 m_Arguments: m_ObjectArgument: {fileID: 0} @@ -606,6 +618,33 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: sequence: 00000000000000000000000000000000 +--- !u!114 &1261823485 +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: f5f9defef7af2514590dadc0a6cddbac, type: 3} + m_Name: + m_EditorClassIdentifier: + loopLengthInBeat: 4 + loopStarted: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 2001732971} + m_MethodName: OnLoopStarted + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 --- !u!1 &1461024797 GameObject: m_ObjectHideFlags: 0 @@ -866,6 +905,97 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1790437803} m_CullTransparentMesh: 0 +--- !u!1 &2001732970 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 2001732974} + - component: {fileID: 2001732973} + - component: {fileID: 2001732972} + - component: {fileID: 2001732971} + m_Layer: 0 + m_Name: Capsule + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &2001732971 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2001732970} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3b84bb1a9cc55a846bfd9ea1161e8098, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!23 &2001732972 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2001732970} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 73c176f402d2c2f4d929aa5da7585d17, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 +--- !u!33 &2001732973 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2001732970} + m_Mesh: {fileID: 10208, guid: 0000000000000000e000000000000000, type: 0} +--- !u!4 &2001732974 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2001732970} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 7 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &2049244779 GameObject: m_ObjectHideFlags: 0 diff --git a/UnityProject/Assets/Scenes/Examples/MIDI/script/CubeSync.cs b/UnityProject/Assets/Scenes/Examples/MIDI/script/CubeSync.cs index ea9bfb9..2a8dcb2 100644 --- a/UnityProject/Assets/Scenes/Examples/MIDI/script/CubeSync.cs +++ b/UnityProject/Assets/Scenes/Examples/MIDI/script/CubeSync.cs @@ -3,23 +3,20 @@ using UnityEngine; public class CubeSync : MonoBehaviour { - private int counter_ = 0; private float currentX_; private float targetX_; private float lastCallBack_; private float callbackInterval_ = 0.05f; - public void onSyncReceived(MidiSync midiSync) + public void OnSyncReceived(MidiSync midiSync, int count) { if (midiSync == MidiSync.Clock) { var now = Time.realtimeSinceStartup; callbackInterval_ = now - lastCallBack_; lastCallBack_ = now; - currentX_ = (counter_ - 12) * 0.2f; - counter_++; - targetX_ = (counter_ - 12) * 0.2f; - counter_ %= 24; + currentX_ = (count % 24 - 12) * 0.2f; + targetX_ = (count % 24 - 12) * 0.2f; } } diff --git a/UnityProject/Assets/Scripts/PdConnection/MidiSyncCounter.cs b/UnityProject/Assets/Scenes/Examples/MIDI/script/MidiSyncCounter.cs similarity index 73% rename from UnityProject/Assets/Scripts/PdConnection/MidiSyncCounter.cs rename to UnityProject/Assets/Scenes/Examples/MIDI/script/MidiSyncCounter.cs index 2a7b365..0af5812 100644 --- a/UnityProject/Assets/Scripts/PdConnection/MidiSyncCounter.cs +++ b/UnityProject/Assets/Scenes/Examples/MIDI/script/MidiSyncCounter.cs @@ -7,15 +7,13 @@ public class MidiSyncCounter : MonoBehaviour [SerializeField] private Text counter; - private int count_ = 0; - public void OnSyncReceived(MidiSync midiSync) + public void OnSyncReceived(MidiSync midiSync, int count) { if (midiSync == MidiSync.Clock) { - count_++; - var tick = count_ % 24; - var beat = count_ / 24; + var tick = count % 24; + var beat = count / 24; var measure = beat / 4; counter.text = (measure + 1) + ":" + (beat % 4 + 1) + ":" + tick; @@ -23,7 +21,6 @@ public class MidiSyncCounter : MonoBehaviour else if (midiSync == MidiSync.Start) { Debug.Log("playback started"); - count_ = 0; } else if (midiSync == MidiSync.Stop) { diff --git a/UnityProject/Assets/Scripts/PdConnection/MidiSyncCounter.cs.meta b/UnityProject/Assets/Scenes/Examples/MIDI/script/MidiSyncCounter.cs.meta similarity index 100% rename from UnityProject/Assets/Scripts/PdConnection/MidiSyncCounter.cs.meta rename to UnityProject/Assets/Scenes/Examples/MIDI/script/MidiSyncCounter.cs.meta diff --git a/UnityProject/Assets/Scenes/Examples/MIDI/script/RandomPosition.cs b/UnityProject/Assets/Scenes/Examples/MIDI/script/RandomPosition.cs new file mode 100644 index 0000000..f19f1a1 --- /dev/null +++ b/UnityProject/Assets/Scenes/Examples/MIDI/script/RandomPosition.cs @@ -0,0 +1,14 @@ +using UnityEngine; + +namespace cylvester +{ + public class RandomPosition : MonoBehaviour + { + public void OnLoopStarted() + { + var x = Random.Range(-3f, 3f); + var y = Random.Range(-3f, 3f); + transform.localPosition = new Vector3(x, y, 0f); + } + } +} diff --git a/UnityProject/Assets/Scenes/Examples/MIDI/script/RandomPosition.cs.meta b/UnityProject/Assets/Scenes/Examples/MIDI/script/RandomPosition.cs.meta new file mode 100644 index 0000000..69cccf7 --- /dev/null +++ b/UnityProject/Assets/Scenes/Examples/MIDI/script/RandomPosition.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3b84bb1a9cc55a846bfd9ea1161e8098 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityProject/Assets/Scripts/PdConnection/MidiParser.cs b/UnityProject/Assets/Scripts/PdConnection/MidiParser.cs index f229bd2..f5b7b88 100644 --- a/UnityProject/Assets/Scripts/PdConnection/MidiParser.cs +++ b/UnityProject/Assets/Scripts/PdConnection/MidiParser.cs @@ -8,7 +8,7 @@ namespace cylvester {} [Serializable] - public class UnitySyncEvent : UnityEvent + public class UnitySyncEvent : UnityEvent {} public enum MidiSync @@ -35,7 +35,7 @@ namespace cylvester public interface IMidiParser : IDisposable { event Action MidiMessageReceived; - event Action MidiSyncReceived; + event Action MidiSyncReceived; } public class MidiParser : IMidiParser @@ -51,7 +51,8 @@ namespace cylvester private Accept accept_ = Accept.StatusByte; private readonly IPdReceiver pdReceiver_; private readonly Action onDataReceived_; - + private int count_; + public MidiParser(IPdReceiver pdReceiver) { pdReceiver_ = pdReceiver; @@ -99,11 +100,16 @@ namespace cylvester element != (byte)MidiSync.Stop) return false; - MidiSyncReceived?.Invoke((MidiSync)element); + if (element == (byte) MidiSync.Clock) + count_++; + else + count_ = 0; + + MidiSyncReceived?.Invoke((MidiSync)element, count_); return true; } public event Action MidiMessageReceived; - public event Action MidiSyncReceived; + public event Action MidiSyncReceived; } } \ No newline at end of file diff --git a/UnityProject/Assets/Scripts/PdConnection/MidiSyncedLoop.cs b/UnityProject/Assets/Scripts/PdConnection/MidiSyncedLoop.cs new file mode 100644 index 0000000..71205ca --- /dev/null +++ b/UnityProject/Assets/Scripts/PdConnection/MidiSyncedLoop.cs @@ -0,0 +1,21 @@ +using UnityEngine; +using UnityEngine.Events; + +namespace cylvester +{ + public class MidiSyncedLoop : MonoBehaviour + { + [SerializeField, Range(1, 100)] private int loopLengthInBeat = 1; + [SerializeField] private UnityEvent loopStarted; + + public void OnSyncReceived(MidiSync midiSync, int counter) + { + var loopLengthTicks = loopLengthInBeat * 24; + if(counter % loopLengthTicks == 0) + loopStarted.Invoke(); + } + } + +} + + diff --git a/UnityProject/Assets/Scripts/PdConnection/MidiSyncedLoop.cs.meta b/UnityProject/Assets/Scripts/PdConnection/MidiSyncedLoop.cs.meta new file mode 100644 index 0000000..660c244 --- /dev/null +++ b/UnityProject/Assets/Scripts/PdConnection/MidiSyncedLoop.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f5f9defef7af2514590dadc0a6cddbac +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs b/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs index 7572b22..713ccee 100644 --- a/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs +++ b/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using UnityEngine; -using UnityEngine.Events; namespace cylvester { @@ -39,7 +38,7 @@ namespace cylvester private Action onSamplePlaybackChanged_; private Action onMidiMessageReceived_; - private Action onMidiSyncReceived_; + private Action onMidiSyncReceived_; private void Awake() { @@ -72,7 +71,7 @@ namespace cylvester }; onMidiMessageReceived_ = (message) => { midiMessageReceived.Invoke(message); }; - onMidiSyncReceived_ = (sync) => { midiSyncReceived.Invoke(sync); }; + onMidiSyncReceived_ = (sync, count) => { midiSyncReceived.Invoke(sync, count); }; samplePlaybackObserver_.ValueChanged += onSamplePlaybackChanged_; midiParser_.MidiMessageReceived += onMidiMessageReceived_;