diff --git a/UnityProject/Assets/Editor/LevelMeter.cs b/UnityProject/Assets/Editor/LevelMeter.cs index 33f2523..1a48caa 100644 --- a/UnityProject/Assets/Editor/LevelMeter.cs +++ b/UnityProject/Assets/Editor/LevelMeter.cs @@ -5,14 +5,20 @@ namespace cylvester { public class LevelMeter { - private string label_; - private float dB_; - private Texture2D meterImageTexture_; + private const int TextureWidth = 1; + private const int TextureHeight = 100; + + private readonly Texture2D meterImageTexture_; + private readonly int index_; + private readonly string label_; + private readonly IPdArray pdArray_; - public LevelMeter(int index) + public LevelMeter(int index, IPdArray pdArray) { - label_ = (index + 1).ToString(); - meterImageTexture_ = new Texture2D(1, 32); + index_ = index; + label_ = (index_ + 1).ToString(); + pdArray_ = pdArray; + meterImageTexture_ = new Texture2D(TextureWidth, TextureHeight); } public void Render() @@ -33,9 +39,10 @@ namespace cylvester private void UpdateTexture() { - for (var i = 0; i < 32; ++i) + var level = pdArray_.Data[index_]; + for (var i = 0; i < 100; ++i) { - meterImageTexture_.SetPixel(0, i, i < 10 ? Color.green : Color.black); + meterImageTexture_.SetPixel(0, i, i < level ? Color.green : Color.black); } meterImageTexture_.Apply(); } diff --git a/UnityProject/Assets/Editor/PdConsole.cs b/UnityProject/Assets/Editor/PdConsole.cs index be314b2..c787f81 100644 --- a/UnityProject/Assets/Editor/PdConsole.cs +++ b/UnityProject/Assets/Editor/PdConsole.cs @@ -1,25 +1,27 @@ -using UnityEditor; -using UnityEngine; +using System; +using UnityEditor; namespace cylvester { public class PdConsole : EditorWindow { + private const int NumChannels = 16; + private IEditorToggle dspToggle_; private ITogglePresenter togglePresenter_; + private Action onDspToggleStateChanged_; private IPdBackend pdBackend_; private LevelMeter[] levelMeters_; + private PdArray levelMeterArray_; [MenuItem("SoundVision/Pd console %#p")] static void Init() { var window = (PdConsole)GetWindow(typeof(PdConsole)); window.Show(); - - } - private void OnEnable() + private void Awake() { var foundObjects = FindObjectsOfType(typeof(PdBackend)); if (foundObjects.Length != 1) @@ -28,40 +30,70 @@ namespace cylvester pdBackend_ = (IPdBackend) foundObjects[0]; dspToggle_ = new EditorToggle(); + onDspToggleStateChanged_ = () => + { + if (pdBackend_.State) + { + levelMeterArray_ = new PdArray("levelmeters", NumChannels); + for (var i = 0; i < NumChannels; ++i) + levelMeters_[i] = new LevelMeter(i, levelMeterArray_); + } + else + levelMeterArray_.Dispose(); + }; + + pdBackend_.StateChanged += onDspToggleStateChanged_; togglePresenter_ = new TogglePresenter(dspToggle_, pdBackend_); - levelMeters_ = new LevelMeter[16]; - for (var i = 0; i < 16; ++i) - levelMeters_[i] = new LevelMeter(i); + levelMeters_ = new LevelMeter[NumChannels]; + + + } + + private void OnDestroy() + { + levelMeterArray_?.Dispose(); + dspToggle_.ToggleStateChanged -= onDspToggleStateChanged_; } private void OnGUI () { - if(!ValidatePdBackend(pdBackend_)) + if(!ValidatePdBackend()) + return; + + EditorGUILayout.Space(); + dspToggle_.State = EditorGUILayout.Toggle("Pure Data Process", dspToggle_.State); + + if(!CheckProcessingState()) return; - EditorGUILayout.Space(); - dspToggle_.State = EditorGUILayout.Toggle("Pure Data Process", dspToggle_.State); - EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); + + levelMeterArray_.Update(); foreach (var levelMeter in levelMeters_) levelMeter.Render(); EditorGUILayout.EndHorizontal(); + + Repaint(); } + private bool ValidatePdBackend() + { + if (pdBackend_ != null) + return true; + EditorGUILayout.LabelField("No Pd backend found in the scene"); + return false; + } + + private bool CheckProcessingState() + { + if (pdBackend_.State) + return true; + EditorGUILayout.LabelField("Pd process is currently inactive"); + return false; + } - - - private bool ValidatePdBackend(IPdBackend pdBackend) - { - var exist = pdBackend_ != null; - if (!exist) - { - EditorGUILayout.LabelField("No Pd backend found in the scene"); - } - return exist; - } } } diff --git a/UnityProject/Assets/PdBackend.meta b/UnityProject/Assets/PdBackend.meta deleted file mode 100644 index 75b3aab..0000000 --- a/UnityProject/Assets/PdBackend.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 7978207823e9dad49b91f32a347617a0 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/UnityProject/Assets/Scenes/Examples/PdBackendDemo/PdBackendDemo.unity b/UnityProject/Assets/Scenes/Examples/PdBackendDemo/PdBackendDemo.unity index 4c3a3a7..cdcf8f9 100644 --- a/UnityProject/Assets/Scenes/Examples/PdBackendDemo/PdBackendDemo.unity +++ b/UnityProject/Assets/Scenes/Examples/PdBackendDemo/PdBackendDemo.unity @@ -218,14 +218,13 @@ MonoBehaviour: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 987772532} - m_Enabled: 1 + m_Enabled: 0 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: 3ba69cee3466a304d9d570268f717413, type: 3} m_Name: m_EditorClassIdentifier: - mainPatch: test.pd + mainPatch: analyzer.pd inchannels: 2 - pdProcess: 0 --- !u!4 &987772534 Transform: m_ObjectHideFlags: 0 @@ -309,107 +308,3 @@ Transform: m_Father: {fileID: 0} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} ---- !u!1 &1232139010 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 1232139015} - - component: {fileID: 1232139014} - - component: {fileID: 1232139013} - - component: {fileID: 1232139012} - - component: {fileID: 1232139011} - m_Layer: 0 - m_Name: Cube - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 1 ---- !u!114 &1232139011 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1232139010} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 1f64c3bbc6cc3ba419c8c56c02e08ec2, type: 3} - m_Name: - m_EditorClassIdentifier: ---- !u!65 &1232139012 -BoxCollider: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1232139010} - m_Material: {fileID: 0} - m_IsTrigger: 0 - m_Enabled: 1 - serializedVersion: 2 - m_Size: {x: 1, y: 1, z: 1} - m_Center: {x: 0, y: 0, z: 0} ---- !u!23 &1232139013 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1232139010} - 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: 10303, guid: 0000000000000000f000000000000000, type: 0} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 0 - 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 &1232139014 -MeshFilter: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1232139010} - m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} ---- !u!4 &1232139015 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1232139010} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 4.8635645, y: -4.5257893, z: 12.978625} - m_LocalScale: {x: 1, y: 1, z: 1} - m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 3 - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/UnityProject/Assets/Scripts/PdConnection/PdArray.cs b/UnityProject/Assets/Scripts/PdConnection/PdArray.cs index 5511a98..2f126dc 100644 --- a/UnityProject/Assets/Scripts/PdConnection/PdArray.cs +++ b/UnityProject/Assets/Scripts/PdConnection/PdArray.cs @@ -3,7 +3,13 @@ using System.IO.MemoryMappedFiles; namespace cylvester { - public class PdArray : IDisposable + public interface IPdArray + { + float[] Data { get; } + void Update(); + } + + public class PdArray : IDisposable, IPdArray { public float[] Data { get; } diff --git a/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs b/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs index 046a82a..9ec83f2 100644 --- a/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs +++ b/UnityProject/Assets/Scripts/PdConnection/PdBackend.cs @@ -1,11 +1,13 @@ using System; +using System.Threading; using UnityEngine; namespace cylvester { public interface IPdBackend { - bool State { set; } + bool State { set; get; } + event Action StateChanged; } [ExecuteInEditMode] @@ -19,6 +21,7 @@ namespace cylvester private void OnEnable() { PdProcess.Instance.Start(mainPatch, inchannels); + Thread.Sleep(500); } private void OnDisable() @@ -28,7 +31,15 @@ namespace cylvester public bool State { - set => enabled = value; + set + { + enabled = value; + if(StateChanged != null) + StateChanged.Invoke(); + } + get => enabled; } + + public event Action StateChanged; } } \ No newline at end of file diff --git a/UnityProject/Assets/Scripts/PdConnection/PdProcess.cs b/UnityProject/Assets/Scripts/PdConnection/PdProcess.cs index 8cd322f..3538225 100644 --- a/UnityProject/Assets/Scripts/PdConnection/PdProcess.cs +++ b/UnityProject/Assets/Scripts/PdConnection/PdProcess.cs @@ -1,3 +1,4 @@ +using System; using System.Diagnostics; using UnityEngine; using Debug = UnityEngine.Debug; @@ -19,10 +20,7 @@ namespace cylvester { } - public static PdProcess Instance - { - get { return instance_ ?? (instance_ = new PdProcess()); } - } + public static PdProcess Instance => instance_ ?? (instance_ = new PdProcess()); public void Start(string mainPatch, int numInputChannels) { @@ -31,12 +29,16 @@ namespace cylvester pdProcess_ = new Process(); pdProcess_.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - pdProcess_.StartInfo.UseShellExecute = true; + pdProcess_.StartInfo.UseShellExecute = false; pdProcess_.StartInfo.FileName = Application.streamingAssetsPath + "/pd/win/pd.com"; var path = Application.streamingAssetsPath + "/pd/patch/" + mainPatch; pdProcess_.StartInfo.Arguments = "-nogui -rt -inchannels " + numInputChannels + " " + path; - pdProcess_.Start(); + + if (!pdProcess_.Start()) + { + throw new Exception("Pd process failed to start"); + } Debug.Log("Pd Process started"); } @@ -47,5 +49,6 @@ namespace cylvester pdProcess_ = null; Debug.Log("Pd Process stopped"); } + } } \ No newline at end of file diff --git a/UnityProject/Assets/StreamingAssets/pd/mac.meta b/UnityProject/Assets/StreamingAssets/pd/mac.meta deleted file mode 100644 index a87d092..0000000 --- a/UnityProject/Assets/StreamingAssets/pd/mac.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 8077544c425dc6d478d40c842bbe25a8 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/UnityProject/Assets/StreamingAssets/pd/patch/test.pd b/UnityProject/Assets/StreamingAssets/pd/patch/test.pd deleted file mode 100644 index 85390e1..0000000 --- a/UnityProject/Assets/StreamingAssets/pd/patch/test.pd +++ /dev/null @@ -1,9 +0,0 @@ -#N canvas 646 421 450 300 10; -#X obj 137 120 dac~; -#X obj 99 41 osc~ 440; -#X obj 142 74 *~ 0.1; -#X msg 232 96 \; pd dsp 1; -#X obj 238 55 loadbang; -#X connect 1 0 2 0; -#X connect 2 0 0 0; -#X connect 4 0 3 0; diff --git a/UnityProject/Assets/StreamingAssets/pd/patch/test.pd.meta b/UnityProject/Assets/StreamingAssets/pd/patch/test.pd.meta deleted file mode 100644 index e7cb08b..0000000 --- a/UnityProject/Assets/StreamingAssets/pd/patch/test.pd.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: babd3ace665dd164ca00417db66238c2 -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: