add support to play stdin.

This commit is contained in:
mrg 1999-03-27 05:14:37 +00:00
parent b4bf620b49
commit 740f05c805
2 changed files with 42 additions and 4 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: audio.c,v 1.2 1999/03/26 15:46:22 mrg Exp $ */
/* $NetBSD: audio.c,v 1.3 1999/03/27 05:14:37 mrg Exp $ */
/*
* Copyright (c) 1999 Matthew R. Green
@ -271,10 +271,13 @@ printf("fmt header is:\n\t%d\ttag\n\t%d\tchannels\n\t%d\tsample rate\n\t%d\tavg_
do {
part = (wav_audioheaderpart *)where;
#if 0
printf("part `%c%c%c%c' len = %d\n", part->name[0], part->name[1], part->name[2], part->name[3], getle32(part->len));
#endif
where += (getle32(part->len) + 8);
} while ((char *)where < end && strncmp(part->name, "data", 4));
if (where <= end) {
if ((where - getle32(part->len)) <= end) {
*channels = getle16(fmt->channels);
*sample = getle32(fmt->sample_rate);
*enc = newenc;

View File

@ -1,4 +1,4 @@
/* $NetBSD: play.c,v 1.3 1999/03/26 15:46:22 mrg Exp $ */
/* $NetBSD: play.c,v 1.4 1999/03/27 05:14:38 mrg Exp $ */
/*
* Copyright (c) 1999 Matthew R. Green
@ -166,6 +166,7 @@ main(argc, argv)
if ((hdrlen = audioctl_write_fromhdr(addr,
(size_t)filesize, ctlfd, 1)) < 0) {
play_error:
if (play_errstring)
errx(1, "%s: %s", play_errstring, *argv);
else
@ -191,7 +192,41 @@ main(argc, argv)
} while (*++argv);
} else {
/* ... handle stdin */
char *buffer = malloc(bufsize);
int n, m;
if (buffer == NULL)
err(1, "malloc of read buffer failed");
n = read(STDIN_FILENO, buffer, bufsize);
if (n < 0)
err(1, "read of standard input failed");
if (n == 0)
errx(1, "EOF on standard input");
hdrlen = audioctl_write_fromhdr(buffer, n, ctlfd, 1);
if (hdrlen < 0)
goto play_error;
/* advance the buffer if we have to */
if (hdrlen > 0) {
/* shouldn't happen */
if (hdrlen > n)
err(1, "bogus hdrlen %d > length %d?", (int)hdrlen, n);
memmove(buffer, buffer + hdrlen, n - hdrlen);
m = read(STDIN_FILENO, buffer + n, hdrlen);
n += m;
}
/* read until EOF or error */
do {
if (n == -1)
err(1, "read of standard input failed");
if (write(audiofd, buffer, n) != n)
err(1, "write failed");
} while ((n = read(STDIN_FILENO, buffer, bufsize)));
}
exit(0);