Quick hack to get Arduino working with MIDI-rx.c, FIXME later...
[mirrors/Programs.git] / c / ghetto-sound-system / midi-rx.c
1 /* Modified code from http://tldp.org/HOWTO/MIDI-HOWTO-9.html
2 * This will print ALSA-Seq (MIDI) events to stdout for further processing eg.: in Arduino
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <alsa/asoundlib.h>
9
10 #define MIDI_NOTE_OFF 128
11 #define MIDI_NOTE_ON 144
12 #define MIDI_CONTROL 176
13 #define MIDI_PITCH_BEND 224
14 #define MIDI_FORMAT "%c%c%c"
15 #define MIDI_FORMAT_ERR "%d:%d:%d\n"
16
17 snd_seq_t *open_seq();
18 void midi_action(snd_seq_t *seq_handle);
19
20 snd_seq_t *open_seq() {
21
22 snd_seq_t *seq_handle;
23 int portid;
24
25 if (snd_seq_open(&seq_handle, "hw", SND_SEQ_OPEN_DUPLEX, 0) < 0) {
26 fprintf(stderr, "Error opening ALSA sequencer.\n");
27 exit(1);
28 }
29 snd_seq_set_client_name(seq_handle, "ALSA Sequencer Pipe");
30 if ((portid = snd_seq_create_simple_port(seq_handle, "ALSA Sequencer Pipe",
31 SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE,
32 SND_SEQ_PORT_TYPE_APPLICATION)) < 0) {
33 fprintf(stderr, "Error creating sequencer port.\n");
34 exit(1);
35 }
36 return(seq_handle);
37 }
38
39 void midi_action(snd_seq_t *seq_handle) {
40
41 snd_seq_event_t *ev;
42
43 do {
44 snd_seq_event_input(seq_handle, &ev);
45 switch (ev->type) {
46 case SND_SEQ_EVENT_CONTROLLER:
47 printf(MIDI_FORMAT, MIDI_CONTROL, ev->data.control.channel, ev->data.control.value);
48 break;
49 case SND_SEQ_EVENT_PITCHBEND:
50 printf(MIDI_FORMAT, MIDI_PITCH_BEND, ev->data.control.channel, ev->data.control.value);
51 break;
52 case SND_SEQ_EVENT_NOTEON:
53 printf(MIDI_FORMAT, MIDI_NOTE_ON, ev->data.control.channel, ev->data.note.note);
54 break;
55 case SND_SEQ_EVENT_NOTEOFF:
56 printf(MIDI_FORMAT, MIDI_NOTE_OFF, ev->data.control.channel, ev->data.note.note);
57 break;
58 }
59 fflush(stdout); fflush(stderr);
60 snd_seq_free_event(ev);
61 } while (snd_seq_event_input_pending(seq_handle, 0) > 0);
62 }
63
64 int main(int argc, char *argv[]) {
65
66 snd_seq_t *seq_handle;
67 int npfd;
68 struct pollfd *pfd;
69
70 seq_handle = open_seq();
71 npfd = snd_seq_poll_descriptors_count(seq_handle, POLLIN);
72 pfd = (struct pollfd *)alloca(npfd * sizeof(struct pollfd));
73 snd_seq_poll_descriptors(seq_handle, pfd, npfd, POLLIN);
74
75 fprintf(stderr, "COMMAND:CHANNEL:NOTE\nCommands are described on http://www.ec.vanderbilt.edu/computermusic/musc216site/MIDI.Commands.html\n\n");
76 while (1) {
77 if (poll(pfd, npfd, 100000) > 0) {
78 midi_action(seq_handle);
79 }
80 }
81 }
This page took 0.286725 seconds and 4 git commands to generate.