/*
|
|
* 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 <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. If\n\
|
|
unspecified, all available MIDI\n\
|
|
ports will be scanned to detect\n\
|
|
a microX synthesizer.\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 data read from standard\n\
|
|
input into the specified slot.\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 const char *modes[] = { "COMBI PLAY", "COMBI EDIT", "PROG PLAY",
|
|
"PROG EDIT", "MULTI", "GLOBAL" };
|
|
static const char *clock[] = { "Internal", "Ext-MIDI", "Ext-USB", "AUTO" };
|
|
static const char *flter[] = { "All", "Odd", "Even" };
|
|
static const char *yesno[] = { "No", "Yes" };
|
|
static const 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;
|
|
struct kmx_microx_dump dump;
|
|
|
|
dump.type = PROGRAM_DATA;
|
|
dump.load = 0;
|
|
dump.bank = KMX_MICROX_DUMP_ALL;
|
|
|
|
len = kmx_microx_get_dump_size(&dump);
|
|
if ( ! (reply = malloc(len)) )
|
|
err(EXIT_FAILURE, "cannot dump data");
|
|
|
|
if ( (n = kmx_microx_dump(midi, &dump, 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 = 1; program <= MICROX_BANK_SIZE; 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);
|
|
}
|
|
|
|
static int
|
|
find_microx_on_port(const char *port,
|
|
struct kmx_microx_version *version,
|
|
int quiet)
|
|
{
|
|
int ret, e;
|
|
|
|
ret = -1;
|
|
|
|
if ( (midi = midi_open(port)) != NULL ) {
|
|
if ( (e = kmx_microx_identify(midi, version)) == 0 )
|
|
ret = 0;
|
|
else {
|
|
midi_close(midi);
|
|
midi = NULL;
|
|
if ( ! quiet )
|
|
errx(EXIT_FAILURE, "cannot identify microX device: %s",
|
|
kmx_microx_error(midi, e));
|
|
}
|
|
}
|
|
else if ( ! quiet )
|
|
errx(EXIT_FAILURE, "cannot open MIDI port %s", port);
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
/* 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_microx_dump dump;
|
|
|
|
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' },
|
|
{ "list-programs", 0, NULL, 'L' },
|
|
{ NULL, 0, NULL, 0 }
|
|
};
|
|
|
|
setprogname(argv[0]);
|
|
setlocale(LC_ALL, "");
|
|
atexit(cleanup);
|
|
|
|
port = param = NULL;
|
|
mode = MODE_STATUS;
|
|
|
|
while ( (c = getopt_long(argc, argv, "hvp:sd:l: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 'L':
|
|
mode = MODE_PROGRAM_LIST;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ( port )
|
|
find_microx_on_port(port, &version, 0);
|
|
else {
|
|
char **ports, **p;
|
|
int found;
|
|
|
|
ports = midi_get_ports();
|
|
|
|
for ( p = ports, found = 0; *p != NULL; p++ ) {
|
|
if ( ! found )
|
|
found = find_microx_on_port(*p, &version, 1) != -1;
|
|
free(*p);
|
|
}
|
|
free(ports);
|
|
|
|
if ( ! found )
|
|
errx(EXIT_FAILURE, "no microX device found");
|
|
}
|
|
|
|
if ( mode == MODE_STATUS ) {
|
|
printf("KORG microX synthesizer, version %02d.%02d.%02d.%02d\n",
|
|
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 *data;
|
|
int n;
|
|
|
|
dump.load = 0;
|
|
if ( kmx_microx_parse_dump_request(param, &dump) == KMX_INVALID_QUERY )
|
|
errx(EXIT_FAILURE, "invalid dump request: %s", param);
|
|
|
|
n = kmx_microx_get_dump_size(&dump);
|
|
if ( ! (data = malloc(n)) )
|
|
err(EXIT_FAILURE, "cannot dump data");
|
|
|
|
if ( (n = kmx_microx_dump(midi, &dump, data, n)) < 0 )
|
|
errx(EXIT_FAILURE, "cannot dump data: %s",
|
|
kmx_microx_error(midi, n));
|
|
|
|
fprinthd(stdout, data, n, 0);
|
|
}
|
|
else if ( mode == MODE_DATA_LOAD ) {
|
|
unsigned char *data;
|
|
ssize_t n;
|
|
|
|
dump.load = 1;
|
|
if ( kmx_microx_parse_dump_request(param, &dump) == KMX_INVALID_QUERY )
|
|
errx(EXIT_FAILURE, "invalid load request: %s", param);
|
|
|
|
data = NULL;
|
|
n = freadhda(stdin, &data, NULL);
|
|
if ( n < 1 )
|
|
err(EXIT_FAILURE, "cannot read data");
|
|
|
|
if ( (n = kmx_microx_load(midi, &dump, data, 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;
|
|
}
|