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

Annotation of src/usr.bin/aucat/aproc.c, Revision 1.15

1.15    ! ratchov     1: /*     $OpenBSD: aproc.c,v 1.14 2008/11/03 22:55:34 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:  * aproc structures are simple audio processing units. They are
                     19:  * interconnected by abuf structures and form a kind of circuit. aproc
                     20:  * structure have call-backs that do the actual processing.
                     21:  *
                     22:  * This module implements the following processing units:
                     23:  *
                     24:  *  - rpipe: read end of an unix file (pipe, socket, device...)
                     25:  *
                     26:  *  - wpipe: write end of an unix file (pipe, socket, device...)
                     27:  *
                     28:  *  - mix: mix N inputs -> 1 output
                     29:  *
                     30:  *  - sub: from 1 input -> extract/copy N outputs
                     31:  *
                     32:  *  - conv: converts/resamples/remaps a single stream
                     33:  *
1.15    ! ratchov    34:  *  - resamp: resample streams in native format
        !            35:  *
1.1       ratchov    36:  * TODO
                     37:  *
                     38:  *     (easy) split the "conv" into 2 converters: one for input (that
                     39:  *     convers anything to 16bit signed) and one for the output (that
                     40:  *     converts 16bit signed to anything)
                     41:  *
                     42:  *     (hard) add a lowpass filter for the resampler. Quality is
                     43:  *     not acceptable as is.
1.12      ratchov    44:  *
1.1       ratchov    45:  */
                     46: #include <err.h>
                     47: #include <limits.h>
                     48: #include <stdio.h>
                     49: #include <stdlib.h>
                     50: #include <string.h>
                     51:
                     52: #include "conf.h"
                     53: #include "aparams.h"
                     54: #include "abuf.h"
                     55: #include "aproc.h"
                     56: #include "file.h"
                     57:
                     58: struct aproc *
                     59: aproc_new(struct aproc_ops *ops, char *name)
                     60: {
                     61:        struct aproc *p;
                     62:
                     63:        p = malloc(sizeof(struct aproc));
                     64:        if (p == NULL)
                     65:                err(1, name);
                     66:        LIST_INIT(&p->ibuflist);
                     67:        LIST_INIT(&p->obuflist);
                     68:        p->name = name;
                     69:        p->ops = ops;
                     70:        return p;
                     71: }
                     72:
                     73: void
                     74: aproc_del(struct aproc *p)
                     75: {
1.12      ratchov    76:        struct abuf *i;
                     77:
                     78:        DPRINTF("aproc_del: %s(%s): terminating...\n", p->ops->name, p->name);
                     79:
1.8       ratchov    80:        if (p->ops->done)
                     81:                p->ops->done(p);
1.12      ratchov    82:
                     83:        while (!LIST_EMPTY(&p->ibuflist)) {
                     84:                i = LIST_FIRST(&p->ibuflist);
                     85:                abuf_hup(i);
                     86:        }
                     87:        while (!LIST_EMPTY(&p->obuflist)) {
                     88:                i = LIST_FIRST(&p->obuflist);
                     89:                abuf_eof(i);
                     90:        }
                     91:        DPRINTF("aproc_del: %s(%s): freed\n", p->ops->name, p->name);
1.1       ratchov    92:        free(p);
                     93: }
                     94:
                     95: void
                     96: aproc_setin(struct aproc *p, struct abuf *ibuf)
                     97: {
                     98:        LIST_INSERT_HEAD(&p->ibuflist, ibuf, ient);
                     99:        ibuf->rproc = p;
                    100:        if (p->ops->newin)
                    101:                p->ops->newin(p, ibuf);
                    102: }
                    103:
                    104: void
                    105: aproc_setout(struct aproc *p, struct abuf *obuf)
                    106: {
                    107:        LIST_INSERT_HEAD(&p->obuflist, obuf, oent);
                    108:        obuf->wproc = p;
                    109:        if (p->ops->newout)
                    110:                p->ops->newout(p, obuf);
                    111: }
                    112:
1.12      ratchov   113: void
                    114: aproc_ipos(struct aproc *p, struct abuf *ibuf, int delta)
                    115: {
                    116:        struct abuf *obuf;
                    117:
                    118:        DPRINTFN(3, "aproc_ipos: %s: delta = %d\n", p->name, delta);
                    119:
                    120:        LIST_FOREACH(obuf, &p->obuflist, oent) {
                    121:                abuf_ipos(obuf, delta);
                    122:        }
                    123: }
                    124:
                    125: void
                    126: aproc_opos(struct aproc *p, struct abuf *obuf, int delta)
                    127: {
                    128:        struct abuf *ibuf;
                    129:
                    130:        DPRINTFN(3, "aproc_opos: %s: delta = %d\n", p->name, delta);
                    131:
                    132:        LIST_FOREACH(ibuf, &p->ibuflist, ient) {
                    133:                abuf_opos(ibuf, delta);
                    134:        }
                    135: }
                    136:
1.1       ratchov   137: int
                    138: rpipe_in(struct aproc *p, struct abuf *ibuf_dummy)
                    139: {
                    140:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                    141:        struct file *f = p->u.io.file;
                    142:        unsigned char *data;
                    143:        unsigned count;
                    144:
1.6       ratchov   145:        DPRINTFN(3, "rpipe_in: %s\n", p->name);
                    146:
1.12      ratchov   147:        if (ABUF_FULL(obuf) || !(f->state & FILE_ROK))
1.1       ratchov   148:                return 0;
                    149:        data = abuf_wgetblk(obuf, &count, 0);
1.6       ratchov   150:        count = file_read(f, data, count);
1.12      ratchov   151:        if (count == 0)
                    152:                return 0;
1.6       ratchov   153:        abuf_wcommit(obuf, count);
1.12      ratchov   154:        if (!abuf_flush(obuf))
                    155:                return 0;
                    156:        return 1;
1.1       ratchov   157: }
                    158:
                    159: int
                    160: rpipe_out(struct aproc *p, struct abuf *obuf)
                    161: {
                    162:        struct file *f = p->u.io.file;
                    163:        unsigned char *data;
                    164:        unsigned count;
                    165:
1.12      ratchov   166:        if (f->refs > 0)
                    167:                return 0;
1.6       ratchov   168:        DPRINTFN(3, "rpipe_out: %s\n", p->name);
1.12      ratchov   169:
                    170:        if (ABUF_FULL(obuf) || !(f->state & FILE_ROK))
1.1       ratchov   171:                return 0;
                    172:        data = abuf_wgetblk(obuf, &count, 0);
1.6       ratchov   173:        count = file_read(f, data, count);
1.12      ratchov   174:        if (count == 0)
                    175:                return 0;
1.6       ratchov   176:        abuf_wcommit(obuf, count);
1.12      ratchov   177:        return 1;
1.1       ratchov   178: }
                    179:
                    180: void
1.8       ratchov   181: rpipe_done(struct aproc *p)
1.1       ratchov   182: {
                    183:        struct file *f = p->u.io.file;
                    184:
                    185:        f->rproc = NULL;
1.12      ratchov   186:        if (f->wproc == NULL)
                    187:                file_del(f);
1.1       ratchov   188: }
                    189:
                    190: void
                    191: rpipe_eof(struct aproc *p, struct abuf *ibuf_dummy)
                    192: {
                    193:        DPRINTFN(3, "rpipe_eof: %s\n", p->name);
1.8       ratchov   194:        aproc_del(p);
1.1       ratchov   195: }
                    196:
                    197: void
                    198: rpipe_hup(struct aproc *p, struct abuf *obuf)
                    199: {
                    200:        DPRINTFN(3, "rpipe_hup: %s\n", p->name);
1.8       ratchov   201:        aproc_del(p);
1.1       ratchov   202: }
                    203:
                    204: struct aproc_ops rpipe_ops = {
1.12      ratchov   205:        "rpipe",
                    206:        rpipe_in,
                    207:        rpipe_out,
                    208:        rpipe_eof,
                    209:        rpipe_hup,
                    210:        NULL, /* newin */
                    211:        NULL, /* newout */
                    212:        aproc_ipos,
                    213:        aproc_opos,
                    214:        rpipe_done
1.1       ratchov   215: };
                    216:
                    217: struct aproc *
                    218: rpipe_new(struct file *f)
                    219: {
                    220:        struct aproc *p;
                    221:
                    222:        p = aproc_new(&rpipe_ops, f->name);
                    223:        p->u.io.file = f;
1.12      ratchov   224:        f->rproc = p;
1.1       ratchov   225:        return p;
                    226: }
                    227:
                    228: void
1.8       ratchov   229: wpipe_done(struct aproc *p)
1.1       ratchov   230: {
                    231:        struct file *f = p->u.io.file;
                    232:
                    233:        f->wproc = NULL;
1.12      ratchov   234:        if (f->rproc == NULL)
                    235:                file_del(f);
1.1       ratchov   236: }
                    237:
                    238: int
                    239: wpipe_in(struct aproc *p, struct abuf *ibuf)
                    240: {
                    241:        struct file *f = p->u.io.file;
                    242:        unsigned char *data;
                    243:        unsigned count;
                    244:
1.12      ratchov   245:        if (f->refs > 0)
                    246:                return 0;
1.6       ratchov   247:        DPRINTFN(3, "wpipe_in: %s\n", p->name);
                    248:
1.12      ratchov   249:        if (ABUF_EMPTY(ibuf) || !(f->state & FILE_WOK))
1.1       ratchov   250:                return 0;
                    251:        data = abuf_rgetblk(ibuf, &count, 0);
                    252:        count = file_write(f, data, count);
1.12      ratchov   253:        if (count == 0)
                    254:                return 0;
1.6       ratchov   255:        abuf_rdiscard(ibuf, count);
1.12      ratchov   256:        return 1;
1.1       ratchov   257: }
                    258:
                    259: int
                    260: wpipe_out(struct aproc *p, struct abuf *obuf_dummy)
                    261: {
                    262:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                    263:        struct file *f = p->u.io.file;
                    264:        unsigned char *data;
                    265:        unsigned count;
                    266:
1.6       ratchov   267:        DPRINTFN(3, "wpipe_out: %s\n", p->name);
                    268:
1.12      ratchov   269:        if (!abuf_fill(ibuf)) {
                    270:                DPRINTFN(3, "wpipe_out: fill failed\n");
                    271:                return 0;
                    272:        }
                    273:        if (ABUF_EMPTY(ibuf) || !(f->state & FILE_WOK))
1.1       ratchov   274:                return 0;
                    275:        data = abuf_rgetblk(ibuf, &count, 0);
1.12      ratchov   276:        if (count == 0) {
                    277:                DPRINTF("wpipe_out: %s: underrun\n", p->name);
                    278:                return 0;
                    279:        }
1.1       ratchov   280:        count = file_write(f, data, count);
1.12      ratchov   281:        if (count == 0)
                    282:                return 0;
1.6       ratchov   283:        abuf_rdiscard(ibuf, count);
1.1       ratchov   284:        return 1;
                    285: }
                    286:
                    287: void
                    288: wpipe_eof(struct aproc *p, struct abuf *ibuf)
                    289: {
                    290:        DPRINTFN(3, "wpipe_eof: %s\n", p->name);
1.8       ratchov   291:        aproc_del(p);
1.1       ratchov   292: }
                    293:
                    294: void
                    295: wpipe_hup(struct aproc *p, struct abuf *obuf_dummy)
                    296: {
                    297:        DPRINTFN(3, "wpipe_hup: %s\n", p->name);
1.8       ratchov   298:        aproc_del(p);
1.1       ratchov   299: }
                    300:
                    301: struct aproc_ops wpipe_ops = {
1.12      ratchov   302:        "wpipe",
                    303:        wpipe_in,
                    304:        wpipe_out,
                    305:        wpipe_eof,
                    306:        wpipe_hup,
                    307:        NULL, /* newin */
                    308:        NULL, /* newout */
                    309:        aproc_ipos,
                    310:        aproc_opos,
                    311:        wpipe_done
1.1       ratchov   312: };
                    313:
                    314: struct aproc *
                    315: wpipe_new(struct file *f)
                    316: {
                    317:        struct aproc *p;
                    318:
                    319:        p = aproc_new(&wpipe_ops, f->name);
                    320:        p->u.io.file = f;
                    321:        f->wproc = p;
                    322:        return p;
                    323: }
                    324:
                    325: /*
                    326:  * Fill an output block with silence.
                    327:  */
                    328: void
                    329: mix_bzero(struct aproc *p)
                    330: {
                    331:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                    332:        short *odata;
                    333:        unsigned ocount;
                    334:
1.6       ratchov   335:        DPRINTFN(4, "mix_bzero: used = %u, todo = %u\n",
1.14      ratchov   336:            obuf->used, obuf->mixitodo);
                    337:        odata = (short *)abuf_wgetblk(obuf, &ocount, obuf->mixitodo);
1.12      ratchov   338:        ocount -= ocount % obuf->bpf;
1.1       ratchov   339:        if (ocount == 0)
                    340:                return;
                    341:        memset(odata, 0, ocount);
1.14      ratchov   342:        obuf->mixitodo += ocount;
                    343:        DPRINTFN(4, "mix_bzero: ocount %u, todo %u\n", ocount, obuf->mixitodo);
1.1       ratchov   344: }
                    345:
                    346: /*
                    347:  * Mix an input block over an output block.
                    348:  */
                    349: void
                    350: mix_badd(struct abuf *ibuf, struct abuf *obuf)
                    351: {
                    352:        short *idata, *odata;
1.14      ratchov   353:        int vol = ibuf->mixivol;
1.13      ratchov   354:        unsigned i, j, icnt, onext, ostart;
                    355:        unsigned scount, icount, ocount;
1.1       ratchov   356:
1.6       ratchov   357:        DPRINTFN(4, "mix_badd: todo = %u, done = %u\n",
1.14      ratchov   358:            obuf->mixitodo, ibuf->mixodone);
1.1       ratchov   359:
                    360:        idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
1.13      ratchov   361:        icount /= ibuf->bpf;
1.1       ratchov   362:        if (icount == 0)
                    363:                return;
                    364:
1.14      ratchov   365:        odata = (short *)abuf_wgetblk(obuf, &ocount, ibuf->mixodone);
1.13      ratchov   366:        ocount /= obuf->bpf;
1.1       ratchov   367:        if (ocount == 0)
                    368:                return;
                    369:
1.13      ratchov   370:        ostart = ibuf->cmin - obuf->cmin;
                    371:        onext = obuf->cmax - ibuf->cmax + ostart;
                    372:        icnt = ibuf->cmax - ibuf->cmin + 1;
                    373:        odata += ostart;
1.1       ratchov   374:        scount = (icount < ocount) ? icount : ocount;
1.13      ratchov   375:        for (i = scount; i > 0; i--) {
                    376:                for (j = icnt; j > 0; j--) {
                    377:                        *odata += (*idata * vol) >> ADATA_SHIFT;
                    378:                        idata++;
                    379:                        odata++;
                    380:                }
                    381:                odata += onext;
                    382:        }
                    383:        abuf_rdiscard(ibuf, scount * ibuf->bpf);
1.14      ratchov   384:        ibuf->mixodone += scount * obuf->bpf;
1.1       ratchov   385:
1.6       ratchov   386:        DPRINTFN(4, "mix_badd: added %u, done = %u, todo = %u\n",
1.14      ratchov   387:            scount, ibuf->mixodone, obuf->mixitodo);
1.1       ratchov   388: }
                    389:
                    390: int
                    391: mix_in(struct aproc *p, struct abuf *ibuf)
                    392: {
                    393:        struct abuf *i, *inext, *obuf = LIST_FIRST(&p->obuflist);
1.7       ratchov   394:        unsigned ocount;
1.1       ratchov   395:
1.7       ratchov   396:        DPRINTFN(4, "mix_in: used = %u, done = %u, todo = %u\n",
1.14      ratchov   397:            ibuf->used, ibuf->mixodone, obuf->mixitodo);
1.3       ratchov   398:
1.14      ratchov   399:        if (!ABUF_ROK(ibuf) || ibuf->mixodone == obuf->mixitodo)
1.1       ratchov   400:                return 0;
1.12      ratchov   401:
1.1       ratchov   402:        mix_badd(ibuf, obuf);
1.14      ratchov   403:        ocount = obuf->mixitodo;
1.1       ratchov   404:        LIST_FOREACH(i, &p->ibuflist, ient) {
1.14      ratchov   405:                if (ocount > i->mixodone)
                    406:                        ocount = i->mixodone;
1.1       ratchov   407:        }
                    408:        if (ocount == 0)
                    409:                return 0;
                    410:
1.6       ratchov   411:        abuf_wcommit(obuf, ocount);
1.12      ratchov   412:        p->u.mix.lat += ocount / obuf->bpf;
1.14      ratchov   413:        obuf->mixitodo -= ocount;
1.12      ratchov   414:        if (!abuf_flush(obuf))
                    415:                return 0; /* hup */
1.1       ratchov   416:        mix_bzero(p);
1.12      ratchov   417:        for (i = LIST_FIRST(&p->ibuflist); i != NULL; i = inext) {
1.1       ratchov   418:                inext = LIST_NEXT(i, ient);
1.14      ratchov   419:                i->mixodone -= ocount;
                    420:                if (i->mixodone < obuf->mixitodo)
1.1       ratchov   421:                        mix_badd(i, obuf);
1.12      ratchov   422:                if (!abuf_fill(i))
                    423:                        continue;
1.1       ratchov   424:        }
                    425:        return 1;
                    426: }
                    427:
                    428: int
                    429: mix_out(struct aproc *p, struct abuf *obuf)
                    430: {
                    431:        struct abuf *i, *inext;
1.3       ratchov   432:        unsigned ocount, drop;
1.1       ratchov   433:
1.6       ratchov   434:        DPRINTFN(4, "mix_out: used = %u, todo = %u\n",
1.14      ratchov   435:            obuf->used, obuf->mixitodo);
1.1       ratchov   436:
1.12      ratchov   437:        if (!ABUF_WOK(obuf))
                    438:                return 0;
                    439:
1.1       ratchov   440:        mix_bzero(p);
1.14      ratchov   441:        ocount = obuf->mixitodo;
1.12      ratchov   442:        for (i = LIST_FIRST(&p->ibuflist); i != NULL; i = inext) {
1.1       ratchov   443:                inext = LIST_NEXT(i, ient);
1.12      ratchov   444:                if (!abuf_fill(i))
                    445:                        continue;
1.5       ratchov   446:                if (!ABUF_ROK(i)) {
1.14      ratchov   447:                        if ((p->u.mix.flags & MIX_DROP) && i->mixodone == 0) {
1.5       ratchov   448:                                if (i->xrun == XRUN_ERROR) {
                    449:                                        abuf_hup(i);
                    450:                                        continue;
                    451:                                }
1.14      ratchov   452:                                drop = obuf->mixitodo;
                    453:                                i->mixodone += drop;
1.5       ratchov   454:                                if (i->xrun == XRUN_SYNC)
1.7       ratchov   455:                                        i->drop += drop;
1.12      ratchov   456:                                else {
                    457:                                        abuf_opos(i, -(int)(drop / i->bpf));
                    458:                                        if (i->duplex) {
                    459:                                                DPRINTF("mix_out: duplex %u\n",
                    460:                                                    drop);
                    461:                                                i->duplex->drop += drop *
                    462:                                                    i->duplex->bpf / i->bpf;
                    463:                                                abuf_ipos(i->duplex,
                    464:                                                    -(int)(drop / i->bpf));
                    465:                                        }
                    466:                                }
1.7       ratchov   467:                                DPRINTF("mix_out: drop = %u\n", i->drop);
1.5       ratchov   468:                        }
1.3       ratchov   469:                } else
                    470:                        mix_badd(i, obuf);
1.14      ratchov   471:                if (ocount > i->mixodone)
                    472:                        ocount = i->mixodone;
1.1       ratchov   473:        }
                    474:        if (ocount == 0)
                    475:                return 0;
1.9       ratchov   476:        if (LIST_EMPTY(&p->ibuflist) && (p->u.mix.flags & MIX_AUTOQUIT)) {
1.1       ratchov   477:                DPRINTF("mix_out: nothing more to do...\n");
                    478:                aproc_del(p);
                    479:                return 0;
                    480:        }
1.6       ratchov   481:        abuf_wcommit(obuf, ocount);
1.12      ratchov   482:        p->u.mix.lat += ocount / obuf->bpf;
1.14      ratchov   483:        obuf->mixitodo -= ocount;
1.1       ratchov   484:        LIST_FOREACH(i, &p->ibuflist, ient) {
1.14      ratchov   485:                i->mixodone -= ocount;
1.1       ratchov   486:        }
                    487:        return 1;
                    488: }
                    489:
                    490: void
                    491: mix_eof(struct aproc *p, struct abuf *ibuf)
                    492: {
                    493:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                    494:
                    495:        DPRINTF("mix_eof: %s: detached\n", p->name);
1.12      ratchov   496:        mix_setmaster(p);
                    497:
1.1       ratchov   498:        /*
                    499:         * If there's no more inputs, abuf_run() will trigger the eof
                    500:         * condition and propagate it, so no need to handle it here.
                    501:         */
                    502:        abuf_run(obuf);
                    503:        DPRINTF("mix_eof: done\n");
                    504: }
                    505:
                    506: void
                    507: mix_hup(struct aproc *p, struct abuf *obuf)
                    508: {
                    509:        struct abuf *ibuf;
                    510:
                    511:        while (!LIST_EMPTY(&p->ibuflist)) {
                    512:                ibuf = LIST_FIRST(&p->ibuflist);
                    513:                abuf_hup(ibuf);
                    514:        }
                    515:        DPRINTF("mix_hup: %s: done\n", p->name);
                    516:        aproc_del(p);
                    517: }
                    518:
                    519: void
                    520: mix_newin(struct aproc *p, struct abuf *ibuf)
                    521: {
1.13      ratchov   522:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                    523:
                    524:        if (!obuf || ibuf->cmin < obuf->cmin || ibuf->cmax > obuf->cmax) {
                    525:                fprintf(stderr, "mix_newin: channel ranges mismatch\n");
                    526:                abort();
                    527:        }
1.14      ratchov   528:        ibuf->mixodone = 0;
                    529:        ibuf->mixivol = ADATA_UNIT;
1.5       ratchov   530:        ibuf->xrun = XRUN_IGNORE;
1.12      ratchov   531:        mix_setmaster(p);
1.1       ratchov   532: }
                    533:
                    534: void
                    535: mix_newout(struct aproc *p, struct abuf *obuf)
                    536: {
1.14      ratchov   537:        obuf->mixitodo = 0;
1.1       ratchov   538:        mix_bzero(p);
                    539: }
                    540:
1.12      ratchov   541: void
                    542: mix_opos(struct aproc *p, struct abuf *obuf, int delta)
                    543: {
                    544:        DPRINTFN(3, "mix_opos: lat = %d/%d\n", p->u.mix.lat, p->u.mix.maxlat);
                    545:        p->u.mix.lat -= delta;
                    546:        aproc_opos(p, obuf, delta);
                    547: }
                    548:
1.1       ratchov   549: struct aproc_ops mix_ops = {
1.12      ratchov   550:        "mix",
                    551:        mix_in,
                    552:        mix_out,
                    553:        mix_eof,
                    554:        mix_hup,
                    555:        mix_newin,
                    556:        mix_newout,
                    557:        aproc_ipos,
                    558:        mix_opos,
                    559:        NULL
1.1       ratchov   560: };
                    561:
                    562: struct aproc *
1.12      ratchov   563: mix_new(char *name, int maxlat)
1.1       ratchov   564: {
                    565:        struct aproc *p;
                    566:
1.12      ratchov   567:        p = aproc_new(&mix_ops, name);
1.5       ratchov   568:        p->u.mix.flags = 0;
1.12      ratchov   569:        p->u.mix.lat = 0;
                    570:        p->u.mix.maxlat = maxlat;
1.1       ratchov   571:        return p;
1.10      ratchov   572: }
                    573:
                    574: void
                    575: mix_pushzero(struct aproc *p)
                    576: {
                    577:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                    578:
1.14      ratchov   579:        abuf_wcommit(obuf, obuf->mixitodo);
                    580:        p->u.mix.lat += obuf->mixitodo / obuf->bpf;
                    581:        obuf->mixitodo = 0;
1.11      ratchov   582:        abuf_run(obuf);
1.10      ratchov   583:        mix_bzero(p);
                    584: }
                    585:
                    586: /*
                    587:  * Normalize input levels
                    588:  */
                    589: void
                    590: mix_setmaster(struct aproc *p)
                    591: {
                    592:        unsigned n;
                    593:        struct abuf *buf;
                    594:
                    595:        n = 0;
                    596:        LIST_FOREACH(buf, &p->ibuflist, ient)
                    597:            n++;
                    598:        LIST_FOREACH(buf, &p->ibuflist, ient)
1.14      ratchov   599:            buf->mixivol = ADATA_UNIT / n;
1.1       ratchov   600: }
                    601:
                    602: /*
                    603:  * Copy data from ibuf to obuf.
                    604:  */
                    605: void
                    606: sub_bcopy(struct abuf *ibuf, struct abuf *obuf)
                    607: {
1.13      ratchov   608:        short *idata, *odata;
                    609:        unsigned i, j, ocnt, inext, istart;
1.1       ratchov   610:        unsigned icount, ocount, scount;
                    611:
1.14      ratchov   612:        idata = (short *)abuf_rgetblk(ibuf, &icount, obuf->subidone);
1.13      ratchov   613:        icount /= ibuf->bpf;
1.1       ratchov   614:        if (icount == 0)
                    615:                return;
1.13      ratchov   616:        odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
                    617:        ocount /= obuf->bpf;
1.1       ratchov   618:        if (ocount == 0)
                    619:                return;
1.13      ratchov   620:        istart = obuf->cmin - ibuf->cmin;
                    621:        inext = ibuf->cmax - obuf->cmax + istart;
                    622:        ocnt = obuf->cmax - obuf->cmin + 1;
1.1       ratchov   623:        scount = (icount < ocount) ? icount : ocount;
1.13      ratchov   624:        idata += istart;
                    625:        for (i = scount; i > 0; i--) {
                    626:                for (j = ocnt; j > 0; j--) {
                    627:                        *odata = *idata;
                    628:                        odata++;
                    629:                        idata++;
                    630:                }
                    631:                idata += inext;
                    632:        }
                    633:        abuf_wcommit(obuf, scount * obuf->bpf);
1.14      ratchov   634:        obuf->subidone += scount * ibuf->bpf;
1.13      ratchov   635:        DPRINTFN(4, "sub_bcopy: %u frames\n", scount);
1.1       ratchov   636: }
                    637:
                    638: int
                    639: sub_in(struct aproc *p, struct abuf *ibuf)
                    640: {
                    641:        struct abuf *i, *inext;
1.3       ratchov   642:        unsigned done, drop;
1.12      ratchov   643:
                    644:        if (!ABUF_ROK(ibuf))
                    645:                return 0;
1.1       ratchov   646:        done = ibuf->used;
1.12      ratchov   647:        for (i = LIST_FIRST(&p->obuflist); i != NULL; i = inext) {
1.1       ratchov   648:                inext = LIST_NEXT(i, oent);
1.5       ratchov   649:                if (!ABUF_WOK(i)) {
1.14      ratchov   650:                        if ((p->u.sub.flags & SUB_DROP) && i->subidone == 0) {
1.5       ratchov   651:                                if (i->xrun == XRUN_ERROR) {
                    652:                                        abuf_eof(i);
                    653:                                        continue;
                    654:                                }
                    655:                                drop = ibuf->used;
                    656:                                if (i->xrun == XRUN_SYNC)
1.7       ratchov   657:                                        i->silence += drop;
1.12      ratchov   658:                                else {
                    659:                                        abuf_ipos(i, -(int)(drop / i->bpf));
                    660:                                        if (i->duplex) {
                    661:                                                DPRINTF("sub_in: duplex %u\n",
                    662:                                                    drop);
                    663:                                                i->duplex->silence += drop *
                    664:                                                    i->duplex->bpf / i->bpf;
                    665:                                                abuf_opos(i->duplex,
                    666:                                                    -(int)(drop / i->bpf));
                    667:                                        }
                    668:                                }
1.14      ratchov   669:                                i->subidone += drop;
1.12      ratchov   670:                                DPRINTF("sub_in: silence = %u\n", i->silence);
1.5       ratchov   671:                        }
1.12      ratchov   672:                } else
1.1       ratchov   673:                        sub_bcopy(ibuf, i);
1.14      ratchov   674:                if (done > i->subidone)
                    675:                        done = i->subidone;
1.12      ratchov   676:                if (!abuf_flush(i))
                    677:                        continue;
1.1       ratchov   678:        }
                    679:        LIST_FOREACH(i, &p->obuflist, oent) {
1.14      ratchov   680:                i->subidone -= done;
1.1       ratchov   681:        }
1.6       ratchov   682:        abuf_rdiscard(ibuf, done);
1.12      ratchov   683:        p->u.sub.lat -= done / ibuf->bpf;
                    684:        return 1;
1.1       ratchov   685: }
                    686:
                    687: int
                    688: sub_out(struct aproc *p, struct abuf *obuf)
                    689: {
                    690:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                    691:        struct abuf *i, *inext;
                    692:        unsigned done;
                    693:
1.12      ratchov   694:        if (!ABUF_WOK(obuf))
                    695:                return 0;
                    696:        if (!abuf_fill(ibuf)) {
                    697:                return 0;
                    698:        }
1.14      ratchov   699:        if (obuf->subidone == ibuf->used)
1.1       ratchov   700:                return 0;
                    701:
                    702:        done = ibuf->used;
1.12      ratchov   703:        for (i = LIST_FIRST(&p->obuflist); i != NULL; i = inext) {
                    704:                inext = LIST_NEXT(i, oent);
                    705:                if (!abuf_flush(i))
                    706:                        continue;
                    707:                sub_bcopy(ibuf, i);
1.14      ratchov   708:                if (done > i->subidone)
                    709:                        done = i->subidone;
1.1       ratchov   710:        }
                    711:        LIST_FOREACH(i, &p->obuflist, oent) {
1.14      ratchov   712:                i->subidone -= done;
1.1       ratchov   713:        }
1.6       ratchov   714:        abuf_rdiscard(ibuf, done);
1.12      ratchov   715:        p->u.sub.lat -= done / ibuf->bpf;
1.1       ratchov   716:        return 1;
                    717: }
                    718:
                    719: void
                    720: sub_eof(struct aproc *p, struct abuf *ibuf)
                    721: {
                    722:        struct abuf *obuf;
                    723:
                    724:        while (!LIST_EMPTY(&p->obuflist)) {
                    725:                obuf = LIST_FIRST(&p->obuflist);
                    726:                abuf_eof(obuf);
                    727:        }
1.8       ratchov   728:        aproc_del(p);
1.1       ratchov   729: }
                    730:
                    731: void
                    732: sub_hup(struct aproc *p, struct abuf *obuf)
                    733: {
                    734:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                    735:
                    736:        DPRINTF("sub_hup: %s: detached\n", p->name);
1.12      ratchov   737:        abuf_run(ibuf);
1.1       ratchov   738:        DPRINTF("sub_hup: done\n");
                    739: }
                    740:
                    741: void
                    742: sub_newout(struct aproc *p, struct abuf *obuf)
                    743: {
1.13      ratchov   744:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                    745:
                    746:        if (!ibuf || obuf->cmin < ibuf->cmin || obuf->cmax > ibuf->cmax) {
                    747:                fprintf(stderr, "sub_newout: channel ranges mismatch\n");
                    748:                abort();
                    749:        }
1.14      ratchov   750:        obuf->subidone = 0;
1.5       ratchov   751:        obuf->xrun = XRUN_IGNORE;
1.1       ratchov   752: }
                    753:
1.12      ratchov   754: void
                    755: sub_ipos(struct aproc *p, struct abuf *ibuf, int delta)
                    756: {
                    757:        p->u.sub.lat += delta;
                    758:        DPRINTFN(3, "sub_ipos: lat = %d/%d\n", p->u.sub.lat, p->u.sub.maxlat);
                    759:        aproc_ipos(p, ibuf, delta);
                    760: }
                    761:
1.1       ratchov   762: struct aproc_ops sub_ops = {
1.12      ratchov   763:        "sub",
                    764:        sub_in,
                    765:        sub_out,
                    766:        sub_eof,
                    767:        sub_hup,
                    768:        NULL,
                    769:        sub_newout,
                    770:        sub_ipos,
                    771:        aproc_opos,
                    772:        NULL
1.1       ratchov   773: };
                    774:
                    775: struct aproc *
1.12      ratchov   776: sub_new(char *name, int maxlat)
1.1       ratchov   777: {
                    778:        struct aproc *p;
                    779:
1.12      ratchov   780:        p = aproc_new(&sub_ops, name);
1.5       ratchov   781:        p->u.sub.flags = 0;
1.12      ratchov   782:        p->u.sub.lat = 0;
                    783:        p->u.sub.maxlat = maxlat;
1.1       ratchov   784:        return p;
                    785: }
                    786:
                    787: /*
                    788:  * Convert one block.
                    789:  */
                    790: void
                    791: conv_bcopy(struct aconv *ist, struct aconv *ost,
                    792:     struct abuf *ibuf, struct abuf *obuf)
                    793: {
                    794:        int *ictx;
                    795:        unsigned inch, ibps;
                    796:        unsigned char *idata;
                    797:        int ibnext, isigbit;
                    798:        unsigned ishift;
                    799:        int isnext;
                    800:        unsigned ipos, orate;
                    801:        unsigned ifr;
                    802:        int *octx;
                    803:        unsigned onch, oshift;
                    804:        int osigbit;
                    805:        unsigned obps;
                    806:        unsigned char *odata;
                    807:        int obnext, osnext;
                    808:        unsigned opos, irate;
                    809:        unsigned ofr;
                    810:        unsigned c, i;
                    811:        int s, *ctx;
                    812:        unsigned icount, ocount;
                    813:
                    814:        /*
                    815:         * It's ok to have s uninitialized, but we dont want the compiler to
                    816:         * complain about it.
                    817:         */
                    818:        s = (int)0xdeadbeef;
                    819:
                    820:        /*
                    821:         * Calculate max frames readable at once from the input buffer.
                    822:         */
                    823:        idata = abuf_rgetblk(ibuf, &icount, 0);
                    824:        ifr = icount / ibuf->bpf;
1.12      ratchov   825:        icount = ifr * ibuf->bpf;
1.1       ratchov   826:
                    827:        odata = abuf_wgetblk(obuf, &ocount, 0);
                    828:        ofr = ocount / obuf->bpf;
1.12      ratchov   829:        ocount = ofr * obuf->bpf;
1.1       ratchov   830:
                    831:        /*
                    832:         * Partially copy structures into local variables, to avoid
                    833:         * unnecessary indirections; this also allows the compiler to
                    834:         * order local variables more "cache-friendly".
                    835:         */
                    836:        ictx = ist->ctx + ist->cmin;
                    837:        octx = ist->ctx + ost->cmin;
                    838:        inch = ist->nch;
                    839:        ibps = ist->bps;
                    840:        ibnext = ist->bnext;
                    841:        isigbit = ist->sigbit;
                    842:        ishift = ist->shift;
                    843:        isnext = ist->snext;
                    844:        ipos = ist->pos;
                    845:        irate = ist->rate;
                    846:        onch = ost->nch;
                    847:        oshift = ost->shift;
                    848:        osigbit = ost->sigbit;
                    849:        obps = ost->bps;
                    850:        obnext = ost->bnext;
                    851:        osnext = ost->snext;
                    852:        opos = ost->pos;
                    853:        orate = ost->rate;
                    854:
                    855:        /*
                    856:         * Start conversion.
                    857:         */
                    858:        idata += ist->bfirst;
                    859:        odata += ost->bfirst;
                    860:        DPRINTFN(4, "conv_bcopy: ifr=%d ofr=%d\n", ifr, ofr);
                    861:        for (;;) {
                    862:                if ((int)(ipos - opos) > 0) {
                    863:                        if (ofr == 0)
                    864:                                break;
                    865:                        ctx = octx;
                    866:                        for (c = onch; c > 0; c--) {
                    867:                                s = *ctx++ << 16;
                    868:                                s >>= oshift;
                    869:                                s ^= osigbit;
                    870:                                for (i = obps; i > 0; i--) {
                    871:                                        *odata = (unsigned char)s;
                    872:                                        s >>= 8;
                    873:                                        odata += obnext;
                    874:                                }
                    875:                                odata += osnext;
                    876:                        }
                    877:                        opos += irate;
                    878:                        ofr--;
                    879:                } else {
                    880:                        if (ifr == 0)
                    881:                                break;
                    882:                        ctx = ictx;
                    883:                        for (c = inch; c > 0; c--) {
                    884:                                for (i = ibps; i > 0; i--) {
                    885:                                        s <<= 8;
                    886:                                        s |= *idata;
                    887:                                        idata += ibnext;
                    888:                                }
                    889:                                s ^= isigbit;
                    890:                                s <<= ishift;
                    891:                                *ctx++ = (short)(s >> 16);
                    892:                                idata += isnext;
                    893:                        }
                    894:                        ipos += orate;
                    895:                        ifr--;
                    896:                }
                    897:        }
                    898:        ist->pos = ipos;
                    899:        ost->pos = opos;
                    900:        DPRINTFN(4, "conv_bcopy: done, ifr=%d ofr=%d\n", ifr, ofr);
                    901:
                    902:        /*
                    903:         * Update FIFO pointers.
                    904:         */
                    905:        icount -= ifr * ist->bpf;
                    906:        ocount -= ofr * ost->bpf;
1.6       ratchov   907:        abuf_rdiscard(ibuf, icount);
                    908:        abuf_wcommit(obuf, ocount);
1.1       ratchov   909: }
                    910:
                    911: int
                    912: conv_in(struct aproc *p, struct abuf *ibuf)
                    913: {
                    914:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                    915:
1.12      ratchov   916:        DPRINTFN(4, "conv_in: %s\n", p->name);
                    917:
                    918:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1.1       ratchov   919:                return 0;
                    920:        conv_bcopy(&p->u.conv.ist, &p->u.conv.ost, ibuf, obuf);
1.12      ratchov   921:        if (!abuf_flush(obuf))
                    922:                return 0;
                    923:        return 1;
1.1       ratchov   924: }
                    925:
                    926: int
                    927: conv_out(struct aproc *p, struct abuf *obuf)
                    928: {
                    929:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                    930:
1.12      ratchov   931:        DPRINTFN(4, "conv_out: %s\n", p->name);
                    932:
                    933:        if (!abuf_fill(ibuf))
                    934:                return 0;
                    935:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
1.1       ratchov   936:                return 0;
                    937:        conv_bcopy(&p->u.conv.ist, &p->u.conv.ost, ibuf, obuf);
                    938:        return 1;
                    939: }
                    940:
                    941: void
                    942: conv_eof(struct aproc *p, struct abuf *ibuf)
                    943: {
1.12      ratchov   944:        DPRINTFN(4, "conv_eof: %s\n", p->name);
                    945:
1.8       ratchov   946:        aproc_del(p);
1.1       ratchov   947: }
                    948:
                    949: void
                    950: conv_hup(struct aproc *p, struct abuf *obuf)
                    951: {
1.12      ratchov   952:        DPRINTFN(4, "conv_hup: %s\n", p->name);
                    953:
1.8       ratchov   954:        aproc_del(p);
1.1       ratchov   955: }
                    956:
                    957: void
                    958: aconv_init(struct aconv *st, struct aparams *par, int input)
                    959: {
                    960:        unsigned i;
                    961:
                    962:        st->bps = par->bps;
                    963:        st->sigbit = par->sig ? 0 : 1 << (par->bits - 1);
                    964:        if (par->msb) {
                    965:                st->shift = 32 - par->bps * 8;
                    966:        } else {
                    967:                st->shift = 32 - par->bits;
                    968:        }
                    969:        if ((par->le && input) || (!par->le && !input)) {
                    970:                st->bfirst = st->bps - 1;
                    971:                st->bnext = -1;
                    972:                st->snext = 2 * st->bps;
                    973:        } else {
                    974:                st->bfirst = 0;
                    975:                st->bnext = 1;
                    976:                st->snext = 0;
                    977:        }
                    978:        st->cmin = par->cmin;
                    979:        st->nch = par->cmax - par->cmin + 1;
                    980:        st->bpf = st->nch * st->bps;
                    981:        st->rate = par->rate;
                    982:        st->pos = 0;
                    983:
1.12      ratchov   984:        for (i = 0; i < NCHAN_MAX; i++)
1.1       ratchov   985:                st->ctx[i] = 0;
                    986: }
                    987:
1.12      ratchov   988: void
                    989: conv_ipos(struct aproc *p, struct abuf *ibuf, int delta)
                    990: {
                    991:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
                    992:        long long ipos;
                    993:        int ifac, ofac;
                    994:
                    995:        DPRINTFN(3, "conv_ipos: %d\n", delta);
                    996:
                    997:        ifac = p->u.conv.ist.rate;
                    998:        ofac = p->u.conv.ost.rate;
                    999:        ipos = p->u.conv.idelta + (long long)delta * ofac;
                   1000:        delta = (ipos + ifac - 1) / ifac;
                   1001:        p->u.conv.idelta = ipos - (long long)delta * ifac;
                   1002:        abuf_ipos(obuf, delta);
                   1003: }
                   1004:
                   1005: void
                   1006: conv_opos(struct aproc *p, struct abuf *obuf, int delta)
                   1007: {
                   1008:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
                   1009:        long long opos;
                   1010:        int ifac, ofac;
                   1011:
                   1012:        DPRINTFN(3, "conv_opos: %d\n", delta);
                   1013:
                   1014:        ifac = p->u.conv.ist.rate;
                   1015:        ofac = p->u.conv.ost.rate;
                   1016:        opos = p->u.conv.odelta + (long long)delta * ifac;
                   1017:        delta = (opos + ofac - 1) / ofac;
                   1018:        p->u.conv.odelta = opos - (long long)delta * ofac;
                   1019:        abuf_opos(ibuf, delta);
                   1020: }
                   1021:
1.1       ratchov  1022: struct aproc_ops conv_ops = {
1.12      ratchov  1023:        "conv",
                   1024:        conv_in,
                   1025:        conv_out,
                   1026:        conv_eof,
                   1027:        conv_hup,
                   1028:        NULL,
                   1029:        NULL,
                   1030:        conv_ipos,
                   1031:        conv_opos,
                   1032:        NULL
1.1       ratchov  1033: };
                   1034:
                   1035: struct aproc *
                   1036: conv_new(char *name, struct aparams *ipar, struct aparams *opar)
                   1037: {
                   1038:        struct aproc *p;
                   1039:
                   1040:        p = aproc_new(&conv_ops, name);
                   1041:        aconv_init(&p->u.conv.ist, ipar, 1);
                   1042:        aconv_init(&p->u.conv.ost, opar, 0);
1.12      ratchov  1043:        p->u.conv.idelta = 0;
                   1044:        p->u.conv.odelta = 0;
                   1045:        if (debug_level > 0) {
                   1046:                DPRINTF("conv_new: %s: ", p->name);
1.15    ! ratchov  1047:                aparams_print2(ipar, opar);
        !          1048:                DPRINTF("\n");
        !          1049:        }
        !          1050:        return p;
        !          1051: }
        !          1052:
        !          1053: /*
        !          1054:  * Convert one block.
        !          1055:  */
        !          1056: void
        !          1057: resamp_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
        !          1058: {
        !          1059:        unsigned inch;
        !          1060:        short *idata;
        !          1061:        unsigned ipos, orate;
        !          1062:        unsigned ifr;
        !          1063:        unsigned onch;
        !          1064:        short *odata;
        !          1065:        unsigned opos, irate;
        !          1066:        unsigned ofr;
        !          1067:        unsigned c;
        !          1068:        short *ctxbuf, *ctx;
        !          1069:        unsigned icount, ocount;
        !          1070:
        !          1071:        /*
        !          1072:         * Calculate max frames readable at once from the input buffer.
        !          1073:         */
        !          1074:        idata = (short *)abuf_rgetblk(ibuf, &icount, 0);
        !          1075:        ifr = icount / ibuf->bpf;
        !          1076:        icount = ifr * ibuf->bpf;
        !          1077:
        !          1078:        odata = (short *)abuf_wgetblk(obuf, &ocount, 0);
        !          1079:        ofr = ocount / obuf->bpf;
        !          1080:        ocount = ofr * obuf->bpf;
        !          1081:
        !          1082:        /*
        !          1083:         * Partially copy structures into local variables, to avoid
        !          1084:         * unnecessary indirections; this also allows the compiler to
        !          1085:         * order local variables more "cache-friendly".
        !          1086:         */
        !          1087:        inch = ibuf->cmax - ibuf->cmin + 1;
        !          1088:        ipos = p->u.resamp.ipos;
        !          1089:        irate = p->u.resamp.irate;
        !          1090:        onch = obuf->cmax - obuf->cmin + 1;
        !          1091:        opos = p->u.resamp.opos;
        !          1092:        orate = p->u.resamp.orate;
        !          1093:        ctxbuf = p->u.resamp.ctx;
        !          1094:
        !          1095:        /*
        !          1096:         * Start conversion.
        !          1097:         */
        !          1098:        DPRINTFN(4, "resamp_bcopy: ifr=%d ofr=%d\n", ifr, ofr);
        !          1099:        for (;;) {
        !          1100:                if ((int)(ipos - opos) > 0) {
        !          1101:                        if (ofr == 0)
        !          1102:                                break;
        !          1103:                        ctx = ctxbuf;
        !          1104:                        for (c = onch; c > 0; c--) {
        !          1105:                                *odata = *ctx;
        !          1106:                                odata++;
        !          1107:                                ctx++;
        !          1108:                        }
        !          1109:                        opos += irate;
        !          1110:                        ofr--;
        !          1111:                } else {
        !          1112:                        if (ifr == 0)
        !          1113:                                break;
        !          1114:                        ctx = ctxbuf;
        !          1115:                        for (c = inch; c > 0; c--) {
        !          1116:                                *ctx = *idata;
        !          1117:                                idata++;
        !          1118:                                ctx++;
        !          1119:                        }
        !          1120:                        ipos += orate;
        !          1121:                        ifr--;
        !          1122:                }
        !          1123:        }
        !          1124:        p->u.resamp.ipos = ipos;
        !          1125:        p->u.resamp.opos = opos;
        !          1126:        DPRINTFN(4, "resamp_bcopy: done, ifr=%d ofr=%d\n", ifr, ofr);
        !          1127:
        !          1128:        /*
        !          1129:         * Update FIFO pointers.
        !          1130:         */
        !          1131:        icount -= ifr * ibuf->bpf;
        !          1132:        ocount -= ofr * obuf->bpf;
        !          1133:        abuf_rdiscard(ibuf, icount);
        !          1134:        abuf_wcommit(obuf, ocount);
        !          1135: }
        !          1136:
        !          1137: int
        !          1138: resamp_in(struct aproc *p, struct abuf *ibuf)
        !          1139: {
        !          1140:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
        !          1141:
        !          1142:        DPRINTFN(4, "resamp_in: %s\n", p->name);
        !          1143:
        !          1144:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
        !          1145:                return 0;
        !          1146:        resamp_bcopy(p, ibuf, obuf);
        !          1147:        if (!abuf_flush(obuf))
        !          1148:                return 0;
        !          1149:        return 1;
        !          1150: }
        !          1151:
        !          1152: int
        !          1153: resamp_out(struct aproc *p, struct abuf *obuf)
        !          1154: {
        !          1155:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
        !          1156:
        !          1157:        DPRINTFN(4, "resamp_out: %s\n", p->name);
        !          1158:
        !          1159:        if (!abuf_fill(ibuf))
        !          1160:                return 0;
        !          1161:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
        !          1162:                return 0;
        !          1163:        resamp_bcopy(p, ibuf, obuf);
        !          1164:        return 1;
        !          1165: }
        !          1166:
        !          1167: void
        !          1168: resamp_eof(struct aproc *p, struct abuf *ibuf)
        !          1169: {
        !          1170:        DPRINTFN(4, "resamp_eof: %s\n", p->name);
        !          1171:
        !          1172:        aproc_del(p);
        !          1173: }
        !          1174:
        !          1175: void
        !          1176: resamp_hup(struct aproc *p, struct abuf *obuf)
        !          1177: {
        !          1178:        DPRINTFN(4, "resamp_hup: %s\n", p->name);
        !          1179:
        !          1180:        aproc_del(p);
        !          1181: }
        !          1182:
        !          1183: void
        !          1184: resamp_ipos(struct aproc *p, struct abuf *ibuf, int delta)
        !          1185: {
        !          1186:        struct abuf *obuf = LIST_FIRST(&p->obuflist);
        !          1187:        long long ipos;
        !          1188:        int ifac, ofac;
        !          1189:
        !          1190:        DPRINTFN(3, "resamp_ipos: %d\n", delta);
        !          1191:
        !          1192:        ifac = p->u.resamp.irate;
        !          1193:        ofac = p->u.resamp.orate;
        !          1194:        ipos = p->u.resamp.idelta + (long long)delta * ofac;
        !          1195:        delta = (ipos + ifac - 1) / ifac;
        !          1196:        p->u.resamp.idelta = ipos - (long long)delta * ifac;
        !          1197:        abuf_ipos(obuf, delta);
        !          1198: }
        !          1199:
        !          1200: void
        !          1201: resamp_opos(struct aproc *p, struct abuf *obuf, int delta)
        !          1202: {
        !          1203:        struct abuf *ibuf = LIST_FIRST(&p->ibuflist);
        !          1204:        long long opos;
        !          1205:        int ifac, ofac;
        !          1206:
        !          1207:        DPRINTFN(3, "resamp_opos: %d\n", delta);
        !          1208:
        !          1209:        ifac = p->u.resamp.irate;
        !          1210:        ofac = p->u.resamp.orate;
        !          1211:        opos = p->u.resamp.odelta + (long long)delta * ifac;
        !          1212:        delta = (opos + ofac - 1) / ofac;
        !          1213:        p->u.resamp.odelta = opos - (long long)delta * ofac;
        !          1214:        abuf_opos(ibuf, delta);
        !          1215: }
        !          1216:
        !          1217: struct aproc_ops resamp_ops = {
        !          1218:        "resamp",
        !          1219:        resamp_in,
        !          1220:        resamp_out,
        !          1221:        resamp_eof,
        !          1222:        resamp_hup,
        !          1223:        NULL,
        !          1224:        NULL,
        !          1225:        resamp_ipos,
        !          1226:        resamp_opos,
        !          1227:        NULL
        !          1228: };
        !          1229:
        !          1230: struct aproc *
        !          1231: resamp_new(char *name, struct aparams *ipar, struct aparams *opar)
        !          1232: {
        !          1233:        struct aproc *p;
        !          1234:
        !          1235:        p = aproc_new(&resamp_ops, name);
        !          1236:        p->u.resamp.irate = ipar->rate;
        !          1237:        p->u.resamp.orate = opar->rate;
        !          1238:        p->u.resamp.ipos = 0;
        !          1239:        p->u.resamp.opos = 0;
        !          1240:        p->u.resamp.idelta = 0;
        !          1241:        p->u.resamp.odelta = 0;
        !          1242:        if (debug_level > 0) {
        !          1243:                DPRINTF("resamp_new: %s: ", p->name);
1.12      ratchov  1244:                aparams_print2(ipar, opar);
                   1245:                DPRINTF("\n");
                   1246:        }
1.1       ratchov  1247:        return p;
                   1248: }