Repository navigation
Expand file tree
/
Copy pathHardwareSetup.cs
More file actions
328 lines (301 loc) · 13.2 KB
/
Copy pathHardwareSetup.cs
File metadata and controls
328 lines (301 loc) · 13.2 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sanford.Multimedia.Midi;
using System.Threading;
namespace MIDI_Interface
{
internal struct outputMessages // Output messages struct containing the note and velocity
{
internal int Note; // MIDI Note Value
internal int Mess; // Velocity of MIDI Note
internal bool isCC; // True for Control Change or false for NoteOn message
}
public class HardwareSetup
{
// Contains all hardware related methods and variables:
// -----------------------------------------
// PUBLIC METHODS:
//=================
// initialise() - sets up MIDI device for sending and receiving messages
// release() - disposes of all MIDI device variables
// INTERNAL METHODS:
//=================
// HardwareSetup(InterfaceForm mainForm) - sets static declaration of inForm to the current form
// resetControlValues() - Set all faders to centre position and all buttons to off
// controlMess(int note, float velocity) - sends a control message to the MIDI device
// noteMess(int note, bool onoff) - sends a NoteOn message to the MIDI device
// PRIVATE METHODS:
//=================
// start() - begins BCF2000 recording input MIDI messages
// HandleChannelMessageReceived() - Event handler for MIDI messages received from MIDI devices
// sortOutmess(outputMessages mess) - sorts MIDI messages for the adjustment of parameters and form values
// UNUSED METHODS:
//=================
// private void CaptureDelegates() - captures non-static methods in action delegates for use in Matlab
// public void Init() - performs CaptureDelegates() and then initialisation delegate
// public void Rel() - performs release() method by delegate
private static ChannelCommand n = ChannelCommand.NoteOn;
private static InterfaceForm inForm;
private static SynchronizationContext context; // Synchronisation context for syncing form
internal static InputDevice BCF2000_i = null; // MIDI input device object
internal static OutputDevice BCF2000_o = null; // MIDI output to send messages to BCF2000
private static outputMessages outmess; // output messages stored in this struct
internal static bool ControlSender = false; // Set true when handling MIDI messages recieved from controller
internal static bool FormSender = false; // Set true when handling messages from Form
internal static bool FirstRun = true;
internal static bool InitSender = false;
internal static Action RunInit;
internal static Action RunRelease;
internal HardwareSetup(InterfaceForm mainForm) // For purposes of changing form values from this class
{
inForm = mainForm; if (FormSender) { }
}
private HardwareSetup() // For purposes of changing form values from this class
{
}
private static void start() // Starts BCF2000 data stream
{
BCF2000_i.SysExBufferSize = 4096; // In case System Exclusive message buffer isn't initialised
BCF2000_i.StartRecording(); // Starts recording to ensure messages are received
return;
}
private static void CaptureDelegates()
{
HardwareSetup temp = new HardwareSetup();
HardwareSetup.RunInit = new Action(temp.initialise);
HardwareSetup.RunRelease = new Action(temp.release);
}
public static void Init()
{
if (FirstRun)
CaptureDelegates();
InitSender = true;
HardwareSetup.RunInit();
InitSender = false;
}
public static void Rel()
{
HardwareSetup.RunRelease();
}
public void initialise() // Initialises device for input and output when called
{
if (!InitSender && FirstRun)
CaptureDelegates();
if (InputDevice.DeviceCount < 1) // If there aren't any connected devices
{
//System.Diagnostics.Debug.WriteLine("No Devices");
}
else
{
try
{
context = SynchronizationContext.Current; // update synchronisation context for threading in program
//System.Diagnostics.Debug.WriteLine("MIDI Devices detected!");
BCF2000_i = new InputDevice(0); // Input from controller
BCF2000_i.ChannelMessageReceived += HandleChannelMessageReceived; // Handles all channel messages
BCF2000_o = new OutputDevice(1); // Output to controller
System.Diagnostics.Debug.WriteLine("MIDI Device " + BCF2000_i.DeviceID + " initialised");
//System.Diagnostics.Debug.WriteLine("Number of output devices: " + OutputDevice.DeviceCount);
//System.Diagnostics.Debug.WriteLine("Number of input devices: " + InputDevice.DeviceCount);
}
catch (Exception ex) // Device failure by any exceptions
{
System.Diagnostics.Debug.WriteLine("Device failed to initialise, " + ex.Message);
return;
}
if (BCF2000_o != null) // if MIDI device has initialised
{
try
{
BCF2000_o.RunningStatusEnabled = true; // allow output messages
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Device failed to initialise, " + ex.Message);
return;
}
}
if (BCF2000_i != null) // if MIDI device has initialised
{
try
{
start(); // Start recording if BCF2000_i is a valid object
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Device failed to initialise, " + ex.Message);
return;
}
}
}
if (FirstRun)
{
resetControlValues();
FirstRun = false;
}
return;
}
internal static void resetControlValues() // Set all faders to default position and all buttons to off
{
for (int i = 0; i < 8; i++)
{
controlMess(i, 63);
controlMess(i + 8, 0);
}
for (int i = 0; i < 20; i++)
{
noteMess(i, false);
}
if (!FirstRun && (BCF2000_i == null || BCF2000_o == null || BCF2000_o.IsDisposed || BCF2000_i.IsDisposed || InterfaceForm.DisconnectNotify))
RunInit();
if (inForm != null)
inForm.ChangeStatusText();
}
private void HandleChannelMessageReceived(object sender, ChannelMessageEventArgs e) // Event Handler for MIDI messages
{
// context.Post(delegate(object dummy)
// {
/* System.Diagnostics.Debug.WriteLine(
e.Message.Command.ToString() + '\t' + '\t' +
e.Message.MidiChannel.ToString() + '\t' +
e.Message.Data1.ToString() + '\t' +
e.Message.Data2.ToString());*/
// }, null); // Writes all channel messages to output console
outmess.Note = e.Message.Data1; // Insert channel messages to variable struct: Note
outmess.Mess = e.Message.Data2; // Velocity
if (e.Message.Command.Equals(n))
{
outmess.isCC = false; // set false if the output message is a NoteOn message
}
else
{
outmess.isCC = true; // True if output message is a Control Change (only two types of message possible)
}
ControlSender = true;
sortOutmess(outmess); // use messages to change parameters
ControlSender = false;
if (inForm != null)
inForm.ChangeStatusText();
}
public void release() // Disposes of all devices and stops recording
{
if (BCF2000_i != null)
{
BCF2000_i.StopRecording();
BCF2000_i.Reset();
BCF2000_i.Close();
System.Diagnostics.Debug.WriteLine("MIDI Device disposed");
}
if (BCF2000_o != null)
{
BCF2000_o.Reset();
BCF2000_o.Close();
System.Diagnostics.Debug.WriteLine("MIDI Device disposed");
}
return;
}
internal static void controlMess(int note, float velocity) // Sends a Control message to the MIDI device
{
if (ControlSender == false)
{
if (HardwareSetup.BCF2000_i != null)
{
ChannelMessageBuilder builder = new ChannelMessageBuilder(); // build MIDI Message
builder.MidiChannel = 0; // Always channel 0
builder.Command = ChannelCommand.Controller; // Control message for knobs and faders
builder.Data1 = note; // Control number
builder.Data2 = (int)velocity; // send the value from interface
builder.Build();
try
{
BCF2000_o.Send(builder.Result);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("MIDI Device not conencted. " + ex.Message);
if (inForm != null)
inForm.ChangeStatusText(ex);
}
}
}
if (FormSender == false)
{
if (inForm != null)
inForm.setValue(note, (decimal)velocity);
}
}
internal static void noteMess(int note, bool onoff) // Sends a NoteOn message to the MIDI device
{
if (ControlSender == false)
{
if (HardwareSetup.BCF2000_i != null)
{
ChannelMessageBuilder builder = new ChannelMessageBuilder(); // build MIDI Message
builder.MidiChannel = 0; // Always channel 0
builder.Command = ChannelCommand.NoteOn; // NoteOn message for buttons
builder.Data1 = note; // Note number
if (onoff)
{
builder.Data2 = 100; // send 100 if true
}
else
{
builder.Data2 = 0;
}
builder.Build();
try
{
BCF2000_o.Send(builder.Result);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Failed to send MIDI Message" + ex.Message);
if (inForm != null)
inForm.ChangeStatusText(ex);
}
}
}
if (FormSender == false)
{
if (inForm != null)
inForm.setButton(note, onoff);
}
}
private void sortOutmess(outputMessages mess) // Sorts MIDI messages into variables in the parameters class
{
if (mess.isCC) // If the current message is a control change
{
float velocity = mess.Mess; // Value of control change
if (inForm != null)
inForm.setValue(mess.Note, (decimal)velocity); // Change form control if form exists
if (mess.Note <= 15) // If note is within valid range
{
parameters.setX(mess.Note, velocity); // change value in parameters class
controlMess(mess.Note, velocity); // Depending on sender, changes controller values
}
else
{
System.Diagnostics.Debug.WriteLine("Invalid control change, please change note value");
}
}
else
{
if (mess.Note <= 20)
{
bool onoff = mess.Mess > 0;
parameters.setY(mess.Note, onoff);
if (inForm != null)
inForm.setButton(mess.Note, onoff);
noteMess(mess.Note, onoff);
}
else
{
System.Diagnostics.Debug.WriteLine("Invalid button pressed, please change note value");
}
}
return;
}
}
}