Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-1-5]: src/usr.bin/audio/play Pull up revisions 1.18-1.21, 1.25-1.3...



details:   https://anonhg.NetBSD.org/src/rev/c4a6a7c768f5
branches:  netbsd-1-5
changeset: 492643:c4a6a7c768f5
user:      he <he%NetBSD.org@localhost>
date:      Tue Jan 29 23:13:26 2002 +0000

description:
Pull up revisions 1.18-1.21,1.25-1.31 (via patch, requested by mrg):
  Bring in several fixes and enhancements to these tools:
   o fix stdin audioplay
   o add WAV support to audiorecord
   o allows any host to output any endian sample
   o don't output a bogus header
   o fix a bug in audiorecord -t
   o many manual page fixes
   o exit immediately on signals

diffstat:

 usr.bin/audio/play/play.c |  296 +++++++++++++++++++++++++--------------------
 1 files changed, 162 insertions(+), 134 deletions(-)

diffs (truncated from 414 to 300 lines):

diff -r 7097dee55360 -r c4a6a7c768f5 usr.bin/audio/play/play.c
--- a/usr.bin/audio/play/play.c Tue Jan 29 23:13:01 2002 +0000
+++ b/usr.bin/audio/play/play.c Tue Jan 29 23:13:26 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: play.c,v 1.17.4.2 2001/03/14 23:22:12 he Exp $ */
+/*     $NetBSD: play.c,v 1.17.4.3 2002/01/29 23:13:26 he Exp $ */
 
 /*
  * Copyright (c) 1999 Matthew R. Green
@@ -36,6 +36,7 @@
 
 #include <err.h>
 #include <fcntl.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -45,10 +46,12 @@
 
 #include "libaudio.h"
 
-int main __P((int, char *[]));
-void usage __P((void));
-void play_fd __P((int, char *));
-ssize_t audioctl_write_fromhdr __P((void *, size_t, int, size_t *));
+int main (int, char *[]);
+void usage (void);
+void play (char *);
+void play_fd (const char *, int);
+ssize_t audioctl_write_fromhdr (void *, size_t, int, size_t *);
+void cleanup (int) __attribute__((__noreturn__));
 
 audio_info_t   info;
 int    volume;
@@ -65,7 +68,9 @@
 char   const *play_errstring = NULL;
 size_t bufsize;
 int    audiofd, ctlfd;
-size_t datasize;
+int    exitstatus = EXIT_SUCCESS;
+
+extern char *__progname;
 
 int
 main(argc, argv)
@@ -73,13 +78,11 @@
        char *argv[];
 {
        size_t  len;
-       off_t   filesize;
        int     ch;
-       int     exitstatus = EXIT_SUCCESS;
        int     iflag = 0;
        int     verbose = 0;
-       char    *device = 0;
-       char    *ctldev = 0;
+       const char *device = 0;
+       const char *ctldev = 0;
 
        while ((ch = getopt(argc, argv, "b:C:c:d:e:fhip:P:qs:Vv:")) != -1) {
                switch (ch) {
@@ -113,9 +116,10 @@
                        break;
                case 'P':
                        decode_int(optarg, &precision);
-                       if (precision != 8 && precision != 16 &&
-                           precision != 24 && precision != 32)
-                               errx(1, "precision must be between 8, 16, 24 or 32");
+                       if (precision != 4 && precision != 8 &&
+                           precision != 16 && precision != 24 &&
+                           precision != 32)
+                               errx(1, "precision must be between 4, 8, 16, 24 or 32");
                        break;
                case 'p':
                        len = strlen(optarg);
@@ -185,151 +189,175 @@
        if (bufsize < 32 * 1024)
                bufsize = 32 * 1024;
 
-       if (*argv) {
-               int fd;
-               struct stat sb;
-               void *addr, *oaddr;
-
-               do {
-                       ssize_t hdrlen;
-
-                       fd = open(*argv, O_RDONLY);
-                       if (fd < 0) {
-                               warn("could not open %s", *argv);
-                               exitstatus = EXIT_FAILURE;
-                               continue;
-                       }
+       signal(SIGINT, cleanup);
+       signal(SIGTERM, cleanup);
+       signal(SIGHUP, cleanup);
 
-                       if (fstat(fd, &sb) < 0)
-                               err(1, "could not fstat %s", *argv);
-                       filesize = sb.st_size;
-
-                       oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
-                           MAP_SHARED, fd, 0);
+       if (*argv)
+               do
+                       play(*argv++);
+               while (*argv);
+       else
+               play_fd("standard input", STDIN_FILENO);
 
-                       /*
-                        * if we failed to mmap the file, try to read it
-                        * instead, so that filesystems, etc, that do not
-                        * support mmap() work
-                        */
-                       if (addr == MAP_FAILED) {
-                               play_fd(fd, *argv);
-                               close(fd);
-                               continue;
-                       }
+       cleanup(0);
+}
+
+void
+cleanup(signo)
+       int signo;
+{
 
-                       /*
-                        * give the VM system a bit of a hint about the type
-                        * of accesses we will make.
-                        */
-                       if (madvise(addr, filesize, MADV_SEQUENTIAL) < 0 &&
-                           !qflag)
-                               warn("madvise failed, ignoring");
-
-                       /*
-                        * get the header length and set up the audio device
-                        */
-                       if ((hdrlen = audioctl_write_fromhdr(addr,
-                           (size_t)filesize, ctlfd, &datasize)) < 0) {
-                               if (play_errstring)
-                                       errx(1, "%s: %s", play_errstring, *argv);
-                               else
-                                       errx(1, "unknown audio file: %s", *argv);
-                       }
+       close(audiofd);
+       (void)ioctl(audiofd, AUDIO_FLUSH, NULL);
+       (void)ioctl(ctlfd, AUDIO_SETINFO, &info);
+       exit(exitstatus);
+}
 
-                       filesize -= hdrlen;
-                       addr = (char *)addr + hdrlen;
-                       if (filesize < datasize || datasize == 0) {
-                               warn("bogus datasize: %lu", (long)datasize);
-                               datasize = filesize;
-                       }
+void
+play(file)
+       char *file;
+{
+       struct stat sb;
+       void *addr, *oaddr;
+       off_t   filesize;
+       size_t datasize;
+       ssize_t hdrlen;
+       int fd;
 
-                       while (datasize > bufsize) {
-                               if (write(audiofd, addr, bufsize) != bufsize)
-                                       err(1, "write failed");
-                               addr = (char *)addr + bufsize;
-                               datasize -= bufsize;
-                       }
-                       if (write(audiofd, addr, (size_t)datasize) != (ssize_t)datasize)
-                               err(1, "final write failed");
+       if (file[0] == '-' && file[1] == 0) {
+               play_fd("standard input", STDIN_FILENO);
+               return;
+       }
 
-                       if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
-                               warn("audio drain ioctl failed");
-                       if (munmap(oaddr, (size_t)filesize) < 0 && !qflag)
-                               err(1, "munmap failed");
-
-                       close(fd);
-                       
-               } while (*++argv);
-       } else {
-               play_fd(STDIN_FILENO, "standard input");
+       fd = open(file, O_RDONLY);
+       if (fd < 0) {
+               if (!qflag)
+                       warn("could not open %s", file);
+               exitstatus = EXIT_FAILURE;
+               return;
        }
 
-       exit(exitstatus);
+       if (fstat(fd, &sb) < 0)
+               err(1, "could not fstat %s", file);
+       filesize = sb.st_size;
+
+       oaddr = addr = mmap(0, (size_t)filesize, PROT_READ,
+           MAP_SHARED, fd, 0);
+
+       /*
+        * if we failed to mmap the file, try to read it
+        * instead, so that filesystems, etc, that do not
+        * support mmap() work
+        */
+       if (addr == MAP_FAILED) {
+               play_fd(file, fd);
+               close(fd);
+               return;
+       }
+
+       /*
+        * give the VM system a bit of a hint about the type
+        * of accesses we will make.
+        */
+       if (madvise(addr, filesize, MADV_SEQUENTIAL) < 0 &&
+           !qflag)
+               warn("madvise failed, ignoring");
+
+       /*
+        * get the header length and set up the audio device
+        */
+       if ((hdrlen = audioctl_write_fromhdr(addr,
+           (size_t)filesize, ctlfd, &datasize)) < 0) {
+               if (play_errstring)
+                       errx(1, "%s: %s", play_errstring, file);
+               else
+                       errx(1, "unknown audio file: %s", file);
+       }
+
+       filesize -= hdrlen;
+       addr = (char *)addr + hdrlen;
+       if (filesize < datasize || datasize == 0) {
+               warnx("bogus datasize: %lu", (long)datasize);
+               datasize = filesize;
+       }
+
+       while (datasize > bufsize) {
+               if (write(audiofd, addr, bufsize) != bufsize)
+                       err(1, "write failed");
+               addr = (char *)addr + bufsize;
+               datasize -= bufsize;
+       }
+       if (write(audiofd, addr, (size_t)datasize) != (ssize_t)datasize)
+               err(1, "final write failed");
+
+       if (ioctl(audiofd, AUDIO_DRAIN) < 0 && !qflag)
+               warn("audio drain ioctl failed");
+       if (munmap(oaddr, (size_t)filesize) < 0)
+               err(1, "munmap failed");
+
+       close(fd);
 }
 
 /*
  * play the file on on the file descriptor fd
  */
 void
-play_fd(fd, file)
+play_fd(file, fd)
+       const char *file;
        int     fd;
-       char    *file;
 {
        char    *buffer = malloc(bufsize);
        ssize_t hdrlen;
-       int     n;
-       size_t  datasize;
-       size_t  datainbuf;
+       int     nr, nw;
+       size_t  datasize = 0;
+       size_t  dataout = 0;
 
        if (buffer == NULL)
                err(1, "malloc of read buffer failed");
 
-       n = read(fd, 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, &datasize);
+       nr = read(fd, buffer, bufsize);
+       if (nr < 0)
+               goto read_error;
+       if (nr == 0) {
+               if (fflag)
+                       return;
+               err(1, "unexpected EOF");
+       }



Home | Main Index | Thread Index | Old Index