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

Annotation of src/usr.bin/aucat/safile.c, Revision 1.5

1.5     ! ratchov     1: /*     $OpenBSD: safile.c,v 1.4 2008/11/08 10:40:52 ratchov Exp $      */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17:
                     18: #include <sys/types.h>
                     19: #include <sys/time.h>
                     20:
                     21: #include <poll.h>
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
1.2       ratchov    25: #include <sndio.h>
1.1       ratchov    26:
                     27: #include "conf.h"
                     28: #include "file.h"
                     29: #include "aproc.h"
                     30: #include "aparams.h"
                     31: #include "safile.h"
                     32: #include "dev.h"
                     33:
                     34: struct safile {
                     35:        struct file file;
1.2       ratchov    36:        struct sio_hdl *hdl;
1.1       ratchov    37: #ifdef DEBUG
                     38:        struct timeval itv, otv;
                     39: #endif
                     40: };
                     41:
                     42: void safile_close(struct file *);
                     43: unsigned safile_read(struct file *, unsigned char *, unsigned);
                     44: unsigned safile_write(struct file *, unsigned char *, unsigned);
                     45: void safile_start(struct file *);
                     46: void safile_stop(struct file *);
                     47: int safile_nfds(struct file *);
                     48: int safile_pollfd(struct file *, struct pollfd *, int);
                     49: int safile_revents(struct file *, struct pollfd *);
                     50:
                     51: struct fileops safile_ops = {
1.3       ratchov    52:        "sndio",
1.1       ratchov    53:        sizeof(struct safile),
                     54:        safile_close,
                     55:        safile_read,
                     56:        safile_write,
                     57:        safile_start,
                     58:        safile_stop,
                     59:        safile_nfds,
                     60:        safile_pollfd,
                     61:        safile_revents
                     62: };
                     63:
                     64: void
                     65: safile_cb(void *addr, int delta)
                     66: {
                     67:        struct safile *f = (struct safile *)addr;
                     68:        struct aproc *p;
                     69:
                     70:        if (delta != 0) {
                     71:                p = f->file.wproc;
                     72:                if (p && p->ops->opos)
                     73:                        p->ops->opos(p, NULL, delta);
                     74:        }
                     75:        if (delta != 0) {
                     76:                p = f->file.rproc;
                     77:                if (p && p->ops->ipos)
                     78:                        p->ops->ipos(p, NULL, delta);
                     79:        }
                     80: }
                     81:
                     82: /*
                     83:  * open the device
                     84:  */
                     85: struct safile *
                     86: safile_new(struct fileops *ops, char *path,
                     87:     struct aparams *ipar, struct aparams *opar,
1.5     ! ratchov    88:     unsigned *bufsz, unsigned *round)
1.1       ratchov    89: {
1.2       ratchov    90:        struct sio_par par;
                     91:        struct sio_hdl *hdl;
1.1       ratchov    92:        struct safile *f;
                     93:        int mode;
                     94:
                     95:        mode = 0;
                     96:        if (ipar)
1.2       ratchov    97:                mode |= SIO_REC;
1.1       ratchov    98:        if (opar)
1.2       ratchov    99:                mode |= SIO_PLAY;
1.4       ratchov   100:        if (!mode) {
                    101:                fprintf(stderr, "select at least play or record mode\n");
                    102:                return NULL;
                    103:        }
1.2       ratchov   104:        hdl = sio_open(path, mode, 1);
1.1       ratchov   105:        if (hdl == NULL) {
                    106:                fprintf(stderr, "safile_new: can't open device\n");
                    107:                return NULL;
                    108:        }
1.2       ratchov   109:        sio_initpar(&par);
1.1       ratchov   110:        if (ipar) {
                    111:                par.bits = ipar->bits;
                    112:                par.bps = ipar->bps;
                    113:                par.sig = ipar->sig;
                    114:                par.le = ipar->le;
                    115:                par.msb = ipar->msb;
                    116:                par.rate = ipar->rate;
                    117:                par.rchan = ipar->cmax - ipar->cmin + 1;
                    118:        } else {
                    119:                par.bits = opar->bits;
                    120:                par.bps = opar->bps;
                    121:                par.sig = opar->sig;
                    122:                par.le = opar->le;
                    123:                par.msb = opar->msb;
                    124:                par.rate = opar->rate;
                    125:        }
                    126:        if (opar)
                    127:                par.pchan = opar->cmax - opar->cmin + 1;
1.3       ratchov   128:        par.bufsz = *bufsz;
                    129:        par.round = *round;
1.5     ! ratchov   130:        if (!sio_setpar(hdl, &par))
        !           131:                return 0;
        !           132:        if (!sio_getpar(hdl, &par))
        !           133:                return 0;
1.1       ratchov   134:        if (ipar) {
                    135:                ipar->bits = par.bits;
                    136:                ipar->bps = par.bps;
                    137:                ipar->sig = par.sig;
                    138:                ipar->le = par.le;
                    139:                ipar->msb = par.msb;
                    140:                ipar->rate = par.rate;
                    141:                ipar->cmax = par.rchan - 1;
                    142:                ipar->cmin = 0;
                    143:        }
                    144:        if (opar) {
                    145:                opar->bits = par.bits;
                    146:                opar->bps = par.bps;
                    147:                opar->sig = par.sig;
                    148:                opar->le = par.le;
                    149:                opar->msb = par.msb;
                    150:                opar->rate = par.rate;
                    151:                opar->cmax = par.pchan - 1;
                    152:                opar->cmin = 0;
                    153:        }
                    154:        *bufsz = par.bufsz;
                    155:        *round = par.round;
1.3       ratchov   156:        DPRINTF("safile_new: using %u(%u) fpb\n", *bufsz, *round);
1.2       ratchov   157:        f = (struct safile *)file_new(ops, "hdl", sio_nfds(hdl));
1.1       ratchov   158:        f->hdl = hdl;
1.2       ratchov   159:        sio_onmove(f->hdl, safile_cb, f);
1.1       ratchov   160:        return f;
                    161: }
                    162:
                    163: void
                    164: safile_start(struct file *file)
                    165: {
                    166:        struct safile *f = (struct safile *)file;
                    167:
1.2       ratchov   168:        if (!sio_start(f->hdl)) {
                    169:                fprintf(stderr, "safile_start: sio_start() failed\n");
1.1       ratchov   170:                exit(1);
                    171:        }
                    172:        DPRINTF("safile_start: play/rec started\n");
                    173: }
                    174:
                    175: void
                    176: safile_stop(struct file *file)
                    177: {
                    178:        struct safile *f = (struct safile *)file;
                    179:
1.2       ratchov   180:        if (!sio_stop(f->hdl)) {
                    181:                fprintf(stderr, "safile_stop: sio_start() filed\n");
1.1       ratchov   182:                exit(1);
                    183:        }
                    184:        DPRINTF("safile_stop: play/rec stopped\n");
                    185: }
                    186:
                    187: unsigned
                    188: safile_read(struct file *file, unsigned char *data, unsigned count)
                    189: {
                    190:        struct safile *f = (struct safile *)file;
                    191:        unsigned n;
                    192: #ifdef DEBUG
                    193:        struct timeval tv0, tv1, dtv;
                    194:        unsigned us;
                    195:
                    196:        if (!(f->file.state & FILE_ROK)) {
                    197:                DPRINTF("file_read: %s: bad state\n", f->file.name);
                    198:                abort();
                    199:        }
                    200:        gettimeofday(&tv0, NULL);
                    201: #endif
1.2       ratchov   202:        n = sio_read(f->hdl, data, count);
1.1       ratchov   203:        if (n == 0) {
                    204:                f->file.state &= ~FILE_ROK;
1.2       ratchov   205:                if (sio_eof(f->hdl)) {
1.1       ratchov   206:                        fprintf(stderr, "safile_read: eof\n");
                    207:                        file_eof(&f->file);
                    208:                } else {
                    209:                        DPRINTFN(3, "safile_read: %s: blocking...\n",
                    210:                            f->file.name);
                    211:                }
                    212:                return 0;
                    213:        }
                    214: #ifdef DEBUG
                    215:        gettimeofday(&tv1, NULL);
                    216:        timersub(&tv1, &tv0, &dtv);
                    217:        us = dtv.tv_sec * 1000000 + dtv.tv_usec;
                    218:        DPRINTFN(us < 5000 ? 4 : 1,
                    219:            "safile_read: %s: got %d bytes in %uus\n",
                    220:            f->file.name, n, us);
                    221: #endif
                    222:        return n;
                    223:
                    224: }
                    225:
                    226: unsigned
                    227: safile_write(struct file *file, unsigned char *data, unsigned count)
                    228: {
                    229:        struct safile *f = (struct safile *)file;
                    230:        unsigned n;
                    231: #ifdef DEBUG
                    232:        struct timeval tv0, tv1, dtv;
                    233:        unsigned us;
                    234:
                    235:        if (!(f->file.state & FILE_WOK)) {
                    236:                DPRINTF("safile_write: %s: bad state\n", f->file.name);
                    237:                abort();
                    238:        }
                    239:        gettimeofday(&tv0, NULL);
                    240: #endif
1.2       ratchov   241:        n = sio_write(f->hdl, data, count);
1.1       ratchov   242:        if (n == 0) {
                    243:                f->file.state &= ~FILE_WOK;
1.2       ratchov   244:                if (sio_eof(f->hdl)) {
1.1       ratchov   245:                        fprintf(stderr, "safile_write: %s: hup\n", f->file.name);
                    246:                        file_hup(&f->file);
                    247:                } else {
                    248:                        DPRINTFN(3, "safile_write: %s: blocking...\n",
                    249:                            f->file.name);
                    250:                }
                    251:                return 0;
                    252:        }
                    253: #ifdef DEBUG
                    254:        gettimeofday(&tv1, NULL);
                    255:        timersub(&tv1, &tv0, &dtv);
                    256:        us = dtv.tv_sec * 1000000 + dtv.tv_usec;
                    257:        DPRINTFN(us < 5000 ? 4 : 1,
                    258:            "safile_write: %s: wrote %d bytes in %uus\n",
                    259:            f->file.name, n, us);
                    260: #endif
                    261:        return n;
                    262: }
                    263:
                    264: int
                    265: safile_nfds(struct file *file)
                    266: {
1.2       ratchov   267:        return sio_nfds(((struct safile *)file)->hdl);
1.1       ratchov   268: }
                    269:
                    270: int
                    271: safile_pollfd(struct file *file, struct pollfd *pfd, int events)
                    272: {
1.2       ratchov   273:        return sio_pollfd(((struct safile *)file)->hdl, pfd, events);
1.1       ratchov   274: }
                    275:
                    276: int
                    277: safile_revents(struct file *file, struct pollfd *pfd)
                    278: {
1.2       ratchov   279:        return sio_revents(((struct safile *)file)->hdl, pfd);
1.1       ratchov   280: }
                    281:
                    282: void
                    283: safile_close(struct file *file)
                    284: {
1.2       ratchov   285:        return sio_close(((struct safile *)file)->hdl);
1.1       ratchov   286: }