add looped trigger

This commit is contained in:
Chikashi Miyama 2019-10-30 16:02:20 +01:00
parent 90afb6b074
commit b6bee77635
11 changed files with 210 additions and 22 deletions

View file

@ -61,7 +61,9 @@
<Compile Include="Assets\Scenes\Examples\LevelThreshold\script\ConstructionColumnBehaviour.cs" /> <Compile Include="Assets\Scenes\Examples\LevelThreshold\script\ConstructionColumnBehaviour.cs" />
<Compile Include="Assets\Scenes\Examples\LevelThreshold\script\Spawner.cs" /> <Compile Include="Assets\Scenes\Examples\LevelThreshold\script\Spawner.cs" />
<Compile Include="Assets\Scenes\Examples\MIDI\script\CubeSync.cs" /> <Compile Include="Assets\Scenes\Examples\MIDI\script\CubeSync.cs" />
<Compile Include="Assets\Scenes\Examples\MIDI\script\MidiSyncCounter.cs" />
<Compile Include="Assets\Scenes\Examples\MIDI\script\PositionBind.cs" /> <Compile Include="Assets\Scenes\Examples\MIDI\script\PositionBind.cs" />
<Compile Include="Assets\Scenes\Examples\MIDI\script\RandomPosition.cs" />
<Compile Include="Assets\Scenes\Examples\PdBackend\script\BoomBall.cs" /> <Compile Include="Assets\Scenes\Examples\PdBackend\script\BoomBall.cs" />
<Compile Include="Assets\Scenes\Examples\PdBackend\script\MoveBall.cs" /> <Compile Include="Assets\Scenes\Examples\PdBackend\script\MoveBall.cs" />
<Compile Include="Assets\Scenes\Examples\PostProcessing\script\BokehBind.cs" /> <Compile Include="Assets\Scenes\Examples\PostProcessing\script\BokehBind.cs" />
@ -75,7 +77,7 @@
<Compile Include="Assets\Scripts\PdConnection\ISpectrumGenerator.cs" /> <Compile Include="Assets\Scripts\PdConnection\ISpectrumGenerator.cs" />
<Compile Include="Assets\Scripts\PdConnection\MidiLogger.cs" /> <Compile Include="Assets\Scripts\PdConnection\MidiLogger.cs" />
<Compile Include="Assets\Scripts\PdConnection\MidiParser.cs" /> <Compile Include="Assets\Scripts\PdConnection\MidiParser.cs" />
<Compile Include="Assets\Scripts\PdConnection\MidiSyncCounter.cs" /> <Compile Include="Assets\Scripts\PdConnection\MidiSyncedLoop.cs" />
<Compile Include="Assets\Scripts\PdConnection\PdArray.cs" /> <Compile Include="Assets\Scripts\PdConnection\PdArray.cs" />
<Compile Include="Assets\Scripts\PdConnection\PdArrayContainer.cs" /> <Compile Include="Assets\Scripts\PdConnection\PdArrayContainer.cs" />
<Compile Include="Assets\Scripts\PdConnection\PdArraySelector.cs" /> <Compile Include="Assets\Scripts\PdConnection\PdArraySelector.cs" />

View file

@ -479,6 +479,7 @@ GameObject:
- component: {fileID: 1261823481} - component: {fileID: 1261823481}
- component: {fileID: 1261823480} - component: {fileID: 1261823480}
- component: {fileID: 1261823482} - component: {fileID: 1261823482}
- component: {fileID: 1261823485}
- component: {fileID: 1261823483} - component: {fileID: 1261823483}
- component: {fileID: 1261823484} - component: {fileID: 1261823484}
m_Layer: 0 m_Layer: 0
@ -540,7 +541,18 @@ MonoBehaviour:
m_BoolArgument: 0 m_BoolArgument: 0
m_CallState: 2 m_CallState: 2
- m_Target: {fileID: 669316603} - 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_Mode: 0
m_Arguments: m_Arguments:
m_ObjectArgument: {fileID: 0} m_ObjectArgument: {fileID: 0}
@ -606,6 +618,33 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
sequence: 00000000000000000000000000000000 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 --- !u!1 &1461024797
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -866,6 +905,97 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1790437803} m_GameObject: {fileID: 1790437803}
m_CullTransparentMesh: 0 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 --- !u!1 &2049244779
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -3,23 +3,20 @@ using UnityEngine;
public class CubeSync : MonoBehaviour public class CubeSync : MonoBehaviour
{ {
private int counter_ = 0;
private float currentX_; private float currentX_;
private float targetX_; private float targetX_;
private float lastCallBack_; private float lastCallBack_;
private float callbackInterval_ = 0.05f; private float callbackInterval_ = 0.05f;
public void onSyncReceived(MidiSync midiSync) public void OnSyncReceived(MidiSync midiSync, int count)
{ {
if (midiSync == MidiSync.Clock) if (midiSync == MidiSync.Clock)
{ {
var now = Time.realtimeSinceStartup; var now = Time.realtimeSinceStartup;
callbackInterval_ = now - lastCallBack_; callbackInterval_ = now - lastCallBack_;
lastCallBack_ = now; lastCallBack_ = now;
currentX_ = (counter_ - 12) * 0.2f; currentX_ = (count % 24 - 12) * 0.2f;
counter_++; targetX_ = (count % 24 - 12) * 0.2f;
targetX_ = (counter_ - 12) * 0.2f;
counter_ %= 24;
} }
} }

View file

@ -7,15 +7,13 @@ public class MidiSyncCounter : MonoBehaviour
[SerializeField] private Text counter; [SerializeField] private Text counter;
private int count_ = 0;
public void OnSyncReceived(MidiSync midiSync) public void OnSyncReceived(MidiSync midiSync, int count)
{ {
if (midiSync == MidiSync.Clock) if (midiSync == MidiSync.Clock)
{ {
count_++; var tick = count % 24;
var tick = count_ % 24; var beat = count / 24;
var beat = count_ / 24;
var measure = beat / 4; var measure = beat / 4;
counter.text = (measure + 1) + ":" + (beat % 4 + 1) + ":" + tick; counter.text = (measure + 1) + ":" + (beat % 4 + 1) + ":" + tick;
@ -23,7 +21,6 @@ public class MidiSyncCounter : MonoBehaviour
else if (midiSync == MidiSync.Start) else if (midiSync == MidiSync.Start)
{ {
Debug.Log("playback started"); Debug.Log("playback started");
count_ = 0;
} }
else if (midiSync == MidiSync.Stop) else if (midiSync == MidiSync.Stop)
{ {

View file

@ -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);
}
}
}

View file

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

View file

@ -8,7 +8,7 @@ namespace cylvester
{} {}
[Serializable] [Serializable]
public class UnitySyncEvent : UnityEvent<MidiSync> public class UnitySyncEvent : UnityEvent<MidiSync, int>
{} {}
public enum MidiSync public enum MidiSync
@ -35,7 +35,7 @@ namespace cylvester
public interface IMidiParser : IDisposable public interface IMidiParser : IDisposable
{ {
event Action<MidiMessage> MidiMessageReceived; event Action<MidiMessage> MidiMessageReceived;
event Action<MidiSync> MidiSyncReceived; event Action<MidiSync, int> MidiSyncReceived;
} }
public class MidiParser : IMidiParser public class MidiParser : IMidiParser
@ -51,7 +51,8 @@ namespace cylvester
private Accept accept_ = Accept.StatusByte; private Accept accept_ = Accept.StatusByte;
private readonly IPdReceiver pdReceiver_; private readonly IPdReceiver pdReceiver_;
private readonly Action<byte[]> onDataReceived_; private readonly Action<byte[]> onDataReceived_;
private int count_;
public MidiParser(IPdReceiver pdReceiver) public MidiParser(IPdReceiver pdReceiver)
{ {
pdReceiver_ = pdReceiver; pdReceiver_ = pdReceiver;
@ -99,11 +100,16 @@ namespace cylvester
element != (byte)MidiSync.Stop) element != (byte)MidiSync.Stop)
return false; return false;
MidiSyncReceived?.Invoke((MidiSync)element); if (element == (byte) MidiSync.Clock)
count_++;
else
count_ = 0;
MidiSyncReceived?.Invoke((MidiSync)element, count_);
return true; return true;
} }
public event Action<MidiMessage> MidiMessageReceived; public event Action<MidiMessage> MidiMessageReceived;
public event Action<MidiSync> MidiSyncReceived; public event Action<MidiSync, int> MidiSyncReceived;
} }
} }

View file

@ -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();
}
}
}

View file

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

View file

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
namespace cylvester namespace cylvester
{ {
@ -39,7 +38,7 @@ namespace cylvester
private Action onSamplePlaybackChanged_; private Action onSamplePlaybackChanged_;
private Action<MidiMessage> onMidiMessageReceived_; private Action<MidiMessage> onMidiMessageReceived_;
private Action<MidiSync> onMidiSyncReceived_; private Action<MidiSync, int> onMidiSyncReceived_;
private void Awake() private void Awake()
{ {
@ -72,7 +71,7 @@ namespace cylvester
}; };
onMidiMessageReceived_ = (message) => { midiMessageReceived.Invoke(message); }; onMidiMessageReceived_ = (message) => { midiMessageReceived.Invoke(message); };
onMidiSyncReceived_ = (sync) => { midiSyncReceived.Invoke(sync); }; onMidiSyncReceived_ = (sync, count) => { midiSyncReceived.Invoke(sync, count); };
samplePlaybackObserver_.ValueChanged += onSamplePlaybackChanged_; samplePlaybackObserver_.ValueChanged += onSamplePlaybackChanged_;
midiParser_.MidiMessageReceived += onMidiMessageReceived_; midiParser_.MidiMessageReceived += onMidiMessageReceived_;