From 325295a089ffadcef4cc1243c9619a1e66d68c48 Mon Sep 17 00:00:00 2001 From: Chikashi Miyama Date: Sun, 29 Sep 2019 21:53:47 +0200 Subject: [PATCH] put everything in playmode --- .../.idea.UnityProject/.idea/contentModel.xml | 1 + .../.idea.UnityProject/.idea/workspace.xml | 502 +++++++++--------- UnityProject/Assembly-CSharp.csproj | 1 + UnityProject/Assets/Editor/LevelMeter.cs | 14 +- UnityProject/Assets/Editor/PdBackendEditor.cs | 30 +- .../Assets/Editor/PdSpectrumBindEditor.cs | 70 ++- .../Assets/Editor/RectangularSelection.cs | 1 - .../Scripts/PdConnection/FFTArrayContainer.cs | 30 ++ .../PdConnection/FFTArrayContainer.cs.meta | 3 + .../Assets/Scripts/PdConnection/PdBackend.cs | 35 +- .../Scripts/PdConnection/PdSpectrumBind.cs | 22 +- .../StreamingAssets/pd/patch/analyzer.pd | 11 +- .../UnityProject.sln.DotSettings.user | 3 +- 13 files changed, 371 insertions(+), 352 deletions(-) create mode 100644 UnityProject/Assets/Scripts/PdConnection/FFTArrayContainer.cs create mode 100644 UnityProject/Assets/Scripts/PdConnection/FFTArrayContainer.cs.meta diff --git a/UnityProject/.idea/.idea.UnityProject/.idea/contentModel.xml b/UnityProject/.idea/.idea.UnityProject/.idea/contentModel.xml index 2193db9..4f0f69e 100644 --- a/UnityProject/.idea/.idea.UnityProject/.idea/contentModel.xml +++ b/UnityProject/.idea/.idea.UnityProject/.idea/contentModel.xml @@ -36,6 +36,7 @@ + diff --git a/UnityProject/.idea/.idea.UnityProject/.idea/workspace.xml b/UnityProject/.idea/.idea.UnityProject/.idea/workspace.xml index 2358a1c..aa55cc0 100644 --- a/UnityProject/.idea/.idea.UnityProject/.idea/workspace.xml +++ b/UnityProject/.idea/.idea.UnityProject/.idea/workspace.xml @@ -1,12 +1,20 @@ - + + + + + + + - + + + - @@ -509,7 +513,7 @@ - + @@ -530,7 +534,7 @@ - + @@ -566,7 +570,8 @@ - @@ -585,49 +590,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1012,10 +974,21 @@ + + + + + + + + + + + - - + + @@ -1030,54 +1003,10 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -1090,28 +1019,10 @@ - - - - - - - - - - - - - - - - - - - - + + @@ -1128,48 +1039,129 @@ - + - - + + - - - - + + + + + + - + - - + + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/UnityProject/Assembly-CSharp.csproj b/UnityProject/Assembly-CSharp.csproj index 65ef83e..254c655 100644 --- a/UnityProject/Assembly-CSharp.csproj +++ b/UnityProject/Assembly-CSharp.csproj @@ -60,6 +60,7 @@ + diff --git a/UnityProject/Assets/Editor/LevelMeter.cs b/UnityProject/Assets/Editor/LevelMeter.cs index 76c723a..b2e41c1 100644 --- a/UnityProject/Assets/Editor/LevelMeter.cs +++ b/UnityProject/Assets/Editor/LevelMeter.cs @@ -5,7 +5,7 @@ namespace cylvester { interface ILevelMeter { - void Render(); + void Render(IPdArray pdArray); } public class LevelMeter : ILevelMeter @@ -16,19 +16,17 @@ namespace cylvester private readonly Texture2D meterImageTexture_; private readonly int index_; private readonly string label_; - private readonly IPdArray pdArray_; - public LevelMeter(int index, IPdArray pdArray) + public LevelMeter(int index) { index_ = index; label_ = (index_ + 1).ToString(); - pdArray_ = pdArray; meterImageTexture_ = new Texture2D(TextureWidth, TextureHeight); } - public void Render() + public void Render(IPdArray pdArray) { - UpdateTexture(); + UpdateTexture(pdArray); var rect = EditorGUILayout.BeginVertical(); var style = new GUIStyle(GUI.skin.label) { @@ -42,9 +40,9 @@ namespace cylvester EditorGUILayout.EndVertical(); } - private void UpdateTexture() + private void UpdateTexture(IPdArray pdArray) { - var level = pdArray_.Data[index_]; + var level = pdArray.Data[index_]; for (var i = 0; i < 100; ++i) { meterImageTexture_.SetPixel(0, i, i < level ? Color.green : Color.black); diff --git a/UnityProject/Assets/Editor/PdBackendEditor.cs b/UnityProject/Assets/Editor/PdBackendEditor.cs index 88ac102..87fc4fe 100644 --- a/UnityProject/Assets/Editor/PdBackendEditor.cs +++ b/UnityProject/Assets/Editor/PdBackendEditor.cs @@ -1,5 +1,4 @@ -using System; -using UnityEditor; +using UnityEditor; using UnityEngine; namespace cylvester @@ -7,7 +6,6 @@ namespace cylvester [CustomEditor(typeof(PdBackend))] public class PdBackendEditor : Editor { - private IPdBackend pdBackend_; private ILevelMeter[] levelMeters_; private readonly string[] channels = { @@ -18,9 +16,8 @@ namespace cylvester { pdBackend_ = (IPdBackend) target; levelMeters_ = new ILevelMeter[16]; - for (var i = 0; i < 16; ++i) - levelMeters_[i] = new LevelMeter(i, pdBackend_.LevelMeterArray); + levelMeters_[i] = new LevelMeter(i); } public override void OnInspectorGUI () @@ -34,23 +31,18 @@ namespace cylvester GUILayout.EndHorizontal(); pdBackend_.NumInputChannels = EditorGUILayout.Popup("Number of input channels", pdBackend_.NumInputChannels, channels); - pdBackend_.State = GUILayout.Toggle(pdBackend_.State, "DSP Processing"); - if (!pdBackend_.State) - return; - - pdBackend_.UpdateShmem(); + if (Application.isPlaying) + { - GUILayout.Space(5); - GUILayout.BeginHorizontal(); - foreach (var levelMeter in levelMeters_) - levelMeter.Render(); - GUILayout.EndHorizontal(); - - Repaint(); + GUILayout.Space(5); + GUILayout.BeginHorizontal(); + foreach (var levelMeter in levelMeters_) + levelMeter.Render(pdBackend_.LevelMeterArray); + GUILayout.EndHorizontal(); + Repaint(); + } } - } - } diff --git a/UnityProject/Assets/Editor/PdSpectrumBindEditor.cs b/UnityProject/Assets/Editor/PdSpectrumBindEditor.cs index aeeced0..fae4ccd 100644 --- a/UnityProject/Assets/Editor/PdSpectrumBindEditor.cs +++ b/UnityProject/Assets/Editor/PdSpectrumBindEditor.cs @@ -11,20 +11,64 @@ namespace cylvester private readonly string[] channels = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16" }; + private ISpectrumGenerator spectrumGenerator_; private IRectangularSelection rectangularSelection_; private Rect paintSpace_; + private SerializedProperty pdBackendProperty_; + + public void OnEnable() { spectrumGenerator_ = new SpectrumGenerator(TextureWidth, TextureHeight); rectangularSelection_ = new RectangularSelection(TextureWidth, TextureHeight); + + pdBackendProperty_ = serializedObject.FindProperty("pdBackend"); + } public override void OnInspectorGUI() { - var behaviour = (PdSpectrumBind)target; + + var behaviour = (IPdSpectrumBind)target; + EditorGUILayout.PropertyField(pdBackendProperty_); + serializedObject.ApplyModifiedProperties(); + + GUILayout.Label("PureData Inputs", EditorStyles.boldLabel); + behaviour.Channel = EditorGUILayout.Popup("Input Channel", behaviour.Channel, channels); + + if (Application.isPlaying && pdBackendProperty_ != null) + RenderSpectrumExtractor(behaviour); + } + + private void RenderSpectrumExtractor(IPdSpectrumBind behaviour) + { + UpdateSelection(behaviour); + GUILayout.Space(5); + GUILayout.Label("Spectrum Extractor", EditorStyles.boldLabel); + + var paintSpace = GUILayoutUtility.GetRect(TextureHeight, TextureWidth, TextureHeight, TextureHeight); + if (Event.current.type == EventType.Repaint) + { + paintSpace_ = paintSpace; + var spectrumArray = behaviour.GetPdArray(behaviour.Channel); + + behaviour.Energy = spectrumGenerator_.Update(spectrumArray.Data, ref behaviour.Selection); + GUI.DrawTexture(paintSpace_, spectrumGenerator_.Spectrum); + } + + GUILayout.BeginHorizontal(); + GUILayout.Label("Extracted Energy", EditorStyles.boldLabel); + GUILayout.Label(behaviour.Energy.ToString()); + GUILayout.EndHorizontal(); + + Repaint(); + } + + private void UpdateSelection(IPdSpectrumBind behaviour) + { if (Event.current.isMouse && Event.current.button == 0) { switch (Event.current.type) @@ -37,33 +81,11 @@ namespace cylvester case EventType.MouseDrag: { rectangularSelection_.Update(Event.current.mousePosition, - ref paintSpace_, ref behaviour.rectangularSelection); + ref paintSpace_, ref behaviour.Selection); break; } } } - - GUILayout.Label("PureData Inputs", EditorStyles.boldLabel); - - behaviour.channel = EditorGUILayout.Popup("Input Channel", behaviour.channel, channels); - GUILayout.Space(5); - GUILayout.Label("Spectrum Extractor", EditorStyles.boldLabel); - - var paintSpace = GUILayoutUtility.GetRect(TextureHeight, TextureWidth, TextureHeight, TextureHeight); - if (Event.current.type == EventType.Repaint) - { - paintSpace_ = paintSpace; - behaviour.PdArray.Update(); - behaviour.Energy = spectrumGenerator_.Update(behaviour.PdArray.Data, ref behaviour.rectangularSelection); - GUI.DrawTexture(paintSpace_, spectrumGenerator_.Spectrum); - } - - GUILayout.BeginHorizontal(); - GUILayout.Label("Extracted Energy", EditorStyles.boldLabel); - GUILayout.Label(behaviour.Energy.ToString()); - GUILayout.EndHorizontal(); - - Repaint(); } } } diff --git a/UnityProject/Assets/Editor/RectangularSelection.cs b/UnityProject/Assets/Editor/RectangularSelection.cs index de74ee9..35b0bd4 100644 --- a/UnityProject/Assets/Editor/RectangularSelection.cs +++ b/UnityProject/Assets/Editor/RectangularSelection.cs @@ -15,7 +15,6 @@ namespace cylvester private Rect selectedArea_; private readonly int textureWidth_; private readonly int textureHeight_; - public RectangularSelection(int textureWidth, int textureHeight) { diff --git a/UnityProject/Assets/Scripts/PdConnection/FFTArrayContainer.cs b/UnityProject/Assets/Scripts/PdConnection/FFTArrayContainer.cs new file mode 100644 index 0000000..3fe4394 --- /dev/null +++ b/UnityProject/Assets/Scripts/PdConnection/FFTArrayContainer.cs @@ -0,0 +1,30 @@ +namespace cylvester +{ + public interface IFftArrayContainer + { + IPdArray this[int index] { get; } + void Update(); + } + + public class FftArrayContainer : IFftArrayContainer + { + private readonly IPdArray[] arrays_; + + public FftArrayContainer() + { + arrays_ = new IPdArray[16]; + for(var i = 0; i < 16; ++i) + arrays_[i] = new PdArray("fft_" + i, 512); + } + + public void Update() + { + foreach (var array in arrays_) + { + array.Update(); + } + } + + public IPdArray this[int index] => arrays_[index]; + } +} \ No newline at end of file diff --git a/UnityProject/Assets/Scripts/PdConnection/FFTArrayContainer.cs.meta b/UnityProject/Assets/Scripts/PdConnection/FFTArrayContainer.cs.meta new file mode 100644 index 0000000..4d85230 --- /dev/null +++ b/UnityProject/Assets/Scripts/PdConnection/FFTArrayContainer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2e03f6cd69114865975e2fc6fb9ff8c1 +timeCreated: 1569781762 \ No newline at end of file diff --git a/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs b/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs index b281ccc..0b5a466 100644 --- a/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs +++ b/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs @@ -1,5 +1,4 @@ using System; -using System.Threading; using UnityEngine; namespace cylvester @@ -9,12 +8,10 @@ namespace cylvester string MainPatch { get; set; } int NumInputChannels { get; set;} - bool State { get; set; } - void UpdateShmem(); IPdArray LevelMeterArray { get; } + IFftArrayContainer FFTArrayContainer { get; } } - [ExecuteInEditMode] public class PdBackend : MonoBehaviour, IPdBackend { public string mainPatch = "analyzer.pd"; @@ -22,50 +19,36 @@ namespace cylvester private Action onToggled_; private PdArray levelMeterArray_; - private UdpSender udpSender_; - private bool state_; + private FftArrayContainer fftArrayContainer_; private const int NumMaxInputChannels = 16; public IPdArray LevelMeterArray => levelMeterArray_; + public IFftArrayContainer FFTArrayContainer => fftArrayContainer_; public string MainPatch { get => mainPatch; set => mainPatch = value; } public int NumInputChannels { get => inchannels -1; set => inchannels = value + 1; } - public bool State - { - get => state_; - set - { - if (state_ == value) - return; - - var bytes = new byte[1]; - bytes[0] = state_ ? (byte)0 : (byte)1; - udpSender_.SendBytes(bytes); - state_ = value; - } - } - private void OnEnable() + private void Start() { PdProcess.Instance.Start(mainPatch, inchannels); - levelMeterArray_ = new PdArray("levelmeters", NumMaxInputChannels); - udpSender_ = new UdpSender("127.0.0.1", 54637); + fftArrayContainer_ = new FftArrayContainer(); } - private void OnDisable() + private void OnDestroy() { PdProcess.Instance.Stop(); levelMeterArray_?.Dispose(); - udpSender_?.Dispose(); } - public void UpdateShmem() + public void Update() { if(PdProcess.Instance.Running) levelMeterArray_.Update(); + + fftArrayContainer_.Update(); } } } \ No newline at end of file diff --git a/UnityProject/Assets/Scripts/PdConnection/PdSpectrumBind.cs b/UnityProject/Assets/Scripts/PdConnection/PdSpectrumBind.cs index 9ca30b9..e01f4ae 100644 --- a/UnityProject/Assets/Scripts/PdConnection/PdSpectrumBind.cs +++ b/UnityProject/Assets/Scripts/PdConnection/PdSpectrumBind.cs @@ -4,24 +4,24 @@ namespace cylvester { public interface IPdSpectrumBind { - IPdArray PdArray { get; } + IPdArray GetPdArray(int index); + int Channel { get; set; } + ref Rect Selection { get; } int Energy { get; set; } } - [ExecuteInEditMode] public class PdSpectrumBind : MonoBehaviour, IPdSpectrumBind { - public int channel; - public Rect rectangularSelection; - private PdArray pdArray_; - - private void Awake() + [SerializeField] private PdBackend pdBackend; + private Rect selection_; + + public IPdArray GetPdArray(int index) { - pdArray_ = new PdArray("fft_" + channel, 512); + return pdBackend.FFTArrayContainer[index]; } - - public IPdArray PdArray => pdArray_; - + + public int Channel { get; set; } + public ref Rect Selection => ref selection_; public int Energy { get; set; } } } \ No newline at end of file diff --git a/UnityProject/Assets/StreamingAssets/pd/patch/analyzer.pd b/UnityProject/Assets/StreamingAssets/pd/patch/analyzer.pd index 0298cc0..9ea3a86 100644 --- a/UnityProject/Assets/StreamingAssets/pd/patch/analyzer.pd +++ b/UnityProject/Assets/StreamingAssets/pd/patch/analyzer.pd @@ -1,5 +1,5 @@ -#N canvas 1334 361 827 452 10; -#X obj 187 61 loadbang; +#N canvas 1494 726 827 452 10; +#X obj 351 23 loadbang; #X obj 80 273 adc~ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; #N canvas 0 50 450 250 (subpatch) 0; #X array loop 211681 float 2; @@ -11,7 +11,7 @@ #X obj 31 228 tabplay~ loop; #X obj 30 179 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 -1; -#N canvas 1194 597 730 390 analyzers_______________________________ +#N canvas 215 619 730 390 analyzers_______________________________ 0; #X obj 95 44 inlet~; #X obj 119 69 inlet~; @@ -211,12 +211,10 @@ #X coords 0 1 1023 0 300 100 1 0 0; #X restore 39 32 graph; #X restore 657 56 pd window; -#X msg 478 110 \; pd dsp \$1; -#X msg 274 17 test; -#X obj 479 83 netreceive -u -b 54637; #N canvas 927 320 450 300 udpmessage 0; #X text 60 33 0 ... dsp; #X restore 660 100 pd udpmessage; +#X msg 478 110 \; pd dsp 1; #X connect 0 0 4 0; #X connect 0 0 7 0; #X connect 0 0 16 0; @@ -245,4 +243,3 @@ #X connect 9 0 10 0; #X connect 10 0 11 0; #X connect 11 0 12 0; -#X connect 17 0 15 0; diff --git a/UnityProject/UnityProject.sln.DotSettings.user b/UnityProject/UnityProject.sln.DotSettings.user index 8b4b586..a375aad 100644 --- a/UnityProject/UnityProject.sln.DotSettings.user +++ b/UnityProject/UnityProject.sln.DotSettings.user @@ -1,2 +1,3 @@  - True \ No newline at end of file + True + True \ No newline at end of file