summaryrefslogtreecommitdiff
path: root/midinator.c
blob: 40b9c1d9d1da3d000c5240f0e5940f6d765189d4 (plain)
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
/*
 * midinator.c
 *
 * Poke MIDI devices
 *
 * (c) 2012 Thomas White <taw@bitwiz.org.uk>
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


static void show_help(const char *s)
{
	printf("Syntax: %s [options]\n\n", s);
	printf(
"Do MIDI stuff\n"
"\n"
" -h, --help              Display this help message.\n"
);
}


static void midi_readable(int fd)
{
	int rval, i;
	unsigned char buf[32];

	rval = read(fd, buf, 32);

	if ( (rval == -1) || (rval == 0) ) {
		fprintf(stderr, "Read error.\n");
		return;
	}

	if ( buf[0] == 0xfe ) {
		write(fd, buf, 1);
		return;
	}

	for ( i=0; i<rval; i++ ) {
		if ( i != 0 ) printf(" : ");
		printf("%hhx", buf[i]);
	}
	printf("\n");

	if ( (buf[0] & 0xf0) == 0x90 ) {
		unsigned char sbuf[32];
		sbuf[0] = 0x91;
		sbuf[1] = buf[1] + 12;
		sbuf[2] = buf[2] >> 0;
		write(fd, sbuf, 3);
	}

	if ( (buf[0] & 0xf0) == 0xc0 ) {
		unsigned char sbuf[32];
		sbuf[0] = 0xc1;
		sbuf[1] = buf[1];
		write(fd, sbuf, 2);
	}

	if ( (buf[0] & 0xf0) == 0xf0 ) {
		unsigned char sbuf[32];
		sbuf[0] = buf[0];
		sbuf[1] = buf[1];
		sbuf[2] = buf[2];
		write(fd, sbuf, 3);
	}
}


int main(int argc, char *argv[])
{
	int c;
	int fd;
	int rval;

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{0, 0, NULL, 0}
	};

	/* Short options */
	while ((c = getopt_long(argc, argv, "h",
	                        longopts, NULL)) != -1) {

		switch (c) {
		case 'h' :
			show_help(argv[0]);
			return 0;

		case 0 :
			break;

		default :
			return 1;
		}

	}

	fd = open("/dev/snd/midiC1D0", O_RDWR);
	if ( fd == -1 ) {
		fprintf(stderr, "Couldn't open MIDI device.\n");
		return 1;
	}

	int i;
	for ( i=0; i<8; i++ ) {
		unsigned char sbuf[32];
		sbuf[0] = 0xb0 || i;
		sbuf[1] = 0x78;
		sbuf[2] = 0x00;
		write(fd, sbuf, 3);
	}

	do {

		fd_set fds;
		struct timeval tv;
		int sval;

		FD_ZERO(&fds);
		FD_SET(fd, &fds);

		tv.tv_sec = 1;
		tv.tv_usec = 0;

		sval = select(fd+1, &fds, NULL, NULL, &tv);

		rval = 0;
		if ( sval == -1 ) {
			int err = errno;
			fprintf(stderr, "select() failed: %s\n", strerror(err));
			rval = 1;
		} else if ( sval != 0 ) {

			midi_readable(fd);

		} /* else timeout */

	} while ( !rval );

	close(fd);

	return 0;
}