soundvision/UnityProject/Assets/Scripts/PdConnection/PdProcess.cs

79 lines
2.1 KiB
C#
Raw Normal View History

using System;
2019-09-28 18:17:39 +00:00
using System.Diagnostics;
2019-09-29 17:47:17 +00:00
using System.Threading;
2019-09-28 18:17:39 +00:00
using UnityEngine;
using Debug = UnityEngine.Debug;
namespace cylvester
{
interface IPdProcess
{
void Start(string mainPatch, int numInputChannels);
void Stop();
2019-09-29 17:47:17 +00:00
bool Running { get; }
2019-09-28 18:17:39 +00:00
}
public class PdProcess : IPdProcess
{
private static PdProcess instance_ = null;
private Process pdProcess_;
2019-09-29 16:04:26 +00:00
private PdProcess()
2019-09-28 18:17:39 +00:00
{
2019-09-29 16:04:26 +00:00
} // cannot be instantiate normally
2019-09-28 18:17:39 +00:00
public static PdProcess Instance => instance_ ?? (instance_ = new PdProcess());
2019-09-28 18:17:39 +00:00
public void Start(string mainPatch, int numInputChannels)
{
2019-09-29 17:47:17 +00:00
2019-09-28 18:17:39 +00:00
if (pdProcess_ != null)
2019-09-29 17:47:17 +00:00
{
pdProcess_.Refresh();
if (!pdProcess_.HasExited)
return;
}
2019-09-28 18:17:39 +00:00
pdProcess_ = new Process();
pdProcess_.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pdProcess_.StartInfo.UseShellExecute = false;
2019-09-28 18:17:39 +00:00
pdProcess_.StartInfo.FileName = Application.streamingAssetsPath + "/pd/win/pd.com";
var path = Application.streamingAssetsPath + "/pd/patch/" + mainPatch;
pdProcess_.StartInfo.Arguments = "-nogui -rt -inchannels " + numInputChannels + " " + path;
if (!pdProcess_.Start())
{
throw new Exception("Pd process failed to start");
}
2019-09-29 17:47:17 +00:00
Thread.Sleep(500);
2019-09-28 18:17:39 +00:00
Debug.Log("Pd Process started");
}
public void Stop()
{
2019-09-29 17:47:17 +00:00
if (pdProcess_ == null)
return;
pdProcess_.Kill();
2019-09-28 18:17:39 +00:00
pdProcess_ = null;
Debug.Log("Pd Process stopped");
2019-09-29 17:47:17 +00:00
}
public bool Running
{
get
{
pdProcess_.Refresh();
if (pdProcess_ == null)
return false;
if (pdProcess_.HasExited)
return false;
return true;
}
2019-09-28 18:17:39 +00:00
}
}
}