2003-10-28 11:53:43 +01:00
|
|
|
/*
|
|
|
|
* amidi.c - read from/write to RawMIDI ports
|
|
|
|
*
|
|
|
|
* Copyright (c) Clemens Ladisch <clemens@ladisch.de>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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 2 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, write to the Free Software
|
2017-11-14 14:28:51 +01:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-10-28 11:53:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2016-08-13 16:41:58 +02:00
|
|
|
#include <math.h>
|
2003-10-28 11:53:43 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
2016-08-13 16:41:58 +02:00
|
|
|
#include <sys/timerfd.h>
|
2003-10-28 11:53:43 +01:00
|
|
|
#include <sys/types.h>
|
2019-11-20 05:28:53 +01:00
|
|
|
#include <poll.h>
|
2003-10-28 11:53:43 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <alsa/asoundlib.h>
|
2021-08-24 11:49:43 +02:00
|
|
|
#include <time.h>
|
2003-10-28 11:53:43 +01:00
|
|
|
#include "aconfig.h"
|
|
|
|
#include "version.h"
|
|
|
|
|
2016-08-13 16:41:58 +02:00
|
|
|
#define NSEC_PER_SEC 1000000000L
|
|
|
|
|
2021-08-24 11:49:43 +02:00
|
|
|
static int do_print_timestamp = 0;
|
2003-10-28 11:53:43 +01:00
|
|
|
static int do_device_list, do_rawmidi_list;
|
|
|
|
static char *port_name = "default";
|
|
|
|
static char *send_file_name;
|
|
|
|
static char *receive_file_name;
|
|
|
|
static char *send_hex;
|
|
|
|
static char *send_data;
|
|
|
|
static int send_data_length;
|
|
|
|
static int receive_file;
|
|
|
|
static int dump;
|
2016-08-13 16:41:58 +02:00
|
|
|
static float timeout;
|
2003-10-28 11:53:43 +01:00
|
|
|
static int stop;
|
2016-08-30 18:02:48 +02:00
|
|
|
static int sysex_interval;
|
2003-10-28 11:53:43 +01:00
|
|
|
static snd_rawmidi_t *input, **inputp;
|
|
|
|
static snd_rawmidi_t *output, **outputp;
|
|
|
|
|
|
|
|
static void error(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stderr, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
putc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
2005-06-23 12:39:16 +02:00
|
|
|
printf(
|
2003-10-28 11:53:43 +01:00
|
|
|
"Usage: amidi options\n"
|
|
|
|
"\n"
|
2016-08-30 18:02:48 +02:00
|
|
|
"-h, --help this help\n"
|
|
|
|
"-V, --version print current version\n"
|
|
|
|
"-l, --list-devices list all hardware ports\n"
|
|
|
|
"-L, --list-rawmidis list all RawMIDI definitions\n"
|
|
|
|
"-p, --port=name select port by name\n"
|
|
|
|
"-s, --send=file send the contents of a (.syx) file\n"
|
|
|
|
"-r, --receive=file write received data into a file\n"
|
|
|
|
"-S, --send-hex=\"...\" send hexadecimal bytes\n"
|
|
|
|
"-d, --dump print received data as hexadecimal bytes\n"
|
2021-08-24 11:49:43 +02:00
|
|
|
"-T, --timestamp=... adds a timestamp in front of each dumped message\n"
|
|
|
|
" realtime\n"
|
|
|
|
" monotonic\n"
|
2022-12-31 08:03:34 +01:00
|
|
|
#ifdef CLOCK_MONOTONIC_RAW
|
2021-08-24 11:49:43 +02:00
|
|
|
" raw\n"
|
2022-12-31 08:03:34 +01:00
|
|
|
#endif
|
2016-08-30 18:02:48 +02:00
|
|
|
"-t, --timeout=seconds exits when no data has been received\n"
|
|
|
|
" for the specified duration\n"
|
|
|
|
"-a, --active-sensing include active sensing bytes\n"
|
|
|
|
"-c, --clock include clock bytes\n"
|
|
|
|
"-i, --sysex-interval=mseconds delay in between each SysEx message\n");
|
2003-10-28 11:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void version(void)
|
|
|
|
{
|
2005-06-23 12:39:16 +02:00
|
|
|
puts("amidi version " SND_UTIL_VERSION_STR);
|
2003-10-28 11:53:43 +01:00
|
|
|
}
|
|
|
|
|
2005-02-16 10:52:25 +01:00
|
|
|
static void *my_malloc(size_t size)
|
|
|
|
{
|
|
|
|
void *p = malloc(size);
|
|
|
|
if (!p) {
|
|
|
|
error("out of memory");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2003-10-28 11:53:43 +01:00
|
|
|
static void list_device(snd_ctl_t *ctl, int card, int device)
|
|
|
|
{
|
|
|
|
snd_rawmidi_info_t *info;
|
|
|
|
const char *name;
|
|
|
|
const char *sub_name;
|
amidi: Fix device list
From: Matthias Koenig <mk@phasorlab.de>
amidi does not show all devices with the --list-devices option.
It assumes that all output ports equals input ports and does not
show an input only port.
This patch extends the output of amidi -l by one column which shows
the input/output direction of each port.
E.g.
zebra:amidi # ./amidi -l
Dir Device Name
IO hw:1,0 MPU-401 MIDI 1-0
IO hw:2,0,0 Keystation MIDI 1
IO hw:3,0,0 Miditerminal 1
IO hw:3,0,1 Miditerminal 2
IO hw:3,0,2 Miditerminal 3
IO hw:3,0,3 Miditerminal 4
I hw:3,0,4 Miditerminal SMPTE
2006-06-23 14:53:38 +02:00
|
|
|
int subs, subs_in, subs_out;
|
2010-01-25 14:11:37 +01:00
|
|
|
int sub;
|
2003-10-28 11:53:43 +01:00
|
|
|
int err;
|
|
|
|
|
|
|
|
snd_rawmidi_info_alloca(&info);
|
|
|
|
snd_rawmidi_info_set_device(info, device);
|
amidi: Fix device list
From: Matthias Koenig <mk@phasorlab.de>
amidi does not show all devices with the --list-devices option.
It assumes that all output ports equals input ports and does not
show an input only port.
This patch extends the output of amidi -l by one column which shows
the input/output direction of each port.
E.g.
zebra:amidi # ./amidi -l
Dir Device Name
IO hw:1,0 MPU-401 MIDI 1-0
IO hw:2,0,0 Keystation MIDI 1
IO hw:3,0,0 Miditerminal 1
IO hw:3,0,1 Miditerminal 2
IO hw:3,0,2 Miditerminal 3
IO hw:3,0,3 Miditerminal 4
I hw:3,0,4 Miditerminal SMPTE
2006-06-23 14:53:38 +02:00
|
|
|
|
|
|
|
snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT);
|
2010-01-25 14:11:37 +01:00
|
|
|
err = snd_ctl_rawmidi_info(ctl, info);
|
|
|
|
if (err >= 0)
|
|
|
|
subs_in = snd_rawmidi_info_get_subdevices_count(info);
|
|
|
|
else
|
|
|
|
subs_in = 0;
|
|
|
|
|
2003-10-28 11:53:43 +01:00
|
|
|
snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT);
|
2010-01-25 14:11:37 +01:00
|
|
|
err = snd_ctl_rawmidi_info(ctl, info);
|
|
|
|
if (err >= 0)
|
|
|
|
subs_out = snd_rawmidi_info_get_subdevices_count(info);
|
|
|
|
else
|
|
|
|
subs_out = 0;
|
amidi: Fix device list
From: Matthias Koenig <mk@phasorlab.de>
amidi does not show all devices with the --list-devices option.
It assumes that all output ports equals input ports and does not
show an input only port.
This patch extends the output of amidi -l by one column which shows
the input/output direction of each port.
E.g.
zebra:amidi # ./amidi -l
Dir Device Name
IO hw:1,0 MPU-401 MIDI 1-0
IO hw:2,0,0 Keystation MIDI 1
IO hw:3,0,0 Miditerminal 1
IO hw:3,0,1 Miditerminal 2
IO hw:3,0,2 Miditerminal 3
IO hw:3,0,3 Miditerminal 4
I hw:3,0,4 Miditerminal SMPTE
2006-06-23 14:53:38 +02:00
|
|
|
|
2010-01-25 14:11:37 +01:00
|
|
|
subs = subs_in > subs_out ? subs_in : subs_out;
|
|
|
|
if (!subs)
|
2003-10-28 11:53:43 +01:00
|
|
|
return;
|
amidi: Fix device list
From: Matthias Koenig <mk@phasorlab.de>
amidi does not show all devices with the --list-devices option.
It assumes that all output ports equals input ports and does not
show an input only port.
This patch extends the output of amidi -l by one column which shows
the input/output direction of each port.
E.g.
zebra:amidi # ./amidi -l
Dir Device Name
IO hw:1,0 MPU-401 MIDI 1-0
IO hw:2,0,0 Keystation MIDI 1
IO hw:3,0,0 Miditerminal 1
IO hw:3,0,1 Miditerminal 2
IO hw:3,0,2 Miditerminal 3
IO hw:3,0,3 Miditerminal 4
I hw:3,0,4 Miditerminal SMPTE
2006-06-23 14:53:38 +02:00
|
|
|
|
2010-01-25 14:11:37 +01:00
|
|
|
for (sub = 0; sub < subs; ++sub) {
|
|
|
|
snd_rawmidi_info_set_stream(info, sub < subs_in ?
|
|
|
|
SND_RAWMIDI_STREAM_INPUT :
|
|
|
|
SND_RAWMIDI_STREAM_OUTPUT);
|
|
|
|
snd_rawmidi_info_set_subdevice(info, sub);
|
|
|
|
err = snd_ctl_rawmidi_info(ctl, info);
|
|
|
|
if (err < 0) {
|
|
|
|
error("cannot get rawmidi information %d:%d:%d: %s\n",
|
|
|
|
card, device, sub, snd_strerror(err));
|
2003-10-28 11:53:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-25 14:11:37 +01:00
|
|
|
name = snd_rawmidi_info_get_name(info);
|
|
|
|
sub_name = snd_rawmidi_info_get_subdevice_name(info);
|
|
|
|
if (sub == 0 && sub_name[0] == '\0') {
|
|
|
|
printf("%c%c hw:%d,%d %s",
|
|
|
|
sub < subs_in ? 'I' : ' ',
|
|
|
|
sub < subs_out ? 'O' : ' ',
|
amidi: Fix device list
From: Matthias Koenig <mk@phasorlab.de>
amidi does not show all devices with the --list-devices option.
It assumes that all output ports equals input ports and does not
show an input only port.
This patch extends the output of amidi -l by one column which shows
the input/output direction of each port.
E.g.
zebra:amidi # ./amidi -l
Dir Device Name
IO hw:1,0 MPU-401 MIDI 1-0
IO hw:2,0,0 Keystation MIDI 1
IO hw:3,0,0 Miditerminal 1
IO hw:3,0,1 Miditerminal 2
IO hw:3,0,2 Miditerminal 3
IO hw:3,0,3 Miditerminal 4
I hw:3,0,4 Miditerminal SMPTE
2006-06-23 14:53:38 +02:00
|
|
|
card, device, name);
|
2010-01-25 14:11:37 +01:00
|
|
|
if (subs > 1)
|
|
|
|
printf(" (%d subdevices)", subs);
|
|
|
|
putchar('\n');
|
|
|
|
break;
|
|
|
|
} else {
|
amidi: Fix device list
From: Matthias Koenig <mk@phasorlab.de>
amidi does not show all devices with the --list-devices option.
It assumes that all output ports equals input ports and does not
show an input only port.
This patch extends the output of amidi -l by one column which shows
the input/output direction of each port.
E.g.
zebra:amidi # ./amidi -l
Dir Device Name
IO hw:1,0 MPU-401 MIDI 1-0
IO hw:2,0,0 Keystation MIDI 1
IO hw:3,0,0 Miditerminal 1
IO hw:3,0,1 Miditerminal 2
IO hw:3,0,2 Miditerminal 3
IO hw:3,0,3 Miditerminal 4
I hw:3,0,4 Miditerminal SMPTE
2006-06-23 14:53:38 +02:00
|
|
|
printf("%c%c hw:%d,%d,%d %s\n",
|
2010-01-25 14:11:37 +01:00
|
|
|
sub < subs_in ? 'I' : ' ',
|
|
|
|
sub < subs_out ? 'O' : ' ',
|
2003-10-28 11:53:43 +01:00
|
|
|
card, device, sub, sub_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void list_card_devices(int card)
|
|
|
|
{
|
|
|
|
snd_ctl_t *ctl;
|
|
|
|
char name[32];
|
|
|
|
int device;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
sprintf(name, "hw:%d", card);
|
|
|
|
if ((err = snd_ctl_open(&ctl, name, 0)) < 0) {
|
|
|
|
error("cannot open control for card %d: %s", card, snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
device = -1;
|
|
|
|
for (;;) {
|
|
|
|
if ((err = snd_ctl_rawmidi_next_device(ctl, &device)) < 0) {
|
|
|
|
error("cannot determine device number: %s", snd_strerror(err));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (device < 0)
|
|
|
|
break;
|
|
|
|
list_device(ctl, card, device);
|
|
|
|
}
|
|
|
|
snd_ctl_close(ctl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void device_list(void)
|
|
|
|
{
|
|
|
|
int card, err;
|
|
|
|
|
|
|
|
card = -1;
|
|
|
|
if ((err = snd_card_next(&card)) < 0) {
|
|
|
|
error("cannot determine card number: %s", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (card < 0) {
|
|
|
|
error("no sound card found");
|
|
|
|
return;
|
|
|
|
}
|
amidi: Fix device list
From: Matthias Koenig <mk@phasorlab.de>
amidi does not show all devices with the --list-devices option.
It assumes that all output ports equals input ports and does not
show an input only port.
This patch extends the output of amidi -l by one column which shows
the input/output direction of each port.
E.g.
zebra:amidi # ./amidi -l
Dir Device Name
IO hw:1,0 MPU-401 MIDI 1-0
IO hw:2,0,0 Keystation MIDI 1
IO hw:3,0,0 Miditerminal 1
IO hw:3,0,1 Miditerminal 2
IO hw:3,0,2 Miditerminal 3
IO hw:3,0,3 Miditerminal 4
I hw:3,0,4 Miditerminal SMPTE
2006-06-23 14:53:38 +02:00
|
|
|
puts("Dir Device Name");
|
2003-10-28 11:53:43 +01:00
|
|
|
do {
|
|
|
|
list_card_devices(card);
|
|
|
|
if ((err = snd_card_next(&card)) < 0) {
|
|
|
|
error("cannot determine card number: %s", snd_strerror(err));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (card >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rawmidi_list(void)
|
|
|
|
{
|
|
|
|
snd_output_t *output;
|
|
|
|
snd_config_t *config;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((err = snd_config_update()) < 0) {
|
|
|
|
error("snd_config_update failed: %s", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((err = snd_output_stdio_attach(&output, stdout, 0)) < 0) {
|
|
|
|
error("snd_output_stdio_attach failed: %s", snd_strerror(err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (snd_config_search(snd_config, "rawmidi", &config) >= 0) {
|
|
|
|
puts("RawMIDI list:");
|
|
|
|
snd_config_save(config, output);
|
|
|
|
}
|
|
|
|
snd_output_close(output);
|
|
|
|
}
|
|
|
|
|
2016-08-30 18:02:48 +02:00
|
|
|
static int send_midi_interleaved(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
char *data = send_data;
|
|
|
|
size_t buffer_size;
|
|
|
|
snd_rawmidi_params_t *param;
|
|
|
|
snd_rawmidi_status_t *st;
|
|
|
|
|
|
|
|
snd_rawmidi_status_alloca(&st);
|
|
|
|
|
|
|
|
snd_rawmidi_params_alloca(¶m);
|
|
|
|
snd_rawmidi_params_current(output, param);
|
|
|
|
buffer_size = snd_rawmidi_params_get_buffer_size(param);
|
|
|
|
|
|
|
|
while (data < (send_data + send_data_length)) {
|
|
|
|
int len = send_data + send_data_length - data;
|
|
|
|
char *temp;
|
|
|
|
|
|
|
|
if (data > send_data) {
|
|
|
|
snd_rawmidi_status(output, st);
|
|
|
|
do {
|
|
|
|
/* 320 µs per byte as noted in Page 1 of MIDI spec */
|
|
|
|
usleep((buffer_size - snd_rawmidi_status_get_avail(st)) * 320);
|
|
|
|
snd_rawmidi_status(output, st);
|
|
|
|
} while(snd_rawmidi_status_get_avail(st) < buffer_size);
|
|
|
|
usleep(sysex_interval * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find end of SysEx */
|
|
|
|
if ((temp = memchr(data, 0xf7, len)) != NULL)
|
|
|
|
len = temp - data + 1;
|
|
|
|
|
|
|
|
if ((err = snd_rawmidi_write(output, data, len)) < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
data += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-10-28 11:53:43 +01:00
|
|
|
static void load_file(void)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
off_t length;
|
|
|
|
|
|
|
|
fd = open(send_file_name, O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
error("cannot open %s - %s", send_file_name, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
length = lseek(fd, 0, SEEK_END);
|
|
|
|
if (length == (off_t)-1) {
|
|
|
|
error("cannot determine length of %s: %s", send_file_name, strerror(errno));
|
|
|
|
goto _error;
|
|
|
|
}
|
2005-02-16 10:52:25 +01:00
|
|
|
send_data = my_malloc(length);
|
2003-10-28 11:53:43 +01:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
if (read(fd, send_data, length) != length) {
|
|
|
|
error("cannot read from %s: %s", send_file_name, strerror(errno));
|
|
|
|
goto _error;
|
|
|
|
}
|
2005-03-14 16:13:48 +01:00
|
|
|
if (length >= 4 && !memcmp(send_data, "MThd", 4)) {
|
|
|
|
error("%s is a Standard MIDI File; use aplaymidi to send it", send_file_name);
|
|
|
|
goto _error;
|
|
|
|
}
|
2003-10-28 11:53:43 +01:00
|
|
|
send_data_length = length;
|
|
|
|
goto _exit;
|
|
|
|
_error:
|
|
|
|
free(send_data);
|
2005-03-14 16:11:45 +01:00
|
|
|
send_data = NULL;
|
2003-10-28 11:53:43 +01:00
|
|
|
_exit:
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hex_value(char c)
|
|
|
|
{
|
|
|
|
if ('0' <= c && c <= '9')
|
|
|
|
return c - '0';
|
|
|
|
if ('A' <= c && c <= 'F')
|
|
|
|
return c - 'A' + 10;
|
|
|
|
if ('a' <= c && c <= 'f')
|
|
|
|
return c - 'a' + 10;
|
|
|
|
error("invalid character %c", c);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_data(void)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
int i, value;
|
|
|
|
|
2005-02-16 10:52:25 +01:00
|
|
|
send_data = my_malloc(strlen(send_hex)); /* guesstimate */
|
2003-10-28 11:53:43 +01:00
|
|
|
i = 0;
|
|
|
|
value = -1; /* value is >= 0 when the first hex digit of a byte has been read */
|
|
|
|
for (p = send_hex; *p; ++p) {
|
|
|
|
int digit;
|
|
|
|
if (isspace((unsigned char)*p)) {
|
|
|
|
if (value >= 0) {
|
|
|
|
send_data[i++] = value;
|
|
|
|
value = -1;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
digit = hex_value(*p);
|
|
|
|
if (digit < 0) {
|
|
|
|
send_data = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (value < 0) {
|
|
|
|
value = digit;
|
|
|
|
} else {
|
|
|
|
send_data[i++] = (value << 4) | digit;
|
|
|
|
value = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value >= 0)
|
|
|
|
send_data[i++] = value;
|
|
|
|
send_data_length = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prints MIDI commands, formatting them nicely
|
|
|
|
*/
|
2021-08-24 11:49:43 +02:00
|
|
|
static void print_byte(unsigned char byte, struct timespec *ts)
|
2003-10-28 11:53:43 +01:00
|
|
|
{
|
|
|
|
static enum {
|
|
|
|
STATE_UNKNOWN,
|
|
|
|
STATE_1PARAM,
|
|
|
|
STATE_1PARAM_CONTINUE,
|
|
|
|
STATE_2PARAM_1,
|
|
|
|
STATE_2PARAM_2,
|
|
|
|
STATE_2PARAM_1_CONTINUE,
|
|
|
|
STATE_SYSEX
|
|
|
|
} state = STATE_UNKNOWN;
|
|
|
|
int newline = 0;
|
|
|
|
|
|
|
|
if (byte >= 0xf8)
|
|
|
|
newline = 1;
|
|
|
|
else if (byte >= 0xf0) {
|
|
|
|
newline = 1;
|
|
|
|
switch (byte) {
|
|
|
|
case 0xf0:
|
|
|
|
state = STATE_SYSEX;
|
|
|
|
break;
|
|
|
|
case 0xf1:
|
|
|
|
case 0xf3:
|
|
|
|
state = STATE_1PARAM;
|
|
|
|
break;
|
|
|
|
case 0xf2:
|
|
|
|
state = STATE_2PARAM_1;
|
|
|
|
break;
|
|
|
|
case 0xf4:
|
|
|
|
case 0xf5:
|
|
|
|
case 0xf6:
|
|
|
|
state = STATE_UNKNOWN;
|
|
|
|
break;
|
|
|
|
case 0xf7:
|
|
|
|
newline = state != STATE_SYSEX;
|
|
|
|
state = STATE_UNKNOWN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (byte >= 0x80) {
|
|
|
|
newline = 1;
|
|
|
|
if (byte >= 0xc0 && byte <= 0xdf)
|
|
|
|
state = STATE_1PARAM;
|
|
|
|
else
|
|
|
|
state = STATE_2PARAM_1;
|
|
|
|
} else /* b < 0x80 */ {
|
|
|
|
int running_status = 0;
|
|
|
|
newline = state == STATE_UNKNOWN;
|
|
|
|
switch (state) {
|
|
|
|
case STATE_1PARAM:
|
|
|
|
state = STATE_1PARAM_CONTINUE;
|
|
|
|
break;
|
|
|
|
case STATE_1PARAM_CONTINUE:
|
|
|
|
running_status = 1;
|
|
|
|
break;
|
|
|
|
case STATE_2PARAM_1:
|
|
|
|
state = STATE_2PARAM_2;
|
|
|
|
break;
|
|
|
|
case STATE_2PARAM_2:
|
|
|
|
state = STATE_2PARAM_1_CONTINUE;
|
|
|
|
break;
|
|
|
|
case STATE_2PARAM_1_CONTINUE:
|
|
|
|
running_status = 1;
|
|
|
|
state = STATE_2PARAM_2;
|
|
|
|
break;
|
2003-11-05 12:08:25 +01:00
|
|
|
default:
|
|
|
|
break;
|
2003-10-28 11:53:43 +01:00
|
|
|
}
|
|
|
|
if (running_status)
|
|
|
|
fputs("\n ", stdout);
|
|
|
|
}
|
2021-08-24 11:49:43 +02:00
|
|
|
|
2022-07-13 17:04:09 +02:00
|
|
|
putchar(newline ? '\n' : ' ');
|
|
|
|
if (newline && do_print_timestamp) {
|
2021-08-24 11:49:43 +02:00
|
|
|
/* Nanoseconds does not make a lot of sense for serial MIDI (the
|
|
|
|
* 31250 bps one) but I'm not sure about MIDI over USB.
|
|
|
|
*/
|
2022-07-13 17:04:09 +02:00
|
|
|
printf("%lld.%.9ld) ", (long long)ts->tv_sec, ts->tv_nsec);
|
2021-08-24 11:49:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("%02X", byte);
|
2003-10-28 11:53:43 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 12:50:57 +02:00
|
|
|
static void sig_handler(int)
|
2003-10-28 11:53:43 +01:00
|
|
|
{
|
|
|
|
stop = 1;
|
|
|
|
}
|
|
|
|
|
2008-11-21 13:09:56 +01:00
|
|
|
static void add_send_hex_data(const char *str)
|
2005-02-16 10:52:25 +01:00
|
|
|
{
|
|
|
|
int length;
|
|
|
|
char *s;
|
|
|
|
|
2005-02-16 11:03:52 +01:00
|
|
|
length = (send_hex ? strlen(send_hex) + 1 : 0) + strlen(str) + 1;
|
2005-02-16 10:52:25 +01:00
|
|
|
s = my_malloc(length);
|
|
|
|
if (send_hex) {
|
|
|
|
strcpy(s, send_hex);
|
|
|
|
strcat(s, " ");
|
|
|
|
} else {
|
|
|
|
s[0] = '\0';
|
|
|
|
}
|
|
|
|
strcat(s, str);
|
|
|
|
free(send_hex);
|
|
|
|
send_hex = s;
|
|
|
|
}
|
|
|
|
|
2003-10-28 11:53:43 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2021-08-24 11:49:43 +02:00
|
|
|
static const char short_options[] = "hVlLp:s:r:S::dt:aci:T:";
|
2008-11-21 13:10:02 +01:00
|
|
|
static const struct option long_options[] = {
|
2003-10-28 11:53:43 +01:00
|
|
|
{"help", 0, NULL, 'h'},
|
|
|
|
{"version", 0, NULL, 'V'},
|
|
|
|
{"list-devices", 0, NULL, 'l'},
|
|
|
|
{"list-rawmidis", 0, NULL, 'L'},
|
|
|
|
{"port", 1, NULL, 'p'},
|
|
|
|
{"send", 1, NULL, 's'},
|
|
|
|
{"receive", 1, NULL, 'r'},
|
2005-02-16 10:52:25 +01:00
|
|
|
{"send-hex", 2, NULL, 'S'},
|
2003-10-28 11:53:43 +01:00
|
|
|
{"dump", 0, NULL, 'd'},
|
2021-08-24 11:49:43 +02:00
|
|
|
{"timestamp", 1, NULL, 'T'},
|
2003-10-28 11:53:43 +01:00
|
|
|
{"timeout", 1, NULL, 't'},
|
2004-01-19 19:39:12 +01:00
|
|
|
{"active-sensing", 0, NULL, 'a'},
|
2016-08-13 16:41:23 +02:00
|
|
|
{"clock", 0, NULL, 'c'},
|
2016-08-30 18:02:48 +02:00
|
|
|
{"sysex-interval", 1, NULL, 'i'},
|
2020-02-05 09:12:21 +01:00
|
|
|
{0}
|
2003-10-28 11:53:43 +01:00
|
|
|
};
|
|
|
|
int c, err, ok = 0;
|
2004-01-19 19:39:12 +01:00
|
|
|
int ignore_active_sensing = 1;
|
2016-08-13 16:41:23 +02:00
|
|
|
int ignore_clock = 1;
|
2005-02-16 10:52:25 +01:00
|
|
|
int do_send_hex = 0;
|
2021-08-24 11:49:43 +02:00
|
|
|
clockid_t cid = CLOCK_REALTIME;
|
2016-08-13 16:41:58 +02:00
|
|
|
struct itimerspec itimerspec = { .it_interval = { 0, 0 } };
|
2003-10-28 11:53:43 +01:00
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, short_options,
|
|
|
|
long_options, NULL)) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
case 'V':
|
|
|
|
version();
|
|
|
|
return 0;
|
|
|
|
case 'l':
|
|
|
|
do_device_list = 1;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
do_rawmidi_list = 1;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
port_name = optarg;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
send_file_name = optarg;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
receive_file_name = optarg;
|
|
|
|
break;
|
|
|
|
case 'S':
|
2005-02-16 10:52:25 +01:00
|
|
|
do_send_hex = 1;
|
|
|
|
if (optarg)
|
|
|
|
add_send_hex_data(optarg);
|
2003-10-28 11:53:43 +01:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dump = 1;
|
|
|
|
break;
|
2021-08-24 11:49:43 +02:00
|
|
|
case 'T':
|
|
|
|
do_print_timestamp = 1;
|
|
|
|
if (optarg == NULL)
|
|
|
|
error("Clock type missing");
|
|
|
|
else if (strcasecmp(optarg, "realtime") == 0)
|
|
|
|
cid = CLOCK_REALTIME;
|
|
|
|
else if (strcasecmp(optarg, "monotonic") == 0)
|
|
|
|
cid = CLOCK_MONOTONIC;
|
2022-12-31 08:03:34 +01:00
|
|
|
#ifdef CLOCK_MONOTONIC_RAW
|
2021-08-24 11:49:43 +02:00
|
|
|
else if (strcasecmp(optarg, "raw") == 0)
|
|
|
|
cid = CLOCK_MONOTONIC_RAW;
|
2022-12-31 08:03:34 +01:00
|
|
|
#endif
|
2021-08-24 11:49:43 +02:00
|
|
|
else
|
|
|
|
error("Clock type not known");
|
|
|
|
break;
|
2003-10-28 11:53:43 +01:00
|
|
|
case 't':
|
2017-02-27 09:04:08 +01:00
|
|
|
if (optarg)
|
|
|
|
timeout = atof(optarg);
|
2003-10-28 11:53:43 +01:00
|
|
|
break;
|
2004-01-19 19:39:12 +01:00
|
|
|
case 'a':
|
|
|
|
ignore_active_sensing = 0;
|
|
|
|
break;
|
2016-08-13 16:41:23 +02:00
|
|
|
case 'c':
|
|
|
|
ignore_clock = 0;
|
|
|
|
break;
|
2016-08-30 18:02:48 +02:00
|
|
|
case 'i':
|
|
|
|
sysex_interval = atoi(optarg);
|
|
|
|
break;
|
2003-10-28 11:53:43 +01:00
|
|
|
default:
|
|
|
|
error("Try `amidi --help' for more information.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2005-02-16 10:52:25 +01:00
|
|
|
if (do_send_hex) {
|
|
|
|
/* data for -S can be specified as multiple arguments */
|
|
|
|
if (!send_hex && !argv[optind]) {
|
|
|
|
error("Please specify some data for --send-hex.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (; argv[optind]; ++optind)
|
|
|
|
add_send_hex_data(argv[optind]);
|
|
|
|
} else {
|
|
|
|
if (argv[optind]) {
|
|
|
|
error("%s is not an option.", argv[optind]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2003-10-28 11:53:43 +01:00
|
|
|
|
|
|
|
if (do_rawmidi_list)
|
|
|
|
rawmidi_list();
|
|
|
|
if (do_device_list)
|
|
|
|
device_list();
|
|
|
|
if (do_rawmidi_list || do_device_list)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!send_file_name && !receive_file_name && !send_hex && !dump) {
|
|
|
|
error("Please specify at least one of --send, --receive, --send-hex, or --dump.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (send_file_name && send_hex) {
|
|
|
|
error("--send and --send-hex cannot be specified at the same time.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (send_file_name)
|
|
|
|
load_file();
|
|
|
|
else if (send_hex)
|
|
|
|
parse_data();
|
|
|
|
if ((send_file_name || send_hex) && !send_data)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (receive_file_name) {
|
2004-01-13 17:11:56 +01:00
|
|
|
receive_file = creat(receive_file_name, 0666);
|
2003-10-28 11:53:43 +01:00
|
|
|
if (receive_file == -1) {
|
|
|
|
error("cannot create %s: %s", receive_file_name, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
receive_file = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (receive_file_name || dump)
|
|
|
|
inputp = &input;
|
|
|
|
else
|
|
|
|
inputp = NULL;
|
|
|
|
if (send_data)
|
|
|
|
outputp = &output;
|
|
|
|
else
|
|
|
|
outputp = NULL;
|
|
|
|
|
2007-04-17 09:25:01 +02:00
|
|
|
if ((err = snd_rawmidi_open(inputp, outputp, port_name, SND_RAWMIDI_NONBLOCK)) < 0) {
|
2003-10-28 11:53:43 +01:00
|
|
|
error("cannot open port \"%s\": %s", port_name, snd_strerror(err));
|
|
|
|
goto _exit2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputp)
|
|
|
|
snd_rawmidi_read(input, NULL, 0); /* trigger reading */
|
|
|
|
|
2007-04-17 09:25:01 +02:00
|
|
|
if (send_data) {
|
|
|
|
if ((err = snd_rawmidi_nonblock(output, 0)) < 0) {
|
|
|
|
error("cannot set blocking mode: %s", snd_strerror(err));
|
|
|
|
goto _exit;
|
|
|
|
}
|
2016-08-30 18:02:48 +02:00
|
|
|
if (!sysex_interval) {
|
|
|
|
if ((err = snd_rawmidi_write(output, send_data, send_data_length)) < 0) {
|
|
|
|
error("cannot send data: %s", snd_strerror(err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((err = send_midi_interleaved()) < 0) {
|
|
|
|
error("cannot send data: %s", snd_strerror(err));
|
|
|
|
return err;
|
|
|
|
}
|
2007-04-17 09:25:01 +02:00
|
|
|
}
|
2003-10-28 11:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inputp) {
|
|
|
|
int read = 0;
|
2016-08-13 16:41:58 +02:00
|
|
|
int npfds;
|
2003-10-28 11:53:43 +01:00
|
|
|
struct pollfd *pfds;
|
|
|
|
|
2016-08-13 16:41:58 +02:00
|
|
|
npfds = 1 + snd_rawmidi_poll_descriptors_count(input);
|
2003-10-28 11:53:43 +01:00
|
|
|
pfds = alloca(npfds * sizeof(struct pollfd));
|
2016-08-13 16:41:58 +02:00
|
|
|
|
|
|
|
if (timeout > 0) {
|
|
|
|
pfds[0].fd = timerfd_create(CLOCK_MONOTONIC, 0);
|
|
|
|
if (pfds[0].fd == -1) {
|
|
|
|
error("cannot create timer: %s", strerror(errno));
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
pfds[0].events = POLLIN;
|
|
|
|
} else {
|
|
|
|
pfds[0].fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
snd_rawmidi_poll_descriptors(input, &pfds[1], npfds - 1);
|
|
|
|
|
2003-10-28 11:53:43 +01:00
|
|
|
signal(SIGINT, sig_handler);
|
2016-08-13 16:41:58 +02:00
|
|
|
|
|
|
|
if (timeout > 0) {
|
|
|
|
float timeout_int;
|
|
|
|
|
|
|
|
itimerspec.it_value.tv_nsec = modff(timeout, &timeout_int) * NSEC_PER_SEC;
|
|
|
|
itimerspec.it_value.tv_sec = timeout_int;
|
|
|
|
err = timerfd_settime(pfds[0].fd, 0, &itimerspec, NULL);
|
|
|
|
if (err < 0) {
|
|
|
|
error("cannot set timer: %s", strerror(errno));
|
|
|
|
goto _exit;
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 11:49:43 +02:00
|
|
|
|
2003-10-28 11:53:43 +01:00
|
|
|
for (;;) {
|
|
|
|
unsigned char buf[256];
|
|
|
|
int i, length;
|
|
|
|
unsigned short revents;
|
2021-08-24 11:49:43 +02:00
|
|
|
struct timespec ts;
|
2003-10-28 11:53:43 +01:00
|
|
|
|
2016-08-13 16:41:58 +02:00
|
|
|
err = poll(pfds, npfds, -1);
|
2003-10-28 11:53:43 +01:00
|
|
|
if (stop || (err < 0 && errno == EINTR))
|
|
|
|
break;
|
|
|
|
if (err < 0) {
|
|
|
|
error("poll failed: %s", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
2016-08-13 16:41:58 +02:00
|
|
|
|
2021-08-24 11:49:43 +02:00
|
|
|
if (clock_gettime(cid, &ts) < 0) {
|
|
|
|
error("clock_getres (%d) failed: %s", cid, strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-13 16:41:58 +02:00
|
|
|
err = snd_rawmidi_poll_descriptors_revents(input, &pfds[1], npfds - 1, &revents);
|
|
|
|
if (err < 0) {
|
2003-10-28 11:53:43 +01:00
|
|
|
error("cannot get poll events: %s", snd_strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (revents & (POLLERR | POLLHUP))
|
|
|
|
break;
|
2016-08-13 16:41:58 +02:00
|
|
|
if (!(revents & POLLIN)) {
|
|
|
|
if (pfds[0].revents & POLLIN)
|
|
|
|
break;
|
2003-10-28 11:53:43 +01:00
|
|
|
continue;
|
2016-08-13 16:41:58 +02:00
|
|
|
}
|
|
|
|
|
2003-10-28 11:53:43 +01:00
|
|
|
err = snd_rawmidi_read(input, buf, sizeof(buf));
|
|
|
|
if (err == -EAGAIN)
|
|
|
|
continue;
|
|
|
|
if (err < 0) {
|
|
|
|
error("cannot read from port \"%s\": %s", port_name, snd_strerror(err));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
length = 0;
|
|
|
|
for (i = 0; i < err; ++i)
|
2016-08-13 16:41:23 +02:00
|
|
|
if ((buf[i] != MIDI_CMD_COMMON_CLOCK &&
|
|
|
|
buf[i] != MIDI_CMD_COMMON_SENSING) ||
|
|
|
|
(buf[i] == MIDI_CMD_COMMON_CLOCK && !ignore_clock) ||
|
|
|
|
(buf[i] == MIDI_CMD_COMMON_SENSING && !ignore_active_sensing))
|
2003-10-28 11:53:43 +01:00
|
|
|
buf[length++] = buf[i];
|
|
|
|
if (length == 0)
|
|
|
|
continue;
|
|
|
|
read += length;
|
2016-08-13 16:41:58 +02:00
|
|
|
|
2021-08-24 11:49:43 +02:00
|
|
|
if (receive_file != -1)
|
|
|
|
write(receive_file, buf, length);
|
2003-10-28 11:53:43 +01:00
|
|
|
if (dump) {
|
|
|
|
for (i = 0; i < length; ++i)
|
2021-08-24 11:49:43 +02:00
|
|
|
print_byte(buf[i], &ts);
|
|
|
|
|
2003-10-28 11:53:43 +01:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
2016-08-13 16:41:58 +02:00
|
|
|
|
|
|
|
if (timeout > 0) {
|
|
|
|
err = timerfd_settime(pfds[0].fd, 0, &itimerspec, NULL);
|
|
|
|
if (err < 0) {
|
|
|
|
error("cannot set timer: %s", strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-10-28 11:53:43 +01:00
|
|
|
}
|
2005-02-16 10:52:25 +01:00
|
|
|
if (isatty(fileno(stdout)))
|
|
|
|
printf("\n%d bytes read\n", read);
|
2003-10-28 11:53:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
_exit:
|
|
|
|
if (inputp)
|
|
|
|
snd_rawmidi_close(input);
|
|
|
|
if (outputp)
|
|
|
|
snd_rawmidi_close(output);
|
|
|
|
_exit2:
|
|
|
|
if (receive_file != -1)
|
|
|
|
close(receive_file);
|
|
|
|
return !ok;
|
|
|
|
}
|