add realtime updated levelmeters in Unity Editor

This commit is contained in:
Chikashi Miyama 2019-09-29 00:19:35 +02:00
parent cd1cf21386
commit 779e936036
10 changed files with 101 additions and 179 deletions

View file

@ -5,14 +5,20 @@ namespace cylvester
{ {
public class LevelMeter public class LevelMeter
{ {
private string label_; private const int TextureWidth = 1;
private float dB_; private const int TextureHeight = 100;
private Texture2D meterImageTexture_;
public LevelMeter(int index) private readonly Texture2D meterImageTexture_;
private readonly int index_;
private readonly string label_;
private readonly IPdArray pdArray_;
public LevelMeter(int index, IPdArray pdArray)
{ {
label_ = (index + 1).ToString(); index_ = index;
meterImageTexture_ = new Texture2D(1, 32); label_ = (index_ + 1).ToString();
pdArray_ = pdArray;
meterImageTexture_ = new Texture2D(TextureWidth, TextureHeight);
} }
public void Render() public void Render()
@ -33,9 +39,10 @@ namespace cylvester
private void UpdateTexture() 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(); meterImageTexture_.Apply();
} }

View file

@ -1,25 +1,27 @@
using UnityEditor; using System;
using UnityEngine; using UnityEditor;
namespace cylvester namespace cylvester
{ {
public class PdConsole : EditorWindow public class PdConsole : EditorWindow
{ {
private const int NumChannels = 16;
private IEditorToggle dspToggle_; private IEditorToggle dspToggle_;
private ITogglePresenter togglePresenter_; private ITogglePresenter togglePresenter_;
private Action onDspToggleStateChanged_;
private IPdBackend pdBackend_; private IPdBackend pdBackend_;
private LevelMeter[] levelMeters_; private LevelMeter[] levelMeters_;
private PdArray levelMeterArray_;
[MenuItem("SoundVision/Pd console %#p")] [MenuItem("SoundVision/Pd console %#p")]
static void Init() static void Init()
{ {
var window = (PdConsole)GetWindow(typeof(PdConsole)); var window = (PdConsole)GetWindow(typeof(PdConsole));
window.Show(); window.Show();
} }
private void OnEnable() private void Awake()
{ {
var foundObjects = FindObjectsOfType(typeof(PdBackend)); var foundObjects = FindObjectsOfType(typeof(PdBackend));
if (foundObjects.Length != 1) if (foundObjects.Length != 1)
@ -28,40 +30,70 @@ namespace cylvester
pdBackend_ = (IPdBackend) foundObjects[0]; pdBackend_ = (IPdBackend) foundObjects[0];
dspToggle_ = new EditorToggle(); 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_); togglePresenter_ = new TogglePresenter(dspToggle_, pdBackend_);
levelMeters_ = new LevelMeter[16]; levelMeters_ = new LevelMeter[NumChannels];
for (var i = 0; i < 16; ++i)
levelMeters_[i] = new LevelMeter(i);
}
private void OnDestroy()
{
levelMeterArray_?.Dispose();
dspToggle_.ToggleStateChanged -= onDspToggleStateChanged_;
} }
private void OnGUI () private void OnGUI ()
{ {
if(!ValidatePdBackend(pdBackend_)) if(!ValidatePdBackend())
return; return;
EditorGUILayout.Space(); EditorGUILayout.Space();
dspToggle_.State = EditorGUILayout.Toggle("Pure Data Process", dspToggle_.State); dspToggle_.State = EditorGUILayout.Toggle("Pure Data Process", dspToggle_.State);
if(!CheckProcessingState())
return;
EditorGUILayout.Space(); EditorGUILayout.Space();
EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginHorizontal();
levelMeterArray_.Update();
foreach (var levelMeter in levelMeters_) foreach (var levelMeter in levelMeters_)
levelMeter.Render(); levelMeter.Render();
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
Repaint();
} }
private bool ValidatePdBackend()
private bool ValidatePdBackend(IPdBackend pdBackend)
{
var exist = pdBackend_ != null;
if (!exist)
{ {
if (pdBackend_ != null)
return true;
EditorGUILayout.LabelField("No Pd backend found in the scene"); EditorGUILayout.LabelField("No Pd backend found in the scene");
return false;
} }
return exist;
private bool CheckProcessingState()
{
if (pdBackend_.State)
return true;
EditorGUILayout.LabelField("Pd process is currently inactive");
return false;
} }
} }
} }

View file

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 7978207823e9dad49b91f32a347617a0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -218,14 +218,13 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 987772532} m_GameObject: {fileID: 987772532}
m_Enabled: 1 m_Enabled: 0
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3ba69cee3466a304d9d570268f717413, type: 3} m_Script: {fileID: 11500000, guid: 3ba69cee3466a304d9d570268f717413, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
mainPatch: test.pd mainPatch: analyzer.pd
inchannels: 2 inchannels: 2
pdProcess: 0
--- !u!4 &987772534 --- !u!4 &987772534
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -309,107 +308,3 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 1 m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} 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}

View file

@ -3,7 +3,13 @@ using System.IO.MemoryMappedFiles;
namespace cylvester namespace cylvester
{ {
public class PdArray : IDisposable public interface IPdArray
{
float[] Data { get; }
void Update();
}
public class PdArray : IDisposable, IPdArray
{ {
public float[] Data { get; } public float[] Data { get; }

View file

@ -1,11 +1,13 @@
using System; using System;
using System.Threading;
using UnityEngine; using UnityEngine;
namespace cylvester namespace cylvester
{ {
public interface IPdBackend public interface IPdBackend
{ {
bool State { set; } bool State { set; get; }
event Action StateChanged;
} }
[ExecuteInEditMode] [ExecuteInEditMode]
@ -19,6 +21,7 @@ namespace cylvester
private void OnEnable() private void OnEnable()
{ {
PdProcess.Instance.Start(mainPatch, inchannels); PdProcess.Instance.Start(mainPatch, inchannels);
Thread.Sleep(500);
} }
private void OnDisable() private void OnDisable()
@ -28,7 +31,15 @@ namespace cylvester
public bool State public bool State
{ {
set => enabled = value; set
{
enabled = value;
if(StateChanged != null)
StateChanged.Invoke();
} }
get => enabled;
}
public event Action StateChanged;
} }
} }

View file

@ -1,3 +1,4 @@
using System;
using System.Diagnostics; using System.Diagnostics;
using UnityEngine; using UnityEngine;
using Debug = UnityEngine.Debug; using Debug = UnityEngine.Debug;
@ -19,10 +20,7 @@ namespace cylvester
{ {
} }
public static PdProcess Instance public static PdProcess Instance => instance_ ?? (instance_ = new PdProcess());
{
get { return instance_ ?? (instance_ = new PdProcess()); }
}
public void Start(string mainPatch, int numInputChannels) public void Start(string mainPatch, int numInputChannels)
{ {
@ -31,12 +29,16 @@ namespace cylvester
pdProcess_ = new Process(); pdProcess_ = new Process();
pdProcess_.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; pdProcess_.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pdProcess_.StartInfo.UseShellExecute = true; pdProcess_.StartInfo.UseShellExecute = false;
pdProcess_.StartInfo.FileName = Application.streamingAssetsPath + "/pd/win/pd.com"; pdProcess_.StartInfo.FileName = Application.streamingAssetsPath + "/pd/win/pd.com";
var path = Application.streamingAssetsPath + "/pd/patch/" + mainPatch; var path = Application.streamingAssetsPath + "/pd/patch/" + mainPatch;
pdProcess_.StartInfo.Arguments = "-nogui -rt -inchannels " + numInputChannels + " " + path; 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"); Debug.Log("Pd Process started");
} }
@ -47,5 +49,6 @@ namespace cylvester
pdProcess_ = null; pdProcess_ = null;
Debug.Log("Pd Process stopped"); Debug.Log("Pd Process stopped");
} }
} }
} }

View file

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 8077544c425dc6d478d40c842bbe25a8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

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

View file

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: babd3ace665dd164ca00417db66238c2
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: