Browse Source

Auto-detect a connected microX device

Do not use a default MIDI port fixed at compile-time. If no port is
specified with the -p option, send a Device Inquiry to each
available MIDI port until a device answers with the correct
identification.
develop
Damien Goutte-Gattat 8 years ago
parent
commit
79148a04fb
  1. 45
      src/kmxtool.c
  2. 28
      src/midi.c
  3. 5
      src/midi.h

45
src/kmxtool.c

@ -35,6 +35,7 @@
#define MODE_DATA_DUMP 0x1
#define MODE_DATA_LOAD 0x2
#define MODE_PROGRAM_LIST 0x4
#define MODE_PORT_LIST 0x8
/* Globals. */
@ -181,6 +182,23 @@ do_list_programs(midi_io_t *midi)
free(reply);
}
int
check_microx_on_port(const char *name, void *user)
{
struct kmx_microx_version *version;
version = (struct kmx_microx_version *)user;
if ( (midi = midi_open(name)) != NULL ) {
if ( kmx_microx_identify(midi, version) == 0 )
return 1;
midi_close(midi);
midi = NULL;
}
return 0;
}
/* Main function. */
@ -202,6 +220,7 @@ main(int argc, char **argv)
{ "load-data", 1, NULL, 'l' },
{ "data-type", 1, NULL, 't' },
{ "list-programs", 0, NULL, 'L' },
{ "list-ports", 0, NULL, 'P' },
{ NULL, 0, NULL, 0 }
};
@ -209,11 +228,11 @@ main(int argc, char **argv)
setlocale(LC_ALL, "");
atexit(cleanup);
port = DEFAULT_MIDI_PORT;
port = NULL;
mode = MODE_STATUS;
dump.load = 0;
while ( (c = getopt_long(argc, argv, "hvp:sd:l:t:L",
while ( (c = getopt_long(argc, argv, "hvp:sd:l:t:LP",
options, NULL)) != -1 ) {
switch ( c ) {
case 'h':
@ -249,19 +268,27 @@ main(int argc, char **argv)
case 'L':
mode = MODE_PROGRAM_LIST;
break;
case 'P':
mode = MODE_PORT_LIST;
break;
}
}
if ( ! (midi = midi_open(port)) )
errx(EXIT_FAILURE, "cannot open MIDI port %s", port);
if ( port ) {
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 ( (e = kmx_microx_identify(midi, &version)) < 0 )
errx(EXIT_FAILURE, "cannot identify microX device: %s",
kmx_microx_error(midi, e));
}
else if ( ! midi_list_ports(check_microx_on_port, &version) )
errx(EXIT_FAILURE, "no microX device found");
if ( mode == MODE_STATUS ) {
printf("%s: KORG microX synthesizer, version %02d.%02d.%02d.%02d\n",
port, version.major, version.minor, version.release, version.build);
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",

28
src/midi.c

@ -147,6 +147,34 @@ midi_error(midi_io_t *midi)
return snd_strerror(midi->error);
}
int
midi_list_ports(midi_list_port_cb cb, void *user)
{
int card, ret;
ret = 0;
card = -1;
while ( ret == 0 && snd_card_next(&card) >= 0 && card != -1 ) {
snd_ctl_t *ctl;
char name[32];
sprintf(name, "hw:%d", card);
if ( snd_ctl_open(&ctl, name, 0) >= 0 ) {
int device;
device = -1;
while ( ret == 0 && snd_ctl_rawmidi_next_device(ctl, &device) >= 0 && device >= 0 ) {
sprintf(name, "hw:%d,%d", card, device);
ret = cb(name, user);
}
snd_ctl_close(ctl);
}
}
return ret;
}
#elif USE_OSS_MIDI_API
/* Open Sound System MIDI API */
#include <fcntl.h>

5
src/midi.h

@ -56,6 +56,11 @@ midi_change_program(midi_io_t *,
unsigned short,
unsigned char);
typedef int (*midi_list_port_cb)(const char *, void *);
int
midi_list_ports(midi_list_port_cb, void *);
#ifdef __cplusplus
}
#endif

Loading…
Cancel
Save