forked from nealey/hdjd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alsa.c
209 lines (177 loc) · 4.64 KB
/
alsa.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
#include <stdio.h>
#include <poll.h>
#include <sys/select.h>
#include <alsa/asoundlib.h>
#include "alsa.h"
#include "usb.h"
#include "log.h"
#include "dump.h"
#define ALSA_BUFSIZE 4096
#define MAX_PFDS 20
static snd_seq_t *snd_handle = NULL;
static int seq_port;
static snd_midi_event_t *midi_event_parser = NULL;
int npfd;
struct pollfd pfd[MAX_PFDS];
void alsa_interrupting()
{
//Currently nothing to do.
}
int
alsa_setup(const char *name)
{
if (snd_seq_open(&snd_handle, "default", SND_SEQ_OPEN_DUPLEX, 0) < 0) {
perror("snd_seq_open");
return -1;
}
snd_seq_nonblock(snd_handle, 1);
snd_seq_set_client_name(snd_handle, name);
seq_port =
snd_seq_create_simple_port(snd_handle, name,
SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_READ |
SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_MIDI_GENERIC);
// Allocate parser object for converting to and from MIDI
if (snd_midi_event_new(ALSA_BUFSIZE, &midi_event_parser) < 0) {
fatal("ALSA cannot allocate MIDI event parser");
return -1;
} else {
snd_midi_event_no_status(midi_event_parser,1);
return 0;
}
}
void
alsa_close()
{
if (midi_event_parser) {
snd_midi_event_free(midi_event_parser);
}
if (snd_handle) {
snd_seq_close(snd_handle);
}
}
void
alsa_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds)
{
npfd = snd_seq_poll_descriptors_count(snd_handle, POLLIN);
int i;
if (npfd > MAX_PFDS) {
fatal("ALSA wants too many file descriptors");
}
snd_seq_poll_descriptors(snd_handle, pfd, npfd, POLLIN);
for (i = 0; i < npfd; i += 1) {
if (pfd[i].fd > *nfds) {
*nfds = pfd[i].fd;
}
if (pfd[i].events & POLLIN) {
FD_SET(pfd[i].fd, rfds);
}
}
}
void
alsa_read_ready()
{
snd_midi_event_init(midi_event_parser);
for (;;) {
snd_seq_event_t *ev;
unsigned char buf[ALSA_BUFSIZE];
long converted;
int r;
r = snd_seq_event_input(snd_handle, &ev);
if (r == -EAGAIN) {
// input queue empty
break;
} else if (r == -ENOSPC) {
warn("Input queue overflow");
continue;
} else if (r < 0) {
warn("snd_seq_event_input() = %d", r);
continue;
}
converted = snd_midi_event_decode(midi_event_parser, buf, ALSA_BUFSIZE, ev);
if (converted < 0) {
if (ev->type == SND_SEQ_EVENT_CLIENT_START ) {
DUMPf("Client started \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_CLIENT_EXIT) {
DUMPf("Client exited \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_CLIENT_CHANGE) {
DUMPf("Client status/info changed \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_PORT_START) {
DUMPf("Port created \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_PORT_EXIT) {
DUMPf("Port deleted \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_PORT_CHANGE) {
DUMPf("Port status/info changed \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_PORT_SUBSCRIBED) {
DUMPf("Port connected \n");
continue;
} else if (ev->type == SND_SEQ_EVENT_PORT_UNSUBSCRIBED) {
DUMPf("Port disconnected \n");
continue;
} else {
warn("Can't decode MIDI event type 0x%02x", ev->type);
}
continue;
}
if (converted < 3) {
int i;
warn("Uh oh, weird MIDI packet with length %ld (does this make sense if prefixed with 90?)", converted);
for (i = 0; i < converted; i += 1) {
fprintf(stderr, " %02x", buf[i]);
}
fprintf(stderr, "\n");
}
usb_write(buf, converted);
}
}
void
alsa_check_fds(fd_set *rfds, fd_set *wfds)
{
int i;
for (i = 0; i < npfd; i += 1) {
int fd = pfd[i].fd;
if (FD_ISSET(fd, rfds) || FD_ISSET(fd, wfds)) {
alsa_read_ready();
return;
}
}
}
void
alsa_write(uint8_t *data, size_t datalen)
{
size_t offset = 0;
snd_midi_event_init(midi_event_parser);
while( datalen > offset) {
snd_seq_event_t ev;
long encoded;
int r;
encoded = snd_midi_event_encode(midi_event_parser, data + offset, datalen - offset, &ev);
if (encoded <= 1) {
int i;
warn("Unable to encode MIDI message (%ld < %ld)", encoded, (long int)datalen);
fprintf(stderr, " ");
for (i = offset; i < (long int)datalen; i += 1) {
fprintf(stderr, "%02x ", data[i]);
}
fprintf(stderr, "\n");
return;
}
snd_seq_ev_set_direct(&ev);
snd_seq_ev_set_source(&ev, seq_port);
snd_seq_ev_set_subs(&ev);
r = snd_seq_event_output(snd_handle, &ev);
if (r < 0) {
warn("Couldn't write an output event");
}
if (r > 200) {
warn("Output buffer size %d", r);
}
offset += encoded;
}
snd_seq_drain_output(snd_handle);
}