2 * aseqdump.c - show the events received at an ALSA sequencer port
4 * Copyright (c) 2005 Clemens Ladisch <clemens@ladisch.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <alsa/asoundlib.h>
33 static snd_seq_t *seq;
34 static int port_count;
35 static snd_seq_addr_t *ports;
36 static volatile sig_atomic_t stop = 0;
39 /* prints an error message to stderr, and dies */
40 static void fatal(const char *msg, ...)
45 vfprintf(stderr, msg, ap);
51 /* memory allocation error handling */
52 static void check_mem(void *p)
55 fatal("Out of memory");
58 /* error handling for ALSA functions */
59 static void check_snd(const char *operation, int err)
62 fatal("Cannot %s - %s", operation, snd_strerror(err));
65 static void init_seq(void)
70 err = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0);
71 check_snd("open sequencer", err);
73 /* set our client's name */
74 err = snd_seq_set_client_name(seq, "aseqdump");
75 check_snd("set client name", err);
78 /* parses one or more port addresses from the string */
79 static void parse_ports(const char *arg)
81 char *buf, *s, *port_name;
84 /* make a copy of the string because we're going to modify it */
88 for (port_name = s = buf; s; port_name = s + 1) {
89 /* Assume that ports are separated by commas. We don't use
90 * spaces because those are valid in client names. */
91 s = strchr(port_name, ',');
96 ports = realloc(ports, port_count * sizeof(snd_seq_addr_t));
99 err = snd_seq_parse_address(seq, &ports[port_count - 1], port_name);
101 fatal("Invalid port %s - %s", port_name, snd_strerror(err));
107 static void create_port(void)
111 err = snd_seq_create_simple_port(seq, "aseqdump",
112 SND_SEQ_PORT_CAP_WRITE |
113 SND_SEQ_PORT_CAP_SUBS_WRITE,
114 SND_SEQ_PORT_TYPE_MIDI_GENERIC |
115 SND_SEQ_PORT_TYPE_APPLICATION);
116 check_snd("create port", err);
119 static void connect_ports(void)
123 for (i = 0; i < port_count; ++i) {
124 err = snd_seq_connect_from(seq, 0, ports[i].client, ports[i].port);
126 fatal("Cannot connect from port %d:%d - %s",
127 ports[i].client, ports[i].port, snd_strerror(err));
131 static void dump_event(const snd_seq_event_t *ev)
133 printf("%3d:%-3d ", ev->source.client, ev->source.port);
135 case SND_SEQ_EVENT_NOTEON:
136 if (ev->data.note.velocity)
137 printf("Note on %2d, note %d, velocity %d\n",
138 ev->data.note.channel, ev->data.note.note, ev->data.note.velocity);
140 printf("Note off %2d, note %d\n",
141 ev->data.note.channel, ev->data.note.note);
143 case SND_SEQ_EVENT_NOTEOFF:
144 printf("Note off %2d, note %d, velocity %d\n",
145 ev->data.note.channel, ev->data.note.note, ev->data.note.velocity);
147 case SND_SEQ_EVENT_KEYPRESS:
148 printf("Polyphonic aftertouch %2d, note %d, value %d\n",
149 ev->data.note.channel, ev->data.note.note, ev->data.note.velocity);
151 case SND_SEQ_EVENT_CONTROLLER:
152 printf("Control change %2d, controller %d, value %d\n",
153 ev->data.control.channel, ev->data.control.param, ev->data.control.value);
155 case SND_SEQ_EVENT_PGMCHANGE:
156 printf("Program change %2d, program %d\n",
157 ev->data.control.channel, ev->data.control.value);
159 case SND_SEQ_EVENT_CHANPRESS:
160 printf("Channel aftertouch %2d, value %d\n",
161 ev->data.control.channel, ev->data.control.value);
163 case SND_SEQ_EVENT_PITCHBEND:
164 printf("Pitch bend %2d, value %d\n",
165 ev->data.control.channel, ev->data.control.value);
167 case SND_SEQ_EVENT_CONTROL14:
168 printf("Control change %2d, controller %d, value %5d\n",
169 ev->data.control.channel, ev->data.control.param, ev->data.control.value);
171 case SND_SEQ_EVENT_NONREGPARAM:
172 printf("Non-reg. parameter %2d, parameter %d, value %d\n",
173 ev->data.control.channel, ev->data.control.param, ev->data.control.value);
175 case SND_SEQ_EVENT_REGPARAM:
176 printf("Reg. parameter %2d, parameter %d, value %d\n",
177 ev->data.control.channel, ev->data.control.param, ev->data.control.value);
179 case SND_SEQ_EVENT_SONGPOS:
180 printf("Song position pointer value %d\n",
181 ev->data.control.value);
183 case SND_SEQ_EVENT_SONGSEL:
184 printf("Song select value %d\n",
185 ev->data.control.value);
187 case SND_SEQ_EVENT_QFRAME:
188 printf("MTC quarter frame %02xh\n",
189 ev->data.control.value);
191 case SND_SEQ_EVENT_TIMESIGN:
192 // XXX how is this encoded?
193 printf("SMF time signature (%#010x)\n",
194 ev->data.control.value);
196 case SND_SEQ_EVENT_KEYSIGN:
197 // XXX how is this encoded?
198 printf("SMF key signature (%#010x)\n",
199 ev->data.control.value);
201 case SND_SEQ_EVENT_START:
202 if (ev->source.client == SND_SEQ_CLIENT_SYSTEM &&
203 ev->source.port == SND_SEQ_PORT_SYSTEM_TIMER)
204 printf("Queue start queue %d\n",
205 ev->data.queue.queue);
209 case SND_SEQ_EVENT_CONTINUE:
210 if (ev->source.client == SND_SEQ_CLIENT_SYSTEM &&
211 ev->source.port == SND_SEQ_PORT_SYSTEM_TIMER)
212 printf("Queue continue queue %d\n",
213 ev->data.queue.queue);
215 printf("Continue\n");
217 case SND_SEQ_EVENT_STOP:
218 if (ev->source.client == SND_SEQ_CLIENT_SYSTEM &&
219 ev->source.port == SND_SEQ_PORT_SYSTEM_TIMER)
220 printf("Queue stop queue %d\n",
221 ev->data.queue.queue);
225 case SND_SEQ_EVENT_SETPOS_TICK:
226 printf("Set tick queue pos. queue %d\n", ev->data.queue.queue);
228 case SND_SEQ_EVENT_SETPOS_TIME:
229 printf("Set rt queue pos. queue %d\n", ev->data.queue.queue);
231 case SND_SEQ_EVENT_TEMPO:
232 printf("Set queue tempo queue %d\n", ev->data.queue.queue);
234 case SND_SEQ_EVENT_CLOCK:
237 case SND_SEQ_EVENT_TICK:
240 case SND_SEQ_EVENT_QUEUE_SKEW:
241 printf("Queue timer skew queue %d\n", ev->data.queue.queue);
243 case SND_SEQ_EVENT_TUNE_REQUEST:
244 printf("Tune request\n");
246 case SND_SEQ_EVENT_RESET:
249 case SND_SEQ_EVENT_SENSING:
250 printf("Active Sensing\n");
252 case SND_SEQ_EVENT_CLIENT_START:
253 printf("Client start client %d\n",
254 ev->data.addr.client);
256 case SND_SEQ_EVENT_CLIENT_EXIT:
257 printf("Client exit client %d\n",
258 ev->data.addr.client);
260 case SND_SEQ_EVENT_CLIENT_CHANGE:
261 printf("Client changed client %d\n",
262 ev->data.addr.client);
264 case SND_SEQ_EVENT_PORT_START:
265 printf("Port start %d:%d\n",
266 ev->data.addr.client, ev->data.addr.port);
268 case SND_SEQ_EVENT_PORT_EXIT:
269 printf("Port exit %d:%d\n",
270 ev->data.addr.client, ev->data.addr.port);
272 case SND_SEQ_EVENT_PORT_CHANGE:
273 printf("Port changed %d:%d\n",
274 ev->data.addr.client, ev->data.addr.port);
276 case SND_SEQ_EVENT_PORT_SUBSCRIBED:
277 printf("Port subscribed %d:%d -> %d:%d\n",
278 ev->data.connect.sender.client, ev->data.connect.sender.port,
279 ev->data.connect.dest.client, ev->data.connect.dest.port);
281 case SND_SEQ_EVENT_PORT_UNSUBSCRIBED:
282 printf("Port unsubscribed %d:%d -> %d:%d\n",
283 ev->data.connect.sender.client, ev->data.connect.sender.port,
284 ev->data.connect.dest.client, ev->data.connect.dest.port);
286 case SND_SEQ_EVENT_SYSEX:
289 printf("System exclusive ");
290 for (i = 0; i < ev->data.ext.len; ++i)
291 printf(" %02X", ((unsigned char*)ev->data.ext.ptr)[i]);
296 printf("Event type %d\n", ev->type);
300 static void list_ports(void)
302 snd_seq_client_info_t *cinfo;
303 snd_seq_port_info_t *pinfo;
305 snd_seq_client_info_alloca(&cinfo);
306 snd_seq_port_info_alloca(&pinfo);
308 puts(" Port Client name Port name");
310 snd_seq_client_info_set_client(cinfo, -1);
311 while (snd_seq_query_next_client(seq, cinfo) >= 0) {
312 int client = snd_seq_client_info_get_client(cinfo);
314 snd_seq_port_info_set_client(pinfo, client);
315 snd_seq_port_info_set_port(pinfo, -1);
316 while (snd_seq_query_next_port(seq, pinfo) >= 0) {
317 /* we need both READ and SUBS_READ */
318 if ((snd_seq_port_info_get_capability(pinfo)
319 & (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ))
320 != (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ))
322 printf("%3d:%-3d %-32.32s %s\n",
323 snd_seq_port_info_get_client(pinfo),
324 snd_seq_port_info_get_port(pinfo),
325 snd_seq_client_info_get_name(cinfo),
326 snd_seq_port_info_get_name(pinfo));
331 static void help(const char *argv0)
333 printf("Usage: %s [options]\n"
334 "\nAvailable options:\n"
335 " -h,--help this help\n"
336 " -V,--version show version\n"
337 " -l,--list list input ports\n"
338 " -p,--port=client:port,... source port(s)\n",
342 static void version(void)
344 puts("aseqdump version " SND_UTIL_VERSION_STR);
347 static void sighandler(int sig)
352 int main(int argc, char *argv[])
354 static const char short_options[] = "hVlp:";
355 static const struct option long_options[] = {
356 {"help", 0, NULL, 'h'},
357 {"version", 0, NULL, 'V'},
358 {"list", 0, NULL, 'l'},
359 {"port", 1, NULL, 'p'},
370 while ((c = getopt_long(argc, argv, short_options,
371 long_options, NULL)) != -1) {
403 err = snd_seq_nonblock(seq, 1);
404 check_snd("set nonblock mode", err);
407 printf("Waiting for data.");
409 printf("Waiting for data at port %d:0.",
410 snd_seq_client_id(seq));
411 printf(" Press Ctrl+C to end.\n");
412 printf("Source Event Ch Data\n");
414 signal(SIGINT, sighandler);
415 signal(SIGTERM, sighandler);
417 npfds = snd_seq_poll_descriptors_count(seq, POLLIN);
418 pfds = alloca(sizeof(*pfds) * npfds);
420 snd_seq_poll_descriptors(seq, pfds, npfds, POLLIN);
421 if (poll(pfds, npfds, -1) < 0)
424 snd_seq_event_t *event;
425 err = snd_seq_event_input(seq, &event);