Browse Source

Factorize get_dump/load_parameters functions

master
Damien Goutte-Gattat 9 years ago
parent
commit
9ce66f4dce
  1. 78
      src/microx.c

78
src/microx.c

@ -219,7 +219,8 @@ static struct dump dumps[] = {
static int
get_dump_parameters(enum kmx_microx_data_type type,
const char *what,
unsigned char *buffer)
unsigned char *buffer,
int load)
{
unsigned char program, bank;
@ -229,8 +230,15 @@ get_dump_parameters(enum kmx_microx_data_type type,
return KMX_INVALID_QUERY;
if ( (bank < 'A' || bank > 'E') || program > 127 )
return KMX_INVALID_QUERY;
buffer[0] = 0x20 + (bank - 'A');
buffer[1] = program;
if ( load ) {
buffer[0] = 1;
buffer[1] = 0x20 + (bank - 'A');
buffer[2] = program;
}
else {
buffer[0] = 0x20 + (bank - 'A');
buffer[1] = program;
}
break;
case COMBINATION_DATA:
@ -299,7 +307,7 @@ kmx_microx_dump(midi_io_t *midi,
return KMX_BUFFER_TOO_SMALL;
query[4] = dumps[type].query_code;
if ( get_dump_parameters(type, what, &(query[5])) < 0 )
if ( get_dump_parameters(type, what, &(query[5]), 0) < 0 )
return KMX_INVALID_QUERY;
if ( (n = midi_write(midi, query, sizeof(query))) < 0 )
@ -357,66 +365,6 @@ kmx_microx_dump(midi_io_t *midi,
return j;
}
static int
get_load_parameters(enum kmx_microx_data_type type,
const char *what,
unsigned char *buffer)
{
unsigned char program, bank;
switch ( type ) {
case PROGRAM_DATA:
buffer[0] = 1;
if ( sscanf(what, "%c%03hhd", &bank, &program) != 2 )
return KMX_INVALID_QUERY;
if ( (bank < 'A' || bank > 'E') || program > 127 )
return KMX_INVALID_QUERY;
buffer[1] = 0x20 + (bank - 'A');
buffer[2] = program;
break;
case COMBINATION_DATA:
if ( sscanf(what, "%c%03hhd", &bank, &program) != 2 )
return KMX_INVALID_QUERY;
if ( (bank < 'A' || bank > 'C') || program > 127 )
return KMX_INVALID_QUERY;
buffer[1] = 0x20 + (bank - 'A');
buffer[2] = program;
break;
case MULTI_DATA:
if ( sscanf(what, "%02hhd", &program) != 1 || program > 127 )
return KMX_INVALID_QUERY;
buffer[0] = 1;
buffer[1] = program;
break;
case DRUMKIT_DATA:
if ( sscanf(what, "%03hhd", &program) != 1 || program > 39 )
return KMX_INVALID_QUERY;
buffer[0] = 1;
buffer[1] = program;
break;
case ARPEGGIO_DATA:
if ( sscanf(what, "%03hhd", &program) != 1 || program > 250 )
return KMX_INVALID_QUERY;
buffer[0] = 1;
buffer[1] = program >> 7;
buffer[2] = program & 0x7F;
break;
case EXTSET_DATA:
if ( sscanf(what, "%02hhd", &program) != 1 || program > 63 )
return KMX_INVALID_QUERY;
buffer[0] = 1;
buffer[1] = program;
break;
}
return 0;
}
int
kmx_microx_load(midi_io_t *midi,
enum kmx_microx_data_type type,
@ -438,7 +386,7 @@ kmx_microx_load(midi_io_t *midi,
return KMX_BUFFER_TOO_SMALL;
buffer[4] = dumps[type].reply_code;
if ( get_load_parameters(type, what, &(buffer[5])) < 0 )
if ( get_dump_parameters(type, what, &(buffer[5]), 1) < 0 )
return KMX_INVALID_QUERY;
n = 8;

Loading…
Cancel
Save