timestamp

folkert folkert at vanheusden.com
Fri Jul 2 19:50:52 CEST 2021


Hi,

Please consider including the following patch. This patch adds a '-T'
flag which prepends MIDI messages outputted by '--dump' with a
timestamp.
E.g.:

1625245188.993861810) C964
1625245188.994318675) F8
1625245188.995468101) 99647F
1625245189.494490183) F8
1625245189.495448424) 99647F
1625245189.995470260) F8
1625245189.996461381) 99647F
1625245190.494519924) F8
[...]


diff -uNr t3/alsa-utils-1.2.3/amidi/amidi.1 alsa-utils-1.2.3/amidi/amidi.1
--- t3/alsa-utils-1.2.3/amidi/amidi.1	2020-06-07 19:19:42.000000000 +0200
+++ alsa-utils-1.2.3/amidi/amidi.1	2021-07-02 14:35:25.416330576 +0200
@@ -6,6 +6,7 @@
 .SH SYNOPSIS
 \fBamidi\fP [\fI\-p port\fP] [\fI\-s file\fP | \fI\-S data\fP]
 [\fI\-r file\fP] [\fI\-d\fP] [\fI\-t seconds\fP] [\fI\-a\fP]
+[\fI\-T\fP]
 
 .SH DESCRIPTION
 .B amidi
@@ -41,6 +42,10 @@
 options to specify what data to send or receive.
 
 .TP
+.I \-T
+Adds a timestamp in front of each MIDI message.
+
+.TP
 .I \-h, \-\-help
 Help: prints a list of options.
 
diff -uNr t3/alsa-utils-1.2.3/amidi/amidi.c alsa-utils-1.2.3/amidi/amidi.c
--- t3/alsa-utils-1.2.3/amidi/amidi.c	2020-06-07 19:19:42.000000000 +0200
+++ alsa-utils-1.2.3/amidi/amidi.c	2021-07-02 19:41:34.476470438 +0200
@@ -36,6 +36,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <alsa/asoundlib.h>
+#include <time.h>
 #include "aconfig.h"
 #include "version.h"
 
@@ -80,6 +81,7 @@
 		"-r, --receive=file              write received data into a file\n"
 		"-S, --send-hex=\"...\"            send hexadecimal bytes\n"
 		"-d, --dump                      print received data as hexadecimal bytes\n"
+		"-T, --timestamp                 adds a timestamp in front of each dumped message\n"
 		"-t, --timeout=seconds           exits when no data has been received\n"
 		"                                for the specified duration\n"
 		"-a, --active-sensing            include active sensing bytes\n"
@@ -356,7 +358,7 @@
 /*
  * prints MIDI commands, formatting them nicely
  */
-static void print_byte(unsigned char byte)
+static void print_byte(unsigned char byte, struct timespec *ts)
 {
 	static enum {
 		STATE_UNKNOWN,
@@ -426,7 +428,18 @@
 		if (running_status)
 			fputs("\n  ", stdout);
 	}
-	printf("%c%02X", newline ? '\n' : ' ', byte);
+
+	if (newline) {
+		printf("\n");
+
+		/* Nanoseconds does not make a lot of sense for serial MIDI (the
+		 * 31250 bps one) but I'm not sure about MIDI over USB.
+		 */
+		if (ts)
+			printf("%lld.%.9ld) ", (long long)ts->tv_sec, ts->tv_nsec);
+	}
+
+	printf("%02X", byte);
 }
 
 static void sig_handler(int dummy)
@@ -454,7 +467,7 @@
 
 int main(int argc, char *argv[])
 {
-	static const char short_options[] = "hVlLp:s:r:S::dt:aci:";
+	static const char short_options[] = "hVlLp:s:r:S::dt:aci:T";
 	static const struct option long_options[] = {
 		{"help", 0, NULL, 'h'},
 		{"version", 0, NULL, 'V'},
@@ -465,6 +478,7 @@
 		{"receive", 1, NULL, 'r'},
 		{"send-hex", 2, NULL, 'S'},
 		{"dump", 0, NULL, 'd'},
+		{"timestamp", 0, NULL, 'T'},
 		{"timeout", 1, NULL, 't'},
 		{"active-sensing", 0, NULL, 'a'},
 		{"clock", 0, NULL, 'c'},
@@ -475,6 +489,7 @@
 	int ignore_active_sensing = 1;
 	int ignore_clock = 1;
 	int do_send_hex = 0;
+	int do_print_timestamp = 0;
 	struct itimerspec itimerspec = { .it_interval = { 0, 0 } };
 
 	while ((c = getopt_long(argc, argv, short_options,
@@ -509,6 +524,9 @@
 		case 'd':
 			dump = 1;
 			break;
+		case 'T':
+			do_print_timestamp = 1;
+			break;
 		case 't':
 			if (optarg)
 				timeout = atof(optarg);
@@ -611,6 +629,7 @@
 	}
 
 	if (inputp) {
+		int need_timestamp = 0;
 		int read = 0;
 		int npfds;
 		struct pollfd *pfds;
@@ -648,6 +667,7 @@
 			unsigned char buf[256];
 			int i, length;
 			unsigned short revents;
+			struct timespec ts;
 
 			err = poll(pfds, npfds, -1);
 			if (stop || (err < 0 && errno == EINTR))
@@ -657,6 +677,11 @@
 				break;
 			}
 
+			if (clock_gettime(CLOCK_REALTIME, &ts) < 0) {
+				error("clock_getres failed: %s", strerror(errno));
+				break;
+			}
+
 			err = snd_rawmidi_poll_descriptors_revents(input, &pfds[1], npfds - 1, &revents);
 			if (err < 0) {
 				error("cannot get poll events: %s", snd_strerror(errno));
@@ -692,7 +717,8 @@
 				write(receive_file, buf, length);
 			if (dump) {
 				for (i = 0; i < length; ++i)
-					print_byte(buf[i]);
+					print_byte(buf[i], do_print_timestamp ? &ts : NULL);
+
 				fflush(stdout);
 			}
 
diff -uNr t3/alsa-utils-1.2.3/configure.ac alsa-utils-1.2.3/configure.ac
--- t3/alsa-utils-1.2.3/configure.ac	2021-07-02 19:41:46.000000000 +0200
+++ alsa-utils-1.2.3/configure.ac	2021-07-02 15:55:13.245585248 +0200
@@ -97,7 +97,7 @@
 if test x$bat = xtrue; then
 
   saved_CFLAGS="$CFLAGS"
-  saved_LDFLAGS="$LDFLAGS"
+  saved_LDFLAGS="$LDFLAGS -lrt"
   saved_LIBS="$LIBS"
   FFTW_INC=""
   FFTW_LIB=""
@@ -116,7 +116,7 @@
   FFTW_CFLAGS="$CFLAGS"
   FFTW_LIB="$LIBS"
   CFLAGS="$saved_CFLAGS"
-  LDFLAGS="$saved_LDFLAGS"
+  LDFLAGS="$saved_LDFLAGS -lrt"
   LIBS="$saved_LIBS"
   AC_SUBST(FFTW_INC)
   AC_SUBST(FFTW_LIB)
@@ -292,11 +292,11 @@
   fi
 
   saved_CFLAGS="$CFLAGS"
-  saved_LDFLAGS="$LDFLAGS"
+  saved_LDFLAGS="$LDFLAGS -lrt"
   saved_LIBS="$LIBS"
   CFLAGS="$CFLAGS $CURSES_CFLAGS"
   if test -n "$CURSESLIBDIR"; then
-    LDFLAGS="$LDFLAGS -L$CURSESLIBDIR"
+    LDFLAGS="$LDFLAGS -L$CURSESLIBDIR -lrt"
   fi
   LIBS="$CURSESLIB $LIBS"
 
@@ -342,7 +342,7 @@
                [AC_MSG_ERROR([form$NCURSESLIBSUFFIX library not found])])
 
   CFLAGS="$saved_CFLAGS"
-  LDFLAGS="$saved_LDFLAGS"
+  LDFLAGS="$saved_LDFLAGS -lrt"
   LIBS="$saved_LIBS"
 
   if test -n "$CURSESLIBDIR"; then


More information about the Alsa-devel mailing list