Annotation of src/usr.bin/aucat/aucat.c, Revision 1.4
1.4 ! millert 1: /* $OpenBSD: aucat.c,v 1.3 1997/01/05 19:00:51 kstailey Exp $ */
1.1 kstailey 2: /*
3: * Copyright (c) 1997 Kenneth Stailey. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
15: * This product includes software developed by Kenneth Stailey.
16: * 4. The name of the author may not be used to endorse or promote products
17: * derived from this software without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29: */
30:
31: #include <sys/types.h>
32: #include <machine/endian.h>
33: #include <fcntl.h>
34: #include <stdio.h>
1.4 ! millert 35: #include <stdlib.h>
1.1 kstailey 36: #include <unistd.h>
1.4 ! millert 37: #include <err.h>
1.1 kstailey 38:
39: /*
40: * aucat: concatinate and play Sun 8-bit .au files
41: */
42:
43: /* function playfile: given a file which is positioned at the beginning
44: * of what is assumed to be an .au data stream copy it out to the audio
45: * device. Return 0 on sucess, -1 on failure.
46: */
47: int
1.4 ! millert 48: playfile(fd, dev)
! 49: int fd;
! 50: char *dev;
1.1 kstailey 51: {
1.4 ! millert 52: static int afd = -1;
! 53: int rd;
! 54: char buf[5120];
! 55:
! 56: if (afd == -1 && (afd = open(dev, O_WRONLY)) < 0) {
! 57: warn("can't open %s", dev);
! 58: return(-1);
! 59: }
! 60: while ((rd = read(fd, buf, sizeof(buf))) > 0) {
! 61: write(afd, buf, rd);
! 62: if (rd < sizeof(buf))
! 63: break;
! 64: }
! 65:
! 66: return (0);
1.1 kstailey 67: }
68:
69: int
1.4 ! millert 70: main(argc, argv)
! 71: int argc;
! 72: char **argv;
1.1 kstailey 73: {
1.4 ! millert 74: int fd, ch;
! 75: unsigned long data;
! 76: char magic[5];
! 77: char *dev;
! 78:
! 79: dev = getenv("AUDIODEVICE");
! 80: if (dev == 0)
! 81: dev = "/dev/audio";
! 82:
! 83: while ((ch = getopt(argc, argv, "f:")) != -1) {
! 84: switch(ch) {
! 85: case 'f':
! 86: dev = optarg;
! 87: break;
! 88: }
! 89: }
! 90: argc -= optind;
! 91: argv += optind;
! 92:
! 93: while (argc) {
! 94: if ((fd = open(*argv, O_RDONLY)) < 0)
! 95: err(1, "cannot open %s", *argv);
! 96:
! 97: read(fd, magic, 4);
! 98: magic[4] = '\0';
! 99: if (strcmp(magic, ".snd")) {
! 100: /*
! 101: * not an .au file, bad header.
! 102: * Assume raw audio data since that's
! 103: * what /dev/audio generates by default.
! 104: */
! 105: lseek(fd, 0, SEEK_SET);
! 106: } else {
! 107: read(fd, &data, sizeof(data));
! 108: data = ntohl(data);
! 109: lseek(fd, (off_t)data, SEEK_SET);
! 110: }
! 111: if (playfile(fd, dev) < 0)
! 112: exit(1);
! 113: argc--;
! 114: argv++;
! 115: }
! 116: exit(0);
1.1 kstailey 117: }