33 using System.Runtime.InteropServices;
34 using System.Collections.Generic;
42 public const string OpenAlDll =
"/System/Library/Frameworks/OpenAL.framework/OpenAL";
44 public const string OpenAlDll =
"libopenal.so.1";
46 public const string OpenAlDll =
"soft_oal_x64.dll";
52 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcSetErrorReasonCallback")]
55 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
56 private delegate
void ErrorReasonCallbackInternal(IntPtr cstr);
58 private static ErrorReasonCallbackInternal CurrentErrorReasonCallback;
59 private static IntPtr CurrentErrorReasonCallbackPtr;
63 CurrentErrorReasonCallback = (IntPtr cstr) =>
66 while (Marshal.ReadByte(cstr, strLen) !=
'\0') { strLen++; }
67 byte[] bytes =
new byte[strLen];
68 Marshal.Copy(cstr, bytes, 0, strLen);
69 string csStr = Encoding.UTF8.GetString(bytes);
71 callback?.Invoke(csStr);
74 CurrentErrorReasonCallbackPtr = Marshal.GetFunctionPointerForDelegate(CurrentErrorReasonCallback);
90 public const int Sync = 0x1009;
123 #region Context Management Functions
125 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcCreateContext")]
126 private static extern IntPtr _CreateContext(IntPtr device, IntPtr attrlist);
130 GCHandle handle = GCHandle.Alloc(attrList, GCHandleType.Pinned);
131 IntPtr retVal = _CreateContext(device, handle.AddrOfPinnedObject());
136 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcMakeContextCurrent")]
139 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcProcessContext")]
142 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcSuspendContext")]
145 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcDestroyContext")]
148 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcGetCurrentContext")]
151 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcGetContextsDevice")]
154 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcOpenDevice")]
155 private static extern IntPtr OpenDevice(IntPtr deviceName);
159 if (deviceName ==
null)
161 return OpenDevice(IntPtr.Zero);
164 byte[] devicenameBytes = Encoding.UTF8.GetBytes(deviceName +
"\0");
165 GCHandle devicenameHandle = GCHandle.Alloc(devicenameBytes, GCHandleType.Pinned);
166 IntPtr retVal = OpenDevice(devicenameHandle.AddrOfPinnedObject());
167 devicenameHandle.Free();
172 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcCloseDevice")]
175 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcGetError")]
185 return "Invalid context";
187 return "Invalid device";
189 return "Invalid enum";
191 return "Invalid value";
193 return "Out of memory";
195 return "Unknown error";
199 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcIsExtensionPresent")]
202 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcGetProcAddress")]
205 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcGetEnumValue")]
210 #region Query Functions
212 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcGetString")]
213 private static extern IntPtr _GetString(IntPtr device,
int param);
215 public static string GetString(IntPtr device,
int param)
217 IntPtr strPtr = _GetString(device, param);
219 while (Marshal.ReadByte(strPtr,strLen)!=
'\0') { strLen++; }
220 byte[] bytes =
new byte[strLen];
221 Marshal.Copy(strPtr, bytes, 0, strLen);
222 return Encoding.UTF8.GetString(bytes);
227 List<string> retVal =
new List<string>();
228 IntPtr strPtr = _GetString(device, param);
229 if (strPtr == IntPtr.Zero) {
return retVal; }
232 byte currChar = Marshal.ReadByte(strPtr, strEnd);
233 if (currChar ==
'\0') {
return retVal; }
239 currChar = Marshal.ReadByte(strPtr, strEnd);
241 if (currChar ==
'\0')
243 if (prevChar ==
'\0')
247 byte[] bytes =
new byte[strEnd-strStart];
248 Marshal.Copy(strPtr+strStart, bytes, 0, strEnd - strStart);
249 retVal.Add(Encoding.UTF8.GetString(bytes));
256 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcGetIntegerv")]
257 public static extern void GetIntegerv(IntPtr device,
int param,
int size, IntPtr data);
259 public static void GetInteger(IntPtr device,
int param, out
int data)
261 int[] dataArr =
new int[1];
262 GCHandle handle = GCHandle.Alloc(dataArr,GCHandleType.Pinned);
263 GetIntegerv(device, param, 1, handle.AddrOfPinnedObject());
270 #region Capture Functions
272 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcCaptureOpenDevice")]
273 private static extern IntPtr CaptureOpenDevice(IntPtr devicename, uint frequency,
int format,
int buffersize);
275 public static IntPtr
CaptureOpenDevice(
string devicename, uint frequency,
int format,
int buffersize)
277 byte[] devicenameBytes = Encoding.UTF8.GetBytes(devicename +
"\0");
278 GCHandle devicenameHandle = GCHandle.Alloc(devicenameBytes, GCHandleType.Pinned);
279 IntPtr retVal = CaptureOpenDevice(devicenameHandle.AddrOfPinnedObject(), frequency, format, buffersize);
280 devicenameHandle.Free();
284 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcCaptureCloseDevice")]
287 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcCaptureStart")]
290 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcCaptureStop")]
293 [DllImport(OpenAlDll, CallingConvention = CallingConvention.Cdecl, EntryPoint =
"alcCaptureSamples")]
294 public static extern void CaptureSamples(IntPtr device, IntPtr buffer,
int samples);
static bool MakeContextCurrent(IntPtr context)
const int CaptureDeviceSpecifier
static IntPtr CaptureOpenDevice(string devicename, uint frequency, int format, int buffersize)
static int GetEnumValue(IntPtr device, string enumname)
static void CaptureStop(IntPtr device)
static IntPtr GetCurrentContext()
static IntPtr OpenDevice(string deviceName)
static IReadOnlyList< string > GetStringList(IntPtr device, int param)
static void CaptureSamples(IntPtr device, IntPtr buffer, int samples)
static int GetError(IntPtr device)
static void DestroyContext(IntPtr context)
const int DeviceSpecifier
const int AllDevicesSpecifier
static bool CloseDevice(IntPtr device)
static void CaptureStart(IntPtr device)
static IntPtr GetContextsDevice(IntPtr context)
static void SetErrorReasonCallback(ErrorReasonCallback callback)
static bool CaptureCloseDevice(IntPtr device)
static string GetErrorString(int errorCode)
const int DefaultAllDevicesSpecifier
delegate void ErrorReasonCallback(string str)
static void GetInteger(IntPtr device, int param, out int data)
static IntPtr CreateContext(IntPtr device, int[] attrList)
const int DefaultDeviceSpecifier
static void SuspendContext(IntPtr context)
static void GetIntegerv(IntPtr device, int param, int size, IntPtr data)
static IntPtr GetProcAddress(IntPtr device, string funcname)
const int EnumCaptureSamples
const int CaptureDefaultDeviceSpecifier
const int OutputDevicesSpecifier
static bool IsExtensionPresent(IntPtr device, string extname)
static void ProcessContext(IntPtr context)
static string GetString(IntPtr device, int param)