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
|
#include <string.h>
#include <alsa/asoundlib.h>
#include "midi.h"
#define BACKEND_NAME "midi"
static snd_seq_t* sequencer = NULL;
typedef union {
struct {
uint8_t pad[5];
uint8_t type;
uint8_t channel;
uint8_t control;
} fields;
uint64_t label;
} midi_channel_ident;
enum /*_midi_channel_type*/ {
none = 0,
note,
cc,
pressure,
aftertouch,
pitchbend,
nrpn,
sysmsg
};
static struct {
uint8_t detect;
} midi_config = {
.detect = 0
};
int init(){
backend midi = {
.name = BACKEND_NAME,
.conf = midi_configure,
.create = midi_instance,
.conf_instance = midi_configure_instance,
.channel = midi_channel,
.handle = midi_set,
.process = midi_handle,
.start = midi_start,
.shutdown = midi_shutdown
};
if(snd_seq_open(&sequencer, "default", SND_SEQ_OPEN_DUPLEX, 0) < 0){
fprintf(stderr, "Failed to open ALSA sequencer\n");
return 1;
}
//register backend
if(mm_backend_register(midi)){
fprintf(stderr, "Failed to register MIDI backend\n");
return 1;
}
snd_seq_nonblock(sequencer, 1);
fprintf(stderr, "MIDI client ID is %d\n", snd_seq_client_id(sequencer));
return 0;
}
static int midi_configure(char* option, char* value){
if(!strcmp(option, "name")){
if(snd_seq_set_client_name(sequencer, value) < 0){
fprintf(stderr, "Failed to set MIDI client name to %s\n", value);
return 1;
}
return 0;
}
if(!strcmp(option, "detect")){
midi_config.detect = 1;
if(!strcmp(value, "off")){
midi_config.detect = 0;
}
return 0;
}
fprintf(stderr, "Unknown MIDI backend option %s\n", option);
return 1;
}
static instance* midi_instance(){
instance* inst = mm_instance();
if(!inst){
return NULL;
}
inst->impl = calloc(1, sizeof(midi_instance_data));
if(!inst->impl){
fprintf(stderr, "Failed to allocate memory\n");
return NULL;
}
return inst;
}
static int midi_configure_instance(instance* instance, char* option, char* value){
midi_instance_data* data = (midi_instance_data*) instance->impl;
//FIXME maybe allow connecting more than one device
if(!strcmp(option, "read")){
//connect input device
if(data->read){
fprintf(stderr, "MIDI port already connected to an input device\n");
return 1;
}
data->read = strdup(value);
return 0;
}
else if(!strcmp(option, "write")){
//connect output device
if(data->write){
fprintf(stderr, "MIDI port already connected to an output device\n");
return 1;
}
data->write = strdup(value);
return 0;
}
fprintf(stderr, "Unknown MIDI instance option %s\n", option);
return 1;
}
static channel* midi_channel(instance* instance, char* spec){
midi_channel_ident ident = {
.label = 0
};
//support deprecated syntax for a transition period...
uint8_t old_syntax = 0;
char* channel;
if(!strncmp(spec, "ch", 2)){
channel = spec + 2;
if(!strncmp(spec, "channel", 7)){
channel = spec + 7;
}
}
else if(!strncmp(spec, "cc", 2)){
ident.fields.type = cc;
channel = spec + 2;
old_syntax = 1;
}
else if(!strncmp(spec, "note", 4)){
ident.fields.type = note;
channel = spec + 4;
old_syntax = 1;
}
else if(!strncmp(spec, "nrpn", 4)){
ident.fields.type = nrpn;
channel = spec + 4;
old_syntax = 1;
}
else{
fprintf(stderr, "Unknown MIDI channel specification %s\n", spec);
return NULL;
}
ident.fields.channel = strtoul(channel, &channel, 10);
if(ident.fields.channel > 15){
fprintf(stderr, "MIDI channel out of range in channel spec %s\n", spec);
return NULL;
}
if(*channel != '.'){
fprintf(stderr, "Need MIDI channel specification of form channel<X>.<control><Y>, had %s\n", spec);
return NULL;
}
//skip the period
channel++;
if(!old_syntax){
if(!strncmp(channel, "cc", 2)){
ident.fields.type = cc;
channel += 2;
}
else if(!strncmp(channel, "note", 4)){
ident.fields.type = note;
channel += 4;
}
else if(!strncmp(channel, "nrpn", 4)){
ident.fields.type = nrpn;
channel += 4;
}
else if(!strncmp(channel, "pressure", 8)){
ident.fields.type = pressure;
channel += 8;
}
else if(!strncmp(channel, "pitch", 8)){
ident.fields.type = pitchbend;
}
else if(!strncmp(channel, "aftertouch", 10)){
ident.fields.type = aftertouch;
}
}
ident.fields.control = strtoul(channel, NULL, 10);
if(ident.label){
return mm_channel(instance, ident.label, 1);
}
return NULL;
}
static int midi_set(instance* inst, size_t num, channel** c, channel_value* v){
size_t u;
snd_seq_event_t ev;
midi_instance_data* data;
midi_channel_ident ident = {
.label = 0
};
for(u = 0; u < num; u++){
data = (midi_instance_data*) c[u]->instance->impl;
ident.label = c[u]->ident;
snd_seq_ev_clear(&ev);
snd_seq_ev_set_source(&ev, data->port);
snd_seq_ev_set_subs(&ev);
snd_seq_ev_set_direct(&ev);
switch(ident.fields.type){
case note:
snd_seq_ev_set_noteon(&ev, ident.fields.channel, ident.fields.control, v[u].normalised * 127.0);
break;
case cc:
snd_seq_ev_set_controller(&ev, ident.fields.channel, ident.fields.control, v[u].normalised * 127.0);
break;
case pressure:
snd_seq_ev_set_keypress(&ev, ident.fields.channel, ident.fields.control, v[u].normalised * 127.0);
break;
case pitchbend:
snd_seq_ev_set_pitchbend(&ev, ident.fields.channel, (v[u].normalised * 16383.0) - 8192);
break;
case aftertouch:
snd_seq_ev_set_chanpress(&ev, ident.fields.channel, v[u].normalised * 127.0);
break;
case nrpn:
//FIXME set to nrpn output
break;
}
snd_seq_event_output(sequencer, &ev);
}
snd_seq_drain_output(sequencer);
return 0;
}
static int midi_handle(size_t num, managed_fd* fds){
snd_seq_event_t* ev = NULL;
instance* inst = NULL;
channel* changed = NULL;
channel_value val;
char* event_type = NULL;
midi_channel_ident ident = {
.label = 0
};
if(!num){
return 0;
}
while(snd_seq_event_input(sequencer, &ev) > 0){
event_type = NULL;
ident.label = 0;
switch(ev->type){
case SND_SEQ_EVENT_NOTEON:
case SND_SEQ_EVENT_NOTEOFF:
case SND_SEQ_EVENT_NOTE:
ident.fields.type = note;
ident.fields.channel = ev->data.note.channel;
ident.fields.control = ev->data.note.note;
val.normalised = (double)ev->data.note.velocity / 127.0;
event_type = "note";
break;
case SND_SEQ_EVENT_KEYPRESS:
ident.fields.type = pressure;
ident.fields.channel = ev->data.note.channel;
ident.fields.control = ev->data.note.note;
val.normalised = (double)ev->data.note.velocity / 127.0;
event_type = "pressure";
break;
case SND_SEQ_EVENT_CHANPRESS:
ident.fields.type = aftertouch;
ident.fields.channel = ev->data.control.channel;
val.normalised = (double)ev->data.control.value / 127.0;
event_type = "aftertouch";
break;
case SND_SEQ_EVENT_PITCHBEND:
ident.fields.type = pitchbend;
ident.fields.channel = ev->data.control.channel;
val.normalised = ((double)ev->data.control.value + 8192) / 16383.0;
event_type = "pitch";
break;
case SND_SEQ_EVENT_CONTROLLER:
ident.fields.type = cc;
ident.fields.channel = ev->data.control.channel;
ident.fields.control = ev->data.control.param;
val.raw.u64 = ev->data.control.value;
val.normalised = (double)ev->data.control.value / 127.0;
event_type = "cc";
break;
case SND_SEQ_EVENT_CONTROL14:
case SND_SEQ_EVENT_NONREGPARAM:
case SND_SEQ_EVENT_REGPARAM:
//FIXME value calculation
ident.fields.type = nrpn;
ident.fields.channel = ev->data.control.channel;
ident.fields.control = ev->data.control.param;
break;
default:
fprintf(stderr, "Ignored MIDI event of unsupported type\n");
continue;
}
inst = mm_instance_find(BACKEND_NAME, ev->dest.port);
if(!inst){
//FIXME might want to return failure
fprintf(stderr, "Delivered MIDI event did not match any instance\n");
continue;
}
changed = mm_channel(inst, ident.label, 0);
if(changed){
if(mm_channel_event(changed, val)){
free(ev);
return 1;
}
}
if(midi_config.detect && event_type){
if(ident.fields.type == pitchbend || ident.fields.type == aftertouch){
fprintf(stderr, "Incoming MIDI data on channel %s.ch%d.%s\n", inst->name, ident.fields.channel, event_type);
}
else{
fprintf(stderr, "Incoming MIDI data on channel %s.ch%d.%s%d\n", inst->name, ident.fields.channel, event_type, ident.fields.control);
}
}
}
free(ev);
return 0;
}
static int midi_start(){
size_t n, p;
int nfds, rv = 1;
struct pollfd* pfds = NULL;
instance** inst = NULL;
midi_instance_data* data = NULL;
snd_seq_addr_t addr;
if(mm_backend_instances(BACKEND_NAME, &n, &inst)){
fprintf(stderr, "Failed to fetch instance list\n");
return 1;
}
//if there are no ports, do nothing
if(!n){
free(inst);
return 0;
}
//create all ports
for(p = 0; p < n; p++){
data = (midi_instance_data*) inst[p]->impl;
data->port = snd_seq_create_simple_port(sequencer, inst[p]->name, SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE | SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ, SND_SEQ_PORT_TYPE_MIDI_GENERIC);
inst[p]->ident = data->port;
//make connections
if(data->write){
if(snd_seq_parse_address(sequencer, &addr, data->write) == 0){
fprintf(stderr, "Connecting output of instance %s to MIDI device %s (%d:%d)\n", inst[p]->name, data->write, addr.client, addr.port);
snd_seq_connect_to(sequencer, data->port, addr.client, addr.port);
}
else{
fprintf(stderr, "Failed to get destination MIDI device address: %s\n", data->write);
}
free(data->write);
data->write = NULL;
}
if(data->read){
if(snd_seq_parse_address(sequencer, &addr, data->read) == 0){
fprintf(stderr, "Connecting input from MIDI device %s to instance %s (%d:%d)\n", data->read, inst[p]->name, addr.client, addr.port);
snd_seq_connect_from(sequencer, data->port, addr.client, addr.port);
}
else{
fprintf(stderr, "Failed to get source MIDI device address: %s\n", data->read);
}
free(data->read);
data->read = NULL;
}
}
//register all fds to core
nfds = snd_seq_poll_descriptors_count(sequencer, POLLIN | POLLOUT);
pfds = calloc(nfds, sizeof(struct pollfd));
if(!pfds){
fprintf(stderr, "Failed to allocate memory\n");
goto bail;
}
nfds = snd_seq_poll_descriptors(sequencer, pfds, nfds, POLLIN | POLLOUT);
fprintf(stderr, "MIDI backend registering %d descriptors to core\n", nfds);
for(p = 0; p < nfds; p++){
if(mm_manage_fd(pfds[p].fd, BACKEND_NAME, 1, NULL)){
goto bail;
}
}
rv = 0;
bail:
free(pfds);
free(inst);
return rv;
}
static int midi_shutdown(){
size_t n, p;
instance** inst = NULL;
midi_instance_data* data = NULL;
if(mm_backend_instances(BACKEND_NAME, &n, &inst)){
fprintf(stderr, "Failed to fetch instance list\n");
return 1;
}
for(p = 0; p < n; p++){
data = (midi_instance_data*) inst[p]->impl;
free(data->read);
free(data->write);
data->read = NULL;
data->write = NULL;
free(inst[p]->impl);
}
free(inst);
//close midi
snd_seq_close(sequencer);
sequencer = NULL;
//free configuration cache
snd_config_update_free_global();
fprintf(stderr, "MIDI backend shut down\n");
return 0;
}
|