Skip to content

Commit

Permalink
arecordmidi2: Add stdout output and --silent option
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tiwai committed Jul 9, 2024
1 parent b1269ee commit f0df4b4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
7 changes: 7 additions & 0 deletions seq/aplaymidi2/arecordmidi2.1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ more ALSA sequencer ports.

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

.TP
Expand Down Expand Up @@ -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
discarded.

.TP
.I \-s,\-\-silent
Don't print messages to stdout.

.SH SEE ALSO
arecordmidi(1)
.br
Expand Down
42 changes: 30 additions & 12 deletions seq/aplaymidi2/arecordmidi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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;
static int silent;

/* Parse a decimal number from a command line argument. */
static long arg_parse_decimal_num(const char *str, int *err)
Expand Down Expand Up @@ -377,7 +378,8 @@ static void help(const char *argv0)
" -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"
" -r,--interactive Interactive mode\n",
" -r,--interactive Interactive mode\n"
" -s,--silent don't print messages\n",
argv0);
}

Expand All @@ -393,7 +395,7 @@ static void sighandler(int sig ATTRIBUTE_UNUSED)

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[] = {
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 'V'},
Expand All @@ -404,6 +406,7 @@ int main(int argc, char *argv[])
{"num-events", 1, NULL, 'n'},
{"ump", 1, NULL, 'u'},
{"interactive", 0, NULL, 'r'},
{"silent", 0, NULL, 's'},
{0}
};

Expand Down Expand Up @@ -463,6 +466,9 @@ int main(int argc, char *argv[])
case 'r':
interactive = 1;
break;
case 's':
silent = 1;
break;
default:
help(argv[0]);
return 1;
Expand All @@ -481,14 +487,21 @@ int main(int argc, char *argv[])

filename = argv[optind];

file = fopen(filename, "wb");
if (!file)
fatal("Cannot open %s - %s", filename, strerror(errno));
if (!strcmp(filename, "-")) {
file = stdout;
silent = 1; // imply silent mode
} else {
file = fopen(filename, "wb");
if (!file)
fatal("Cannot open %s - %s", filename, strerror(errno));
}

write_file_header(file);
if (interactive) {
printf("Press RETURN to start recording:");
fflush(stdout);
if (!silent) {
printf("Press RETURN to start recording:");
fflush(stdout);
}
} else {
start_bar(file);
start = 1;
Expand All @@ -515,8 +528,10 @@ int main(int argc, char *argv[])
if (!start) {
start_bar(file);
start = 1;
printf("Press RETURN to stop recording:");
fflush(stdout);
if (!silent) {
printf("Press RETURN to stop recording:");
fflush(stdout);
}
continue;
} else {
stop = 1;
Expand Down Expand Up @@ -544,11 +559,14 @@ int main(int argc, char *argv[])
break;
}

if (num_events && events_received < num_events)
fputs("Warning: Received signal before num_events\n", stdout);
if (num_events && events_received < num_events) {
if (!silent)
fputs("Warning: Received signal before num_events\n", stdout);
}

write_end_clip(file);
fclose(file);
if (file != stdout)
fclose(file);
snd_seq_close(seq);
return 0;
}

0 comments on commit f0df4b4

Please sign in to comment.