-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_midi.cpp
217 lines (188 loc) · 5.83 KB
/
my_midi.cpp
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
#include "alloca.h"
#include <alsa/asoundlib.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <iostream>
#include <math.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>
static void error(const char *format, ...) {
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
putc('\n', stderr);
}
static void list_device(snd_ctl_t *ctl, int card, int device) {
snd_rawmidi_info_t *info;
const char *name;
const char *sub_name;
int subs, subs_in, subs_out;
int sub;
int err;
snd_rawmidi_info_alloca(&info);
snd_rawmidi_info_set_device(info, device);
snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT);
err = snd_ctl_rawmidi_info(ctl, info);
if (err >= 0)
subs_in = snd_rawmidi_info_get_subdevices_count(info);
else
subs_in = 0;
snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT);
err = snd_ctl_rawmidi_info(ctl, info);
if (err >= 0)
subs_out = snd_rawmidi_info_get_subdevices_count(info);
else
subs_out = 0;
subs = subs_in > subs_out ? subs_in : subs_out;
if (!subs)
return;
for (sub = 0; sub < subs; ++sub) {
snd_rawmidi_info_set_stream(info,
sub < subs_in ? SND_RAWMIDI_STREAM_INPUT
: SND_RAWMIDI_STREAM_OUTPUT);
snd_rawmidi_info_set_subdevice(info, sub);
err = snd_ctl_rawmidi_info(ctl, info);
if (err < 0) {
error("cannot get rawmidi information %d:%d:%d: %s\n", card, device, sub,
snd_strerror(err));
return;
}
name = snd_rawmidi_info_get_name(info);
sub_name = snd_rawmidi_info_get_subdevice_name(info);
if (sub == 0 && sub_name[0] == '\0') {
printf("%c%c hw:%d,%d %s", sub < subs_in ? 'I' : ' ',
sub < subs_out ? 'O' : ' ', card, device, name);
if (subs > 1)
printf(" (%d subdevices)", subs);
putchar('\n');
break;
} else {
printf("%c%c hw:%d,%d,%d %s\n", sub < subs_in ? 'I' : ' ',
sub < subs_out ? 'O' : ' ', card, device, sub, sub_name);
}
}
}
struct my_chan {
std::string port;
snd_rawmidi_t* input;
snd_rawmidi_t** inputp;
size_t size;
public:
my_chan(const std::string s) : port(s) {
std::cout << "Woot " << s << std::endl;
inputp = &input;
std::cout << "ptr=" << input << " *ptr=" << inputp << std::endl;
}
};
int main(int argc, char *argv[]) {
// device_list();
// list_card_devices(1);
snd_ctl_t *ctl;
int device = -1;
int card = -1;
int err;
if ((err = snd_card_next(&card)) < 0) {
error("cannot determine card number: %s", snd_strerror(err));
return 0;
}
std::cout << "Card is " << std::to_string(card) << std::endl;
auto x = snd_ctl_open(&ctl, "hw:1", 0);
std::cout << " x is " << x << std::endl;
auto x2 = snd_ctl_rawmidi_next_device(ctl, &device);
std::cout << "x2 is " << std::to_string(x2) << std::endl;
std::cout << "device is " << device << std::endl;
//list_device(ctl, x, device);
// snd_rawmidi_t *nput, **inputp;
// snd_rawmidi_t *output, **outputp;
std::vector<my_chan> ports = { {"hw:1,0,2"}, { "hw:1,0,1"}, {"hw:1,0,0"} };
// std::cout << "Input is " << input << std::endl;
for ( auto& chan : ports) {
std::cout << "port is " << chan.port << std::endl;
std::cout << "input is " << chan.input << " ptr=" << chan.inputp << std::endl;
if ((err = snd_rawmidi_open(&chan.input, nullptr, chan.port.c_str(), SND_RAWMIDI_NONBLOCK)) <
0) {
error("cannot open port \"%s\": %s", chan.port.c_str(), snd_strerror(err));
}
std::cout << "err=" << err << std::endl;
std::cout << "input is " << chan.input << " ptr=" << chan.inputp << std::endl;
snd_rawmidi_read(chan.input, nullptr, 0); /* trigger reading */
}
//std::cout << "Input is " << input << std::endl;
int read = 0;
int npfds;
struct pollfd *pfds;
npfds = 1;
for( auto &p : ports) {
p.size = snd_rawmidi_poll_descriptors_count(p.input);
npfds += p.size;
}
pfds = (pollfd *)alloca(npfds * sizeof(struct pollfd));
pfds[0].fd = -1;
for(auto &p : ports) {
int idx= 0;
snd_rawmidi_poll_descriptors(p.input, &pfds[idx], npfds - 1);
idx += p.size;
}
std::cout << "npfds is " << std::endl;
// signal(SIGINT, sig_handler);
std::cout << "Main loop" << std::endl;
for (;;) {
// std::cout << "Poll" << std::endl;
uint8_t buf[256];
int i, length;
unsigned short revents;
err = poll(pfds, npfds, -1);
if (err < 0) {
error("poll failed: %s", strerror(errno));
break;
}
err = snd_rawmidi_poll_descriptors_revents(ports[0].input, &pfds[1], npfds - 1,
&revents);
if (err < 0) {
error("cannot get poll events: %s", snd_strerror(err));
break;
}
if (revents & (POLLERR | POLLHUP))
break;
if (!(revents & POLLIN)) {
if (pfds[0].revents & POLLIN)
continue;
}
err = snd_rawmidi_read(ports[0].input, buf, sizeof(buf));
if (err == -EAGAIN)
continue;
if (err < 0) {
error("cannot read from port \"%s\": %s", "foo",
snd_strerror(err));
break;
}
length = 0;
for (i = 0; i < err; ++i)
// if ((buf[i] != MIDI_CMD_COMMON_CLOCK &&
// buf[i] != MIDI_CMD_COMMON_SENSING) ||
// (buf[i] == MIDI_CMD_COMMON_CLOCK && !ignore_clock) ||
// (buf[i] == MIDI_CMD_COMMON_SENSING && !ignore_active_sensing))
buf[length++] = buf[i];
if (length == 0)
continue;
read += length;
for (i = 0; i < length; ++i) {
std::cout << std::hex << buf[i] << " ";
// print_byte(buf[i]);
};
std::cout << std::endl;
}
};