Browse Source

Delegate the MIDI output to a specialized function

master
Damien Goutte-Gattat 9 years ago
parent
commit
d47b47d2e3
  1. 36
      src/asysex.c

36
src/asysex.c

@ -149,15 +149,35 @@ read_message_from_file(FILE *in, unsigned char **buffer, size_t *len)
return n;
}
/* MIDI output functions. */
typedef void (*print_midi_fn_t)(FILE *, unsigned char *, size_t);
static void
do_query(midi_io_t *midi, FILE *in, FILE *out)
print_midi_hex(FILE *out, unsigned char *buffer, size_t len)
{
int i;
static int j = 0;
for ( i = 0; i < len; i++, j++ ) {
if ( j % 16 == 0 )
fprintf(out, "%s%08x:", j > 0 ? "\n" : "", j);
fprintf(out, " %s%02X", j % 8 == 0 ? " " : "", buffer[i]);
}
}
/* Mode functions. */
static void
do_query(midi_io_t *midi, FILE *in, FILE *out, print_midi_fn_t print_midi)
{
unsigned char *query, reply[256];
size_t n;
int i, j;
query = NULL;
j = 0;
n = read_message_from_file(in, &query, NULL);
if ( n < 2 || query[0] != 0xF0 || query[n-1] != 0xF7 )
@ -170,11 +190,7 @@ do_query(midi_io_t *midi, FILE *in, FILE *out)
if ( (n = sysex_read(midi, reply, sizeof(reply))) < 0 )
errx(EXIT_FAILURE, "cannot read MIDI data");
for ( i = 0; i < n; i++, j++ ) {
if ( j % 16 == 0 )
fprintf(out, "%s%08X:", j > 0 ? "\n" : "", j);
fprintf(out, " %s%02X", j % 8 == 0 ? " " : "", reply[i]);
}
print_midi(out, reply, n);
}
while ( reply[n-1] != 0xF7 );
@ -206,6 +222,7 @@ main(int argc, char **argv)
{
char c, *port;
int mode;
print_midi_fn_t print_midi;
struct option options[] = {
{ "help", 0, NULL, 'h' },
@ -221,6 +238,7 @@ main(int argc, char **argv)
port = "hw:1,0,0";
mode = ASYSEX_MODE_QUERY;
print_midi = print_midi_hex;
while ( (c = getopt_long(argc, argv, "hvp:I", options, NULL)) != -1 ) {
switch ( c ) {
@ -251,7 +269,7 @@ main(int argc, char **argv)
switch ( mode ) {
case ASYSEX_MODE_QUERY:
do_query(midi, stdin, stdout);
do_query(midi, stdin, stdout, print_midi);
break;
case ASYSEX_MODE_INQUIRY:

Loading…
Cancel
Save