-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidiConnection.cs
232 lines (210 loc) · 8.15 KB
/
MidiConnection.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
using Melanchall.DryWetMidi.Common;
using Melanchall.DryWetMidi.Core;
using Melanchall.DryWetMidi.Devices;
using System;
using System.Collections.Generic;
using System.Reactive.Subjects;
namespace FSKontrol.WPF
{
/*
* Lots of information here on controlling the LEDs:
* https://stackoverflow.com/questions/39435550/changing-leds-on-x-touch-mini-mackie-control-mc-mode
*/
class MidiConnection
{
private InputDevice inputDevice;
private OutputDevice outputDevice;
private List<MidiControlAdaptor> controlAdaptors = new List<MidiControlAdaptor>();
public MidiConnection()
{
inputDevice = InputDevice.GetByName("X-TOUCH MINI");
inputDevice.EventReceived += OnEventReceived;
inputDevice.StartEventsListening();
outputDevice = OutputDevice.GetByName("X-TOUCH MINI");
}
private void OnEventReceived(object sender, MidiEventReceivedEventArgs e)
{
var midiDevice = (MidiDevice)sender;
Console.WriteLine($"Event received from '{midiDevice.Name}' at {DateTime.Now}: {e.Event}");
var map = MapEventToControl(e.Event);
Console.WriteLine($"Mapped to {map.MidiControlType}:{map.ControlId}");
foreach (var controlAdaptor in controlAdaptors)
{
if (!controlAdaptor.MatchesEvent(map)) continue;
controlAdaptor.HandleEvent(e.Event);
break;
}
}
public MidiControlAdaptor CreateAdaptor(MidiControlType controlType, int controlId, Field field, FsConnection fsConnection)
{
var adaptor = new MidiControlAdaptor(controlType, controlId, field, fsConnection);
controlAdaptors.Add(adaptor);
adaptor.LightControl.Subscribe(HandleLightControl);
return adaptor;
}
private void HandleLightControl(LightControlMessage message)
{
var controlNumber = MapControlToLedControlNumber(message.Adaptor.ControlType, message.Adaptor.ControlId);
var midi = new ControlChangeEvent((SevenBitNumber)controlNumber, (SevenBitNumber)message.ControlValue);
Console.WriteLine($"Sending midi: {midi}");
outputDevice.SendEvent(midi);
}
private ControlEventMap MapEventToControl(MidiEvent evt)
{
MidiControlType controlType = MidiControlType.Fader;
int controlId = 0;
if (evt.EventType == MidiEventType.PitchBend)
{
controlType = MidiControlType.Fader;
controlId = 0;
}
else if (evt.EventType == MidiEventType.ControlChange)
{
var cc = (ControlChangeEvent)evt;
if (cc.ControlNumber <= 23 && cc.ControlNumber >= 16)
{
controlType = MidiControlType.Encoder;
controlId = cc.ControlNumber - 16;
}
}
return new ControlEventMap(controlType, controlId);
}
private int MapControlToLedControlNumber(MidiControlType controlType, int controlId)
{
switch (controlType)
{
case MidiControlType.Encoder:
return controlId + 48;
default:
return 0;
}
}
}
class ControlEventMap
{
public ControlEventMap(MidiControlType midiControlType, int controlId)
{
MidiControlType = midiControlType;
ControlId = controlId;
}
public MidiControlType MidiControlType { get; }
public int ControlId { get; }
}
class MidiControlAdaptor
{
public MidiControlAdaptor(MidiControlType controlType, int controlId, Field field, FsConnection fsConnection)
{
ControlType = controlType;
ControlId = controlId;
Field = field;
FsConnection = fsConnection;
UnitType = FsConnection.FieldDefinition(field).UnitType;
FsConnection.Subscribe(field).Subscribe(p => HandleValueChange(p));
// TODO: Make sure the lights are initialised
}
public void HandleEvent(MidiEvent evt)
{
Console.WriteLine($"Got event: {evt.EventType}");
switch (evt.EventType)
{
case MidiEventType.PitchBend:
var pitchBend = (PitchBendEvent)evt;
var value = MidiUnitConverter.ConvertFromPitch(UnitType, pitchBend.PitchValue);
Console.WriteLine($"Pitch bend value: {pitchBend.PitchValue} -> {value}");
FsConnection.SetValue(Field, value);
break;
case MidiEventType.ControlChange:
// TODO: How to control the sensitivity?
var cc = (ControlChangeEvent)evt;
var sensitivity = encoderSensitivity[UnitType];
var ccValue = FsConnection.GetValue(Field) + MidiUnitConverter.ConvertFromControlChange(UnitType, cc.ControlValue) * sensitivity;
Console.WriteLine($"ccValue: {ccValue}");
FsConnection.SetValue(Field, ccValue);
break;
}
}
public MidiControlType ControlType { get; }
public int ControlId { get; }
public Field Field { get; }
public UnitType UnitType { get; }
public FsConnection FsConnection { get; }
public SimControlAdaptor SimAdaptor { get; }
private Subject<LightControlMessage> lightControl = new Subject<LightControlMessage>();
public IObservable<LightControlMessage> LightControl { get { return lightControl; } }
private void HandleValueChange(double value)
{
var controlValue = MidiLed.CreateLedMessage(value, UnitType);
lightControl.OnNext(new LightControlMessage(this, controlValue));
}
public bool MatchesEvent(ControlEventMap eventMap)
{
return ControlType == eventMap.MidiControlType && ControlId == eventMap.ControlId;
}
Dictionary<UnitType, double> encoderSensitivity = new Dictionary<UnitType, double>
{
{ UnitType.Radians, 0.005 },
{ UnitType.Degrees, 1 },
{ UnitType.Percent, 0.01 }
};
}
class MidiUnitConverter
{
public static double ConvertFromPitch(UnitType unitType, int value)
{
switch (unitType)
{
case UnitType.Percent:
return value / 16256.0;
default:
return double.NaN;
}
}
public static double ConvertFromControlChange(UnitType unitType, int value)
{
if (value <= 64)
{
return value;
}
else
{
return -(value - 64);
}
}
}
class MidiLed
{
public static uint CreateLedMessage(double value, UnitType unitType)
{
switch (unitType)
{
case UnitType.Radians:
return Convert.ToUInt32(22 + value * 5);
case UnitType.Percent:
return Convert.ToUInt32(33 + value * 10);
case UnitType.Degrees:
if (value >= 0 && value < 135 - 22.5 / 2 || value >= 360 - 22.5 / 2)
{
return Convert.ToUInt32(6 + value / 22.5);
}
if (value > 270 - 1.5 * 22.5 && value < 360 - 22.5 / 2)
{
var tmp = 1 + (value - (270 - 22.5)) / 22.5;
return Convert.ToUInt32(tmp);
}
return 0;
default:
return 0;
}
}
}
class LightControlMessage
{
public LightControlMessage(MidiControlAdaptor adaptor, uint controlValue)
{
Adaptor = adaptor;
ControlValue = controlValue;
}
public MidiControlAdaptor Adaptor { get; }
public uint ControlValue { get; }
}
}