-
Notifications
You must be signed in to change notification settings - Fork 6
/
alsaseqio.c
218 lines (202 loc) · 4.59 KB
/
alsaseqio.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <unistd.h>
#include <alsa/asoundlib.h>
#include "arg.h"
#define LEN(a) (sizeof (a) / sizeof *(a))
static snd_seq_t *seq;
static snd_midi_event_t *dev;
static void
usage(void)
{
fprintf(stderr, "usage: alsaseq client:port cmd [arg...]\n");
exit(1);
}
static void *
midiread(void *arg)
{
int fd;
ssize_t ret;
size_t len;
snd_seq_event_t *evt;
unsigned char *pos, buf[1024];
fd = *(int *)arg;
for (;;) {
ret = snd_seq_event_input(seq, &evt);
if (ret < 0) {
fprintf(stderr, "snd_seq_event_input: %s\n", snd_strerror(ret));
if (ret == -ENOSPC)
continue;
exit(1);
}
ret = snd_midi_event_decode(dev, buf, sizeof buf, evt);
if (ret < 0) {
fprintf(stderr, "snd_midi_event_decode: %s\n", snd_strerror(ret));
exit(1);
}
len = ret;
pos = buf;
while (len > 0) {
ret = write(fd, pos, len);
if (ret < 0) {
perror("write");
exit(1);
}
pos += ret;
len -= ret;
}
}
return NULL;
}
int
main(int argc, char *argv[])
{
int err;
ssize_t ret;
size_t len;
snd_seq_port_info_t *info;
snd_seq_addr_t dest, self;
snd_seq_port_subscribe_t *sub;
snd_seq_event_t evt;
pid_t pid;
pthread_t thread;
char *end;
int rfd[2], wfd[2];
unsigned char *pos, buf[1024];
ARGBEGIN {
default:
usage();
} ARGEND
if (argc < 2)
usage();
dest.client = strtol(argv[0], &end, 10);
if (*end != ':')
usage();
dest.port = strtol(end + 1, &end, 10);
if (*end)
usage();
err = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
if (err) {
fprintf(stderr, "snd_seq_open: %s\n", snd_strerror(err));
return 1;
}
err = snd_seq_set_client_name(seq, "alsaseq");
if (err) {
fprintf(stderr, "snd_seq_set_client_name: %s\n", snd_strerror(err));
return 1;
}
err = snd_seq_create_simple_port(seq, "alsaseq", SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE, SND_SEQ_PORT_TYPE_MIDI_GENERIC);
if (err) {
fprintf(stderr, "snd_seq_create_simple_port: %s\n", snd_strerror(err));
return 1;
}
err = snd_seq_port_info_malloc(&info);
if (err) {
fprintf(stderr, "snd_seq_port_info_malloc: %s\n", snd_strerror(err));
return 1;
}
err = snd_seq_get_any_port_info(seq, dest.client, dest.port, info);
if (err) {
fprintf(stderr, "snd_seq_get_any_port_info: %s\n", snd_strerror(err));
return 1;
}
setenv("MIDIPORT", snd_seq_port_info_get_name(info), 1);
snd_seq_port_info_free(info);
err = snd_seq_port_subscribe_malloc(&sub);
if (err) {
fprintf(stderr, "snd_seq_port_subscribe_malloc: %s\n", snd_strerror(err));
return 1;
}
self.client = snd_seq_client_id(seq);
self.port = 0;
snd_seq_port_subscribe_set_sender(sub, &self);
snd_seq_port_subscribe_set_dest(sub, &dest);
err = snd_seq_subscribe_port(seq, sub);
if (err) {
fprintf(stderr, "snd_seq_subscribe_port: %s\n", snd_strerror(err));
return 1;
}
snd_seq_port_subscribe_set_sender(sub, &dest);
snd_seq_port_subscribe_set_dest(sub, &self);
err = snd_seq_subscribe_port(seq, sub);
if (err) {
fprintf(stderr, "snd_seq_subscribe_port: %s\n", snd_strerror(err));
return 1;
}
err = snd_midi_event_new(1024, &dev);
if (err) {
fprintf(stderr, "snd_midi_event_new: %s\n", snd_strerror(err));
return 1;
}
if (pipe(wfd) != 0) {
perror("pipe2");
return 1;
}
if (pipe(rfd) != 0) {
perror("pipe2");
return 1;
}
pid = fork();
switch (pid) {
case -1:
perror("fork");
return 1;
case 0:
close(rfd[1]);
close(wfd[0]);
break;
default:
close(wfd[1]);
close(rfd[0]);
if (dup2(wfd[0], 6) < 0 || dup2(rfd[1], 7) < 0) {
perror("dup2");
return 1;
}
execvp(argv[1], argv + 1);
fprintf(stderr, "execvp %s: %s\n", argv[1], strerror(errno));
return 1;
}
err = pthread_create(&thread, NULL, midiread, &wfd[1]);
if (err) {
fprintf(stderr, "pthread_create: %s\n", strerror(err));
return 1;
}
snd_seq_ev_set_source(&evt, 0);
snd_seq_ev_set_subs(&evt);
snd_seq_ev_set_direct(&evt);
for (;;) {
ret = read(rfd[0], buf, sizeof buf);
if (ret < 0) {
perror("read");
exit(1);
}
if (ret == 0)
break;
pos = buf;
len = ret;
while (len > 0) {
ret = snd_midi_event_encode(dev, pos, len, &evt);
if (ret < 0) {
fprintf(stderr, "snd_midi_event_encode: %s\n", snd_strerror(ret));
return 1;
}
pos += ret;
len -= ret;
if (evt.type != SND_SEQ_EVENT_NONE) {
ret = snd_seq_event_output(seq, &evt);
if (ret < 0) {
fprintf(stderr, "snd_seq_event_output: %s\n", snd_strerror(ret));
return 1;
}
}
}
ret = snd_seq_drain_output(seq);
if (ret < 0) {
fprintf(stderr, "snd_seq_drain_output: %s\n", snd_strerror(ret));
return 1;
}
}
}