[BACK]Return to legacy.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / aucat

Annotation of src/usr.bin/aucat/legacy.c, Revision 1.1

1.1     ! ratchov     1: /*     $OpenBSD$       */
        !             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 <fcntl.h>
        !            32: #include <unistd.h>
        !            33: #include <err.h>
        !            34:
        !            35: #include "file.h"
        !            36: #include "aparams.h"
        !            37: #include "dev.h"
        !            38:
        !            39:
        !            40: /* headerless data files.  played at /dev/audio's defaults.
        !            41:  */
        !            42: #define FMT_RAW        0
        !            43:
        !            44: /* Sun/NeXT .au files.  header is skipped and /dev/audio is configured
        !            45:  * for monaural 8-bit ulaw @ 8kHz, the de facto format for .au files,
        !            46:  * as well as the historical default configuration for /dev/audio.
        !            47:  */
        !            48: #define FMT_AU 1
        !            49:
        !            50: /* RIFF WAV files.  header is parsed for format details which are
        !            51:  * applied to /dev/audio.
        !            52:  */
        !            53: #define FMT_WAV        2
        !            54:
        !            55:
        !            56: int
        !            57: legacy_play(char *dev, char *aufile)
        !            58: {
        !            59:        struct audio_prinfo ai;
        !            60:        struct audio_info info;
        !            61:        struct aparams par;
        !            62:        ssize_t rd;
        !            63:        off_t datasz;
        !            64:        char buf[5120];
        !            65:        int afd, fd, fmt = FMT_RAW;
        !            66:        u_int32_t pos = 0;
        !            67:        char magic[4];
        !            68:
        !            69:        if ((fd = open(aufile, O_RDONLY)) < 0) {
        !            70:                warn("cannot open %s", aufile);
        !            71:                return(1);
        !            72:        }
        !            73:
        !            74:        if (read(fd, magic, sizeof(magic)) != sizeof(magic)) {
        !            75:                /* read() error, or the file is smaller than sizeof(magic).
        !            76:                 * treat as a raw file, like previous versions of aucat.
        !            77:                 */
        !            78:        } else if (!strncmp(magic, ".snd", 4)) {
        !            79:                fmt = FMT_AU;
        !            80:                if (read(fd, &pos, sizeof(pos)) == sizeof(pos))
        !            81:                        pos = ntohl(pos);
        !            82:        } else if (!strncmp(magic, "RIFF", 4) &&
        !            83:                    wav_readhdr(fd, &par, &datasz)) {
        !            84:                        fmt = FMT_WAV;
        !            85:        }
        !            86:
        !            87:        /* seek to start of audio data.  wav_readhdr already took care
        !            88:         * of this for FMT_WAV.
        !            89:         */
        !            90:        if (fmt == FMT_RAW || fmt == FMT_AU)
        !            91:                if (lseek(fd, (off_t)pos, SEEK_SET) == -1)
        !            92:                        warn("lseek");
        !            93:
        !            94:        if ((afd = open(dev, O_WRONLY)) < 0) {
        !            95:                warn("can't open %s", dev);
        !            96:                return(1);
        !            97:        }
        !            98:
        !            99:        AUDIO_INITINFO(&info);
        !           100:        ai = info.play;
        !           101:
        !           102:        switch(fmt) {
        !           103:        case FMT_WAV:
        !           104:                sun_partoinfo(&ai, &par);
        !           105:                break;
        !           106:        case FMT_AU:
        !           107:                ai.encoding = AUDIO_ENCODING_ULAW;
        !           108:                ai.precision = 8;
        !           109:                ai.sample_rate = 8000;
        !           110:                ai.channels = 1;
        !           111:                break;
        !           112:        case FMT_RAW:
        !           113:        default:
        !           114:                break;
        !           115:        }
        !           116:
        !           117:        info.play = ai;
        !           118:        if (ioctl(afd, AUDIO_SETINFO, &info) < 0) {
        !           119:                warn("%s", dev);
        !           120:                /* only WAV could fail in previous aucat versions (unless
        !           121:                 * the parameters returned by AUDIO_GETINFO would fail,
        !           122:                 * which is unlikely)
        !           123:                 */
        !           124:                if (fmt == FMT_WAV)
        !           125:                        return(1);
        !           126:        }
        !           127:
        !           128:        /* parameters may be silently modified.  see audio(9)'s
        !           129:         * description of set_params.  for compatability with previous
        !           130:         * aucat versions, continue running if something doesn't match.
        !           131:         */
        !           132:        (void) ioctl(afd, AUDIO_GETINFO, &info);
        !           133:        if (info.play.encoding != ai.encoding ||
        !           134:            info.play.precision != ai.precision ||
        !           135:            info.play.channels != ai.channels ||
        !           136:            /* devices may return a very close rate, such as 44099 when
        !           137:             * 44100 was requested.  the difference is inaudible.  allow
        !           138:             * 2% deviation as an example of how to cope.
        !           139:             */
        !           140:            (info.play.sample_rate > ai.sample_rate * 1.02 ||
        !           141:            info.play.sample_rate < ai.sample_rate * 0.98)) {
        !           142:                warnx("format not supported by %s", dev);
        !           143:        }
        !           144:
        !           145:        while ((rd = read(fd, buf, sizeof(buf))) > 0)
        !           146:                if (write(afd, buf, rd) != rd)
        !           147:                        warn("write");
        !           148:        if (rd == -1)
        !           149:                warn("read");
        !           150:
        !           151:        (void) close(afd);
        !           152:        (void) close(fd);
        !           153:
        !           154:        return(0);
        !           155: }