KORG microX utility.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

330 lines
10 KiB

/*
* kmxtool - KORG microX utility
* Copyright (C) 2012,2013 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <getopt.h>
#include <err.h>
#include <hexio.h>
#include <xmem.h>
#include <microx.h>
#define MODE_STATUS 0x0
#define MODE_DATA_DUMP 0x1
#define MODE_DATA_LOAD 0x2
#define MODE_PROGRAM_LIST 0x4
/* Globals. */
static midi_io_t *midi = NULL;
/* Help and informations about the program. */
static void
usage(int status)
{
puts("\
Usage: kmxtool [options]\n\
Send/receive data to/from a connected microX synthesizer.\n");
puts("Options:\n\
-h, --help Display this help message.\n\
-v, --version Display the version message.\n\
");
puts("\
-p, --port PORT Specify MIDI port to use.\n\
");
puts("\
-s, --status Print device status (default).\n\
-L, --list-programs List all programs in all banks.\n\
-d, --dump-data ID Dump the specified slot.\n\
-l, --load-data ID Load program read from standard\n\
input into the specified slot.\n\
-t, --data-type {program|combination|multi|drumkit|arpeggio|extset}\n\
Specify the type of data to dump\n\
or load. Default is `program'.\n\
");
printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
exit(status);
}
static void
info(void)
{
printf("\
kmxtool %s\n\
Copyright (C) 2012,2013 Damien Goutte-Gattat\n\
\n\
This program is released under the GNU General Public License.\n\
See the COPYING file or <http://www.gnu.org/licenses/gpl.html>.\n\
", VERSION);
exit(EXIT_SUCCESS);
}
/* Helper functions. */
static void
cleanup(void)
{
if ( midi ) {
midi_close(midi);
midi = NULL;
}
}
static void
print_microx_status(struct kmx_microx_status *s)
{
static char *modes[] = { "COMBI PLAY", "COMBI EDIT", "PROG PLAY",
"PROG EDIT", "MULTI", "GLOBAL" };
static char *clock[] = { "Internal", "Ext-MIDI", "Ext-USB", "AUTO" };
static char *flter[] = { "All", "Odd", "Even" };
static char *yesno[] = { "No", "Yes" };
static char *bnkmp[] = { "KORG", "GM" };
printf("Global channel: %d\n", s->global_channel);
printf("Active mode: %s\n",
s->active_mode == DEMO ? "DEMO" : modes[s->active_mode]);
printf("Status of Global mode:\n");
printf(" Local Control: %s\n", yesno[s->global.local_control]);
printf(" Clock Source: %s\n", clock[s->global.clock_source]);
printf(" Bank Map: %s\n", bnkmp[s->global.bank_map]);
printf(" Memorize Mode on Power On: %s\n", yesno[s->global.memorize_mode]);
printf(" Rec. Ext.Realtime Command: %s\n", yesno[s->global.rec_ext_rt_cmd]);
printf(" Enable Program Change: %s\n", yesno[s->global.prgm_change]);
printf(" Enable Bank Change: %s\n", yesno[s->global.bank_change]);
printf(" Enable Combination Change: %s\n", yesno[s->global.comb_change]);
printf(" Enable Control Change: %s\n", yesno[s->global.ctrl_change]);
printf(" Enable Aftertouch: %s\n", yesno[s->global.aftertouch]);
printf(" Note filter: %s\n", flter[s->global.note_filter]);
printf(" Protected Memory:\n");
printf(" Program: %s\n", yesno[s->global.protected_memory.program]);
printf(" Bank: %s\n", yesno[s->global.protected_memory.bank]);
printf(" Multi: %s\n", yesno[s->global.protected_memory.multi]);
printf(" Drums: %s\n", yesno[s->global.protected_memory.drum]);
printf(" Arpeggio Patterns: %s\n", yesno[s->global.protected_memory.arpeggio]);
printf(" Ext.Controllers: %s\n", yesno[s->global.protected_memory.ext_ctrl]);
printf("Status of Program mode:\n");
printf(" Current Bank: %c\n", 'A' + s->program.current_bank);
printf(" Current Program: %d\n", s->program.current_program);
printf("Status of Combination mode:\n");
printf(" Current Bank: %c\n", 'A' + s->combination.current_bank);
printf(" Current Combination: %d\n", s->combination.current_combination);
printf("Status of Multi mode:\n");
printf(" Current Multi: %d\n", s->multi.current_multi);
printf(" Control Track: %d\n", s->multi.control_track + 1);
printf("Status of Ext.Controllers:\n");
printf(" Ext.Controllers Enabled: %s\n", yesno[s->ext_ctrl.enabled]);
printf(" Current Controller: %d\n", s->ext_ctrl.current);
}
static void
do_list_programs(midi_io_t *midi)
{
size_t len;
int n;
unsigned char *reply, *cursor, bank, program;
len = MICROX_PROGRAM_SIZE * MICROX_N_PROGRAM;
reply = xmalloc(len);
if ( (n = kmx_microx_dump(midi, PROGRAM_DATA, "????", reply, len)) < 0 )
errx(EXIT_FAILURE, "cannot dump data: %s", kmx_microx_error(midi, n));
for ( cursor = reply, bank = 'A'; bank < 'F'; bank++ ) {
for ( program = 0; program < 128; program++ ) {
printf("%c%03d %.16s\n", bank, program, cursor);
cursor += MICROX_PROGRAM_SIZE;
}
}
/* Also list non-modifiable programs of the GM bank */
for ( program = 0; program < 128; program++ )
printf("G%03d %s\n", program + 1, midi_gm_patches[program]);
free(reply);
}
struct kmx_data_type
{
enum kmx_microx_data_type code;
size_t size;
const char *name;
};
struct kmx_data_type kmx_data_types[] =
{
{ PROGRAM_DATA, MICROX_PROGRAM_SIZE, "program" },
{ COMBINATION_DATA, MICROX_COMBINATION_SIZE, "combination" },
{ MULTI_DATA, MICROX_MULTI_SIZE, "multi" },
{ DRUMKIT_DATA, MICROX_DRUMKIT_SIZE, "drumkit" },
{ ARPEGGIO_DATA, MICROX_ARPEGGIO_SIZE, "arpeggio" },
{ EXTSET_DATA, MICROX_EXTSET_SIZE, "extset" },
{ 0, 0, NULL }
};
/* Main function. */
int
main(int argc, char **argv)
{
char c, *port, *param;
int e, mode;
struct kmx_microx_version version;
struct kmx_microx_status status;
struct kmx_data_type *type;
struct option options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ "port", 1, NULL, 'p' },
{ "status", 0, NULL, 's' },
{ "dump-data", 1, NULL, 'd' },
{ "load-data", 1, NULL, 'l' },
{ "data-type", 1, NULL, 't' },
{ "list-programs", 0, NULL, 'L' },
{ NULL, 0, NULL, 0 }
};
setprogname(argv[0]);
setlocale(LC_ALL, "");
atexit(cleanup);
port = DEFAULT_MIDI_PORT;
mode = MODE_STATUS;
type = kmx_data_types;
while ( (c = getopt_long(argc, argv, "hvp:sd:l:t:L",
options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
usage(EXIT_SUCCESS);
break;
case '?':
usage(EXIT_FAILURE);
break;
case 'v':
info();
break;
case 'p':
port = optarg;
break;
case 's':
mode = MODE_STATUS;
break;
case 'd':
mode = MODE_DATA_DUMP;
param = optarg;
break;
case 'l':
mode = MODE_DATA_LOAD;
param = optarg;
break;
case 't':
while ( type->name ) {
if ( strcmp(type->name, optarg) == 0 )
break;
type += 1;
}
if ( type->name == NULL )
errx(EXIT_FAILURE, "invalid data type: %s", optarg);
break;
case 'L':
mode = MODE_PROGRAM_LIST;
break;
}
}
if ( ! (midi = midi_open(port)) )
errx(EXIT_FAILURE, "cannot open MIDI port %s", port);
if ( (e = kmx_microx_identify(midi, &version)) < 0 )
errx(EXIT_FAILURE, "cannot identify microX device: %s",
kmx_microx_error(midi, e));
if ( mode == MODE_STATUS ) {
printf("%s: KORG microX synthesizer, version %02d.%02d.%02d.%02d\n",
port, version.major, version.minor, version.release, version.build);
if ( (e = kmx_microx_query_status(midi, &status)) < 0 )
errx(EXIT_FAILURE, "cannot query microX status: %s",
kmx_microx_error(midi, e));
print_microx_status(&status);
}
else if ( mode == MODE_DATA_DUMP ) {
unsigned char *program;
int n;
program = xmalloc(type->size);
if ( (n = kmx_microx_dump(midi, type->code, param, program,
type->size)) < 0 )
errx(EXIT_FAILURE, "cannot dump data: %s",
kmx_microx_error(midi, n));
fprinthd(stdout, program, n, 0);
}
else if ( mode == MODE_DATA_LOAD ) {
unsigned char *program;
ssize_t n;
program = NULL;
n = freadhda(stdin, &program, NULL);
if ( n < 1 )
err(EXIT_FAILURE, "cannot read data");
if ( (n = kmx_microx_load(midi, type->code, param, program, n)) < 0 )
errx(EXIT_FAILURE, "cannot load data: %s",
kmx_microx_error(midi, n));
}
else if ( mode == MODE_PROGRAM_LIST ) {
do_list_programs(midi);
}
return EXIT_SUCCESS;
}