Wrapping up example 4 with reading MIDI

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2024-10-07 19:18:06 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2024-10-07 19:18:06 +0200
Commit 0eeada47529c98b7a3b213146ec4552f02aa9038 (patch)
-rw-r--r-- example4.c 74
1 files changed, 73 insertions, 1 deletions
diff --git a/example4.c b/example4.c
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
  
8
static snd_seq_t *seq_handle;
9
static snd_seq_t *seq_handle;
9
static snd_seq_addr_t *ports;
10
static snd_seq_addr_t *ports;
10
static int rate = 44100;
11
static int rate = 44100;
  
12
static int stop = 0;
  
13
  
  
14
static void sighandler(int sig ATTRIBUTE_UNUSED) {
  
15
	stop = 1;
  
16
}
11
  
17
  
12
int main(void) {
18
int main(void) {
13
	fprintf(stdout, "Example: Reading MIDI input\n");
19
	fprintf(stdout, "Example: Reading MIDI input\n");
...
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
		}
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
}
...