[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.12

1.12    ! ratchov     1: /*     $OpenBSD: safile.c,v 1.11 2009/02/04 20:35:14 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.10      ratchov    37: #ifdef DEBUG
1.1       ratchov    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;
                    100:        hdl = sio_open(path, mode, 1);
1.12    ! ratchov   101:        if (hdl == NULL)
1.1       ratchov   102:                return NULL;
1.2       ratchov   103:        sio_initpar(&par);
1.1       ratchov   104:        if (ipar) {
                    105:                par.bits = ipar->bits;
                    106:                par.bps = ipar->bps;
                    107:                par.sig = ipar->sig;
                    108:                par.le = ipar->le;
                    109:                par.msb = ipar->msb;
                    110:                par.rate = ipar->rate;
                    111:                par.rchan = ipar->cmax - ipar->cmin + 1;
                    112:        } else {
                    113:                par.bits = opar->bits;
                    114:                par.bps = opar->bps;
                    115:                par.sig = opar->sig;
                    116:                par.le = opar->le;
                    117:                par.msb = opar->msb;
                    118:                par.rate = opar->rate;
                    119:        }
                    120:        if (opar)
                    121:                par.pchan = opar->cmax - opar->cmin + 1;
1.6       ratchov   122:        par.appbufsz = *bufsz;
1.3       ratchov   123:        par.round = *round;
1.5       ratchov   124:        if (!sio_setpar(hdl, &par))
1.11      ratchov   125:                goto bad_close;
1.5       ratchov   126:        if (!sio_getpar(hdl, &par))
1.11      ratchov   127:                goto bad_close;
1.1       ratchov   128:        if (ipar) {
                    129:                ipar->bits = par.bits;
                    130:                ipar->bps = par.bps;
                    131:                ipar->sig = par.sig;
                    132:                ipar->le = par.le;
                    133:                ipar->msb = par.msb;
                    134:                ipar->rate = par.rate;
1.7       ratchov   135:                ipar->cmax = ipar->cmin + par.rchan - 1;
1.1       ratchov   136:        }
                    137:        if (opar) {
                    138:                opar->bits = par.bits;
                    139:                opar->bps = par.bps;
                    140:                opar->sig = par.sig;
                    141:                opar->le = par.le;
                    142:                opar->msb = par.msb;
                    143:                opar->rate = par.rate;
1.7       ratchov   144:                opar->cmax = opar->cmin + par.pchan - 1;
1.1       ratchov   145:        }
                    146:        *bufsz = par.bufsz;
                    147:        *round = par.round;
1.3       ratchov   148:        DPRINTF("safile_new: using %u(%u) fpb\n", *bufsz, *round);
1.2       ratchov   149:        f = (struct safile *)file_new(ops, "hdl", sio_nfds(hdl));
1.11      ratchov   150:        if (f == NULL)
                    151:                goto bad_close;
1.1       ratchov   152:        f->hdl = hdl;
1.2       ratchov   153:        sio_onmove(f->hdl, safile_cb, f);
1.1       ratchov   154:        return f;
1.11      ratchov   155:  bad_close:
                    156:        sio_close(hdl);
                    157:        return NULL;
1.1       ratchov   158: }
                    159:
                    160: void
                    161: safile_start(struct file *file)
1.10      ratchov   162: {
1.1       ratchov   163:        struct safile *f = (struct safile *)file;
                    164:
1.2       ratchov   165:        if (!sio_start(f->hdl)) {
1.8       ratchov   166:                DPRINTF("safile_start: sio_start() failed\n");
1.9       ratchov   167:                file_close(file);
1.8       ratchov   168:                return;
1.1       ratchov   169:        }
                    170:        DPRINTF("safile_start: play/rec started\n");
                    171: }
                    172:
                    173: void
                    174: safile_stop(struct file *file)
                    175: {
                    176:        struct safile *f = (struct safile *)file;
                    177:
1.2       ratchov   178:        if (!sio_stop(f->hdl)) {
1.8       ratchov   179:                DPRINTF("safile_stop: sio_stop() filed\n");
1.9       ratchov   180:                file_close(file);
1.8       ratchov   181:                return;
1.1       ratchov   182:        }
                    183:        DPRINTF("safile_stop: play/rec stopped\n");
                    184: }
                    185:
                    186: unsigned
                    187: safile_read(struct file *file, unsigned char *data, unsigned count)
                    188: {
                    189:        struct safile *f = (struct safile *)file;
                    190:        unsigned n;
                    191: #ifdef DEBUG
                    192:        struct timeval tv0, tv1, dtv;
                    193:        unsigned us;
                    194:
                    195:        if (!(f->file.state & FILE_ROK)) {
                    196:                DPRINTF("file_read: %s: bad state\n", f->file.name);
                    197:                abort();
                    198:        }
                    199:        gettimeofday(&tv0, NULL);
                    200: #endif
1.2       ratchov   201:        n = sio_read(f->hdl, data, count);
1.1       ratchov   202:        if (n == 0) {
                    203:                f->file.state &= ~FILE_ROK;
1.2       ratchov   204:                if (sio_eof(f->hdl)) {
1.1       ratchov   205:                        fprintf(stderr, "safile_read: eof\n");
                    206:                        file_eof(&f->file);
                    207:                } else {
                    208:                        DPRINTFN(3, "safile_read: %s: blocking...\n",
                    209:                            f->file.name);
                    210:                }
                    211:                return 0;
                    212:        }
                    213: #ifdef DEBUG
                    214:        gettimeofday(&tv1, NULL);
                    215:        timersub(&tv1, &tv0, &dtv);
1.10      ratchov   216:        us = dtv.tv_sec * 1000000 + dtv.tv_usec;
1.1       ratchov   217:        DPRINTFN(us < 5000 ? 4 : 1,
1.10      ratchov   218:            "safile_read: %s: got %d bytes in %uus\n",
1.1       ratchov   219:            f->file.name, n, us);
                    220: #endif
                    221:        return n;
                    222:
                    223: }
                    224:
                    225: unsigned
                    226: safile_write(struct file *file, unsigned char *data, unsigned count)
                    227: {
                    228:        struct safile *f = (struct safile *)file;
                    229:        unsigned n;
                    230: #ifdef DEBUG
                    231:        struct timeval tv0, tv1, dtv;
                    232:        unsigned us;
1.10      ratchov   233:
1.1       ratchov   234:        if (!(f->file.state & FILE_WOK)) {
                    235:                DPRINTF("safile_write: %s: bad state\n", f->file.name);
                    236:                abort();
                    237:        }
                    238:        gettimeofday(&tv0, NULL);
                    239: #endif
1.2       ratchov   240:        n = sio_write(f->hdl, data, count);
1.1       ratchov   241:        if (n == 0) {
                    242:                f->file.state &= ~FILE_WOK;
1.2       ratchov   243:                if (sio_eof(f->hdl)) {
1.1       ratchov   244:                        fprintf(stderr, "safile_write: %s: hup\n", f->file.name);
                    245:                        file_hup(&f->file);
                    246:                } else {
                    247:                        DPRINTFN(3, "safile_write: %s: blocking...\n",
                    248:                            f->file.name);
                    249:                }
                    250:                return 0;
                    251:        }
                    252: #ifdef DEBUG
                    253:        gettimeofday(&tv1, NULL);
                    254:        timersub(&tv1, &tv0, &dtv);
1.10      ratchov   255:        us = dtv.tv_sec * 1000000 + dtv.tv_usec;
1.1       ratchov   256:        DPRINTFN(us < 5000 ? 4 : 1,
                    257:            "safile_write: %s: wrote %d bytes in %uus\n",
                    258:            f->file.name, n, us);
                    259: #endif
                    260:        return n;
                    261: }
                    262:
                    263: int
                    264: safile_nfds(struct file *file)
                    265: {
1.2       ratchov   266:        return sio_nfds(((struct safile *)file)->hdl);
1.1       ratchov   267: }
                    268:
                    269: int
                    270: safile_pollfd(struct file *file, struct pollfd *pfd, int events)
                    271: {
1.2       ratchov   272:        return sio_pollfd(((struct safile *)file)->hdl, pfd, events);
1.1       ratchov   273: }
                    274:
                    275: int
                    276: safile_revents(struct file *file, struct pollfd *pfd)
                    277: {
1.2       ratchov   278:        return sio_revents(((struct safile *)file)->hdl, pfd);
1.1       ratchov   279: }
                    280:
                    281: void
                    282: safile_close(struct file *file)
                    283: {
1.2       ratchov   284:        return sio_close(((struct safile *)file)->hdl);
1.1       ratchov   285: }