-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDssiInstance.cs
194 lines (176 loc) · 8.23 KB
/
DssiInstance.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace LADSPA.NET
{
public class DssiInstance : LadspaInstance
{
public readonly DssiDescriptor DssiDescriptor;
protected readonly List<DssiMidiEvent> eventList = new List<DssiMidiEvent>();
protected IntPtr eventBuffer;
protected int eventBufferSize;
protected int eventBufferCount;
internal DssiInstance(DssiDescriptor descriptor, IntPtr handle, int sampleRate, int bufferSize)
: base(descriptor.LadspaDescriptor, handle, sampleRate, bufferSize)
{
DssiDescriptor = descriptor;
}
protected void PrepareEvents()
{
int i, offset, count = eventList.Count;
int size = count * 28;
if (eventBuffer == IntPtr.Zero || eventBufferSize < size)
{
if (eventBuffer != IntPtr.Zero) { Marshal.FreeHGlobal(eventBuffer); eventBuffer = IntPtr.Zero; }
eventBuffer = Marshal.AllocHGlobal(eventBufferSize = size + 140);
}
for (i = 0, offset = 0, eventBufferCount = 0; i < count; i++)
{
DssiMidiEvent e = eventList[i];
int command = e.MidiCommand & 0xf0;
switch (command)
{
case 0x80: // Note Off
case 0x90: // Note On
case 0xA0: // Aftertouch
int code = (command == 0x80 || (e.MidiData1 == 0 && command == 0x90)) ? 7 : command == 0x90 ? 6 : 8;
Marshal.WriteInt32(eventBuffer, offset, code); offset += 4; // Type code
Marshal.WriteInt32(eventBuffer, offset, e.SampleIndex); offset += 4; // Ticks
Marshal.WriteInt64(eventBuffer, offset, 0); offset += 8; // Realtime timestamp + addresses (not used)
Marshal.WriteByte(eventBuffer, offset, (byte)(e.MidiCommand & 0x0f)); offset++; // Channel
Marshal.WriteByte(eventBuffer, offset, e.MidiData0); offset++; // Pitch
Marshal.WriteByte(eventBuffer, offset, e.MidiData1 == 0 ? (byte)64 : e.MidiData1); offset++; // Velocity
Marshal.WriteByte(eventBuffer, offset, 0); offset++; // Off Velocity (not used)
Marshal.WriteInt64(eventBuffer, offset, 0); offset += 8; // Duration (not used)
eventBufferCount++;
break;
case 0xB0: // Controller
Marshal.WriteInt32(eventBuffer, offset, 10); offset += 4; // Type code
Marshal.WriteInt32(eventBuffer, offset, e.SampleIndex); offset += 4; // Ticks
Marshal.WriteInt64(eventBuffer, offset, 0); offset += 8; // Realtime timestamp + addresses (not used)
Marshal.WriteInt32(eventBuffer, offset, e.MidiCommand & 0x0f); offset += 4; // Channel + Unused
Marshal.WriteInt32(eventBuffer, offset, e.MidiData0); offset += 4; // Control parameter
Marshal.WriteInt32(eventBuffer, offset, e.MidiData1); offset += 4; // Value
eventBufferCount++;
break;
case 0xC0: // Change program
SelectProgram((uint)(e.MidiCommand & 0x0f), e.MidiData0);
break;
case 0xD0: // Channel pressure
Marshal.WriteInt32(eventBuffer, offset, 12); offset += 4; // Type code
Marshal.WriteInt32(eventBuffer, offset, e.SampleIndex); offset += 4; // Ticks
Marshal.WriteInt64(eventBuffer, offset, 0); offset += 8; // Realtime timestamp + addresses (not used)
Marshal.WriteInt32(eventBuffer, offset, e.MidiCommand & 0x0f); offset += 4; // Channel + Unused
Marshal.WriteInt32(eventBuffer, offset, 0); offset += 4; // Control parameter
Marshal.WriteInt32(eventBuffer, offset, e.MidiData0); offset += 4; // Value
eventBufferCount++;
break;
case 0xE0: // Pitchbend
Marshal.WriteInt32(eventBuffer, offset, 13); offset += 4; // Type code
Marshal.WriteInt32(eventBuffer, offset, e.SampleIndex); offset += 4; // Ticks
Marshal.WriteInt64(eventBuffer, offset, 0); offset += 8; // Realtime timestamp + addresses (not used)
Marshal.WriteInt32(eventBuffer, offset, e.MidiCommand & 0x0f); offset += 4; // Channel + Unused
Marshal.WriteInt32(eventBuffer, offset, 0); offset += 4; // Control parameter
Marshal.WriteInt32(eventBuffer, offset, (short)(((int)e.MidiData0 << 2) + ((int)e.MidiData1 << 9)) >> 2); offset += 4; // Value
eventBufferCount++;
break;
}
}
eventList.Clear();
}
public string Configure(string key, string value)
{
if (handle == IntPtr.Zero || DssiDescriptor.Configure == null) { return (null); }
return (DssiDescriptor.Configure(handle, key, value));
}
public void SelectProgram(uint bank, uint program)
{
if (handle == IntPtr.Zero || DssiDescriptor.SelectProgram == null) { return; }
DssiDescriptor.SelectProgram(handle, bank, program);
}
public int GetMidiControllerForPort(uint port)
{
if (handle == IntPtr.Zero || DssiDescriptor.GetMidiControllerForPort == null) { return (0); }
return (DssiDescriptor.GetMidiControllerForPort(handle, port));
}
public void AddSynthEvents(params DssiMidiEvent[] events)
{
if (events != null && events.Length > 0)
{
eventList.AddRange(events);
}
}
public bool Run(float[][] outputs)
{
if (handle != IntPtr.Zero)
{
if (DssiDescriptor.RunSynth != null)
{
PrepareEvents();
DssiDescriptor.RunSynth(handle, (uint)bufferSize, eventBuffer, (uint)eventBufferCount);
CopyOutputs(outputs);
return (true);
}
else if (LadspaDescriptor.Run != null)
{
LadspaDescriptor.Run(handle, (uint)bufferSize);
CopyOutputs(outputs);
return (true);
}
}
if (eventList.Count > 0) { eventList.Clear(); }
return (false);
}
public bool RunAdding(float[][] outputs)
{
if (handle != IntPtr.Zero)
{
if (DssiDescriptor.RunSynthAdding != null)
{
PrepareEvents();
DssiDescriptor.RunSynthAdding(handle, (uint)bufferSize, eventBuffer, (uint)eventBufferCount);
CopyOutputs(outputs);
return (true);
}
else if (LadspaDescriptor.RunAdding != null)
{
LadspaDescriptor.RunAdding(handle, (uint)bufferSize);
CopyOutputs(outputs);
return (true);
}
}
if (eventList.Count > 0) { eventList.Clear(); }
return (false);
}
public override bool Run(float[][] inputs, float[][] outputs)
{
if (handle != IntPtr.Zero) { CopyInputs(inputs); }
return (Run(outputs));
}
public override bool RunAdding(float[][] inputs, float[][] outputs)
{
if (handle != IntPtr.Zero) { CopyInputs(inputs); }
return (RunAdding(outputs));
}
public override LadspaInstance Clone(int sampleRate = 0, int bufferSize = 0)
{
if (sampleRate == 0) { sampleRate = this.sampleRate; }
if (bufferSize == 0) { bufferSize = this.bufferSize; }
DssiInstance instance = DssiDescriptor.CreateInstance(sampleRate, bufferSize);
for (int i = 0; i < LadspaDescriptor.Ports.Length; i++) { instance.SetParameter(i, GetParameter(i)); }
return (instance);
}
public override void Dispose()
{
base.Dispose();
if (eventBuffer != IntPtr.Zero)
{
Marshal.FreeHGlobal(eventBuffer);
eventBuffer = IntPtr.Zero;
}
}
}
}