-
Notifications
You must be signed in to change notification settings - Fork 2
/
midi_controller.c
360 lines (304 loc) · 8.32 KB
/
midi_controller.c
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
// Pithesiser - a software synthesiser for Raspberry Pi
// Copyright (C) 2015 Nicholas Tuckett
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
* midi_controller.c
*
* Created on: 11 Feb 2013
* Author: ntuckett
*/
#include "midi_controller.h"
#include <memory.h>
#include "midi.h"
#include "setting.h"
static int read_midi_controller(midi_controller_t* controller, int* value)
{
int changed = 0;
if (controller->midi_cc[0] != -1)
{
if (controller->midi_cc[1] != -1)
{
int msb, lsb;
changed = midi_get_raw_controller_changed(controller->midi_channel, controller->midi_cc[0]);
msb = midi_get_raw_controller_value(controller->midi_channel, controller->midi_cc[0]);
changed |= midi_get_raw_controller_changed(controller->midi_channel, controller->midi_cc[1]);
lsb = midi_get_raw_controller_value(controller->midi_channel, controller->midi_cc[1]);
*value = lsb | msb << 7;
}
else if (midi_get_raw_controller_changed(controller->midi_channel, controller->midi_cc[0]))
{
*value = midi_get_raw_controller_value(controller->midi_channel, controller->midi_cc[0]);
changed = 1;
}
}
return changed;
}
static int process_continuous_controller(midi_controller_t* controller, int* changed)
{
int midi_value;
if (read_midi_controller(controller, &midi_value))
{
*changed = 1;
int midi_range = controller->midi_range.max - controller->midi_range.min;
if (midi_value < controller->midi_range.min)
{
midi_value = 0;
}
else if (midi_value > controller->midi_range.max)
{
midi_value = midi_range;
}
else
{
midi_value -= controller->midi_range.min;
}
int64_t controller_range = controller->output_max - controller->output_min;
controller->last_output = controller->output_min + ((controller_range * (int64_t)midi_value) / (int64_t)midi_range);
}
return controller->last_output;
}
static int relative_controller_delta(int midi_delta, midi_controller_t* controller)
{
int controller_delta;
if (controller->midi_cc[1] != -1)
{
const int SIGN_EXTEND_14BITS_M = 1U << 13;
controller_delta = (midi_delta ^ SIGN_EXTEND_14BITS_M) - SIGN_EXTEND_14BITS_M;
}
else
{
const int SIGN_EXTEND_7BITS_M = 1U << 6;
controller_delta = (midi_delta ^ SIGN_EXTEND_7BITS_M) - SIGN_EXTEND_7BITS_M;
}
return controller_delta * controller->delta_scale;
}
static int process_continuous_relative_controller(midi_controller_t* controller, int* changed)
{
int midi_delta;
if (read_midi_controller(controller, &midi_delta))
{
if (midi_delta != 0)
{
*changed = 1;
controller->last_output += relative_controller_delta(midi_delta, controller);
if (controller->last_output < controller->output_min)
{
controller->last_output = controller->output_min;
}
else if (controller->last_output > controller->output_max)
{
controller->last_output = controller->output_max;
}
}
}
return controller->last_output;
}
static int process_continuous_relative_controller_with_held(midi_controller_t* controller, int* changed)
{
int midi_delta;
if (read_midi_controller(controller, &midi_delta))
{
if (midi_delta != 0)
{
*changed = 1;
if (controller->last_output == controller->output_held)
{
controller->last_output = controller->output_max;
}
controller->last_output += relative_controller_delta(midi_delta, controller);
if (controller->last_output < controller->output_min)
{
controller->last_output = controller->output_min;
}
else if (controller->last_output > controller->output_max)
{
controller->last_output = controller->output_held;
}
}
}
return controller->last_output;
}
static int process_continuous_controller_with_held(midi_controller_t* controller, int* changed)
{
int midi_value;
if (read_midi_controller(controller, &midi_value))
{
*changed = 1;
int midi_range = controller->midi_range.max - controller->midi_range.min;
if (midi_value < controller->midi_range.min)
{
midi_value = controller->midi_range.min;
}
if (midi_value > controller->midi_range.max)
{
controller->last_output = controller->output_held;
}
else
{
midi_value -= controller->midi_range.min;
int controller_range = controller->output_max - controller->output_min;
controller->last_output = controller->output_min + ((controller_range * midi_value) / midi_range);
}
}
return controller->last_output;
}
static int process_toggle_controller(midi_controller_t* controller, int* changed)
{
int midi_value;
if (read_midi_controller(controller, &midi_value))
{
*changed = 1;
if (midi_value > controller->midi_threshold)
{
controller->last_output++;
if (controller->last_output > controller->output_max)
{
controller->last_output = controller->output_min;
}
}
}
return controller->last_output;
}
static int process_event_controller(midi_controller_t* controller, int* changed)
{
int midi_value;
if (read_midi_controller(controller, &midi_value))
{
if (midi_value > controller->midi_threshold && controller->last_output < controller->midi_threshold)
{
*changed = 1;
}
controller->last_output = midi_value;
}
if (*changed)
{
return controller->last_output;
}
else
{
return 0;
}
}
void midi_controller_create(midi_controller_t* controller, const char* name)
{
memset(controller, 0, sizeof(midi_controller_t));
controller->name = name;
controller->midi_cc[0] = -1;
controller->midi_cc[1] = -1;
controller->type = NONE;
controller->delta_scale = 1;
}
void midi_controller_init(midi_controller_t* controller)
{
controller->last_output = controller->output_min;
}
int midi_controller_update_and_read(midi_controller_t* controller, int* value)
{
int changed = 0;
if (controller->indexer_control != NULL)
{
if (midi_controller_read(controller->indexer_control) != controller->indexer_value)
{
return changed;
}
}
switch (controller->type)
{
case CONTINUOUS:
{
*value = process_continuous_controller(controller, &changed);
break;
}
case CONTINUOUS_RELATIVE:
{
*value = process_continuous_relative_controller(controller, &changed);
break;
}
case CONTINUOUS_WITH_HELD:
{
*value = process_continuous_controller_with_held(controller, &changed);
break;
}
case CONTINUOUS_RELATIVE_WITH_HELD:
{
*value = process_continuous_relative_controller_with_held(controller, &changed);
break;
}
case TOGGLE:
{
*value = process_toggle_controller(controller, &changed);
break;
}
case EVENT:
{
*value = process_event_controller(controller, &changed);
break;
}
default:
{
break;
}
}
return changed;
}
int midi_controller_update(midi_controller_t* controller)
{
int dummy_value;
return midi_controller_update_and_read(controller, &dummy_value);
}
int midi_controller_read(midi_controller_t* controller)
{
return controller->last_output;
}
void midi_controller_set_setting(midi_controller_t* controller, setting_t* setting)
{
int value = midi_controller_read(controller);
setting_set_value_int(setting, value);
}
#define MAX_INDEX_CONTROLS 16
static midi_controller_t index_controls[MAX_INDEX_CONTROLS];
static int next_free_index_control = 0;
midi_controller_t* midi_controller_new_index_control(const char* name)
{
if (next_free_index_control < MAX_INDEX_CONTROLS)
{
midi_controller_t* index_control = index_controls + next_free_index_control;
next_free_index_control++;
midi_controller_create(index_control, name);
return index_control;
}
else
{
return NULL;
}
}
midi_controller_t* midi_controller_find_index_control(const char* name)
{
for (int i = 0; i < next_free_index_control; i++)
{
if (strcasecmp(name, index_controls[i].name) == 0)
{
return index_controls + i;
}
}
return NULL;
}
void midi_controller_update_index_controls()
{
for (int i = 0; i < next_free_index_control; i++)
{
midi_controller_update(index_controls + i);
}
}