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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
* Simple event decoder
*/
static char *event_names[256] = {
[SND_SEQ_EVENT_SYSTEM]= "System",
[SND_SEQ_EVENT_RESULT]= "Result",
[SND_SEQ_EVENT_NOTE]= "Note",
[SND_SEQ_EVENT_NOTEON]= "Note On",
[SND_SEQ_EVENT_NOTEOFF]= "Note Off",
[SND_SEQ_EVENT_KEYPRESS]= "Key Pressure",
[SND_SEQ_EVENT_CONTROLLER]= "Controller",
[SND_SEQ_EVENT_PGMCHANGE]= "Program Change",
[SND_SEQ_EVENT_CHANPRESS]= "Channel Pressure",
[SND_SEQ_EVENT_PITCHBEND]= "Pitchbend",
[SND_SEQ_EVENT_CONTROL14]= "Control14",
[SND_SEQ_EVENT_NONREGPARAM]= "Nonregparam",
[SND_SEQ_EVENT_REGPARAM]= "Regparam",
[SND_SEQ_EVENT_SONGPOS]= "Song Position",
[SND_SEQ_EVENT_SONGSEL]= "Song Select",
[SND_SEQ_EVENT_QFRAME]= "Qframe",
[SND_SEQ_EVENT_TIMESIGN]= "SMF Time Signature",
[SND_SEQ_EVENT_KEYSIGN]= "SMF Key Signature",
[SND_SEQ_EVENT_START]= "Start",
[SND_SEQ_EVENT_CONTINUE]= "Continue",
[SND_SEQ_EVENT_STOP]= "Stop",
[SND_SEQ_EVENT_SETPOS_TICK]= "Set Position Tick",
[SND_SEQ_EVENT_SETPOS_TIME]= "Set Position Time",
[SND_SEQ_EVENT_TEMPO]= "Tempo",
[SND_SEQ_EVENT_CLOCK]= "Clock",
[SND_SEQ_EVENT_TICK]= "Tick",
[SND_SEQ_EVENT_SYNC]= "Sync",
[SND_SEQ_EVENT_SYNC_POS]= "Sync Position",
[SND_SEQ_EVENT_TUNE_REQUEST]= "Tune Request",
[SND_SEQ_EVENT_RESET]= "Reset",
[SND_SEQ_EVENT_SENSING]= "Active Sensing",
[SND_SEQ_EVENT_ECHO]= "Echo",
[SND_SEQ_EVENT_OSS]= "OSS",
[SND_SEQ_EVENT_CLIENT_START]= "Client Start",
[SND_SEQ_EVENT_CLIENT_EXIT]= "Client Exit",
[SND_SEQ_EVENT_CLIENT_CHANGE]= "Client Change",
[SND_SEQ_EVENT_PORT_START]= "Port Start",
[SND_SEQ_EVENT_PORT_EXIT]= "Port Exit",
[SND_SEQ_EVENT_PORT_CHANGE]= "Port Change",
[SND_SEQ_EVENT_PORT_SUBSCRIBED]= "Port Subscribed",
[SND_SEQ_EVENT_PORT_UNSUBSCRIBED]= "Port Unsubscribed",
[SND_SEQ_EVENT_SAMPLE]= "Sample",
[SND_SEQ_EVENT_SAMPLE_CLUSTER]= "Sample Cluster",
[SND_SEQ_EVENT_SAMPLE_START]= "Sample Start",
[SND_SEQ_EVENT_SAMPLE_STOP]= "Sample Stop",
[SND_SEQ_EVENT_SAMPLE_FREQ]= "Sample Freq",
[SND_SEQ_EVENT_SAMPLE_VOLUME]= "Sample Volume",
[SND_SEQ_EVENT_SAMPLE_LOOP]= "Sample Loop",
[SND_SEQ_EVENT_SAMPLE_POSITION]= "Sample Position",
[SND_SEQ_EVENT_SAMPLE_PRIVATE1]= "Sample Private1",
[SND_SEQ_EVENT_USR0]= "User 0",
[SND_SEQ_EVENT_USR1]= "User 1",
[SND_SEQ_EVENT_USR2]= "User 2",
[SND_SEQ_EVENT_USR3]= "User 3",
[SND_SEQ_EVENT_USR4]= "User 4",
[SND_SEQ_EVENT_USR5]= "User 5",
[SND_SEQ_EVENT_USR6]= "User 6",
[SND_SEQ_EVENT_USR7]= "User 7",
[SND_SEQ_EVENT_USR8]= "User 8",
[SND_SEQ_EVENT_USR9]= "User 9",
[SND_SEQ_EVENT_INSTR_BEGIN]= "Instr Begin",
[SND_SEQ_EVENT_INSTR_END]= "Instr End",
[SND_SEQ_EVENT_INSTR_INFO]= "Instr Info",
[SND_SEQ_EVENT_INSTR_INFO_RESULT]= "Instr Info Result",
[SND_SEQ_EVENT_INSTR_FINFO]= "Instr Font Info",
[SND_SEQ_EVENT_INSTR_FINFO_RESULT]= "Instr Font Info Result",
[SND_SEQ_EVENT_INSTR_RESET]= "Instr Reset",
[SND_SEQ_EVENT_INSTR_STATUS]= "Instr Status",
[SND_SEQ_EVENT_INSTR_STATUS_RESULT]= "Instr Status Result",
[SND_SEQ_EVENT_INSTR_PUT]= "Instr Put",
[SND_SEQ_EVENT_INSTR_GET]= "Instr Get",
[SND_SEQ_EVENT_INSTR_GET_RESULT]= "Instr Get Result",
[SND_SEQ_EVENT_INSTR_FREE]= "Instr Free",
[SND_SEQ_EVENT_INSTR_LIST]= "Instr List",
[SND_SEQ_EVENT_INSTR_LIST_RESULT]= "Instr List Result",
[SND_SEQ_EVENT_INSTR_CLUSTER]= "Instr Cluster",
[SND_SEQ_EVENT_INSTR_CLUSTER_GET]= "Instr Cluster Get",
[SND_SEQ_EVENT_INSTR_CLUSTER_RESULT]= "Instr Cluster Result",
[SND_SEQ_EVENT_INSTR_CHANGE]= "Instr Change",
[SND_SEQ_EVENT_SYSEX]= "Sysex",
[SND_SEQ_EVENT_BOUNCE]= "Bounce",
[SND_SEQ_EVENT_USR_VAR0]= "User Var0",
[SND_SEQ_EVENT_USR_VAR1]= "User Var1",
[SND_SEQ_EVENT_USR_VAR2]= "User Var2",
[SND_SEQ_EVENT_USR_VAR3]= "User Var3",
[SND_SEQ_EVENT_USR_VAR4]= "User Var4",
#if 0
[SND_SEQ_EVENT_IPCSHM]= "IPC Shm",
[SND_SEQ_EVENT_USR_VARIPC0]= "User IPC0",
[SND_SEQ_EVENT_USR_VARIPC1]= "User IPC1",
[SND_SEQ_EVENT_USR_VARIPC2]= "User IPC2",
[SND_SEQ_EVENT_USR_VARIPC3]= "User IPC3",
[SND_SEQ_EVENT_USR_VARIPC4]= "User IPC4",
#endif
[SND_SEQ_EVENT_NONE]= "None",
};
int decode_event(snd_seq_event_t * ev)
{
char *space = " ";
printf("EVENT>>> Type = %d, flags = 0x%x", ev->type, ev->flags);
switch (ev->flags & SND_SEQ_TIME_STAMP_MASK) {
case SND_SEQ_TIME_STAMP_TICK:
printf(", time = %d ticks",
ev->time.tick);
break;
case SND_SEQ_TIME_STAMP_REAL:
printf(", time = %d.%09d",
(int)ev->time.time.tv_sec,
(int)ev->time.time.tv_nsec);
break;
}
printf("\n%sSource = %d.%d, dest = %d.%d, queue = %d\n",
space,
ev->source.client,
ev->source.port,
ev->dest.client,
ev->dest.port,
ev->queue);
if (event_names[ev->type])
printf("%sEvent = %s", space, event_names[ev->type]);
else
printf("%sEvent = Reserved %d\n", space, ev->type);
/* decode actual event data... */
switch (ev->type) {
case SND_SEQ_EVENT_NOTE:
printf("; ch=%d, note=%d, velocity=%d, off_velocity=%d, duration=%d\n",
ev->data.note.channel,
ev->data.note.note,
ev->data.note.velocity,
ev->data.note.off_velocity,
ev->data.note.duration);
break;
case SND_SEQ_EVENT_NOTEON:
case SND_SEQ_EVENT_NOTEOFF:
case SND_SEQ_EVENT_KEYPRESS:
printf("; ch=%d, note=%d, velocity=%d\n",
ev->data.note.channel,
ev->data.note.note,
ev->data.note.velocity);
break;
case SND_SEQ_EVENT_CONTROLLER:
printf("; ch=%d, param=%i, value=%i\n",
ev->data.control.channel,
ev->data.control.param,
ev->data.control.value);
break;
case SND_SEQ_EVENT_PGMCHANGE:
printf("; ch=%d, program=%i\n",
ev->data.control.channel,
ev->data.control.value);
break;
case SND_SEQ_EVENT_CHANPRESS:
case SND_SEQ_EVENT_PITCHBEND:
printf("; ch=%d, value=%i\n",
ev->data.control.channel,
ev->data.control.value);
break;
case SND_SEQ_EVENT_SYSEX:
{
unsigned char *sysex = (unsigned char *) ev + sizeof(snd_seq_event_t);
unsigned int c;
printf("; len=%d [", ev->data.ext.len);
for (c = 0; c < ev->data.ext.len; c++) {
printf("%02x%s", sysex[c], c < ev->data.ext.len - 1 ? ":" : "");
}
printf("]\n");
}
break;
case SND_SEQ_EVENT_QFRAME:
printf("; frame=0x%02x\n", ev->data.control.value);
break;
case SND_SEQ_EVENT_CLOCK:
case SND_SEQ_EVENT_START:
case SND_SEQ_EVENT_CONTINUE:
case SND_SEQ_EVENT_STOP:
printf("; queue = %i\n", ev->data.queue.queue);
break;
case SND_SEQ_EVENT_SENSING:
printf("\n");
break;
case SND_SEQ_EVENT_ECHO:
{
int i;
printf("; ");
for (i = 0; i < 8; i++) {
printf("%02i%s", ev->data.raw8.d[i], i < 7 ? ":" : "\n");
}
}
break;
case SND_SEQ_EVENT_CLIENT_START:
case SND_SEQ_EVENT_CLIENT_EXIT:
case SND_SEQ_EVENT_CLIENT_CHANGE:
printf("; client=%i\n", ev->data.addr.client);
break;
case SND_SEQ_EVENT_PORT_START:
case SND_SEQ_EVENT_PORT_EXIT:
case SND_SEQ_EVENT_PORT_CHANGE:
printf("; client=%i, port = %i\n", ev->data.addr.client, ev->data.addr.port);
break;
case SND_SEQ_EVENT_PORT_SUBSCRIBED:
case SND_SEQ_EVENT_PORT_UNSUBSCRIBED:
printf("; %i:%i -> %i:%i\n",
ev->data.connect.sender.client, ev->data.connect.sender.port,
ev->data.connect.dest.client, ev->data.connect.dest.port);
break;
default:
printf("; not implemented\n");
}
switch (ev->flags & SND_SEQ_EVENT_LENGTH_MASK) {
case SND_SEQ_EVENT_LENGTH_FIXED:
return sizeof(snd_seq_event_t);
case SND_SEQ_EVENT_LENGTH_VARIABLE:
return sizeof(snd_seq_event_t) + ev->data.ext.len;
}
return 0;
}
void event_decoder_start_timer(snd_seq_t *handle, int queue, int client, int port)
{
int err;
if ((err = snd_seq_start_queue(handle, queue, NULL))<0)
fprintf(stderr, "Timer event output error: %s\n", snd_strerror(err));
while (snd_seq_drain_output(handle)>0)
sleep(1);
}
void event_decoder(snd_seq_t *handle, int argc, char *argv[])
{
snd_seq_event_t *ev;
snd_seq_port_info_t *pinfo;
snd_seq_port_subscribe_t *sub;
snd_seq_addr_t addr;
int client, port, queue, max, err, v1, v2;
char *ptr;
struct pollfd *pfds;
if ((client = snd_seq_client_id(handle))<0) {
fprintf(stderr, "Cannot determine client number: %s\n", snd_strerror(client));
return;
}
printf("Client ID = %i\n", client);
if ((queue = snd_seq_alloc_queue(handle))<0) {
fprintf(stderr, "Cannot allocate queue: %s\n", snd_strerror(queue));
return;
}
printf("Queue ID = %i\n", queue);
if ((err = snd_seq_nonblock(handle, 1))<0)
fprintf(stderr, "Cannot set nonblock mode: %s\n", snd_strerror(err));
snd_seq_port_info_alloca(&pinfo);
snd_seq_port_info_set_name(pinfo, "Input");
snd_seq_port_info_set_capability(pinfo, SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_WRITE);
if ((err = snd_seq_create_port(handle, pinfo)) < 0) {
fprintf(stderr, "Cannot create input port: %s\n", snd_strerror(err));
return;
}
port = snd_seq_port_info_get_port(pinfo);
event_decoder_start_timer(handle, queue, client, port);
snd_seq_port_subscribe_alloca(&sub);
addr.client = SND_SEQ_CLIENT_SYSTEM;
addr.port = SND_SEQ_PORT_SYSTEM_ANNOUNCE;
snd_seq_port_subscribe_set_sender(sub, &addr);
addr.client = client;
addr.port = port;
snd_seq_port_subscribe_set_dest(sub, &addr);
snd_seq_port_subscribe_set_queue(sub, queue);
snd_seq_port_subscribe_set_time_update(sub, 1);
snd_seq_port_subscribe_set_time_real(sub, 1);
if ((err = snd_seq_subscribe_port(handle, sub))<0) {
fprintf(stderr, "Cannot subscribe announce port: %s\n", snd_strerror(err));
return;
}
addr.client = SND_SEQ_CLIENT_SYSTEM;
addr.port = SND_SEQ_PORT_SYSTEM_TIMER;
snd_seq_port_subscribe_set_sender(sub, &addr);
if ((err = snd_seq_subscribe_port(handle, sub))<0) {
fprintf(stderr, "Cannot subscribe timer port: %s\n", snd_strerror(err));
return;
}
for (max = 0; max < argc; max++) {
ptr = argv[max];
if (!ptr)
continue;
snd_seq_port_subscribe_set_time_real(sub, 0);
if (tolower(*ptr) == 'r') {
snd_seq_port_subscribe_set_time_real(sub, 1);
ptr++;
}
if (sscanf(ptr, "%i.%i", &v1, &v2) != 2) {
fprintf(stderr, "Wrong argument '%s'...\n", argv[max]);
return;
}
addr.client = v1;
addr.port = v2;
snd_seq_port_subscribe_set_sender(sub, &addr);
if ((err = snd_seq_subscribe_port(handle, sub))<0) {
fprintf(stderr, "Cannot subscribe port %i from client %i: %s\n", v2, v1, snd_strerror(err));
return;
}
}
max = snd_seq_poll_descriptors_count(handle, POLLIN);
pfds = alloca(sizeof(*pfds) * max);
while (1) {
snd_seq_poll_descriptors(handle, pfds, max, POLLIN);
if (poll(pfds, max, -1) < 0)
break;
do {
if ((err = snd_seq_event_input(handle, &ev))<0)
break;
if (!ev)
continue;
decode_event(ev);
snd_seq_free_event(ev);
} while (err > 0);
}
}
|