-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
459 lines (369 loc) · 15.2 KB
/
Form1.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Midi;
using MIDI_SysEx.Buttons;
using MIDI_SysEx.lib;
namespace MIDI_SysEx
{
public partial class Form1 : Form
{
private Dictionary<string, Button> allButtons;
private Dictionary<string, Button> usedButtons;
/// <summary>
/// APC Information taken from https://forums.pangolin.com/threads/apc20-initialization-script.1827/
/// </summary>
public Form1()
{
InitializeComponent();
allButtons = new Dictionary<string, Button>();
usedButtons = new Dictionary<string, Button>();
}
private EventHandler ButtonClickEventHandler { get; set; }
private MouseEventHandler ButtonMouseDownEventHandler { get; set; }
private EventHandler SelectedOptionChangedEventHandler { get; set; }
private void LoadLEDButtons()
{
PopulateButtonDictionary();
// all per-track buttons
for(var track=1; track<9; track++)
{
// clip launch buttons
SetupButton($"btn_cl_{track}_1", new APCLEDClipLaunch1Button(track));
SetupButton($"btn_cl_{track}_2", new APCLEDClipLaunch2Button(track));
SetupButton($"btn_cl_{track}_3", new APCLEDClipLaunch3Button(track));
SetupButton($"btn_cl_{track}_4", new APCLEDClipLaunch4Button(track));
SetupButton($"btn_cl_{track}_5", new APCLEDClipLaunch5Button(track));
//clip stop
SetupButton($"btn_cs_{track}", new APCLEDClipStopButton(track));
// track selection
SetupButton($"btn_ts_{track}", new APCLEDTrackSelectionButton(track));
// activator
SetupButton($"btn_act_{track}", new APCLEDActivatorButton(track));
// solo/cue
SetupButton($"btn_sc_{track}", new APCLEDSoloButton(track));
// record/arm
SetupButton($"btn_ra_{track}", new APCLEDRecordArmButton(track));
}
// master/Note Mode
SetupButton($"btn_master", new APCLEDMasterButton());
// Scene Launch 1
SetupButton($"btn_sl_1", new APCLEDSceneLaunch1Button());
// Scene Launch 2
SetupButton($"btn_sl_2", new APCLEDSceneLaunch2Button());
// Scene Launch 3
SetupButton($"btn_sl_3", new APCLEDSceneLaunch3Button());
// Scene Launch 4
SetupButton($"btn_sl_4", new APCLEDSceneLaunch4Button());
// Scene Launch 5
SetupButton($"btn_sl_5", new APCLEDSceneLaunch5Button());
}
/// <summary>
/// Sets up the button on the form with the necessary parameters
/// </summary>
/// <param name="name">id/name of the button to find</param>
/// <param name="led">led to attach to the button</param>
public void SetupButton(string name, IAPCLEDButton led)
{
Button btn = GetButton(name);
btn.Tag = led;
btn.Click += ButtonClickEventHandler;
btn.MouseDown += ButtonMouseDownEventHandler;
led.SelectedOptionChanged += SelectedOptionChangedEventHandler;
led.SetParentButton(btn);
usedButtons.Add(btn.Name, btn);
}
/// <summary>
/// Populates the <see cref="Button"/> dictionary with all of the buttons on the form
/// </summary>
public void PopulateButtonDictionary()
{
var buttons = GetAll(this, typeof(Button)).ToList();
allButtons = buttons.ToDictionary(x => x.Name, x => (Button)x);
}
/// <summary>
/// Recursive method getting all the <see cref="Button"/> controls in the form
/// </summary>
public IEnumerable<System.Windows.Forms.Control> GetAll(System.Windows.Forms.Control control, Type type)
{
var controls = control.Controls.Cast<System.Windows.Forms.Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
private void btnGeneric_Click(object sender, EventArgs e)
{
try
{
SendMode(new byte[] { 0xF0, 0x47, 0x7F, 0x7B, 0x60, 0x00, 0x04, 0x40, 0x08, 0x02, 0x01, 0xF7 });
// --------------------------------------------------------------^ 0x40 for Generic mode
}
catch (Exception ex)
{
MessageBox.Show($"An error ocurred while changing to Generic mode: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
MessageBox.Show("Generic mode signal sent to selected device successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnAbleton_Click(object sender, EventArgs e)
{
try
{
SendMode(new byte[] { 0xF0, 0x47, 0x7F, 0x7B, 0x60, 0x00, 0x04, 0x41, 0x08, 0x02, 0x01, 0xF7 });
// ----------------------------------------------------------------^ 0x41 for Ableton Live Mode
}
catch (Exception ex)
{
MessageBox.Show($"An error ocurred while changing to Ableton Live mode: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
MessageBox.Show("Ableton Live mode signal sent to selected device successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnAltAbleton_Click(object sender, EventArgs e)
{
try
{
SendMode(new byte[] { 0xF0, 0x47, 0x7F, 0x7B, 0x60, 0x00, 0x04, 0x42, 0x08, 0x02, 0x01, 0xF7 });
// -----------------------------------------------------------------^ 0x42 for Alternate Ableton Live Mode
}
catch (Exception ex)
{
MessageBox.Show($"An error ocurred while changing to Alternate Ableton Live mode: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
MessageBox.Show("Alternate Ableton Live mode signal sent to selected device successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
/// <summary>
/// common right-click event for all APC buttons
/// </summary>
private void APCButton_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Button btn = (Button)sender;
APCLEDButton led = (APCLEDButton)btn.Tag;
btn.BackColor = SystemColors.Control;
btn.Text = "";
led.SetSelectedOption(-1);
TurnOffLED(led.Channel, led.NoteNumber);
}
}
/// <summary>
/// common click event for all APC buttons
/// </summary>
private void APCButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
APCLEDButton led = (APCLEDButton)btn.Tag;
int index;
if (((MouseEventArgs)e).Button == MouseButtons.Right || led.SelectedOptionIndex == (led.AvailableOptions.Count - 1))
{
index = -1;
}
else
{
if (led.SelectedOptionIndex == -1)
index = 0;
else
index = led.SelectedOptionIndex + 1;
}
led.SetSelectedOption(index);
}
/// <summary>
/// Sends a note on to the selected device
/// </summary>
private void TurnOnLED(IAPCLEDButton button, IAPCLEDButtonOption option)
{
OutputDevice selectedDevice = (OutputDevice)lbDevices.SelectedItem;
selectedDevice.Open();
try
{
selectedDevice.SendNoteOn(button.Channel, button.NoteNumber, option.Velocity);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while sending the SysEx message to " + selectedDevice.Name + ".", "Error");
}
finally
{
selectedDevice.Close();
}
}
/// <summary>
/// Sends a note off to the selected device
/// </summary>
private void TurnOffLED(Channel channel, Pitch noteNumber)
{
OutputDevice selectedDevice = (OutputDevice)lbDevices.SelectedItem;
selectedDevice.Open();
try
{
selectedDevice.SendNoteOff(channel, noteNumber,0);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while sending the SysEx message to " + selectedDevice.Name + ".", "Error");
}
finally
{
selectedDevice.Close();
}
}
/// <summary>
/// Retrieves the button on the form
/// </summary>
private Button GetButton(string key)
{
//return (Button)Controls.Find(key, true).First();
if (allButtons.TryGetValue(key, out Button value))
return value;
else
throw new ArgumentOutOfRangeException(nameof(key));
}
/// <summary>
/// Sends the mode to the APC20
/// </summary>
private void SendMode(byte[] modeSysEx)
{
OutputDevice selectedDevice = (OutputDevice)lbDevices.SelectedItem;
try
{
selectedDevice.Open();
try
{
selectedDevice.SendSysEx(modeSysEx);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while sending the SysEx message to " + selectedDevice.Name + ".", "Error");
}
finally
{
selectedDevice.Close();
}
}
catch
{
MessageBox.Show("An error occurred while attempting to open " + selectedDevice.Name + ". Be sure another application isn't currently accessing it.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
/// <summary>
/// Load event handler for the form
/// </summary>
private void Form1_Load(object sender, EventArgs e)
{
lbDevices.Items.Clear();
foreach (OutputDevice device in OutputDevice.InstalledDevices)
{
lbDevices.Items.Add(device);
if (device.Name == "Akai APC20")
{
lbDevices.SelectedIndex = lbDevices.Items.Count - 1;
}
}
ButtonClickEventHandler = new EventHandler(APCButton_Click);
ButtonMouseDownEventHandler = new MouseEventHandler(APCButton_MouseDown);
SelectedOptionChangedEventHandler = new EventHandler(LEDButtonSelectedOptionChanged);
dlgOpen.FileOk += new CancelEventHandler(dlgOpen_FileOk);
dlgSave.FileOk += new CancelEventHandler(dlgSave_FileOk);
LoadLEDButtons();
}
/// <summary>
/// Click event handler for the Exit button
/// </summary>
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnClearAll_Click(object sender, EventArgs e)
{
var result = MessageBox.Show("Are you sure you want to clear all of the APC20 buttons?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
ClearAllButtons();
}
/// <summary>
/// event handler for when a selected option changed for <see cref="IAPCLEDButton"/>
/// </summary>
private void LEDButtonSelectedOptionChanged(object sender, EventArgs e)
{
var led = (IAPCLEDButton)sender;
var btn = led.Parent;
if (led.SelectedOption == APCLEDButtonOption.OFF)
{
btn.BackColor = SystemColors.Control;
btn.Text = "";
TurnOffLED(led.Channel, led.NoteNumber);
}
else
{
var option = led.SelectedOption;
btn.BackColor = option.Color;
btn.Text = option.IsBlinking ? "(B)" : "";
TurnOnLED(led, option);
}
}
/// <summary>
/// Sets all buttons back to default color
/// </summary>
private void ClearAllButtons()
{
APCLEDButton led;
foreach(string key in usedButtons.Keys)
{
usedButtons.TryGetValue(key, out Button button);
led = (APCLEDButton)button.Tag;
led.TurnOffButton();
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
dlgOpen.ShowDialog();
}
private void btnSave_Click(object sender, EventArgs e)
{
dlgSave.ShowDialog();
}
private void dlgSave_FileOk(object sender, CancelEventArgs e)
{
SaveConfig(dlgSave.FileName);
}
private void dlgOpen_FileOk(object sender, CancelEventArgs e)
{
OpenConfig(dlgOpen.FileName);
}
/// <summary>
/// method to persist the config to the specified filename
/// </summary>
private void SaveConfig(string filename)
{
try
{
var serializable_dict = usedButtons.ToDictionary(x => x.Key, x => (IAPCLEDButton)x.Value.Tag);
BinarySerialization.WriteToBinaryFile(filename, serializable_dict);
MessageBox.Show($"Config saved to {filename}.", "Config Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Error saving config to {filename}. {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// method to take persisted config and load into the GUI (and send to APC)
/// </summary>
private void OpenConfig(string filename)
{
var loadedButtons = BinarySerialization.ReadFromBinaryFile<Dictionary<string, IAPCLEDButton>>(filename);
foreach(string key in loadedButtons.Keys)
{
if (usedButtons.TryGetValue(key, out Button value))
{
loadedButtons.TryGetValue(key, out IAPCLEDButton loaded_value);
((IAPCLEDButton)value.Tag).SetSelectedOption(loaded_value.SelectedOptionIndex);
}
}
}
}
}