summaryrefslogtreecommitdiff
path: root/x1k2-midi-osc.c
blob: e82185a3d6b89aa47ce2e032331d1e0072974b01 (plain)
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
 * x1k2-midi-osc.c
 *
 * (c) 2023 Thomas White <taw@bitwiz.me.uk>
 *
 * X1K2-midi-osc 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.
 *
 * X1K2-midi-osc 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 X1K2-midi-osc.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

#include <alsa/asoundlib.h>
#include <lo/lo.h>


struct faderpot
{
	int id;
	int cc_val;
	int physical_value;
	int physical_value_known;
	int enabled;
	int congruent;
	int pickup_value;
	int has_button_and_led;
	int button;
	int led_green;
	int led_orange;
	int led_red;
	const char *type;
};


struct encoder
{
	int id;
	int cc_val;
	int fine_button;
	int fine_active;
	int has_led;
	int led_green;
	int led_orange;
	int led_red;
};


struct button
{
	const char *name;
	int note;
	int led_green;
	int led_orange;
	int led_red;
};


struct button buttons[18];
struct encoder encoders[6];
struct faderpot faders[4];
struct faderpot potentiometers[12];
int n_buttons = 18;
int n_encoders = 6;
int n_faders = 4;
int n_potentiometers = 12;

snd_rawmidi_t *midi_out;


static void send_note_on(snd_rawmidi_t *midi_out, int note)
{
	unsigned char sbuf[3];
	ssize_t r;
	sbuf[0] = 0x9e;
	sbuf[1] = note;
	sbuf[2] = 127;
	r = snd_rawmidi_write(midi_out, sbuf, 3);
	if ( r != 3 ) {
		printf("snd_rawmidi_write said %li\n", r);
	}
	usleep(1000);
}


static void send_note_off(snd_rawmidi_t *midi_out, int note)
{
	unsigned char sbuf[3];
	ssize_t r;
	sbuf[0] = 0x8e;
	sbuf[1] = note;
	sbuf[2] = 0;
	r = snd_rawmidi_write(midi_out, sbuf, 3);
	if ( r != 3 ) {
		printf("snd_rawmidi_write said %li\n", r);
	}
	usleep(1000);
}


static void init_fader(struct faderpot *fad, int id, int cc_val)
{
	fad->id = id;
	fad->cc_val = cc_val;
	fad->physical_value = 0;
	fad->physical_value_known = 0;
	fad->pickup_value = 0;
	fad->has_button_and_led = 0;
	fad->enabled = 1;
	fad->type = "faders";
}


static void init_potentiometer(struct faderpot *fad, int id, int cc_val,
                               int button)
{
	fad->id = id;
	fad->cc_val = cc_val;
	fad->physical_value = 0;
	fad->physical_value_known = 0;
	fad->pickup_value = 0;
	fad->congruent = 0;
	fad->has_button_and_led = 1;
	fad->button = button;
	fad->led_red = button;
	fad->led_orange = button+36;
	fad->led_green = button+72;
	fad->enabled = 0;
	fad->type = "potentiometers";
}


static void faderpot_note_on(struct faderpot *fad)
{
	/* Ignored for now */
}


static void faderpot_note_off(struct faderpot *fad)
{
	/* Ignored for now */
}


static int in_range(int a, int val1, int val2)
{
	return ((a>=val1) && (a<=val2)) || ((a>=val2) && (a<=val1));
}


static void pot_set_led(struct faderpot *fad)
{
	if ( fad->has_button_and_led ) {
		if ( fad->enabled ) {
			if ( fad->congruent ) {
				send_note_on(midi_out, fad->led_green);
			} else {
				send_note_on(midi_out, fad->led_orange);
			}
		} else {
			send_note_off(midi_out, fad->led_green);
		}
	}
}


static void faderpot_cc(struct faderpot *fad, int val, lo_address osc_send_addr)
{
	if ( !fad->congruent ) {
		int inr = in_range(fad->pickup_value, fad->physical_value, val);
		if ( fad->physical_value_known && inr ) fad->congruent = 1;

		/* Special case for fader coming from zero, where we didn't
		 * know its previous position. */
		if ( (fad->pickup_value == 0) && (val <= 2) ) fad->congruent = 1;
	}

	if ( fad->enabled ) {

		pot_set_led(fad);

		if ( fad->congruent ) {
			char tmp[64];
			snprintf(tmp, 64, "/x1k2/%s/%i/value-change", fad->type, fad->id);
			lo_send(osc_send_addr, tmp, "i", val);
			printf("sending %s = %i\n", tmp, val);
		}

	}

	fad->physical_value = val;
	fad->physical_value_known = 1;
}


static int pot_set_pickup_handler(const char *path, const char *types,
                                  lo_arg **argv, int argc,
                                  lo_message msg, void *vp)
{
	struct faderpot *fad = vp;

	fad->pickup_value = argv[0]->i;

	if ( fad->physical_value_known
	  && (fad->pickup_value == fad->physical_value) )
	{
		fad->congruent = 1;
	} else {
		fad->congruent = 0;
	}

	pot_set_led(fad);

	return 1;
}


static int pot_enable_handler(const char *path, const char *types,
                              lo_arg **argv, int argc,
                              lo_message msg, void *vp)
{
	struct faderpot *fad = vp;
	fad->enabled = 1;
	pot_set_led(fad);
	return 1;
}


static int pot_disable_handler(const char *path, const char *types,
                               lo_arg **argv, int argc,
                               lo_message msg, void *vp)
{
	struct faderpot *fad = vp;

	fad->enabled = 0;

	if ( fad->has_button_and_led ) {
		send_note_off(midi_out, fad->led_green);
	}

	return 1;
}


static void add_faderpot_methods(struct faderpot *fad, lo_server osc_server)
{
	char tmp[256];

	snprintf(tmp, 255, "/x1k2/%s/%i/set-pickup", fad->type, fad->id);
	lo_server_add_method(osc_server, tmp, "i", pot_set_pickup_handler, fad);

	snprintf(tmp, 255, "/x1k2/%s/%i/enable", fad->type, fad->id);
	lo_server_add_method(osc_server, tmp, "", pot_enable_handler, fad);

	snprintf(tmp, 255, "/x1k2/%s/%i/disable",  fad->type, fad->id);
	lo_server_add_method(osc_server, tmp, "", pot_disable_handler, fad);
}


static void init_encoder_noled(struct encoder *enc, int id, int cc_val,
                               int fine_button)
{
	enc->id = id;
	enc->cc_val = cc_val;
	enc->fine_button = fine_button;
	enc->fine_active = 0;
	enc->has_led = 0;
}


static void init_encoder(struct encoder *enc, int id, int cc_val,
                         int fine_button)
{
	enc->id = id;
	enc->cc_val = cc_val;
	enc->fine_button = fine_button;
	enc->fine_active = 0;
	enc->has_led = 1;
	enc->led_red = fine_button;
	enc->led_orange = fine_button+36;
	enc->led_green = fine_button+72;
}


static int enc_set_led_handler(const char *path, const char *types,
                               lo_arg **argv, int argc,
                               lo_message msg, void *vp)
{
	struct encoder *enc = vp;
	if ( strcmp("red", &argv[0]->s) == 0 ) {
		send_note_on(midi_out, enc->led_red);
	} else if ( strcmp("orange", &argv[0]->s) == 0 ) {
		send_note_on(midi_out, enc->led_orange);
	} else if ( strcmp("green", &argv[0]->s) == 0 ) {
		send_note_on(midi_out, enc->led_green);
	} else if ( strcmp("off", &argv[0]->s) == 0 ) {
		send_note_off(midi_out, enc->led_red);
	} else {
		fprintf(stderr, "Unrecognised LED mode '%s'\n", &argv[0]->s);
	}

	return 1;
}


static void encoder_note_on(struct encoder *enc)
{
	enc->fine_active = 1;
}


static void encoder_note_off(struct encoder *enc)
{
	enc->fine_active = 0;
}


static void encoder_cc(struct encoder *enc, int val, lo_address osc_send_addr)
{
	char tmp[256];
	const char *v;

	if ( val == 1 ) {
		v = "inc";
	} else if ( val == 127 ) {
		v = "dec";
	} else {
		fprintf(stderr, "Invalid encoder value %i\n", val);
		return;
	}

	snprintf(tmp, 255, "/x1k2/encoders/%i/%s%s",
	         enc->id, v, enc->fine_active ? "-fine" : "");
	lo_send(osc_send_addr, tmp, "");
}


static void add_encoder_methods(struct encoder *enc, lo_server osc_server)
{
	char tmp[256];
	snprintf(tmp, 255, "/x1k2/encoders/%i/set-led", enc->id);
	lo_server_add_method(osc_server, tmp, "s", enc_set_led_handler, enc);
}


static void init_button_full(struct button *but, const char *name, int note,
                             int r, int o, int g)
{
	but->name = name;
	but->note = note;
	but->led_red = r;
	but->led_orange = o;
	but->led_green = g;
}


static void init_button(struct button *but, const char *name, int note)
{
	init_button_full(but, name, note, note, note+36, note+72);
}


static void button_note_off(struct button *but, lo_address osc_send_addr)
{
}


static void button_note_on(struct button *but, lo_address osc_send_addr)
{
	char tmp[256];
	snprintf(tmp, 255, "/x1k2/buttons/%s/press", but->name);
	lo_send(osc_send_addr, tmp, "");
}


static int button_set_led_handler(const char *path, const char *types,
                                  lo_arg **argv, int argc,
                                  lo_message msg, void *vp)
{
	struct button *but = vp;
	if ( strcmp("red", &argv[0]->s) == 0 ) {
		send_note_on(midi_out, but->led_red);
	} else if ( strcmp("orange", &argv[0]->s) == 0 ) {
		send_note_on(midi_out, but->led_orange);
	} else if ( strcmp("green", &argv[0]->s) == 0 ) {
		send_note_on(midi_out, but->led_green);
	} else if ( strcmp("off", &argv[0]->s) == 0 ) {

		/* Usually, turning off any one of the colours turns off the
		 * LED, regardless of the current colour.  However, the bottom
		 * left button's LED is weird, I think because it's
		 * also the "layer" button.  It can only be switched off
		 * from the same colour.  So, we force it to be red. */
		if ( but->led_red == 12 ) {
			send_note_on(midi_out, but->led_red);
		}
		send_note_off(midi_out, but->led_red);
	} else {
		fprintf(stderr, "Unrecognised LED mode '%s'\n", &argv[0]->s);
	}

	return 1;
}


static void add_button_methods(struct button *but, lo_server osc_server)
{
	char tmp[256];
	snprintf(tmp, 255, "/x1k2/buttons/%s/set-led", but->name);
	lo_server_add_method(osc_server, tmp, "s", button_set_led_handler, but);
}


static void show_help(const char *s)
{
	printf("Syntax: %s [-h] [-d /dev/snd/midiXXXX]\n\n", s);
	printf("MIDI to OSC interface for A&H Xone:K2\n"
	       "\n"
	       " -h, --help              Display this help message.\n"
	       " -d, --device <dev>      MIDI device name.\n");
}


static void error_callback(int num, const char *msg, const char *path)
{
	fprintf(stderr, "liblo error %i (%s) for path %s\n", num, msg, path);
}


static void handle_note_off(int note, int vel, lo_address osc_send_addr)
{
	int i;

	for ( i=0; i<n_potentiometers; i++ ) {
		if ( note == potentiometers[i].button ) {
			faderpot_note_off(&potentiometers[i]);
			return;
		}
	}

	for ( i=0; i<n_encoders; i++ ) {
		if ( note == encoders[i].fine_button ) {
			encoder_note_off(&encoders[i]);
			return;
		}
	}

	for ( i=0; i<n_buttons; i++ ) {
		if ( note == buttons[i].note ) {
			button_note_off(&buttons[i], osc_send_addr);
			return;
		}
	}

	fprintf(stderr, "Unrecognised note %i\n", note);
}


static void handle_note(int note, int vel, lo_address osc_send_addr)
{
	int i;

	for ( i=0; i<n_potentiometers; i++ ) {
		if ( note == potentiometers[i].button ) {
			faderpot_note_on(&potentiometers[i]);
			return;
		}
	}

	for ( i=0; i<n_encoders; i++ ) {
		if ( note == encoders[i].fine_button ) {
			encoder_note_on(&encoders[i]);
			return;
		}
	}

	for ( i=0; i<n_buttons; i++ ) {
		if ( note == buttons[i].note ) {
			button_note_on(&buttons[i], osc_send_addr);
			return;
		}
	}

	fprintf(stderr, "Unrecognised note %i\n", note);
}


static void handle_cc(int cc, int val, lo_address osc_send_addr)
{
	int i;

	for ( i=0; i<n_faders; i++ ) {
		if ( cc == faders[i].cc_val ) {
			faderpot_cc(&faders[i], val, osc_send_addr);
			return;
		}
	}

	for ( i=0; i<n_potentiometers; i++ ) {
		if ( cc == potentiometers[i].cc_val ) {
			faderpot_cc(&potentiometers[i], val, osc_send_addr);
			return;
		}
	}

	for ( i=0; i<n_encoders; i++ ) {
		if ( cc == encoders[i].cc_val) {
			encoder_cc(&encoders[i], val, osc_send_addr);
			return;
		}
	}

	fprintf(stderr, "Unrecognised CC %i\n", cc);
}


static size_t process_midi(unsigned char *buf, size_t avail, lo_address osc_send_addr)
{
	if ( avail < 1 ) return 0;

	if ( (buf[0] & 0xf0) == 0x90 ) {
		/* Note on */
		if ( avail < 3 ) return 0;
		handle_note(buf[1], buf[2], osc_send_addr);
		return 3;
	} else if ( (buf[0] & 0xf0) == 0x80 ) {
		/* Note off */
		if ( avail < 3 ) return 0;
		handle_note_off(buf[1], buf[2], osc_send_addr);
		return 3;
	} else if ( (buf[0] & 0xf0) == 0xb0 ) {
		/* CC change */
		if ( avail < 3 ) return 0;
		handle_cc(buf[1], buf[2], osc_send_addr);
		return 3;
	} else {
		printf("Ignoring MIDI command %i\n", buf[0]);
		return 1;
	}
}


static int hup_err(struct pollfd *pfds, int nfds)
{
	int i;
	for ( i=0; i<nfds; i++ ) {
		if ( pfds[i].revents & (POLLERR | POLLHUP) ) return 1;
	}
	return 0;
}


int main(int argc, char *argv[])
{
	int c, r, i;
	char *dev = NULL;
	snd_rawmidi_t *midi_in;
	lo_server osc_server;
	lo_address osc_send_addr;
	int lo_fd;
	unsigned char midi_buf[4096];
	size_t midi_buf_pos = 0;

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{"device",             1, NULL,               'd'},
		{0, 0, NULL, 0}
	};

	/* Short options */
	while ((c = getopt_long(argc, argv, "hd:",
	                        longopts, NULL)) != -1) {

		switch (c) {

			case 'h' :
			show_help(argv[0]);
			return 0;

			case 'd':
			dev = strdup(optarg);
			break;

			case 0 :
			break;

			default :
			return 1;

		}

	}

	if ( dev == NULL ) {

		/* FIXME: Enumerate and select device */
		dev = strdup("hw:1");

		printf("Found MIDI device: %s\n", dev);

	}

	r = snd_rawmidi_open(&midi_in, &midi_out, dev, SND_RAWMIDI_SYNC);
	if ( r ) {
		fprintf(stderr, "Couldn't open MIDI device: %i\n", r);
		return 1;
	}

	/* Set non-blocking mode for input (read) stream only */
	snd_rawmidi_nonblock(midi_in, 1);

	/* Create OSC server and destination */
	osc_server = lo_server_new("7771", error_callback);
	lo_fd = lo_server_get_socket_fd(osc_server);
	osc_send_addr = lo_address_new(NULL, "7770");

	init_button(&buttons[0], "A", 36);
	init_button(&buttons[1], "B", 37);
	init_button(&buttons[2], "C", 38);
	init_button(&buttons[3], "D", 39);
	init_button(&buttons[4], "E", 32);
	init_button(&buttons[5], "F", 33);
	init_button(&buttons[6], "G", 34);
	init_button(&buttons[7], "H", 35);
	init_button(&buttons[8], "I", 28);
	init_button(&buttons[9], "J", 29);
	init_button(&buttons[10], "K", 30);
	init_button(&buttons[11], "L", 31);
	init_button(&buttons[12], "M", 24);
	init_button(&buttons[13], "N", 25);
	init_button(&buttons[14], "O", 26);
	init_button(&buttons[15], "P", 27);
	init_button_full(&buttons[16], "LAYER", 12, 12, 16, 20);
	init_button_full(&buttons[17], "SHIFT", 15, 15, 19, 23);
	for ( i=0; i<n_buttons; i++ ) {
		add_button_methods(&buttons[i], osc_server);
	}

	init_encoder(&encoders[0], 1, 0, 52);
	init_encoder(&encoders[1], 2, 1, 53);
	init_encoder(&encoders[2], 3, 2, 54);
	init_encoder(&encoders[3], 4, 3, 55);
	init_encoder_noled(&encoders[4], 5, 20, 13);
	init_encoder_noled(&encoders[5], 6, 21, 14);
	for ( i=0; i<n_encoders; i++ ) {
		add_encoder_methods(&encoders[i], osc_server);
	}

	init_fader(&faders[0], 1, 16);
	init_fader(&faders[1], 2, 17);
	init_fader(&faders[2], 3, 18);
	init_fader(&faders[3], 4, 19);
	for ( i=0; i<n_faders; i++ ) {
		add_faderpot_methods(&faders[i], osc_server);
	}

	init_potentiometer(&potentiometers[0], 1, 4, 48);
	init_potentiometer(&potentiometers[1], 2, 5, 49);
	init_potentiometer(&potentiometers[2], 3, 6, 50);
	init_potentiometer(&potentiometers[3], 4, 7, 51);
	init_potentiometer(&potentiometers[4], 5, 8, 44);
	init_potentiometer(&potentiometers[5], 6, 9, 45);
	init_potentiometer(&potentiometers[6], 7, 10, 46);
	init_potentiometer(&potentiometers[7], 8, 11, 47);
	init_potentiometer(&potentiometers[8], 9, 12, 40);
	init_potentiometer(&potentiometers[9], 10, 13, 41);
	init_potentiometer(&potentiometers[10], 11, 14, 42);
	init_potentiometer(&potentiometers[11], 12, 15, 43);
	for ( i=0; i<n_potentiometers; i++ ) {
		add_faderpot_methods(&potentiometers[i], osc_server);
	}

	do {

		struct pollfd pfds[16];
		int nfds;
		int r;

		pfds[0].fd = lo_fd;
		pfds[0].events = POLLIN;
		pfds[0].revents = 0;

		/* Add MIDI fds */
		nfds = snd_rawmidi_poll_descriptors(midi_in, &pfds[1], 15);

		r = poll(pfds, 1+nfds, 1000);
		if ( r < 0 ) {
			fprintf(stderr, "poll() failed: %s\n", strerror(errno));
		} else {

			unsigned short revents;

			if ( hup_err(pfds, 1+nfds) ) {
				fprintf(stderr, "Error!\n");
				break;
			}

			if ( pfds[0].revents & POLLIN ) {
				lo_server_recv_noblock(osc_server, 0);
			}

			snd_rawmidi_poll_descriptors_revents(midi_in,
			                                     &pfds[1], nfds,
			                                     &revents);

			if ( (revents & POLLIN) && (midi_buf_pos < 2048) ) {

				ssize_t r;

				r = snd_rawmidi_read(midi_in,
				                     &midi_buf[midi_buf_pos],
				                     4096-midi_buf_pos);
				if ( r < 0 ) {
					fprintf(stderr, "MIDI read failed\n");
				} else {
					size_t total_proc = 0;
					size_t p;
					midi_buf_pos += r;
					do {
						p = process_midi(midi_buf+total_proc,
						                 midi_buf_pos-total_proc,
						                 osc_send_addr);
						total_proc += p;
					} while ( p > 0 );
					memmove(midi_buf, midi_buf+total_proc,
					        4096-total_proc);
					midi_buf_pos -= total_proc;
				}
			}
		}

	} while ( 1 );

	snd_rawmidi_drain(midi_in);
	snd_rawmidi_close(midi_in);

	snd_rawmidi_drain(midi_out);
	snd_rawmidi_close(midi_out);

	return 0;
}