-
Notifications
You must be signed in to change notification settings - Fork 0
/
tut.cpp
50 lines (44 loc) · 1.58 KB
/
tut.cpp
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
#include <alsa/asoundlib.h>
static snd_seq_t *seq_handle;
static int in_port;
#define CHK(stmt, msg) if((stmt) < 0) {puts("ERROR: "#msg); exit(1);}
void midi_open(void)
{
CHK(snd_seq_open(&seq_handle, "default", SND_SEQ_OPEN_INPUT, 0),
"Could not open sequencer");
CHK(snd_seq_set_client_name(seq_handle, "Midi Listener"),
"Could not set client name");
CHK(in_port = snd_seq_create_simple_port(seq_handle, "listen:in",
SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE,
SND_SEQ_PORT_TYPE_APPLICATION),
"Could not open port");
}
snd_seq_event_t *midi_read(void)
{
snd_seq_event_t *ev = NULL;
snd_seq_event_input(seq_handle, &ev);
return ev;
}
void midi_process(const snd_seq_event_t *ev)
{
if((ev->type == SND_SEQ_EVENT_NOTEON)
||(ev->type == SND_SEQ_EVENT_NOTEOFF)) {
const char *type = (ev->type==SND_SEQ_EVENT_NOTEON) ? "on " : "off";
printf("[%d] Note %s: %2x vel(%2x)\n", ev->time.tick, type,
ev->data.note.note,
ev->data.note.velocity);
}
else if(ev->type == SND_SEQ_EVENT_CONTROLLER)
printf("[%d] Control: %2x val(%2x)\n", ev->time.tick,
ev->data.control.param,
ev->data.control.value);
else
printf("[%d] Unknown: Unhandled Event Received\n", ev->time.tick);
}
int main()
{
midi_open();
while(1)
midi_process(midi_read());
return -1;
}