30using System.Runtime.InteropServices;
32using System.Collections.Generic;
33#if UNITY_EDITOR || UNITY_STANDALONE
34using MYFLT = System.Double;
35#elif UNITY_ANDROID || UNITY_IOS
36using MYFLT = System.Single;
45 bool compiledOk =
false;
46 Action onCsoundCreated;
48 private void SetEnvironmentSettings(List<EnvironmentSettings> environmentSettings)
50 if (environmentSettings ==
null || environmentSettings.Count == 0)
return;
51 foreach (var env
in environmentSettings)
53 if (env ==
null)
continue;
54 var path = env.GetPath();
55 if (
string.IsNullOrWhiteSpace(path))
continue;
57 switch (Application.platform)
59 case RuntimePlatform.OSXEditor:
60 case RuntimePlatform.OSXPlayer:
63 Debug.Log($
"Setting {env.GetTypeString()} for MacOS to: {path}");
67 case RuntimePlatform.WindowsPlayer:
68 case RuntimePlatform.WindowsEditor:
71 Debug.Log($
"Setting {env.GetTypeString()} for Windows to: {path}");
75 case RuntimePlatform.Android:
78 Debug.Log($
"Setting {env.GetTypeString()} for Android to: {path}");
83 if (onCsoundCreated ==
null || onCsoundCreated.GetInvocationList().Length == 0)
85 onCsoundCreated += () =>
87 Debug.Log(
"Csound Force Loading Plugins!");
89 Debug.Log($
"PLUGINS LOADED? {loaded}");
95 case RuntimePlatform.IPhonePlayer:
98 Debug.Log($
"Setting {env.GetTypeString()} for iOS to: {path}");
117 SetEnvironmentSettings(environmentSettings);
134 Debug.LogError(
"Couldn't create Csound!");
145 parms.control_rate_override = AudioSettings.outputSampleRate;
146 parms.sample_rate_override = AudioSettings.outputSampleRate;
149 onCsoundCreated?.Invoke();
150 onCsoundCreated =
null;
156 compiledOk = ret == 0 ? true :
false;
160 #region Instantiation
254 var buffer = Marshal.AllocHGlobal(
sizeof(MYFLT) * (
int)bufsiz);
255 Marshal.Copy(audio, 0, buffer, (
int)Math.Min(audio.Length, bufsiz));
257 Marshal.FreeHGlobal(buffer);
263 var buffer = Marshal.AllocHGlobal(
sizeof(MYFLT) * (
int)bufsiz);
264 MYFLT[] dest =
new MYFLT[bufsiz];
266 Marshal.Copy(buffer, dest, 0, dest.Length);
267 Marshal.FreeHGlobal(buffer);
290 public void SetTable(
int table,
int index, MYFLT value)
307 dest =
new MYFLT[len];
308 IntPtr des = Marshal.AllocHGlobal(
sizeof(MYFLT) * dest.Length);
310 Marshal.Copy(des, dest, 0, len);
311 Marshal.FreeHGlobal(des);
326 dest =
new MYFLT[len];
327 IntPtr des = Marshal.AllocHGlobal(
sizeof(MYFLT) * dest.Length);
329 Marshal.Copy(des, dest, 0, len);
330 Marshal.FreeHGlobal(des);
340 if (len < 1 || len < source.Length)
return;
341 IntPtr src = Marshal.AllocHGlobal(
sizeof(MYFLT) * source.Length);
342 Marshal.Copy(source, 0, src, source.Length);
344 Marshal.FreeHGlobal(src);
353 if (len < 1 || len < source.Length)
return;
354 IntPtr src = Marshal.AllocHGlobal(
sizeof(MYFLT) * source.Length);
355 Marshal.Copy(source, 0, src, source.Length);
357 Marshal.FreeHGlobal(src);
364 public int GetTable(out MYFLT[] tableValues,
int numTable)
373 IntPtr tablePtr =
new IntPtr();
374 tableValues =
new MYFLT[len];
377 Marshal.Copy(tablePtr, tableValues, 0, len);
378 else tableValues =
null;
379 GCHandle gc = GCHandle.FromIntPtr(tablePtr);
391 IntPtr addr =
new IntPtr();
393 args =
new MYFLT[len];
395 Marshal.Copy(addr, args, 0, len);
397 Marshal.FreeHGlobal(addr);
422 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
423 private class NamedGenProxy
437 IDictionary<string, int> gens =
new Dictionary<string, int>();
439 while (pNAMEDGEN != IntPtr.Zero)
441 NamedGenProxy namedGen = (NamedGenProxy)Marshal.PtrToStructure(pNAMEDGEN, typeof(NamedGenProxy));
442 gens.Add(Marshal.PtrToStringAnsi(namedGen.name), namedGen.genum);
443 pNAMEDGEN = namedGen.next;
500 var spin =
new MYFLT[size];
502 Marshal.Copy(addr, spin, 0, size);
514 var spout =
new MYFLT[size];
516 Marshal.Copy(addr, spout, 0, size);
558 return CharPtr2String(message);
570 var opcodes =
new SortedDictionary<string, IList<OpcodeArgumentTypes>>();
571 IntPtr ppOpcodeList = IntPtr.Zero;
573 if ((ppOpcodeList != IntPtr.Zero) && (size >= 0))
575 int proxySize = Marshal.SizeOf(typeof(OpcodeListProxy));
576 for (
int i = 0; i < size; i++)
578 OpcodeListProxy proxy = Marshal.PtrToStructure(ppOpcodeList + (i * proxySize), typeof(OpcodeListProxy)) as OpcodeListProxy;
579 string opname = Marshal.PtrToStringAnsi(proxy.opname);
582 outypes = Marshal.PtrToStringAnsi(proxy.outtypes),
583 intypes = Marshal.PtrToStringAnsi(proxy.intypes),
586 if (!opcodes.ContainsKey(opname))
588 IList<OpcodeArgumentTypes> types =
new List<OpcodeArgumentTypes>();
590 opcodes.Add(opname, types);
594 opcodes[opname].Add(opcode);
611 IDictionary<string, ChannelInfo> channels =
new SortedDictionary<string, ChannelInfo>();
613 IntPtr ppChannels = IntPtr.Zero;
615 if ((size > 0) && (ppChannels != IntPtr.Zero))
617 int proxySize = Marshal.SizeOf(typeof(ChannelInfoProxy));
618 for (
int i = 0; i < size; i++)
620 var proxy = Marshal.PtrToStructure(ppChannels + (i * proxySize), typeof(ChannelInfoProxy)) as ChannelInfoProxy;
621 string chanName = CharPtr2String(proxy.name);
624 var hintProxy = proxy.hints;
629 height = hintProxy.height,
630 width = hintProxy.width,
631 attributes = CharPtr2String(proxy.name)
634 channels.Add(chanName, info);
681 [StructLayout(LayoutKind.Sequential)]
682 private class OpcodeListProxy
684 public IntPtr opname;
685 public IntPtr outtypes;
686 public IntPtr intypes;
693 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
694 private class ChannelInfoProxy
696 [MarshalAs(UnmanagedType.AnsiBStr)]
699 [MarshalAs(UnmanagedType.Struct)]
700 public ChannelHintsProxy hints;
706 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
707 private struct ChannelHintsProxy
709 public ChannelHintsProxy(ChannelHints hints)
711 behav = (int)hints.behav;
712 dflt = hints.dflt; min = hints.min; max = hints.max;
713 x = hints.x; y = hints.y; height = hints.height; width = hints.width;
714 attributes = IntPtr.Zero;
725 [MarshalAs(UnmanagedType.AnsiBStr)]
726 public IntPtr attributes;
817 [StructLayout(LayoutKind.Sequential)]
893 internal static String CharPtr2String(IntPtr pString)
895 return ((pString !=
null) && (pString != IntPtr.Zero)) ? Marshal.PtrToStringAnsi(pString) :
string.Empty;
EnvironmentPathOrigin
The base folder where to set the Environment Variables
int hardware_buffer_frames
double control_rate_override
double sample_rate_override
This structure holds the parameter hints for control channels.
ChannelHints(ChannelBehavior ibehav, MYFLT idflt, MYFLT imin, MYFLT imax)
Creates a channel hint initialized with the most common Control Channel values as provided.
ChannelHints()
Creates an empty hint by calling main constructor with all zeros
ChannelInfo(string _name, ChannelType _type, ChannelDirection _direction)
ChannelDirection Direction
Defines a class to hold out and in types, and flags
int CompileOrc(string orchStr)
void SetTable(int table, int index, MYFLT value)
Sets the value of a slot in a function table. The table number and index are assumed to be valid.
int GetTable(out MYFLT[] tableValues, int numTable)
Stores values to function table 'tableNum' in tableValues, and returns the table length (not includin...
IDictionary< string, int > GetNamedGens()
Returns a Dictionary keyed by the names of all named table generators. Each name is paired with its i...
long GetCurrentTimeSamples()
uint GetNchnls()
Get number of input channels
void SetSpinSample(int frame, int channel, MYFLT sample)
Set a sample from Csound's audio output buffer
IDictionary< string, IList< OpcodeArgumentTypes > > GetOpcodeList()
Returns a sorted Dictionary keyed by all opcodes which are active in the current instance of csound....
void GetNamedGEN(int num, out string name, int len)
Gets the GEN name from a number num, if this is a named GEN The final parameter is the max len of the...
MYFLT[] GetAudioChannel(string name)
int GetTableArgs(out MYFLT[] args, int index)
Stores the arguments used to generate function table 'tableNum' in args, and returns the number of ar...
void ClearSpin()
Clears the input buffer (spin).
uint GetNchnlsInput()
Get number of input channels
MYFLT GetTable(int table, int index)
Returns the value of a slot in a function table. The table number and index are assumed to be valid.
void SetAudioChannel(string name, MYFLT[] audio)
void TableCopyIn(int table, MYFLT[] source)
Copy the contents of an array source into a given function table The table number is assumed to be va...
void SendScoreEvent(string scoreEvent)
void SetParams(CSOUND_PARAMS parms)
Transfers the contents of the provided raw CSOUND_PARAMS object into csound's internal data structues...
void TableCopyOutAsync(int table, out MYFLT[] dest)
Asynchronous version of tableCopyOut()
void SetStringChannel(string channel, string value)
int GetCsoundMessageCount()
string GetCsoundMessage()
void AddSpinSample(int frame, int channel, MYFLT sample)
CSOUND_PARAMS GetParams()
Fills in a provided raw CSOUND_PARAMS object with csounds current parameter settings....
string GetEnv(string key)
Gets a string value from csound's environment values. Meaningful values include the contents of Windo...
int LoadPlugins(string dir)
IDictionary< string, ChannelInfo > GetChannelList()
Provides a dictionary of all currently defined channels resulting from compilation of an orchestra co...
bool CompiledWithoutError()
MYFLT GetSpoutSample(int frame, int channel)
Get a sample from Csound's audio output buffer
CsoundUnityBridge(string csdFile, List< EnvironmentSettings > environmentSettings)
The CsoundUnityBridge constructor sets up the Csound Global Environment Variables set by the user....
MYFLT GetChannel(string channel)
Get a a control channel
void TableCopyInAsync(int table, MYFLT[] source)
Asynchronous version of csoundTableCopyIn()
int SetGlobalEnv(string name, string value)
Set the global value of environment variable 'name' to 'value', or delete variable if 'value' is NULL...
int IsNamedGEN(int num)
Checks if a given GEN number num is a named GEN if so, it returns the string length (excluding termin...
void CsoundSetScoreOffsetSeconds(MYFLT value)
void TableCopyOut(int table, out MYFLT[] dest)
Copy the contents of a function table into a supplied array dest The table number is assumed to be va...
void SetChannel(string channel, MYFLT value)
int TableLength(int table)
Returns the length of a function table (not including the guard point), or -1 if the table does not e...
MYFLT[] GetSpout()
Returns the Csound audio output working buffer (spout) as a MYFLT array. Enables external software to...
MYFLT[] GetSpin()
Returns the Csound audio input working buffer (spin) as a MYFLT array. Enables external software to w...
static string GetMessageText(IntPtr message)