[PATCH alsa-utils 1/5] aplaymidi2: Add initial version
aplaymidi2 is a program similar like aplaymidi, but intended for playing back a MIDI Clip file that was introduced for handling UMP. MIDI Clip file contains UMP packets, and its structure is much simpler than SMF.
The options are mostly same as aplaymidi, but I omitted -l option for simplifying the code.
Signed-off-by: Takashi Iwai tiwai@suse.de --- configure.ac | 2 +- seq/Makefile.am | 2 +- seq/aplaymidi2/Makefile.am | 5 + seq/aplaymidi2/aplaymidi2.1 | 71 ++++++ seq/aplaymidi2/aplaymidi2.c | 447 ++++++++++++++++++++++++++++++++++++ 5 files changed, 525 insertions(+), 2 deletions(-) create mode 100644 seq/aplaymidi2/Makefile.am create mode 100644 seq/aplaymidi2/aplaymidi2.1 create mode 100644 seq/aplaymidi2/aplaymidi2.c
diff --git a/configure.ac b/configure.ac index 173b9ed1644c..708380cf31dc 100644 --- a/configure.ac +++ b/configure.ac @@ -486,7 +486,7 @@ AC_OUTPUT(Makefile alsactl/Makefile alsactl/init/Makefile \ bat/Makefile bat/tests/Makefile bat/tests/asound_state/Makefile \ aplay/Makefile include/Makefile iecset/Makefile utils/Makefile \ utils/alsa-utils.spec seq/Makefile seq/aconnect/Makefile \ - seq/aplaymidi/Makefile seq/aseqdump/Makefile seq/aseqnet/Makefile \ + seq/aplaymidi/Makefile seq/aplaymidi2/Makefile seq/aseqdump/Makefile seq/aseqnet/Makefile \ seq/aseqsend/Makefile speaker-test/Makefile speaker-test/samples/Makefile \ alsaloop/Makefile alsa-info/Makefile \ axfer/Makefile axfer/test/Makefile \ diff --git a/seq/Makefile.am b/seq/Makefile.am index b0f628aac4da..a6171268186b 100644 --- a/seq/Makefile.am +++ b/seq/Makefile.am @@ -1 +1 @@ -SUBDIRS=aconnect aplaymidi aseqdump aseqnet aseqsend +SUBDIRS=aconnect aplaymidi aplaymidi2 aseqdump aseqnet aseqsend diff --git a/seq/aplaymidi2/Makefile.am b/seq/aplaymidi2/Makefile.am new file mode 100644 index 000000000000..0c5c743f7eb8 --- /dev/null +++ b/seq/aplaymidi2/Makefile.am @@ -0,0 +1,5 @@ +AM_CPPFLAGS = -I$(top_srcdir)/include +EXTRA_DIST = aplaymidi2.1 + +bin_PROGRAMS = aplaymidi2 +man_MANS = aplaymidi2.1 diff --git a/seq/aplaymidi2/aplaymidi2.1 b/seq/aplaymidi2/aplaymidi2.1 new file mode 100644 index 000000000000..99be608f107d --- /dev/null +++ b/seq/aplaymidi2/aplaymidi2.1 @@ -0,0 +1,71 @@ +.TH APLAYMIDI2 1 "4 July 2024" + +.SH NAME +aplaymidi2 - play MIDI Clip Files + +.SH SYNOPSIS +.B aplaymidi2 +-p client:port[,...] midi2file ... + +.SH DESCRIPTION +.B aplaymidi2 +is a command-line utility that plays the specified MIDI Clip file(s) to one +or more ALSA sequencer ports. + +.SH OPTIONS + +.TP +.I -h, --help +Prints a list of options. + +.TP +.I -V, --version +Prints the current version. + +.TP +.I -p, --port=client:port,... +Sets the sequencer port(s) to which the events in the MIDI Clip file(s) are +sent. + +A client can be specified by its number, its name, or a prefix of its +name. A port is specified by its number; for port 0 of a client, the +":0" part of the port specification can be omitted. + +Multiple ports can be specified to allow playback of MIDI Clip file(s) that +contain events for multiple devices (ports) corresponding to the +multiple UMP Groups. + +For compatibility with +.B pmidi(1), +the port specification is taken from the +.I ALSA_OUTPUT_PORTS +environment variable if none is given on the command line. + +.B aplaymidi2 +supports only basic UMP events: in addition to the standard MIDI1 and +MIDI2 CVMs and 7bit SysEx, only the following are supported: +DCTPQ, DC, Set Tempo, Start Clip and End Clip. +Lyrics and other meta data in Flex Data are skipped, so far. + +The multiple output ports are useful when the given MIDI Clip file +contains the UMP packets for multiple Groups. +When the destination port is a UMP MIDI 2.0 port, the single +connection should suffice, though, since a MIDI 2.0 port can process +the inputs for multiple Groups. For other cases (e.g. connecting to a +legacy MIDI port), you would need to specify the destination port per +Group. If undefined, it's sent to the first destination port as +default. + +.TP +.I -d, --delay=seconds +Specifies how long to wait after the end of each MIDI Clip file, +to allow the last notes to die away. +Default is 2 seconds. + +.SH SEE ALSO +pmidi(1) +.br +aplaymidi(1) + +.SH AUTHOR +Takashi Iwai tiwai@suse.de diff --git a/seq/aplaymidi2/aplaymidi2.c b/seq/aplaymidi2/aplaymidi2.c new file mode 100644 index 000000000000..435b7de1aa95 --- /dev/null +++ b/seq/aplaymidi2/aplaymidi2.c @@ -0,0 +1,447 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * aplaymidi2.c - simple player of a MIDI Clip File over ALSA sequencer + */ + +#include "aconfig.h" +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <getopt.h> +#include <unistd.h> +#include <alsa/asoundlib.h> +#include <alsa/ump_msg.h> +#include "version.h" + +static snd_seq_t *seq; +static int client; +static int port_count; +static snd_seq_addr_t ports[16]; +static int queue; +static int end_delay = 2; + +static unsigned int _current_tempo = 50000000; /* default 120 bpm */ +static unsigned int tempo_base = 10; +static unsigned int current_tick; + +/* prints an error message to stderr */ +static void errormsg(const char *msg, ...) +{ + va_list ap; + + va_start(ap, msg); + vfprintf(stderr, msg, ap); + va_end(ap); + fputc('\n', stderr); +} + +/* prints an error message to stderr, and dies */ +static void fatal(const char *msg, ...) +{ + va_list ap; + + va_start(ap, msg); + vfprintf(stderr, msg, ap); + va_end(ap); + fputc('\n', stderr); + exit(EXIT_FAILURE); +} + +/* memory allocation error handling */ +static void check_mem(void *p) +{ + if (!p) + fatal("Out of memory"); +} + +/* error handling for ALSA functions */ +static void check_snd(const char *operation, int err) +{ + if (err < 0) + fatal("Cannot %s - %s", operation, snd_strerror(err)); +} + +/* open and initialize the sequencer client */ +static void init_seq(void) +{ + int err; + + err = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0); + check_snd("open sequencer", err); + + err = snd_seq_set_client_name(seq, "aplaymidi2"); + check_snd("set client name", err); + + client = snd_seq_client_id(seq); + check_snd("get client id", client); + + err = snd_seq_set_client_midi_version(seq, SND_SEQ_CLIENT_UMP_MIDI_2_0); + check_snd("set midi version", err); +} + +/* parses one or more port addresses from the string */ +static void parse_ports(const char *arg) +{ + char *buf, *s, *port_name; + int err; + + /* make a copy of the string because we're going to modify it */ + buf = strdup(arg); + check_mem(buf); + + for (port_name = s = buf; s; port_name = s + 1) { + /* Assume that ports are separated by commas. We don't use + * spaces because those are valid in client names. */ + s = strchr(port_name, ','); + if (s) + *s = '\0'; + + ++port_count; + if (port_count > 16) + fatal("Too many ports specified"); + + err = snd_seq_parse_address(seq, &ports[port_count - 1], port_name); + if (err < 0) + fatal("Invalid port %s - %s", port_name, snd_strerror(err)); + } + + free(buf); +} + +/* create a source port to send from */ +static void create_source_port(void) +{ + snd_seq_port_info_t *pinfo; + int err; + + snd_seq_port_info_alloca(&pinfo); + + /* the first created port is 0 anyway, but let's make sure ... */ + snd_seq_port_info_set_port(pinfo, 0); + snd_seq_port_info_set_port_specified(pinfo, 1); + + snd_seq_port_info_set_name(pinfo, "aplaymidi2"); + + snd_seq_port_info_set_capability(pinfo, 0); /* sic */ + snd_seq_port_info_set_type(pinfo, + SND_SEQ_PORT_TYPE_MIDI_GENERIC | + SND_SEQ_PORT_TYPE_APPLICATION); + + err = snd_seq_create_port(seq, pinfo); + check_snd("create port", err); +} + +/* create a queue */ +static void create_queue(void) +{ + if (!snd_seq_has_queue_tempo_base(seq)) + tempo_base = 1000; + + queue = snd_seq_alloc_named_queue(seq, "aplaymidi2"); + check_snd("create queue", queue); +} + +/* connect to destination ports */ +static void connect_ports(void) +{ + int i, err; + + for (i = 0; i < port_count; ++i) { + err = snd_seq_connect_to(seq, 0, ports[i].client, ports[i].port); + if (err < 0) + fatal("Cannot connect to port %d:%d - %s", + ports[i].client, ports[i].port, snd_strerror(err)); + } +} + +/* read 32bit word and convert to native endian: + * return 0 on success, -1 on error + */ +static int read_word(FILE *file, uint32_t *dest) +{ + uint32_t v; + + if (fread(&v, 4, 1, file) != 1) + return -1; + *dest = be32toh(v); + return 0; +} + +/* read a UMP packet: return the number of packets, -1 on error */ +static int read_ump_packet(FILE *file, uint32_t *buf) +{ + snd_ump_msg_hdr_t *h = (snd_ump_msg_hdr_t *)buf; + + int i, num; + + if (read_word(file, buf) < 0) + return -1; + num = snd_ump_packet_length(h->type); + for (i = 1; i < num; i++) { + if (read_word(file, buf + i) < 0) + return -1; + } + return num; +} + +/* read the file header and verify it's MIDI Clip File: return 0 on success */ +static int verify_file_header(FILE *file) +{ + unsigned char buf[8]; + + if (fread(buf, 1, 8, file) != 8) + return -1; + if (memcmp(buf, "SMF2CLIP", 8)) + return -1; + return 0; +} + +/* return the current tempo, corrected to be sent to host */ +static int current_tempo(void) +{ + if (tempo_base != 10) + return _current_tempo / 100; /* down to us */ + return _current_tempo; +} + +/* send a timer event */ +static void send_timer_event(unsigned int type, unsigned int val) +{ + snd_seq_ump_event_t ev = { + .type = type, + .flags = SND_SEQ_TIME_STAMP_TICK | SND_SEQ_EVENT_LENGTH_FIXED, + }; + + ev.queue = queue; + ev.source.port = 0; + ev.time.tick = current_tick; + + ev.dest.client = SND_SEQ_CLIENT_SYSTEM; + ev.dest.port = SND_SEQ_PORT_SYSTEM_TIMER; + ev.data.queue.queue = queue; + ev.data.queue.param.value = val; + + snd_seq_ump_event_output(seq, &ev); +} + +/* set DCTPQ */ +static void set_dctpq(unsigned int ppq) +{ + snd_seq_queue_tempo_t *queue_tempo; + + snd_seq_queue_tempo_alloca(&queue_tempo); + snd_seq_queue_tempo_set_tempo(queue_tempo, current_tempo()); + snd_seq_queue_tempo_set_ppq(queue_tempo, ppq); + snd_seq_queue_tempo_set_tempo_base(queue_tempo, tempo_base); + + if (snd_seq_set_queue_tempo(seq, queue, queue_tempo) < 0) + errormsg("Cannot set queue tempo (%d)", queue); +} + +/* set DC */ +static void set_dc(unsigned int ticks) +{ + current_tick += ticks; +} + +/* set tempo event */ +static void set_tempo(unsigned int tempo) +{ + _current_tempo = tempo; + send_timer_event(SND_SEQ_EVENT_TEMPO, current_tempo()); +} + +/* start clip */ +static void start_clip(void) +{ + if (snd_seq_start_queue(seq, queue, NULL) < 0) + errormsg("Cannot start queue (%d)", queue); +} + +/* end clip */ +static void end_clip(void) +{ + send_timer_event(SND_SEQ_EVENT_STOP, 0); +} + +/* send a UMP packet */ +static void send_ump(const uint32_t *ump, int len) +{ + snd_seq_ump_event_t ev = { + .flags = SND_SEQ_TIME_STAMP_TICK | SND_SEQ_EVENT_LENGTH_FIXED | + SND_SEQ_EVENT_UMP, + }; + int group; + + memcpy(ev.ump, ump, len * 4); + + ev.queue = queue; + ev.source.port = 0; + ev.time.tick = current_tick; + group = snd_ump_msg_group(ump); + if (group >= port_count) + ev.dest = ports[0]; + else + ev.dest = ports[group]; + + snd_seq_ump_event_output(seq, &ev); +} + +/* play the given MIDI Clip File content */ +static void play_midi(FILE *file) +{ + uint32_t ump[4]; + int len; + + current_tick = 0; + + while ((len = read_ump_packet(file, ump)) > 0) { + const snd_ump_msg_hdr_t *h = (snd_ump_msg_hdr_t *)ump; + + if (h->type == SND_UMP_MSG_TYPE_UTILITY) { + const snd_ump_msg_utility_t *uh = + (const snd_ump_msg_utility_t *)ump; + switch (h->status) { + case SND_UMP_UTILITY_MSG_STATUS_DCTPQ: + set_dctpq(uh->dctpq.ticks); + continue; + case SND_UMP_UTILITY_MSG_STATUS_DC: + set_dc(uh->dctpq.ticks); + continue; + } + } else if (h->type == SND_UMP_MSG_TYPE_FLEX_DATA) { + const snd_ump_msg_flex_data_t *fh = + (const snd_ump_msg_flex_data_t *)ump; + if (fh->meta.status_bank == SND_UMP_FLEX_DATA_MSG_BANK_SETUP && + fh->meta.status == SND_UMP_FLEX_DATA_MSG_STATUS_SET_TEMPO) { + set_tempo(fh->set_tempo.tempo); + continue; + } + } else if (h->type == SND_UMP_MSG_TYPE_STREAM) { + const snd_ump_msg_stream_t *sh = + (const snd_ump_msg_stream_t *)ump; + switch (sh->gen.status) { + case SND_UMP_STREAM_MSG_STATUS_START_CLIP: + start_clip(); + continue; + case SND_UMP_STREAM_MSG_STATUS_END_CLIP: + end_clip(); + continue; + } + } else if (h->type == SND_UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE || + h->type == SND_UMP_MSG_TYPE_DATA || + h->type == SND_UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE) { + send_ump(ump, len); + } + } + + snd_seq_drain_output(seq); + snd_seq_sync_output_queue(seq); + + /* give the last notes time to die away */ + if (end_delay > 0) + sleep(end_delay); +} + +static void play_file(const char *file_name) +{ + FILE *file; + + if (!strcmp(file_name, "-")) + file = stdin; + else + file = fopen(file_name, "rb"); + if (!file) { + errormsg("Cannot open %s - %s", file_name, strerror(errno)); + return; + } + + if (verify_file_header(file) < 0) { + errormsg("%s is not a MIDI Clip File", file_name); + goto error; + } + + play_midi(file); + + error: + if (file != stdin) + fclose(file); +} + +static void usage(const char *argv0) +{ + printf( + "Usage: %s -p client:port[,...] [-d delay] midifile ...\n" + "-h, --help this help\n" + "-V, --version print current version\n" + "-p, --port=client:port,... set port(s) to play to\n" + "-d, --delay=seconds delay after song ends\n", + argv0); +} + +static void version(void) +{ + puts("aplaymidi2 version " SND_UTIL_VERSION_STR); +} + +int main(int argc, char *argv[]) +{ + static const struct option long_options[] = { + {"help", 0, NULL, 'h'}, + {"version", 0, NULL, 'V'}, + {"port", 1, NULL, 'p'}, + {"delay", 1, NULL, 'd'}, + {0} + }; + int c; + + init_seq(); + + while ((c = getopt_long(argc, argv, "hVp:d:", + long_options, NULL)) != -1) { + switch (c) { + case 'h': + usage(argv[0]); + return 0; + case 'V': + version(); + return 0; + case 'p': + parse_ports(optarg); + break; + case 'd': + end_delay = atoi(optarg); + break; + default: + usage(argv[0]); + return 1; + } + } + + + if (port_count < 1) { + /* use env var for compatibility with pmidi */ + const char *ports_str = getenv("ALSA_OUTPUT_PORTS"); + if (ports_str) + parse_ports(ports_str); + if (port_count < 1) { + errormsg("Please specify at least one port with --port."); + return 1; + } + } + if (optind >= argc) { + errormsg("Please specify a file to play."); + return 1; + } + + create_source_port(); + create_queue(); + connect_ports(); + + for (; optind < argc; optind++) + play_file(argv[optind]); + + snd_seq_close(seq); + return 0; +}
arecordmidi2 is a similar program like arecordmidi for recording the incoming MIDI events, but storing in a MIDI Clip file for MIDI 2.0.
Most options are kept from arecordmidi, but some are dropped: namely, the -l, -m and -f options are dropped for code simplicity. Also -s option is dropped as well, as there is no need for split for MIDI Clip file unlike SMF.
Signed-off-by: Takashi Iwai tiwai@suse.de --- seq/aplaymidi2/Makefile.am | 6 +- seq/aplaymidi2/arecordmidi2.1 | 73 +++++ seq/aplaymidi2/arecordmidi2.c | 512 ++++++++++++++++++++++++++++++++++ 3 files changed, 588 insertions(+), 3 deletions(-) create mode 100644 seq/aplaymidi2/arecordmidi2.1 create mode 100644 seq/aplaymidi2/arecordmidi2.c
diff --git a/seq/aplaymidi2/Makefile.am b/seq/aplaymidi2/Makefile.am index 0c5c743f7eb8..8985382efe82 100644 --- a/seq/aplaymidi2/Makefile.am +++ b/seq/aplaymidi2/Makefile.am @@ -1,5 +1,5 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -EXTRA_DIST = aplaymidi2.1 +EXTRA_DIST = aplaymidi2.1 arecordmidi2.1
-bin_PROGRAMS = aplaymidi2 -man_MANS = aplaymidi2.1 +bin_PROGRAMS = aplaymidi2 arecordmidi2 +man_MANS = aplaymidi2.1 arecordmidi2.1 diff --git a/seq/aplaymidi2/arecordmidi2.1 b/seq/aplaymidi2/arecordmidi2.1 new file mode 100644 index 000000000000..0e41a300b553 --- /dev/null +++ b/seq/aplaymidi2/arecordmidi2.1 @@ -0,0 +1,73 @@ +.TH ARECORDMIDI2 1 "4 July 2024" + +.SH NAME +arecordmidi2 - record a MIDI Clip file + +.SH SYNOPSIS +.B arecordmidi2 +-p client:port[,...] [options] midi2file + +.SH DESCRIPTION +.B arecordmidi2 +is a command-line utility that records a MIDI Clip file from one or +more ALSA sequencer ports. + +To stop recording, press Ctrl+C. + +.SH OPTIONS + +.TP +.I -h,--help +Prints a list of options. + +.TP +.I -V,--version +Prints the current version. + +.TP +.I -p,--port=client:port,... +Sets the sequencer port(s) from which events are recorded. + +A client can be specified by its number, its name, or a prefix of its +name. A port is specified by its number; for port 0 of a client, the +":0" part of the port specification can be omitted. + +.TP +.I -b,--bpm=beats +Sets the musical tempo of the MIDI file, in beats per minute. +The default value is 120 BPM. + +.TP +.I -t,--ticks=ticks +Sets the resolution of timestamps (ticks) in the MIDI file, +in ticks per beat. +The default value is 384 ticks/beat. + +.TP +.I -i,--timesig=numerator:denominator +Sets the time signature for the MIDI file. + +The time signature is specified as usual with two numbers, representing +the numerator and denominator of the time signature as it would be +notated. The denominator must be a power of two. Both numbers should be +separated by a colon. The time signature is 4:4 by default. + +.TP +.I -n,--num-events=events +Stops the recording after receiving the given number of events. + +.TP +.I -u,--ump=version +Sets the UMP MIDI protocol version. Either 1 or 2 has to be given for +MIDI 1.0 and MIDI 2.0 protocol, respectively. +Default is 1. + +.SH SEE ALSO +arecordmidi(1) +.br +aplaymidi2(1) + +.SH AUTHOR +Takashi Iwai tiwai@suse.de + + diff --git a/seq/aplaymidi2/arecordmidi2.c b/seq/aplaymidi2/arecordmidi2.c new file mode 100644 index 000000000000..32693854b7a8 --- /dev/null +++ b/seq/aplaymidi2/arecordmidi2.c @@ -0,0 +1,512 @@ +/* + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> +#include <string.h> +#include <signal.h> +#include <getopt.h> +#include <poll.h> +#include <alsa/asoundlib.h> +#include <alsa/ump_msg.h> +#include "aconfig.h" +#include "version.h" + +static snd_seq_t *seq; +static int client; +static int port_count; +static snd_seq_addr_t *ports; +static int queue; +static int midi_version = 1; +static int beats = 120; +static int ticks = 384; +static int tempo_base = 10; +static volatile sig_atomic_t stop; +static int ts_num = 4; /* time signature: numerator */ +static int ts_div = 4; /* time signature: denominator */ +static int last_tick; + +/* Parse a decimal number from a command line argument. */ +static long arg_parse_decimal_num(const char *str, int *err) +{ + long val; + char *endptr; + + errno = 0; + val = strtol(str, &endptr, 0); + if (errno > 0) { + *err = -errno; + return 0; + } + if (*endptr != '\0') { + *err = -EINVAL; + return 0; + } + + return val; +} + +/* prints an error message to stderr, and dies */ +static void fatal(const char *msg, ...) +{ + va_list ap; + + va_start(ap, msg); + vfprintf(stderr, msg, ap); + va_end(ap); + fputc('\n', stderr); + exit(EXIT_FAILURE); +} + +/* memory allocation error handling */ +static void check_mem(void *p) +{ + if (!p) + fatal("Out of memory"); +} + +/* error handling for ALSA functions */ +static void check_snd(const char *operation, int err) +{ + if (err < 0) + fatal("Cannot %s - %s", operation, snd_strerror(err)); +} + +/* open a sequencer client */ +static void init_seq(void) +{ + int err; + + /* open sequencer */ + err = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0); + check_snd("open sequencer", err); + + /* find out our client's id */ + client = snd_seq_client_id(seq); + check_snd("get client id", client); +} + +/* set up UMP virtual client/port */ +static void create_ump_client(void) +{ + snd_ump_endpoint_info_t *ep; + snd_ump_block_info_t *blk; + snd_seq_port_info_t *pinfo; + int i, err; + + /* create a UMP Endpoint */ + snd_ump_endpoint_info_alloca(&ep); + snd_ump_endpoint_info_set_name(ep, "arecordmidi2"); + if (midi_version == 1) { + snd_ump_endpoint_info_set_protocol_caps(ep, SND_UMP_EP_INFO_PROTO_MIDI1); + snd_ump_endpoint_info_set_protocol(ep, SND_UMP_EP_INFO_PROTO_MIDI1); + } else { + snd_ump_endpoint_info_set_protocol_caps(ep, SND_UMP_EP_INFO_PROTO_MIDI2); + snd_ump_endpoint_info_set_protocol(ep, SND_UMP_EP_INFO_PROTO_MIDI2); + } + snd_ump_endpoint_info_set_num_blocks(ep, port_count); + + err = snd_seq_create_ump_endpoint(seq, ep, port_count); + check_snd("create UMP endpoint", err); + + /* create UMP Function Blocks */ + snd_ump_block_info_alloca(&blk); + for (i = 0; i < port_count; i++) { + char blkname[32]; + + sprintf(blkname, "Group %d", i + 1); + snd_ump_block_info_set_name(blk, blkname); + snd_ump_block_info_set_direction(blk, SND_UMP_DIR_INPUT); + snd_ump_block_info_set_first_group(blk, i); + snd_ump_block_info_set_num_groups(blk, 1); + snd_ump_block_info_set_ui_hint(blk, SND_UMP_BLOCK_UI_HINT_RECEIVER); + + err = snd_seq_create_ump_block(seq, i, blk); + check_snd("create UMP block", err); + } + + /* toggle timestamping for all input ports */ + snd_seq_port_info_alloca(&pinfo); + for (i = 0; i <= port_count; i++) { + err = snd_seq_get_port_info(seq, i, pinfo); + check_snd("get port info", err); + snd_seq_port_info_set_timestamping(pinfo, 1); + snd_seq_port_info_set_timestamp_queue(pinfo, queue); + snd_seq_set_port_info(seq, i, pinfo); + check_snd("set port info", err); + } +} + +/* parses one or more port addresses from the string */ +static void parse_ports(const char *arg) +{ + char *buf, *s, *port_name; + int err; + + /* make a copy of the string because we're going to modify it */ + buf = strdup(arg); + check_mem(buf); + + for (port_name = s = buf; s; port_name = s + 1) { + /* Assume that ports are separated by commas. We don't use + * spaces because those are valid in client names. + */ + s = strchr(port_name, ','); + if (s) + *s = '\0'; + + ++port_count; + ports = realloc(ports, port_count * sizeof(snd_seq_addr_t)); + check_mem(ports); + + err = snd_seq_parse_address(seq, &ports[port_count - 1], port_name); + if (err < 0) + fatal("Invalid port %s - %s", port_name, snd_strerror(err)); + } + + free(buf); +} + +/* parses time signature specification */ +static void time_signature(const char *arg) +{ + long x = 0; + char *sep; + + x = strtol(arg, &sep, 10); + if (x < 1 || x > 64 || *sep != ':') + fatal("Invalid time signature (%s)", arg); + ts_num = x; + x = strtol(++sep, NULL, 10); + if (x < 1 || x > 64) + fatal("Invalid time signature (%s)", arg); + ts_div = x; +} + +/* create a queue, set up the default tempo */ +static void create_queue(void) +{ + snd_seq_queue_tempo_t *tempo; + + if (!snd_seq_has_queue_tempo_base(seq)) + tempo_base = 1000; + + queue = snd_seq_alloc_named_queue(seq, "arecordmidi2"); + check_snd("create queue", queue); + + snd_seq_queue_tempo_alloca(&tempo); + if (tempo_base == 1000) + snd_seq_queue_tempo_set_tempo(tempo, 60000000 / beats); + else + snd_seq_queue_tempo_set_tempo(tempo, (unsigned int)(6000000000ULL / beats)); + snd_seq_queue_tempo_set_ppq(tempo, ticks); + snd_seq_queue_tempo_set_tempo_base(tempo, tempo_base); + if (snd_seq_set_queue_tempo(seq, queue, tempo) < 0) + fatal("Cannot set queue tempo (%d)", queue); +} + +/* connect to the input ports */ +static void connect_ports(void) +{ + int i, err; + + for (i = 0; i < port_count; ++i) { + err = snd_seq_connect_from(seq, i + 1, + ports[i].client, ports[i].port); + check_snd("port connection", err); + } +} + +/* write the given UMP packet */ +static void write_ump(FILE *file, const void *src) +{ + const snd_ump_msg_hdr_t *h = src; + const uint32_t *p = src; + uint32_t v; + int len; + + len = snd_ump_packet_length(h->type); + while (len-- > 0) { + v = htobe32(*p++); + fwrite(&v, 4, 1, file); + } +} + +/* write a DC message */ +static void write_dcs(FILE *file, unsigned int t) +{ + snd_ump_msg_dc_t d = {}; + + d.type = SND_UMP_MSG_TYPE_UTILITY; + d.status = SND_UMP_UTILITY_MSG_STATUS_DC; + d.ticks = t; + write_ump(file, &d); +} + +/* write a DCTPQ message */ +static void write_dctpq(FILE *file) +{ + snd_ump_msg_dctpq_t d = {}; + + d.type = SND_UMP_MSG_TYPE_UTILITY; + d.status = SND_UMP_UTILITY_MSG_STATUS_DCTPQ; + d.ticks = ticks; + write_ump(file, &d); +} + +/* write a Start Clip message */ +static void write_start_clip(FILE *file) +{ + snd_ump_msg_stream_gen_t d = {}; + + d.type = SND_UMP_MSG_TYPE_STREAM; + d.status = SND_UMP_STREAM_MSG_STATUS_START_CLIP; + write_ump(file, &d); +} + +/* write an End Clip message */ +static void write_end_clip(FILE *file) +{ + snd_ump_msg_stream_gen_t d = {}; + + d.type = SND_UMP_MSG_TYPE_STREAM; + d.status = SND_UMP_STREAM_MSG_STATUS_END_CLIP; + write_ump(file, &d); +} + +/* write a Set Tempo message */ +static void write_tempo(FILE *file) +{ + snd_ump_msg_set_tempo_t d = {}; + + d.type = SND_UMP_MSG_TYPE_FLEX_DATA; + d.group = 0; + d.format = SND_UMP_FLEX_DATA_MSG_FORMAT_SINGLE; + d.addrs = SND_UMP_FLEX_DATA_MSG_ADDR_GROUP; + d.status_bank = SND_UMP_FLEX_DATA_MSG_BANK_SETUP; + d.status = SND_UMP_FLEX_DATA_MSG_STATUS_SET_TEMPO; + d.tempo = (unsigned int)(6000000000ULL / beats); + write_ump(file, &d); +} + +/* write a Set Time Signature message */ +static void write_time_sig(FILE *file) +{ + snd_ump_msg_set_time_sig_t d = {}; + + d.type = SND_UMP_MSG_TYPE_FLEX_DATA; + d.group = 0; + d.format = SND_UMP_FLEX_DATA_MSG_FORMAT_SINGLE; + d.addrs = SND_UMP_FLEX_DATA_MSG_ADDR_GROUP; + d.status_bank = SND_UMP_FLEX_DATA_MSG_BANK_SETUP; + d.status = SND_UMP_FLEX_DATA_MSG_STATUS_SET_TIME_SIGNATURE; + d.numerator = ts_num; + d.denominator = ts_div; + d.num_notes = 8; + write_ump(file, &d); +} + +/* record the delta time from the last event */ +static void delta_time(FILE *file, const snd_seq_ump_event_t *ev) +{ + int diff = ev->time.tick - last_tick; + + if (diff <= 0) + return; + if (tempo_base == 1000) + diff *= 100; + write_dcs(file, diff); + last_tick = ev->time.tick; +} + +static void record_event(FILE *file, const snd_seq_ump_event_t *ev) +{ + /* ignore events without proper timestamps */ + if (ev->queue != queue || !snd_seq_ev_is_tick(ev) || + !snd_seq_ev_is_ump(ev)) + return; + + delta_time(file, ev); + write_ump(file, ev->ump); +} + +/* write MIDI Clip file header and the configuration packets */ +static void write_file_header(FILE *file) +{ + /* header id */ + fwrite("SMF2CLIP", 1, 8, file); + + /* clip configuration header */ + /* FIXME: add profiles */ + + /* first DCS */ + write_dcs(file, 0); + write_dctpq(file); + + /* start bar */ + write_start_clip(file); + write_tempo(file); + write_time_sig(file); +} + +static void help(const char *argv0) +{ + fprintf(stderr, "Usage: %s [options] outputfile\n" + "\nAvailable options:\n" + " -h,--help this help\n" + " -V,--version show version\n" + " -p,--port=client:port,... source port(s)\n" + " -b,--bpm=beats tempo in beats per minute\n" + " -t,--ticks=ticks resolution in ticks per beat or frame\n" + " -i,--timesig=nn:dd time signature\n" + " -n,--num-events=events fixed number of events to record, then exit\n" + " -u,--ump=version UMP MIDI version (1 or 2)\n", + argv0); +} + +static void version(void) +{ + fputs("arecordmidi version " SND_UTIL_VERSION_STR "\n", stderr); +} + +static void sighandler(int sig ATTRIBUTE_UNUSED) +{ + stop = 1; +} + +int main(int argc, char *argv[]) +{ + static const char short_options[] = "hVp:b:t:n:u:"; + static const struct option long_options[] = { + {"help", 0, NULL, 'h'}, + {"version", 0, NULL, 'V'}, + {"port", 1, NULL, 'p'}, + {"bpm", 1, NULL, 'b'}, + {"ticks", 1, NULL, 't'}, + {"timesig", 1, NULL, 'i'}, + {"num-events", 1, NULL, 'n'}, + {"ump", 1, NULL, 'u'}, + {0} + }; + + char *filename; + FILE *file; + struct pollfd *pfds; + int npfds; + int c, err; + /* If |num_events| isn't specified, leave it at 0. */ + long num_events = 0; + long events_received = 0; + + init_seq(); + + while ((c = getopt_long(argc, argv, short_options, + long_options, NULL)) != -1) { + switch (c) { + case 'h': + help(argv[0]); + return 0; + case 'V': + version(); + return 0; + case 'p': + parse_ports(optarg); + break; + case 'b': + beats = atoi(optarg); + if (beats < 4 || beats > 6000) + fatal("Invalid tempo"); + break; + case 't': + ticks = atoi(optarg); + if (ticks < 1 || ticks > 0x7fff) + fatal("Invalid number of ticks"); + break; + case 'i': + time_signature(optarg); + break; + case 'n': + err = 0; + num_events = arg_parse_decimal_num(optarg, &err); + if (err != 0) { + fatal("Couldn't parse num_events argument: %s\n", + strerror(-err)); + } + if (num_events <= 0) + fatal("num_events must be greater than 0"); + break; + case 'u': + midi_version = atoi(optarg); + if (midi_version != 1 && midi_version != 2) + fatal("Invalid MIDI version %d\n", midi_version); + break; + default: + help(argv[0]); + return 1; + } + } + + if (port_count < 1) { + fputs("Pleast specify a source port with --port.\n", stderr); + return 1; + } + + if (optind >= argc) { + fputs("Please specify a file to record to.\n", stderr); + return 1; + } + + create_queue(); + create_ump_client(); + connect_ports(); + + filename = argv[optind]; + + file = fopen(filename, "wb"); + if (!file) + fatal("Cannot open %s - %s", filename, strerror(errno)); + + write_file_header(file); + + err = snd_seq_start_queue(seq, queue, NULL); + check_snd("start queue", err); + snd_seq_drain_output(seq); + + err = snd_seq_nonblock(seq, 1); + check_snd("set nonblock mode", err); + + signal(SIGINT, sighandler); + signal(SIGTERM, sighandler); + + npfds = snd_seq_poll_descriptors_count(seq, POLLIN); + pfds = alloca(sizeof(*pfds) * npfds); + for (;;) { + snd_seq_poll_descriptors(seq, pfds, npfds, POLLIN); + if (poll(pfds, npfds, -1) < 0) + break; + do { + snd_seq_ump_event_t *event; + + err = snd_seq_ump_event_input(seq, &event); + if (err < 0) + break; + if (event) { + record_event(file, event); + events_received++; + } + } while (err > 0); + if (stop) + break; + if (num_events && (events_received >= num_events)) + break; + } + + if (num_events && events_received < num_events) + fputs("Warning: Received signal before num_events\n", stdout); + + write_end_clip(file); + fclose(file); + snd_seq_close(seq); + return 0; +}
Signed-off-by: Takashi Iwai tiwai@suse.de --- .gitignore | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/.gitignore b/.gitignore index b61e1df34b11..7a5817f253d7 100644 --- a/.gitignore +++ b/.gitignore @@ -48,6 +48,8 @@ iecset/iecset seq/aconnect/aconnect seq/aplaymidi/aplaymidi seq/aplaymidi/arecordmidi +seq/aplaymidi2/aplaymidi2 +seq/aplaymidi2/arecordmidi2 seq/aseqdump/aseqdump seq/aseqsend/aseqsend seq/aseqnet/aseqnet
Signed-off-by: Takashi Iwai tiwai@suse.de --- .gitignore | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/.gitignore b/.gitignore index 7a5817f253d7..1763f60a8376 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,8 @@ ABOUT-NLS *.o *~ .deps +.libs +.dirstamp
alsactl/alsactl alsactl/alsactl_init.7
Allow arecordmidi2 running without specifying the source ports via -p option. This will create a UMP Endpoint with the full 16 FBs, and simply reads from the input ports via subscribers. User needs to connect to the ports manually, though.
Also, add -r option to run in the interactive mode. In the interactive mode, arecordmidi2 waits for the RETURN key entered from the terminal to start the recording, and the recording ends after another RETURN key.
Signed-off-by: Takashi Iwai tiwai@suse.de --- seq/aplaymidi2/arecordmidi2.1 | 20 +++++++++- seq/aplaymidi2/arecordmidi2.c | 75 +++++++++++++++++++++++++++-------- 2 files changed, 77 insertions(+), 18 deletions(-)
diff --git a/seq/aplaymidi2/arecordmidi2.1 b/seq/aplaymidi2/arecordmidi2.1 index 0e41a300b553..a9cfb00d0318 100644 --- a/seq/aplaymidi2/arecordmidi2.1 +++ b/seq/aplaymidi2/arecordmidi2.1 @@ -5,7 +5,7 @@ arecordmidi2 - record a MIDI Clip file
.SH SYNOPSIS .B arecordmidi2 --p client:port[,...] [options] midi2file +[options] midi2file
.SH DESCRIPTION .B arecordmidi2 @@ -32,6 +32,16 @@ A client can be specified by its number, its name, or a prefix of its name. A port is specified by its number; for port 0 of a client, the ":0" part of the port specification can be omitted.
+\fBarecordmidi2\fP creates a UMP Endpoint containing the same number +of Function Blocks as specified by this option, each of which is +connected to the specified port as a source. + +When no source ports are specified with \fI-p\fP option, +\fBarecordmidi2\fP creates a UMP Endpoint with full 16 Function Blocks +and records from those inputs. User can connect the sequencer ports +freely via \fBaconnect\fP, for example. This mode can be used +together with the interactive mode via \fI-r\fP option. + .TP .I -b,--bpm=beats Sets the musical tempo of the MIDI file, in beats per minute. @@ -62,6 +72,14 @@ Sets the UMP MIDI protocol version. Either 1 or 2 has to be given for MIDI 1.0 and MIDI 2.0 protocol, respectively. Default is 1.
+.TP +.I -r,--interactive +Run in the interactive mode. \fBarecordmidi2\fP waits for a RETURN +key input from the terminal to start the recording. After starting, +the recording ends when another RETURN key is input from the +terminal. The received events before the start of recording are +discarded. + .SH SEE ALSO arecordmidi(1) .br diff --git a/seq/aplaymidi2/arecordmidi2.c b/seq/aplaymidi2/arecordmidi2.c index 32693854b7a8..cad5851c48ea 100644 --- a/seq/aplaymidi2/arecordmidi2.c +++ b/seq/aplaymidi2/arecordmidi2.c @@ -93,8 +93,15 @@ static void create_ump_client(void) snd_ump_endpoint_info_t *ep; snd_ump_block_info_t *blk; snd_seq_port_info_t *pinfo; + int num_groups; int i, err;
+ /* in passive mode, create full 16 groups */ + if (port_count) + num_groups = port_count; + else + num_groups = 16; + /* create a UMP Endpoint */ snd_ump_endpoint_info_alloca(&ep); snd_ump_endpoint_info_set_name(ep, "arecordmidi2"); @@ -105,14 +112,14 @@ static void create_ump_client(void) snd_ump_endpoint_info_set_protocol_caps(ep, SND_UMP_EP_INFO_PROTO_MIDI2); snd_ump_endpoint_info_set_protocol(ep, SND_UMP_EP_INFO_PROTO_MIDI2); } - snd_ump_endpoint_info_set_num_blocks(ep, port_count); + snd_ump_endpoint_info_set_num_blocks(ep, num_groups);
- err = snd_seq_create_ump_endpoint(seq, ep, port_count); + err = snd_seq_create_ump_endpoint(seq, ep, num_groups); check_snd("create UMP endpoint", err);
/* create UMP Function Blocks */ snd_ump_block_info_alloca(&blk); - for (i = 0; i < port_count; i++) { + for (i = 0; i < num_groups; i++) { char blkname[32];
sprintf(blkname, "Group %d", i + 1); @@ -128,7 +135,7 @@ static void create_ump_client(void)
/* toggle timestamping for all input ports */ snd_seq_port_info_alloca(&pinfo); - for (i = 0; i <= port_count; i++) { + for (i = 0; i <= num_groups; i++) { err = snd_seq_get_port_info(seq, i, pinfo); check_snd("get port info", err); snd_seq_port_info_set_timestamping(pinfo, 1); @@ -343,8 +350,11 @@ static void write_file_header(FILE *file) /* first DCS */ write_dcs(file, 0); write_dctpq(file); +}
- /* start bar */ +/* write start bar */ +static void start_bar(FILE *file) +{ write_start_clip(file); write_tempo(file); write_time_sig(file); @@ -361,7 +371,8 @@ static void help(const char *argv0) " -t,--ticks=ticks resolution in ticks per beat or frame\n" " -i,--timesig=nn:dd time signature\n" " -n,--num-events=events fixed number of events to record, then exit\n" - " -u,--ump=version UMP MIDI version (1 or 2)\n", + " -u,--ump=version UMP MIDI version (1 or 2)\n" + " -r,--interactive Interactive mode\n", argv0); }
@@ -377,7 +388,7 @@ static void sighandler(int sig ATTRIBUTE_UNUSED)
int main(int argc, char *argv[]) { - static const char short_options[] = "hVp:b:t:n:u:"; + static const char short_options[] = "hVp:b:t:n:u:r"; static const struct option long_options[] = { {"help", 0, NULL, 'h'}, {"version", 0, NULL, 'V'}, @@ -387,6 +398,7 @@ int main(int argc, char *argv[]) {"timesig", 1, NULL, 'i'}, {"num-events", 1, NULL, 'n'}, {"ump", 1, NULL, 'u'}, + {"interactive", 0, NULL, 'r'}, {0} };
@@ -398,6 +410,8 @@ int main(int argc, char *argv[]) /* If |num_events| isn't specified, leave it at 0. */ long num_events = 0; long events_received = 0; + int start = 0; + int interactive = 0;
init_seq();
@@ -441,17 +455,15 @@ int main(int argc, char *argv[]) if (midi_version != 1 && midi_version != 2) fatal("Invalid MIDI version %d\n", midi_version); break; + case 'r': + interactive = 1; + break; default: help(argv[0]); return 1; } }
- if (port_count < 1) { - fputs("Pleast specify a source port with --port.\n", stderr); - return 1; - } - if (optind >= argc) { fputs("Please specify a file to record to.\n", stderr); return 1; @@ -459,7 +471,8 @@ int main(int argc, char *argv[])
create_queue(); create_ump_client(); - connect_ports(); + if (port_count) + connect_ports();
filename = argv[optind];
@@ -468,6 +481,13 @@ int main(int argc, char *argv[]) fatal("Cannot open %s - %s", filename, strerror(errno));
write_file_header(file); + if (interactive) { + printf("Press RETURN to start recording:"); + fflush(stdout); + } else { + start_bar(file); + start = 1; + }
err = snd_seq_start_queue(seq, queue, NULL); check_snd("start queue", err); @@ -480,18 +500,39 @@ int main(int argc, char *argv[]) signal(SIGTERM, sighandler);
npfds = snd_seq_poll_descriptors_count(seq, POLLIN); - pfds = alloca(sizeof(*pfds) * npfds); + pfds = alloca(sizeof(*pfds) * (npfds + 1)); for (;;) { snd_seq_poll_descriptors(seq, pfds, npfds, POLLIN); - if (poll(pfds, npfds, -1) < 0) - break; + if (interactive) { + pfds[npfds].fd = STDIN_FILENO; + pfds[npfds].events = POLLIN | POLLERR | POLLNVAL; + if (poll(pfds, npfds + 1, -1) < 0) + break; + if (pfds[npfds].revents & POLLIN) { + while (!feof(stdin) && getchar() != '\n') + ; + if (!start) { + start_bar(file); + start = 1; + printf("Press RETURN to stop recording:"); + fflush(stdout); + continue; + } else { + stop = 1; + } + } + } else { + if (poll(pfds, npfds, -1) < 0) + break; + } + do { snd_seq_ump_event_t *event;
err = snd_seq_ump_event_input(seq, &event); if (err < 0) break; - if (event) { + if (start && event) { record_event(file, event); events_received++; }
participants (1)
-
Takashi Iwai