-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEncoderRule.cs
243 lines (241 loc) · 5.62 KB
/
EncoderRule.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
using org.dmxc.lumos.Kernel.Input;
using org.dmxc.lumos.Kernel.Resource;
using System;
using System.Xml.Linq;
using System.Globalization;
namespace MidiPlugin
{
[FriendlyName("Encoder")]
public class EncoderRule : DeviceRule
{
private static NumberFormatInfo nfi = new NumberFormatInfo { NumberDecimalSeparator = "." };
private const string nolearn = "LearnMode disabled.";
private const string learn1 = "Turn the encoder clockwise.";
private const string learn2 = "Turn the encoder counter clockwise.";
private double value;
private bool first = true;
private MidiInputChannel c = null;
public override event EventHandler LearningFinished;
public MidiMessage CWMessage
{
get;
set;
}
public MidiMessage MinimumBacktrack
{
get;
set;
}
public MidiMessage CCWMessage
{
get;
set;
}
public MidiMessage MaximumBacktrack
{
get;
set;
}
public double Increment
{
get;
set;
}
public override string ControlType
{
get
{
return "Encoder";
}
}
public override double Value
{
get
{
return this.value;
}
set
{
this.value = value;
if (this.value > 1.0)
{
this.value = 1.0;
}
if (this.value < 0.0)
{
this.value = 0.0;
}
this.UpdateBacktrack();
}
}
public override string LearnStatus
{
get;
protected set;
}
public bool LearnMode
{
get;
private set;
}
public EncoderRule()
{
this.LearnStatus = nolearn;
}
public override void Init(ManagedTreeItem i)
{
base.Init(i);
if (i.hasValue<double>("Increment"))
{
this.Increment = i.getValue<double>("Increment");
}
if (i.hasValue<int>("CWMessage"))
{
this.CWMessage = new MidiMessage
{
Data = i.getValue<int>("CWMessage")
};
}
if (i.hasValue<int>("MinimumBacktrack"))
{
this.MinimumBacktrack = new MidiMessage
{
Data = i.getValue<int>("MinimumBacktrack")
};
}
if (i.hasValue<int>("CCWMessage"))
{
this.CCWMessage = new MidiMessage
{
Data = i.getValue<int>("CCWMessage")
};
}
if (i.hasValue<int>("MaximumBacktrack"))
{
this.MaximumBacktrack = new MidiMessage
{
Data = i.getValue<int>("MaximumBacktrack")
};
}
if (i.hasValue<double>("Value"))
{
this.Value = i.getValue<double>("Value");
}
}
public override void Save(ManagedTreeItem i)
{
ContextManager.log.Debug("Saving EncoderRule {0}, {1}, {2}", CWMessage.Data, CCWMessage.Data, Increment);
base.Save(i);
i.setValue<double>("Value", this.Value);
i.setValue<double>("Increment", this.Increment);
i.setValue<int>("CWMessage", this.CWMessage.Data);
i.setValue<int>("MinimumBacktrack", this.MinimumBacktrack.Data);
i.setValue<int>("CCWMessage", this.CCWMessage.Data);
i.setValue<int>("MaximumBacktrack", this.MaximumBacktrack.Data);
}
protected override void Serialize(XElement item)
{
item.Add(new XAttribute("Increment", this.Increment.ToString(nfi)));
item.Add(new XAttribute("CWMessage", this.CWMessage.Data));
item.Add(new XAttribute("MinimumBacktrack", this.MinimumBacktrack.Data));
item.Add(new XAttribute("CCWMessage", this.CCWMessage.Data));
item.Add(new XAttribute("MaximumBacktrack", this.MaximumBacktrack.Data));
}
protected override void Deserialize(XElement item)
{
this.Increment = double.Parse(item.Attribute("Increment").Value, nfi);
this.CWMessage = new MidiMessage { Data = int.Parse(item.Attribute("CWMessage").Value) };
this.MinimumBacktrack = new MidiMessage { Data = int.Parse(item.Attribute("MinimumBacktrack").Value) };
this.CCWMessage = new MidiMessage { Data = int.Parse(item.Attribute("CCWMessage").Value) };
this.MaximumBacktrack = new MidiMessage { Data = int.Parse(item.Attribute("MaximumBacktrack").Value) };
}
public override void Process(MidiMessage m)
{
if (m.Equals(this.CWMessage))
{
this.Value += this.Increment;
base.OnValueChanged();
}
if (m.Equals(this.CCWMessage))
{
this.Value -= this.Increment;
base.OnValueChanged();
}
}
public override void UpdateBacktrack()
{
int delta = (int)(this.MaximumBacktrack.data2 - this.MinimumBacktrack.data2);
MidiMessage msg = this.MaximumBacktrack;
msg.data2 = (byte)((double)this.MinimumBacktrack.data2 + (double)delta * this.Value);
base.OnSendMessage(msg);
}
public override void BeginLearn()
{
this.LearnMode = true;
this.first = true;
this.LearnStatus = learn1;
}
public override void CancelLearn()
{
this.EndLearn();
}
private void EndLearn()
{
this.LearnMode = false;
this.LearnStatus = nolearn;
if (this.LearningFinished != null)
{
this.LearningFinished(this, EventArgs.Empty);
}
}
public override bool TryLearnMessage(MidiMessage m)
{
bool result;
if (!this.LearnMode)
{
result = false;
}
else
{
if (this.first)
{
this.CWMessage = m;
this.first = false;
this.LearnStatus = learn2;
if (base.UseBacktrack)
{
MidiMessage m2 = m;
m2.data2 = 0;
this.MinimumBacktrack = m2;
}
}
else
{
if (m == this.CWMessage)
{
result = false;
return result;
}
MidiMessage m2 = m;
m2.data2 = 127;
this.CCWMessage = m;
if (base.UseBacktrack)
{
this.MaximumBacktrack = m2;
}
this.EndLearn();
}
result = true;
}
return result;
}
public override MidiInputChannel GetInputChannel(IInputLayer parent)
{
if (this.c == null)
{
this.c = new MidiRangeInputChannel(parent, this);
}
return this.c;
}
}
}