forked from xtremegaida/ladspa.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LadspaLibraryContext.cs
79 lines (71 loc) · 2.97 KB
/
LadspaLibraryContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace LADSPA.NET
{
public class LadspaLibraryContext : IDisposable
{
[DllImport("Kernel32.dll")]
private static extern IntPtr LoadLibrary(string path);
[DllImport("Kernel32.dll")]
private static extern void FreeLibrary(IntPtr hModule);
[DllImport("Kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr LadspaDescriptorCallback(uint index);
private IntPtr library;
public readonly LadspaDescriptor[] LadspaDescriptors;
public readonly DssiDescriptor[] DssiDescriptors;
public LadspaLibraryContext(string fileName)
{
library = LoadLibrary(fileName);
if (library == IntPtr.Zero) { throw new Exception("Failed to load library - file not found or not a valid library."); }
try
{
IntPtr func = GetProcAddress(library, "ladspa_descriptor");
IntPtr dssiFunc = GetProcAddress(library, "dssi_descriptor");
if (func == IntPtr.Zero && dssiFunc == IntPtr.Zero)
{
throw new Exception("Not a LADSPA plugin: ladspa_descriptor not found.");
}
List<LadspaDescriptor> descriptors = new List<LadspaDescriptor>();
List<DssiDescriptor> dssiDescriptors = new List<DssiDescriptor>();
if (func != IntPtr.Zero)
{
LadspaDescriptorCallback callback = (LadspaDescriptorCallback)Marshal.GetDelegateForFunctionPointer(func, typeof(LadspaDescriptorCallback));
for (uint index = 0; true; index++)
{
IntPtr data = callback(index);
if (data == IntPtr.Zero) { break; }
descriptors.Add(new LadspaDescriptor(this, index, data));
}
}
if (dssiFunc != IntPtr.Zero)
{
LadspaDescriptorCallback callback = (LadspaDescriptorCallback)Marshal.GetDelegateForFunctionPointer(dssiFunc, typeof(LadspaDescriptorCallback));
for (uint index = 0; true; index++)
{
IntPtr data = callback(index);
if (data == IntPtr.Zero) { break; }
dssiDescriptors.Add(new DssiDescriptor(this, index, data));
}
}
LadspaDescriptors = descriptors.ToArray();
DssiDescriptors = dssiDescriptors.ToArray();
}
catch
{
FreeLibrary(library);
throw;
}
}
public void Dispose()
{
if (library == IntPtr.Zero) { return; }
FreeLibrary(library);
library = IntPtr.Zero;
}
}
}