File: kernel-client_midi.c

package info (click to toggle)
alsadriver 0.2.0-pre8-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,808 kB
  • ctags: 6,550
  • sloc: ansic: 43,490; sh: 916; makefile: 759; perl: 54
file content (659 lines) | stat: -rw-r--r-- 15,801 bytes parent folder | download
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
/*
 *   Kernel client MIDI driver for ALSA sequencer
 *   Copyright (c) 1998 by Frank van de Pol <F.K.W.van.de.Pol@inter.nl.net>
 *
 *
 *   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 2 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, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#define SND_MAIN_OBJECT_FILE
#include "driver.h"
#include "midi.h"
#include "seq.h"



/* uncomment next line to bounce received midi data directly to the output (ie. MIDI thru) */
/*#define TEST */



/* data for this midi synth driver */
typedef struct {
	int seq_client;
	int seq_port;	
	snd_rawmidi_t *rmidi;
} seq_midisynth_t;






/* fill standard header data, source port & channel are filled in */
static void snd_seq_midi_setheader(snd_seq_event_t * ev, int port, int channel)
{
	ev->flags = SND_SEQ_EVENT_LENGTH_FIXED;

	ev->source.queue = -1;
	ev->source.client = -1;	/* myid */
	ev->source.port = port;
	ev->source.channel = channel;

	ev->dest.queue = SND_SEQ_ADDRESS_SUBSCRIBERS;
	ev->dest.client = -1;
	ev->dest.port = -1;
	ev->dest.channel = channel;

	ev->flags = SND_SEQ_TIME_STAMP_REAL | SND_SEQ_TIME_MODE_REL;
	ev->time.real.tv_sec = 0;
	ev->time.real.tv_nsec = 0;

#ifdef TEST
	ev->dest.queue = 0;
	ev->dest.client = 1;
	ev->dest.port = port;
#endif
}



/*
 * the ALSA low-level midi routines appear to return 'whole' midi events and 
 * have already handled midi running state. I don't know if will remain in 
 * the future. If not, some more elaborate MIDI parser is needed.
 */
static void snd_midi_command(snd_rawmidi_t * rmidi, void *cmd_private_data, unsigned char *command, int count)
{
	seq_midisynth_t *msynth = (seq_midisynth_t*) cmd_private_data;
	int channel;
	int port;
	int client;

	if (msynth == NULL) {
		snd_printk("msynth == NULL\n");
		return;
	}	
	port = msynth->seq_port;
	client = msynth->seq_client;
	channel = command[0] & 0x0f;

	switch (command[0] & 0xf0) {

		case 0x80:	// note off

			if (count == 3) {
				snd_seq_event_t ev;

				snd_seq_midi_setheader(&ev, port, channel);

				ev.type = SND_SEQ_EVENT_NOTEOFF;
				ev.data.note.note = command[1];
				ev.data.note.velocity = command[2];

				snd_seq_kernel_client_enqueue(client, &ev);
				return;
			}
			break;

		case 0x90:	// note on

			if (count == 3) {
				snd_seq_event_t ev;

				snd_seq_midi_setheader(&ev, port, channel);

				ev.type = SND_SEQ_EVENT_NOTEON;
				ev.data.note.note = command[1];
				ev.data.note.velocity = command[2];

				snd_seq_kernel_client_enqueue(client, &ev);
				return;
			}
			break;

		case 0xa0:	// poly key pressure

			if (count == 3) {
				snd_seq_event_t ev;

				snd_seq_midi_setheader(&ev, port, channel);

				ev.type = SND_SEQ_EVENT_KEYPRESS;
				ev.data.control.param = command[1];
				ev.data.control.value = command[2];

				snd_seq_kernel_client_enqueue(client, &ev);
				return;
			}
			break;

		case 0xb0:	// control change

			if (count == 3) {
				snd_seq_event_t ev;

				snd_seq_midi_setheader(&ev, port, channel);

				ev.type = SND_SEQ_EVENT_CONTROLLER;
				ev.data.control.param = command[1];
				ev.data.control.value = command[2];

				snd_seq_kernel_client_enqueue(client, &ev);
				return;
			}
			break;

		case 0xc0:	// program change

			if (count == 2) {
				snd_seq_event_t ev;

				snd_seq_midi_setheader(&ev, port, channel);

				ev.type = SND_SEQ_EVENT_PGMCHANGE;
				ev.data.control.value = command[1];

				snd_seq_kernel_client_enqueue(client, &ev);
				return;
			}
			break;

		case 0xd0:	// channel pressure

			if (count == 2) {
				snd_seq_event_t ev;

				snd_seq_midi_setheader(&ev, port, channel);

				ev.type = SND_SEQ_EVENT_CHANPRESS;
				ev.data.control.value = command[1];

				snd_seq_kernel_client_enqueue(client, &ev);
				return;
			}
			break;

		case 0xe0:	// pitch bender

			if (count == 3) {
				snd_seq_event_t ev;

				snd_seq_midi_setheader(&ev, port, channel);

				ev.type = SND_SEQ_EVENT_PITCHBEND;
				ev.data.control.value = (command[1] & 0x7f) + ((command[2] & 0x7f) << 7) - 8192;

				snd_seq_kernel_client_enqueue(client, &ev);
				return;
			}
			break;

		case 0xf0:
			switch (command[0]) {
				case 0xf0:	/* sysex */
					{
						snd_seq_event_t ev;

						snd_seq_midi_setheader(&ev, port, channel);
						ev.flags = (ev.flags & ~SND_SEQ_EVENT_LENGTH_MASK) | SND_SEQ_EVENT_LENGTH_VARIABLE;
						ev.type = SND_SEQ_EVENT_SYSEX;
						ev.data.ext.ptr = snd_seq_ext_malloc(count);
						ev.data.ext.len = count;
						if (ev.data.ext.ptr) {
							memcpy(ev.data.ext.ptr, command, count);
							snd_seq_kernel_client_enqueue(client, &ev);
						} else {
							snd_printk("failed to get %d bytes for sysex\n", count);
						}
						return;
					}

				case 0xf1:	/* MTC quarter frame */
					if (count == 2) {
						snd_seq_event_t ev;

						snd_seq_midi_setheader(&ev, port, channel);

						ev.type = SND_SEQ_EVENT_QFRAME;
						ev.data.control.value = command[1];

						snd_seq_kernel_client_enqueue(client, &ev);
						return;
					}
					break;

				case 0xf2:	/* song position */
					if (count == 3) {
						snd_seq_event_t ev;

						snd_seq_midi_setheader(&ev, port, channel);

						ev.type = SND_SEQ_EVENT_SONGPOS;
						ev.data.control.value = (command[1] & 0x7f) + ((command[2] & 0x7f) << 7);

						snd_seq_kernel_client_enqueue(client, &ev);
						return;
					}
					break;

				case 0xf3:	/* song select */
					if (count == 2) {
						snd_seq_event_t ev;

						snd_seq_midi_setheader(&ev, port, channel);

						ev.type = SND_SEQ_EVENT_SONGSEL;
						ev.data.control.value = command[1];

						snd_seq_kernel_client_enqueue(client, &ev);
						return;
					}
					break;

				case 0xf4:	/* undefined */
					return;

				case 0xf5:	/* undefined */
					return;

				case 0xf6:	/* tune request */
					snd_printk("Rx: tune request\n");
					return;

				case 0xf7:	/* end of sysex */
					return;

					// system real-time messages

				case 0xf8:	/* timing clock */
					if (count == 1) {
						snd_seq_event_t ev;

						snd_seq_midi_setheader(&ev, port, channel);

						ev.type = SND_SEQ_EVENT_CLOCK;
						snd_seq_kernel_client_enqueue(client, &ev);
						return;
					}
				case 0xfa:	/* start */
					if (count == 1) {
						snd_seq_event_t ev;

						snd_seq_midi_setheader(&ev, port, channel);

						ev.type = SND_SEQ_EVENT_START;
						snd_seq_kernel_client_enqueue(client, &ev);
						return;
					}
				case 0xfb:	// continue

					if (count == 1) {
						snd_seq_event_t ev;

						snd_seq_midi_setheader(&ev, port, channel);

						ev.type = SND_SEQ_EVENT_CONTINUE;
						snd_seq_kernel_client_enqueue(client, &ev);
						return;
					}
				case 0xfc:	// stop

					if (count == 1) {
						snd_seq_event_t ev;

						snd_seq_midi_setheader(&ev, port, channel);

						ev.type = SND_SEQ_EVENT_STOP;
						snd_seq_kernel_client_enqueue(client, &ev);
						return;
					}
				case 0xfd:	/* undefined */
					return;

				case 0xfe:	// active sensing

					if (count == 1) {
						snd_seq_event_t ev;

						snd_seq_midi_setheader(&ev, port, channel);

						ev.type = SND_SEQ_EVENT_HEARTBEAT;
						snd_seq_kernel_client_enqueue(client, &ev);
						return;
					}
				case 0xff:	// system reset

					snd_printk("Rx: system reset\n");
					return;
			}
	}

	/* not a legal MIDI sequence.... */
	snd_printk("rx command '%s': ", rmidi->name);
	while (count-- > 0)
		printk("%02x:", *command++);
	printk("\n");
}



/* send data to specified midi device */
static void dump_midi(snd_rawmidi_t * rmidi, unsigned char *buf, int count)
{
	int done = snd_midi_transmit(rmidi, buf, count);

	if (done != count) {
		snd_printk("only wrote %d instead of %d bytes to midi device\n", done, count);
	}
}


static int event_process_midi(snd_seq_event_t * ev, void *private_data)
{
	seq_midisynth_t *msynth = (seq_midisynth_t*) private_data;
	int channel = ev->dest.channel;
	static unsigned char msg[10];	/* buffer for constructing midi messages */
	static unsigned char running_state;	/* FIXME: should be in struct passed to function so we can handle multiple ports */
	int client;
	snd_rawmidi_t *rmidi;

	if (msynth == NULL) {
		snd_printk("msynth == NULL\n");
		return 1;
	}	


	client = msynth->seq_client;
	rmidi = msynth->rmidi;




	/* decode actual event data... */
	switch (ev->type) {
		case SND_SEQ_EVENT_NOTE:
			/* note event with specified length, first trigger note on */
			msg[0] = (channel & 0x0f) | 0x90;	/* note on */
			msg[1] = ev->data.note.note & 0x7f;
			msg[2] = ev->data.note.velocity & 0x7f;
			if (running_state == msg[0]) {
				dump_midi(rmidi, msg + 1, 2);
			} else {
				running_state = msg[0];
				dump_midi(rmidi, msg, 3);
			}

			/* enqueue note off event */
			switch (ev->flags & SND_SEQ_TIME_STAMP_MASK) {
				case SND_SEQ_TIME_STAMP_TICK:
					ev->time.tick = ev->data.note.duration;
					break;
				case SND_SEQ_TIME_STAMP_REAL:
					ev->time.real.tv_sec = ev->data.note.duration / 1000;	/* unit for duration is ms */
					ev->time.real.tv_nsec = 1E6 * (ev->data.note.duration % 1000);
					break;
			}
			ev->flags = (ev->flags & ~SND_SEQ_TIME_MODE_MASK) | SND_SEQ_TIME_MODE_REL;
			ev->type = SND_SEQ_EVENT_NOTEOFF;
			snd_seq_kernel_client_enqueue(client, ev);
			break;


		case SND_SEQ_EVENT_NOTEOFF:
			msg[0] = (channel & 0x0f) | 0x80;	/* note off */
			msg[1] = ev->data.note.note & 0x7f;
			msg[2] = ev->data.note.velocity & 0x7f;
			if (running_state == msg[0]) {
				dump_midi(rmidi, msg + 1, 2);
			} else {
				running_state = msg[0];
				dump_midi(rmidi, msg, 3);
			}
			break;

		case SND_SEQ_EVENT_NOTEON:
			msg[0] = (channel & 0x0f) | 0x90;	/* note on */
			msg[1] = ev->data.note.note & 0x7f;
			msg[2] = ev->data.note.velocity & 0x7f;
			if (running_state == msg[0]) {
				dump_midi(rmidi, msg + 1, 2);
			} else {
				running_state = msg[0];
				dump_midi(rmidi, msg, 3);
			}
			break;

		case SND_SEQ_EVENT_KEYPRESS:
			msg[0] = (channel & 0x0f) | 0xa0;	/* polyphonic key pressure */
			msg[1] = ev->data.control.param & 0x7f;
			msg[2] = ev->data.control.value & 0x7f;
			if (running_state == msg[0]) {
				dump_midi(rmidi, msg + 1, 2);
			} else {
				running_state = msg[0];
				dump_midi(rmidi, msg, 3);
			}
			break;

		case SND_SEQ_EVENT_CONTROLLER:
			msg[0] = (channel & 0x0f) | 0xb0;	/* control change */
			msg[1] = ev->data.control.param & 0x7f;
			msg[2] = ev->data.control.value & 0x7f;
			if (running_state == msg[0]) {
				dump_midi(rmidi, msg + 1, 2);
			} else {
				running_state = msg[0];
				dump_midi(rmidi, msg, 3);
			}
			break;

		case SND_SEQ_EVENT_PGMCHANGE:
			msg[0] = (channel & 0x0f) | 0xc0;	/* program change */
			msg[1] = ev->data.control.value & 0x7f;
			if (running_state == msg[0]) {
				dump_midi(rmidi, msg + 1, 1);
			} else {
				running_state = msg[0];
				dump_midi(rmidi, msg, 2);
			}
			break;

		case SND_SEQ_EVENT_CHANPRESS:
			msg[0] = (channel & 0x0f) | 0xd0;	/* channel pressure */
			msg[1] = ev->data.control.value & 0x7f;
			if (running_state == msg[0]) {
				dump_midi(rmidi, msg + 1, 1);
			} else {
				running_state = msg[0];
				dump_midi(rmidi, msg, 2);
			}
			break;

		case SND_SEQ_EVENT_PITCHBEND:
			msg[0] = (channel & 0x0f) | 0xe0;	/* pitch bender */
			msg[1] = (ev->data.control.value + 8192) & 0x7f;	/* lsb */
			msg[2] = ((ev->data.control.value + 8192) >> 7) & 0x7f;		/* msb */
			if (running_state == msg[0]) {
				dump_midi(rmidi, msg + 1, 2);
			} else {
				running_state = msg[0];
				dump_midi(rmidi, msg, 3);
			}
			break;



		case SND_SEQ_EVENT_SYSEX:{
				unsigned char *sysex = (unsigned char *) ev->data.ext.ptr;

				//printf("Event  = System Exclusive len=%d\n", ev->data.ext.len);
				dump_midi(rmidi, sysex, ev->data.ext.len);
				snd_seq_ext_free(sysex, ev->data.ext.len);
				running_state = 0;
			}
			break;


		case SND_SEQ_EVENT_QFRAME:
			msg[0] = 0xf1;	/* MTC quarter frame */
			msg[1] = ev->data.control.value & 0x7f;
			dump_midi(rmidi, msg, 2);
			running_state = 0;
			break;

		case SND_SEQ_EVENT_CLOCK:
			msg[0] = 0xf8;
			dump_midi(rmidi, msg, 1);
			running_state = 0;
			break;

		case SND_SEQ_EVENT_START:
			msg[0] = 0xfa;
			dump_midi(rmidi, msg, 1);
			running_state = 0;
			break;

		case SND_SEQ_EVENT_CONTINUE:
			msg[0] = 0xfb;
			dump_midi(rmidi, msg, 1);
			running_state = 0;
			break;

		case SND_SEQ_EVENT_STOP:
			msg[0] = 0xfc;
			dump_midi(rmidi, msg, 1);
			running_state = 0;
			break;

		case SND_SEQ_EVENT_HEARTBEAT:
			msg[0] = 0xfe;	/* active sensing */
			dump_midi(rmidi, msg, 1);
			running_state = 0;
			break;


			/* messages which we are sending over the MIDI buss */
		case SND_SEQ_EVENT_TEMPO:
		case SND_SEQ_EVENT_TIMESIGN:
		case SND_SEQ_EVENT_KEYSIGN:
			break;


		default:
			snd_printk("Event  = Decoding for type %d is not implemented\n", ev->type);
	}
	return 1;
}


/* call-back function for event input */
static int event_input(snd_seq_event_t * ev, void *private_data)
{
	seq_midisynth_t *msynth = (seq_midisynth_t*) private_data;
	int port = ev->dest.port;	/* use for different devices... */

	if (msynth == NULL) {
		snd_printk("msynth == NULL\n");
		return 1;
	}	

	if (port == msynth->seq_port) {
		/* event is to be send to MIDI port */
		event_process_midi(ev, private_data);
	}
	/* FIXME: How to handle situation where the MIDI output queue is full??? 
	   should we sleep for a while, or simply not process the event (return failure code to 
	   higher level sequencer (<-- last option) */

	return 1;		/* success */
}



/*
 *  INIT PART
 */

 
static seq_midisynth_t msynth;


int init_module(void)
{
	int client;
	snd_seq_client_callback_t callbacks;
	snd_seq_client_info_t inf;
	snd_seq_port_info_t port;

	callbacks.input = event_input;

	client = snd_seq_register_kernel_client(&callbacks, &msynth);

	/* set our client name */
	strcpy(inf.name, "Kernel MIDI client");
	snd_seq_kernel_client_ctl(client, SND_SEQ_IOCTL_SET_CLIENT_INFO, &inf);
	

	/* open midi port */
	if ((snd_midi_open(0, 0, SND_RAWMIDI_LFLG_OUTPUT | SND_RAWMIDI_LFLG_INPUT, &msynth.rmidi)) < 0) {
		snd_printk("midi open failed!!!\n");
		return -EINVAL;
	}
	
	/* attach input handler */
	msynth.rmidi->input.u.p.command = snd_midi_command;
	msynth.rmidi->input.u.p.cmd_private_data = &msynth;
	snd_midi_start_input(msynth.rmidi);

	/* declare port */
	strcpy(port.name, msynth.rmidi->name);
	port.capability = SND_SEQ_PORT_CAP_MIDI_OUT | SND_SEQ_PORT_CAP_SYNC_OUT |
		SND_SEQ_PORT_CAP_MIDI_IN | SND_SEQ_PORT_CAP_SYNC_IN |
		SND_SEQ_PORT_CAP_SUBSCRIPTION;
	port.port_type = SND_SEQ_PORT_TYPE_MIDI_GENERIC;
	snd_seq_kernel_client_ctl(client, SND_SEQ_IOCTL_CREATE_PORT, &port);
	
	msynth.seq_client = client;
	msynth.seq_port = port.port;


	/* add midi thru for testing.... */
	{
		/* subscribe for events from the midi input device */
		snd_seq_port_subscribe_t subs;

		subs.sender.client = client;	/* this MIDI driver */
		subs.sender.port = msynth.seq_port;

		subs.dest.queue = 0;	/* use this queue for timestamps */
		subs.dest.client = client;	/* this MIDI driver */
		subs.dest.port = msynth.seq_port;
		
		snd_seq_kernel_client_ctl(client, SND_SEQ_IOCTL_SUBSCRIBE_PORT, &subs);
	}

	return 0;
}


void cleanup_module(void)
{
	snd_seq_unregister_kernel_client(msynth.seq_client);

	snd_midi_flush_output(msynth.rmidi);
	snd_midi_close(0, 0, SND_RAWMIDI_LFLG_OUTPUT | SND_RAWMIDI_LFLG_INPUT);
}