amidi: fix handling of ignored messages
cover amidi: fix handling of ignored messages
Clemens Ladisch (2):
amidi: ignore not only Active Sensing but also Clock bytes
amidi: fix timeout handling
amidi/amidi.1 | 19 ++++++++--- amidi/amidi.c | 82 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 23 deletions(-)
PATCH 1/2 amidi: ignore not only Active Sensing but also Clock bytes
Active Sensing messages are sent by many devices in the background and would only interfere with the actual messages that amidi is supposed to capture. Therefore, amidi ignores them by default. However, there are also devices that send Clock messages with the same problem, so it is a better idea to filter them out, too.
--- amidi/amidi.1 | 19 ++++++++++++++----- amidi/amidi.c | 13 +++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-)
amidi/amidi.1 +14 -5
@@ -1,4 +1,4 @@ -.TH AMIDI 1 "26 Jun 2006" +.TH AMIDI 1 "16 Apr 2016" .SH NAME amidi \- read from and write to ALSA RawMIDI ports
@@ -80,9 +80,11 @@ to record a Standard MIDI (.mid) file, use .B arecordmidi(1). .B amidi -will filter out any Active Sensing bytes (FEh), unless the +will filter out any Active Sensing and Clock bytes (FEh, F8h), unless the .I \-a -option has been given. +or +.I \-c +options have been given. .TP .I \-S, \-\-send\-hex="..."
@@ -91,9 +93,11 @@ Sends the bytes specified as hexadecimal numbers to the MIDI port. .TP .I \-d, \-\-dump Prints data received from the MIDI port as hexadecimal bytes. -Active Sensing bytes (FEh) will not be shown, unless the +Active Sensing and Clock bytes (FEh, F8h) will not be shown, unless the .I \-a -option has been given. +or +.I \-c +options have been given. This option is useful for debugging.
@@ -111,6 +115,11 @@ to stop receiving data. Does not ignore Active Sensing bytes (FEh) when saving or printing received MIDI commands. +.TP +.I \-c, \-\-clock +Does not ignore Clock bytes (F8h) when saving or printing received +MIDI commands. + .SH EXAMPLES .TP
amidi/amidi.c +11 -2
@@ -77,7 +77,8 @@ static void usage(void)"-d, --dump print received data as hexadecimal bytes\n" "-t, --timeout=seconds exits when no data has been received\n" " for the specified duration\n" - "-a, --active-sensing don't ignore active sensing bytes\n"); +"-a, --active-sensing include active sensing bytes\n" + "-c, --clock include clock bytes\n"); }static void version (void )
@@ -419,10 +420,12 @@ int main(int argc, char *argv[]) {"dump", 0, NULL, 'd'}, {"timeout", 1, NULL, 't'}, {"active-sensing", 0, NULL, 'a'}, + {"clock", 0, NULL, 'c'}, { } };int c, err, ok =0 ;int ignore_active_sensing =1 ; +int ignore_clock =1 ;int do_send_hex =0 ; while ((c = getopt_long(argc, argv, short_options,
@@ -463,6 +466,9 @@ int main(int argc, char *argv[])case 'a' : ignore_active_sensing =0 ;break ; +case 'c' : + ignore_clock =0 ; +break ;default :error ("Try `amidi --help' for more information." );return 1 ;
@@ -589,7 +595,10 @@ int main(int argc, char *argv[]) } length =0 ;for (i =0 ; i < err; ++i) -if (!ignore_active_sensing || buf[i] !=0xfe ) +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 ;
Re: [PATCH 1/2] amidi: ignore not only Active Sensing but also Clock bytes
Hi Clemens,
Quoted 30 lines · Clemens Ladisch
Active Sensing messages are sent by many devices in the background and would only interfere with the actual messages that amidi is supposed to capture. Therefore, amidi ignores them by default. However, there are also devices that send Clock messages with the same problem, so it is a better idea to filter them out, too.
Reported-by Martin Tarenskeen <[email protected]>Signed-off-by Clemens Ladisch <[email protected]>--- amidi/amidi.1 | 19 ++++++++++++++----- amidi/amidi.c | 13 +++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-)...
amidi/amidi.c +2
-1@@ -77,7 +77,8 @@ static void usage(void)"-d, --dump print received data as hexadecimal bytes\n" "-t, --timeout=seconds exits when no data has been received\n" " for the specified duration\n" - "-a, --active-sensing don't ignore active sensing bytes\n"); +"-a, --active-sensing include active sensing bytes\n" + "-c, --clock include clock bytes\n"); }static void version (void )
amidi/amidi.c int main(int argc, char *argv[]) · line 424
Quoted 5 lines
amidi/amidi.c +1
-0@@ -419,10 +420,12 @@ int main(int argc, char *argv[]) {"dump", 0, NULL, 'd'}, {"timeout", 1, NULL, 't'}, {"active-sensing", 0, NULL, 'a'}, + {"clock", 0, NULL, 'c'}, { }
Need to modify a table for short options.
amidi/amidi.c +1 -1
@@ -411,7 +411,7 @@ static void add_send_hex_data(const char *str)int main (int argc,char *argv[]) { -static const char short_options[] ="hVlLp:s:r:S::dt:a" ; +static const char short_options[] ="hVlLp:s:r:S::dt:a:c" ;static const struct option long_options[] = { {"help" ,0 ,NULL ,'h' }, {"version" ,0 ,NULL ,'V' },
Regards
Takashi Sakamoto
Re: [PATCH 1/2] amidi: ignore not only Active Sensing but also Clock bytes
amidi/amidi.c static void add_send_hex_data(const char *str) · line 414
Quoted 12 lines · Takashi Sakamoto
Need to modify a table for short options.
amidi/amidi.c +1
-1@@ -411,7 +411,7 @@ static void add_send_hex_data(const char *str)int main (int argc,char *argv[]) { -static const char short_options[] ="hVlLp:s:r:S::dt:a" ; +static const char short_options[] ="hVlLp:s:r:S::dt:a:c" ;
Oops. This should be "hVlLp:s:r:S::dt:ac"...
amidi/amidi.c static void add_send_hex_data(const char *str) · line 417
Quoted 3 lines
amidi/amidi.c +0
-0static const struct option long_options[] = { {"help" ,0 ,NULL ,'h' }, {"version" ,0 ,NULL ,'V' },
Regards
Takashi Sakamoto
v2 cover amidi: fix handling of ignored messages
Clemens Ladisch (2):
amidi: ignore not only Active Sensing but also Clock bytes
amidi: fix timeout handling
v2: fix short_options
amidi/amidi.1 | 19 ++++++++--- amidi/amidi.c | 84 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 79 insertions(+), 24 deletions(-)
PATCH v2 1/2 amidi: ignore not only Active Sensing but also Clock bytes
Active Sensing messages are sent by many devices in the background and would only interfere with the actual messages that amidi is supposed to capture. Therefore, amidi ignores them by default. However, there are also devices that send Clock messages with the same problem, so it is a better idea to filter them out, too.
--- amidi/amidi.1 | 19 ++++++++++++++----- amidi/amidi.c | 15 ++++++++++++--- 2 files changed, 26 insertions(+), 8 deletions(-)
amidi/amidi.1 +14 -5
@@ -1,4 +1,4 @@ -.TH AMIDI 1 "26 Jun 2006" +.TH AMIDI 1 "16 Apr 2016" .SH NAME amidi \- read from and write to ALSA RawMIDI ports
@@ -80,9 +80,11 @@ to record a Standard MIDI (.mid) file, use .B arecordmidi(1). .B amidi -will filter out any Active Sensing bytes (FEh), unless the +will filter out any Active Sensing and Clock bytes (FEh, F8h), unless the .I \-a -option has been given. +or +.I \-c +options have been given. .TP .I \-S, \-\-send\-hex="..."
@@ -91,9 +93,11 @@ Sends the bytes specified as hexadecimal numbers to the MIDI port. .TP .I \-d, \-\-dump Prints data received from the MIDI port as hexadecimal bytes. -Active Sensing bytes (FEh) will not be shown, unless the +Active Sensing and Clock bytes (FEh, F8h) will not be shown, unless the .I \-a -option has been given. +or +.I \-c +options have been given. This option is useful for debugging.
@@ -111,6 +115,11 @@ to stop receiving data. Does not ignore Active Sensing bytes (FEh) when saving or printing received MIDI commands. +.TP +.I \-c, \-\-clock +Does not ignore Clock bytes (F8h) when saving or printing received +MIDI commands. + .SH EXAMPLES .TP
amidi/amidi.c +12 -3
@@ -77,7 +77,8 @@ static void usage(void)"-d, --dump print received data as hexadecimal bytes\n" "-t, --timeout=seconds exits when no data has been received\n" " for the specified duration\n" - "-a, --active-sensing don't ignore active sensing bytes\n"); +"-a, --active-sensing include active sensing bytes\n" + "-c, --clock include clock bytes\n"); }static void version (void )
@@ -406,7 +407,7 @@ static void add_send_hex_data(const char *str)int main (int argc,char *argv[]) { -static const char short_options[] ="hVlLp:s:r:S::dt:a" ; +static const char short_options[] ="hVlLp:s:r:S::dt:ac" ;static const struct option long_options[] = { {"help" ,0 ,NULL ,'h' }, {"version" ,0 ,NULL ,'V' },
@@ -419,10 +420,12 @@ int main(int argc, char *argv[]) {"dump", 0, NULL, 'd'}, {"timeout", 1, NULL, 't'}, {"active-sensing", 0, NULL, 'a'}, + {"clock", 0, NULL, 'c'}, { } };int c, err, ok =0 ;int ignore_active_sensing =1 ; +int ignore_clock =1 ;int do_send_hex =0 ; while ((c = getopt_long(argc, argv, short_options,
@@ -463,6 +466,9 @@ int main(int argc, char *argv[])case 'a' : ignore_active_sensing =0 ;break ; +case 'c' : + ignore_clock =0 ; +break ;default :error ("Try `amidi --help' for more information." );return 1 ;
@@ -589,7 +595,10 @@ int main(int argc, char *argv[]) } length =0 ;for (i =0 ; i < err; ++i) -if (!ignore_active_sensing || buf[i] !=0xfe ) +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 ;
PATCH v2 2/2 amidi: fix timeout handling
In reply to Clemens LadischThe timeout is not supposed to expire when ignored messages are received. This cannot be handled with the poll() timeout, so add a separate timer.
--- amidi/amidi.c | 69 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 16 deletions(-)
amidi/amidi.c +53 -16
@@ -25,9 +25,11 @@#include <stdarg.h> #include <string.h> #include <ctype.h> +#include <math.h> #include <getopt.h> #include <errno.h> #include <signal.h> +#include <sys/timerfd.h> #include <sys/types.h> #include <sys/poll.h> #include <sys/stat.h>
@@ -37,6 +39,8 @@#include "aconfig.h" #include "version.h" +#define NSEC_PER_SEC 1000000000L +static int do_device_list, do_rawmidi_list;static char *port_name ="default" ;static char *send_file_name;
@@ -46,7 +50,7 @@ static char *send_data;static int send_data_length;static int receive_file;static int dump; -static int timeout; +static float timeout;static int stop;static snd_rawmidi_t *input, **inputp;static snd_rawmidi_t *output, **outputp;
@@ -427,6 +431,7 @@ int main(int argc, char *argv[])int ignore_active_sensing =1 ;int ignore_clock =1 ;int do_send_hex =0 ; +struct itimerspec itimerspec = { .it_interval = {0 ,0 } };while ((c =getopt_long (argc, argv, short_options, long_options,NULL )) !=-1 ) {
@@ -461,7 +466,7 @@ int main(int argc, char *argv[]) dump =1 ;break ;case 't' : - timeout =atoi (optarg); + timeout =atof (optarg);break ;case 'a' : ignore_active_sensing =0 ;
@@ -552,40 +557,64 @@ int main(int argc, char *argv[])if (inputp) {int read =0 ; -int npfds, time =0 ; +int npfds;struct pollfd *pfds; - timeout *=1000 ; - npfds =snd_rawmidi_poll_descriptors_count (input); + npfds =1 +snd_rawmidi_poll_descriptors_count (input); pfds =alloca (npfds *sizeof (struct pollfd )); -snd_rawmidi_poll_descriptors (input, pfds, npfds); + +if (timeout >0 ) { + pfds[0 ].fd =timerfd_create (CLOCK_MONOTONIC ,0 ); +if (pfds[0 ].fd ==-1 ) { +error ("cannot create timer: %s" ,strerror (errno)); + goto _exit; + } + pfds[0 ].events =POLLIN ; + }else { + pfds[0 ].fd =-1 ; + } + +snd_rawmidi_poll_descriptors (input, &pfds[1 ], npfds -1 ); +signal (SIGINT , sig_handler); + +if (timeout >0 ) { +float timeout_int; + + itimerspec.it_value.tv_nsec =modff (timeout, &timeout_int) *NSEC_PER_SEC ; + itimerspec.it_value.tv_sec = timeout_int; + err =timerfd_settime (pfds[0 ].fd,0 , &itimerspec,NULL ); +if (err <0 ) { +error ("cannot set timer: %s" ,strerror (errno)); + goto _exit; + } + }for (;;) {unsigned char buf[256 ];int i, length;unsigned short revents; - err =poll (pfds, npfds,200 ); + err =poll (pfds, npfds,-1 );if (stop || (err <0 && errno ==EINTR ))break ;if (err <0 ) {error ("poll failed: %s" ,strerror (errno));break ; } -if (err ==0 ) { - time +=200 ; -if (timeout && time >= timeout) -break ; -continue ; - } -if ((err =snd_rawmidi_poll_descriptors_revents (input, pfds, npfds, &revents)) <0 ) { + + err =snd_rawmidi_poll_descriptors_revents (input, &pfds[1 ], npfds -1 , &revents); +if (err <0 ) {error ("cannot get poll events: %s" ,snd_strerror (errno));break ; }if (revents & (POLLERR |POLLHUP ))break ; -if (!(revents &POLLIN )) +if (!(revents &POLLIN )) { +if (pfds[0 ].revents &POLLIN ) +break ;continue ; + } + err =snd_rawmidi_read (input, buf,sizeof (buf));if (err == -EAGAIN )continue ;
@@ -603,7 +632,7 @@ int main(int argc, char *argv[])if (length ==0 )continue ; read += length; - time =0 ; +if (receive_file !=-1 )write (receive_file, buf, length);if (dump) {
@@ -611,6 +640,14 @@ int main(int argc, char *argv[])print_byte (buf[i]);fflush (stdout); } + +if (timeout >0 ) { + err =timerfd_settime (pfds[0 ].fd,0 , &itimerspec,NULL ); +if (err <0 ) { +error ("cannot set timer: %s" ,strerror (errno)); +break ; + } + } }if (isatty (fileno (stdout)))printf ("\n%d bytes read\n" , read);
Quoted 9 lines · Clemens Ladisch
Clemens Ladisch (2):
amidi: ignore not only Active Sensing but also Clock bytes
amidi: fix timeout handlingv2: fix
short_optionsamidi/amidi.1 | 19 ++++++++--- amidi/amidi.c | 84 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 79 insertions(+), 24 deletions(-)
I tested these patches with loopbacked MIDI interface and this program. Then, This command finishes as expected.
$ ./amidi -p hw:1,0,0 -d -t 5
-------- 8< --------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
int main(void)
{
int fd;
uint8_t buf = 0xfe;
fd = open("/dev/snd/midiC1D0", O_WRONLY);
if (fd < 0) {
return EXIT_FAILURE;
}
while (write(fd, &buf, sizeof(buf)) > 0)
;
close(fd);
return EXIT_FAILURE;
}-------- 8< --------
Regards
Takashi Sakamoto
Takashi Iwai
AppliedQuoted 6 lines · Clemens Ladisch
Clemens Ladisch (2):
amidi: ignore not only Active Sensing but also Clock bytes
amidi: fix timeout handlingv2: fix
short_options
Applied both patches now. Thanks!
Takashi
PATCH 2/2 amidi: fix timeout handling
In reply to Clemens LadischThe timeout is not supposed to expire when ignored messages are received. This cannot be handled with the poll() timeout, so add a separate timer.
--- amidi/amidi.c | 69 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 16 deletions(-)
amidi/amidi.c +53 -16
@@ -25,9 +25,11 @@#include <stdarg.h> #include <string.h> #include <ctype.h> +#include <math.h> #include <getopt.h> #include <errno.h> #include <signal.h> +#include <sys/timerfd.h> #include <sys/types.h> #include <sys/poll.h> #include <sys/stat.h>
@@ -37,6 +39,8 @@#include "aconfig.h" #include "version.h" +#define NSEC_PER_SEC 1000000000L +static int do_device_list, do_rawmidi_list;static char *port_name ="default" ;static char *send_file_name;
@@ -46,7 +50,7 @@ static char *send_data;static int send_data_length;static int receive_file;static int dump; -static int timeout; +static float timeout;static int stop;static snd_rawmidi_t *input, **inputp;static snd_rawmidi_t *output, **outputp;
@@ -427,6 +431,7 @@ int main(int argc, char *argv[])int ignore_active_sensing =1 ;int ignore_clock =1 ;int do_send_hex =0 ; +struct itimerspec itimerspec = { .it_interval = {0 ,0 } };while ((c =getopt_long (argc, argv, short_options, long_options,NULL )) !=-1 ) {
@@ -461,7 +466,7 @@ int main(int argc, char *argv[]) dump =1 ;break ;case 't' : - timeout =atoi (optarg); + timeout =atof (optarg);break ;case 'a' : ignore_active_sensing =0 ;
@@ -552,40 +557,64 @@ int main(int argc, char *argv[])if (inputp) {int read =0 ; -int npfds, time =0 ; +int npfds;struct pollfd *pfds; - timeout *=1000 ; - npfds =snd_rawmidi_poll_descriptors_count (input); + npfds =1 +snd_rawmidi_poll_descriptors_count (input); pfds =alloca (npfds *sizeof (struct pollfd )); -snd_rawmidi_poll_descriptors (input, pfds, npfds); + +if (timeout >0 ) { + pfds[0 ].fd =timerfd_create (CLOCK_MONOTONIC ,0 ); +if (pfds[0 ].fd ==-1 ) { +error ("cannot create timer: %s" ,strerror (errno)); + goto _exit; + } + pfds[0 ].events =POLLIN ; + }else { + pfds[0 ].fd =-1 ; + } + +snd_rawmidi_poll_descriptors (input, &pfds[1 ], npfds -1 ); +signal (SIGINT , sig_handler); + +if (timeout >0 ) { +float timeout_int; + + itimerspec.it_value.tv_nsec =modff (timeout, &timeout_int) *NSEC_PER_SEC ; + itimerspec.it_value.tv_sec = timeout_int; + err =timerfd_settime (pfds[0 ].fd,0 , &itimerspec,NULL ); +if (err <0 ) { +error ("cannot set timer: %s" ,strerror (errno)); + goto _exit; + } + }for (;;) {unsigned char buf[256 ];int i, length;unsigned short revents; - err =poll (pfds, npfds,200 ); + err =poll (pfds, npfds,-1 );if (stop || (err <0 && errno ==EINTR ))break ;if (err <0 ) {error ("poll failed: %s" ,strerror (errno));break ; } -if (err ==0 ) { - time +=200 ; -if (timeout && time >= timeout) -break ; -continue ; - } -if ((err =snd_rawmidi_poll_descriptors_revents (input, pfds, npfds, &revents)) <0 ) { + + err =snd_rawmidi_poll_descriptors_revents (input, &pfds[1 ], npfds -1 , &revents); +if (err <0 ) {error ("cannot get poll events: %s" ,snd_strerror (errno));break ; }if (revents & (POLLERR |POLLHUP ))break ; -if (!(revents &POLLIN )) +if (!(revents &POLLIN )) { +if (pfds[0 ].revents &POLLIN ) +break ;continue ; + } + err =snd_rawmidi_read (input, buf,sizeof (buf));if (err == -EAGAIN )continue ;
@@ -603,7 +632,7 @@ int main(int argc, char *argv[])if (length ==0 )continue ; read += length; - time =0 ; +if (receive_file !=-1 )write (receive_file, buf, length);if (dump) {
@@ -611,6 +640,14 @@ int main(int argc, char *argv[])print_byte (buf[i]);fflush (stdout); } + +if (timeout >0 ) { + err =timerfd_settime (pfds[0 ].fd,0 , &itimerspec,NULL ); +if (err <0 ) { +error ("cannot set timer: %s" ,strerror (errno)); +break ; + } + } }if (isatty (fileno (stdout)))printf ("\n%d bytes read\n" , read);
Hi Clemens,
Quoted 8 lines · Clemens Ladisch
Clemens Ladisch (2):
amidi: ignore not only Active Sensing but also Clock bytes
amidi: fix timeout handlingamidi/amidi.1 | 19 ++++++++--- amidi/amidi.c | 82 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 23 deletions(-)
I can't see the patches emails.
Signature · 1 line
Felipe
Quoted 11 lines · Felipe Ferreri Tonello
Quoted 8 lines · Clemens Ladisch
Clemens Ladisch (2):
amidi: ignore not only Active Sensing but also Clock bytes
amidi: fix timeout handlingamidi/amidi.1 | 19 ++++++++--- amidi/amidi.c | 82 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 23 deletions(-)I can't see the patches emails.
http://mailman.alsa-project.org/pipermail/alsa-devel/2016-August/111737.html