summaryrefslogtreecommitdiff
path: root/example4.c
diff options
context:
space:
mode:
Diffstat (limited to 'example4.c')
-rw-r--r--example4.c74
1 files changed, 73 insertions, 1 deletions
diff --git a/example4.c b/example4.c
index c14e5bc..a24680e 100644
--- a/example4.c
+++ b/example4.c
@@ -1,13 +1,19 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <signal.h>
3#include <alsa/asoundlib.h> 4#include <alsa/asoundlib.h>
4 5
5#define CLIENT_NAME "ttdaw" 6#define CLIENT_NAME "ttdaw"
6#define MAX_MIDI_PORTS 4 7#define MAX_MIDI_PORTS 1
7 8
8static snd_seq_t *seq_handle; 9static snd_seq_t *seq_handle;
9static snd_seq_addr_t *ports; 10static snd_seq_addr_t *ports;
10static int rate = 44100; 11static int rate = 44100;
12static int stop = 0;
13
14static void sighandler(int sig ATTRIBUTE_UNUSED) {
15 stop = 1;
16}
11 17
12int main(void) { 18int main(void) {
13 fprintf(stdout, "Example: Reading MIDI input\n"); 19 fprintf(stdout, "Example: Reading MIDI input\n");
@@ -33,12 +39,20 @@ int main(void) {
33 exit(1); 39 exit(1);
34 } 40 }
35 41
42 // Connecting ports.
36 char *port_name = "28:0"; 43 char *port_name = "28:0";
44 ports = realloc(ports, MAX_MIDI_PORTS * sizeof(snd_seq_addr_t));
37 if (snd_seq_parse_address(seq_handle, &ports[0], port_name) < 0) { 45 if (snd_seq_parse_address(seq_handle, &ports[0], port_name) < 0) {
38 fprintf(stderr, "Invalid port %s.\n", port_name); 46 fprintf(stderr, "Invalid port %s.\n", port_name);
39 exit(1); 47 exit(1);
40 } 48 }
41 49
50 // Listing assigned ports.
51 fprintf(stdout, "Ports:\n");
52 for (int j = 0; j < MAX_MIDI_PORTS; j++) {
53 fprintf(stdout, " client: %d, port: %d\n", ports[j].client, ports[j].port);
54 }
55
42 // Connecting ports. 56 // Connecting ports.
43 for (int i = 0; i < MAX_MIDI_PORTS; ++i) { 57 for (int i = 0; i < MAX_MIDI_PORTS; ++i) {
44 int err = snd_seq_connect_from(seq_handle, 0, ports[i].client, ports[i].port); 58 int err = snd_seq_connect_from(seq_handle, 0, ports[i].client, ports[i].port);
@@ -48,6 +62,64 @@ int main(void) {
48 } 62 }
49 } 63 }
50 64
65 if (snd_seq_nonblock(seq_handle, 1) < 0) {
66 fprintf(stderr, "Set nonblock mode failed.");
67 exit(1);
68 }
69
70 signal(SIGINT, sighandler);
71 signal(SIGTERM, sighandler);
72
73 // Reading MIDI device.
74 struct pollfd *pfds;
75 int npfds;
76
77 npfds = snd_seq_poll_descriptors_count(seq_handle, POLLIN);
78 pfds = alloca(sizeof(*pfds) * npfds);
79
80 for (;;) {
81 snd_seq_poll_descriptors(seq_handle, pfds, npfds, POLLIN);
82 if (poll(pfds, npfds, -1) < 0) {
83 break;
84 }
85
86 for (;;) {
87 snd_seq_event_t *ev;
88
89 if (snd_seq_event_input(seq_handle, &ev) < 0) {
90 break;
91 }
92
93 if (ev) {
94 switch (ev->type) {
95 case SND_SEQ_EVENT_NOTEON:
96 printf("%3d:%-3dNote on %2d, note %d, velocity: %3d\n",
97 ev->source.client, ev->source.port,
98 ev->data.note.channel,
99 ev->data.note.note,
100 ev->data.note.velocity);
101 break;
102
103 case SND_SEQ_EVENT_NOTEOFF:
104 printf("%3d:%-3dNote off\t%2d, note %d\n",
105 ev->source.client, ev->source.port,
106 ev->data.note.channel,
107 ev->data.note.note);
108 break;
109
110
111 }
112 }
113 }
114
115 fflush(stdout);
116
117 if (stop) {
118 break;
119 }
120 }
121
122 fprintf(stdout, "Finishing up\n");
51 snd_seq_close(seq_handle); 123 snd_seq_close(seq_handle);
52 return 0; 124 return 0;
53} 125}