arecordmidi2: Add stdout output and --silent option

When the output file is '-', it's recorded to stdout.

For avoiding the corruption, this mode also suppresses the messages to
stdout, too, which can be enabled also via -s / --silent option.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
This commit is contained in:
Takashi Iwai 2024-07-09 07:40:24 +02:00
parent b1269eefdd
commit f0df4b4cfe
2 changed files with 37 additions and 12 deletions

View file

@ -14,6 +14,9 @@ more ALSA sequencer ports.
To stop recording, press Ctrl+C. To stop recording, press Ctrl+C.
When \fB\-\fP is passed to the MIDI Clip file argument,
it's recorded to stdout. It implies \fI\-s\fP option, too.
.SH OPTIONS .SH OPTIONS
.TP .TP
@ -80,6 +83,10 @@ the recording ends when another RETURN key is input from the
terminal. The received events before the start of recording are terminal. The received events before the start of recording are
discarded. discarded.
.TP
.I \-s,\-\-silent
Don't print messages to stdout.
.SH SEE ALSO .SH SEE ALSO
arecordmidi(1) arecordmidi(1)
.br .br

View file

@ -26,6 +26,7 @@ static volatile sig_atomic_t stop;
static int ts_num = 4; /* time signature: numerator */ static int ts_num = 4; /* time signature: numerator */
static int ts_div = 4; /* time signature: denominator */ static int ts_div = 4; /* time signature: denominator */
static int last_tick; static int last_tick;
static int silent;
/* Parse a decimal number from a command line argument. */ /* Parse a decimal number from a command line argument. */
static long arg_parse_decimal_num(const char *str, int *err) static long arg_parse_decimal_num(const char *str, int *err)
@ -377,7 +378,8 @@ static void help(const char *argv0)
" -i,--timesig=nn:dd time signature\n" " -i,--timesig=nn:dd time signature\n"
" -n,--num-events=events fixed number of events to record, then exit\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", " -r,--interactive Interactive mode\n"
" -s,--silent don't print messages\n",
argv0); argv0);
} }
@ -393,7 +395,7 @@ static void sighandler(int sig ATTRIBUTE_UNUSED)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
static const char short_options[] = "hVp:b:t:n:u:r"; static const char short_options[] = "hVp:b:t:n:u:rs";
static const struct option long_options[] = { static const struct option long_options[] = {
{"help", 0, NULL, 'h'}, {"help", 0, NULL, 'h'},
{"version", 0, NULL, 'V'}, {"version", 0, NULL, 'V'},
@ -404,6 +406,7 @@ int main(int argc, char *argv[])
{"num-events", 1, NULL, 'n'}, {"num-events", 1, NULL, 'n'},
{"ump", 1, NULL, 'u'}, {"ump", 1, NULL, 'u'},
{"interactive", 0, NULL, 'r'}, {"interactive", 0, NULL, 'r'},
{"silent", 0, NULL, 's'},
{0} {0}
}; };
@ -463,6 +466,9 @@ int main(int argc, char *argv[])
case 'r': case 'r':
interactive = 1; interactive = 1;
break; break;
case 's':
silent = 1;
break;
default: default:
help(argv[0]); help(argv[0]);
return 1; return 1;
@ -481,14 +487,21 @@ int main(int argc, char *argv[])
filename = argv[optind]; filename = argv[optind];
if (!strcmp(filename, "-")) {
file = stdout;
silent = 1; // imply silent mode
} else {
file = fopen(filename, "wb"); file = fopen(filename, "wb");
if (!file) if (!file)
fatal("Cannot open %s - %s", filename, strerror(errno)); fatal("Cannot open %s - %s", filename, strerror(errno));
}
write_file_header(file); write_file_header(file);
if (interactive) { if (interactive) {
if (!silent) {
printf("Press RETURN to start recording:"); printf("Press RETURN to start recording:");
fflush(stdout); fflush(stdout);
}
} else { } else {
start_bar(file); start_bar(file);
start = 1; start = 1;
@ -515,8 +528,10 @@ int main(int argc, char *argv[])
if (!start) { if (!start) {
start_bar(file); start_bar(file);
start = 1; start = 1;
if (!silent) {
printf("Press RETURN to stop recording:"); printf("Press RETURN to stop recording:");
fflush(stdout); fflush(stdout);
}
continue; continue;
} else { } else {
stop = 1; stop = 1;
@ -544,10 +559,13 @@ int main(int argc, char *argv[])
break; break;
} }
if (num_events && events_received < num_events) if (num_events && events_received < num_events) {
if (!silent)
fputs("Warning: Received signal before num_events\n", stdout); fputs("Warning: Received signal before num_events\n", stdout);
}
write_end_clip(file); write_end_clip(file);
if (file != stdout)
fclose(file); fclose(file);
snd_seq_close(seq); snd_seq_close(seq);
return 0; return 0;