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

1.63    ! ratchov     1: /*     $OpenBSD: aproc.c,v 1.62 2010/10/21 21:42:46 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:  */
                     37: #include <err.h>
                     38: #include <stdlib.h>
                     39: #include <string.h>
                     40:
1.33      ratchov    41: #include "abuf.h"
1.1       ratchov    42: #include "aparams.h"
                     43: #include "aproc.h"
1.33      ratchov    44: #include "conf.h"
1.1       ratchov    45: #include "file.h"
1.38      ratchov    46: #include "midi.h"
1.42      ratchov    47: #ifdef DEBUG
                     48: #include "dbg.h"
                     49: #endif
                     50:
1.51      ratchov    51: /*
                     52:  * Same as ABUF_ROK(), but consider that a buffer is
                     53:  * readable if there's silence pending to be inserted
                     54:  */
                     55: #define MIX_ROK(buf) (ABUF_ROK(buf) || (buf)->r.mix.drop < 0)
                     56:
                     57: /*
                     58:  * Same as ABUF_WOK(), but consider that a buffer is
                     59:  * writeable if there are samples to drop
                     60:  */
                     61: #define SUB_WOK(buf) (ABUF_WOK(buf) || (buf)->w.sub.silence < 0)
                     62:
1.42      ratchov    63: #ifdef DEBUG
                     64: void
                     65: aproc_dbg(struct aproc *p)
                     66: {
                     67:        dbg_puts(p->ops->name);
                     68:        dbg_puts("(");
                     69:        dbg_puts(p->name);
                     70:        dbg_puts(")");
                     71: }
                     72:
                     73: int
                     74: zomb_in(struct aproc *p, struct abuf *ibuf)
                     75: {
                     76:        aproc_dbg(p);
                     77:        dbg_puts(": in: terminated\n");
                     78:        dbg_panic();
                     79:        return 0;
                     80: }
                     81:
1.1       ratchov    82:
1.42      ratchov    83: int
                     84: zomb_out(struct aproc *p, struct abuf *obuf)
                     85: {
                     86:        aproc_dbg(p);
                     87:        dbg_puts(": out: terminated\n");
                     88:        dbg_panic();
                     89:        return 0;
                     90: }
                     91:
                     92: void
                     93: zomb_eof(struct aproc *p, struct abuf *ibuf)
                     94: {
                     95:        aproc_dbg(p);
                     96:        dbg_puts(": eof: terminated\n");
                     97:        dbg_panic();
                     98: }
                     99:
                    100: void
                    101: zomb_hup(struct aproc *p, struct abuf *obuf)
                    102: {
                    103:        aproc_dbg(p);
                    104:        dbg_puts(": hup: terminated\n");
                    105:        dbg_panic();
                    106: }
                    107:
                    108: void
                    109: zomb_newin(struct aproc *p, struct abuf *ibuf)
                    110: {
                    111:        aproc_dbg(p);
                    112:        dbg_puts(": newin: terminated\n");
                    113:        dbg_panic();
                    114: }
                    115:
                    116: void
                    117: zomb_newout(struct aproc *p, struct abuf *obuf)
                    118: {
                    119:        aproc_dbg(p);
                    120:        dbg_puts(": newout: terminated\n");
                    121:        dbg_panic();
                    122: }
                    123:
                    124: void
                    125: zomb_ipos(struct aproc *p, struct abuf *ibuf, int delta)
                    126: {
                    127:        aproc_dbg(p);
                    128:        dbg_puts(": ipos: terminated\n");
                    129:        dbg_panic();
                    130: }
                    131:
                    132: void
                    133: zomb_opos(struct aproc *p, struct abuf *obuf, int delta)
                    134: {
                    135:        aproc_dbg(p);
                    136:        dbg_puts(": opos: terminated\n");
                    137:        dbg_panic();
                    138: }
                    139:
                    140: struct aproc_ops zomb_ops = {
                    141:        "zomb",
                    142:        zomb_in,
                    143:        zomb_out,
                    144:        zomb_eof,
                    145:        zomb_hup,
                    146:        zomb_newin,
                    147:        zomb_newout,
                    148:        zomb_ipos,
                    149:        zomb_opos,
                    150:        NULL
                    151: };
                    152: #endif
1.34      ratchov   153:
1.1       ratchov   154: struct aproc *
                    155: aproc_new(struct aproc_ops *ops, char *name)
                    156: {
                    157:        struct aproc *p;
                    158:
                    159:        p = malloc(sizeof(struct aproc));
                    160:        if (p == NULL)
                    161:                err(1, name);
1.54      ratchov   162:        LIST_INIT(&p->ins);
                    163:        LIST_INIT(&p->outs);
1.1       ratchov   164:        p->name = name;
                    165:        p->ops = ops;
1.30      ratchov   166:        p->refs = 0;
1.38      ratchov   167:        p->flags = 0;
1.1       ratchov   168:        return p;
                    169: }
                    170:
                    171: void
                    172: aproc_del(struct aproc *p)
                    173: {
1.12      ratchov   174:        struct abuf *i;
                    175:
1.38      ratchov   176:        if (!(p->flags & APROC_ZOMB)) {
1.42      ratchov   177: #ifdef DEBUG
                    178:                if (debug_level >= 3) {
                    179:                        aproc_dbg(p);
                    180:                        dbg_puts(": terminating...\n");
                    181:                }
                    182: #endif
1.35      ratchov   183:                if (p->ops->done) {
1.42      ratchov   184: #ifdef DEBUG
                    185:                        if (debug_level >= 3) {
                    186:                                aproc_dbg(p);
                    187:                                dbg_puts(": done\n");
                    188:                        }
                    189: #endif
1.35      ratchov   190:                        p->ops->done(p);
                    191:                }
1.54      ratchov   192:                while (!LIST_EMPTY(&p->ins)) {
                    193:                        i = LIST_FIRST(&p->ins);
1.35      ratchov   194:                        abuf_hup(i);
                    195:                }
1.54      ratchov   196:                while (!LIST_EMPTY(&p->outs)) {
                    197:                        i = LIST_FIRST(&p->outs);
1.35      ratchov   198:                        abuf_eof(i);
                    199:                }
1.38      ratchov   200:                p->flags |= APROC_ZOMB;
1.34      ratchov   201:        }
1.35      ratchov   202:        if (p->refs > 0) {
1.42      ratchov   203: #ifdef DEBUG
                    204:                if (debug_level >= 3) {
                    205:                        aproc_dbg(p);
                    206:                        dbg_puts(": free delayed\n");
                    207:                        p->ops = &zomb_ops;
                    208:                }
                    209: #endif
1.35      ratchov   210:                return;
1.12      ratchov   211:        }
1.42      ratchov   212: #ifdef DEBUG
                    213:        if (debug_level >= 3) {
                    214:                aproc_dbg(p);
                    215:                dbg_puts(": freed\n");
                    216:        }
                    217: #endif
1.1       ratchov   218:        free(p);
                    219: }
                    220:
                    221: void
                    222: aproc_setin(struct aproc *p, struct abuf *ibuf)
                    223: {
1.54      ratchov   224:        LIST_INSERT_HEAD(&p->ins, ibuf, ient);
1.1       ratchov   225:        ibuf->rproc = p;
                    226:        if (p->ops->newin)
                    227:                p->ops->newin(p, ibuf);
                    228: }
                    229:
                    230: void
                    231: aproc_setout(struct aproc *p, struct abuf *obuf)
                    232: {
1.54      ratchov   233:        LIST_INSERT_HEAD(&p->outs, obuf, oent);
1.1       ratchov   234:        obuf->wproc = p;
                    235:        if (p->ops->newout)
                    236:                p->ops->newout(p, obuf);
                    237: }
                    238:
1.12      ratchov   239: void
                    240: aproc_ipos(struct aproc *p, struct abuf *ibuf, int delta)
                    241: {
                    242:        struct abuf *obuf;
                    243:
1.54      ratchov   244:        LIST_FOREACH(obuf, &p->outs, oent) {
1.12      ratchov   245:                abuf_ipos(obuf, delta);
                    246:        }
                    247: }
                    248:
                    249: void
                    250: aproc_opos(struct aproc *p, struct abuf *obuf, int delta)
                    251: {
                    252:        struct abuf *ibuf;
                    253:
1.54      ratchov   254:        LIST_FOREACH(ibuf, &p->ins, ient) {
1.12      ratchov   255:                abuf_opos(ibuf, delta);
                    256:        }
                    257: }
                    258:
1.1       ratchov   259: int
1.28      ratchov   260: aproc_inuse(struct aproc *p)
                    261: {
                    262:        struct abuf *i;
                    263:
1.54      ratchov   264:        LIST_FOREACH(i, &p->ins, ient) {
1.28      ratchov   265:                if (i->inuse)
                    266:                        return 1;
                    267:        }
1.54      ratchov   268:        LIST_FOREACH(i, &p->outs, oent) {
1.28      ratchov   269:                if (i->inuse)
                    270:                        return 1;
                    271:        }
                    272:        return 0;
                    273: }
                    274:
                    275: int
1.32      ratchov   276: aproc_depend(struct aproc *p, struct aproc *dep)
                    277: {
                    278:        struct abuf *i;
                    279:
                    280:        if (p == dep)
                    281:                return 1;
1.54      ratchov   282:        LIST_FOREACH(i, &p->ins, ient) {
1.32      ratchov   283:                if (i->wproc && aproc_depend(i->wproc, dep))
                    284:                        return 1;
                    285:        }
                    286:        return 0;
                    287: }
                    288:
                    289: int
1.51      ratchov   290: rfile_do(struct aproc *p, unsigned todo, unsigned *done)
                    291: {
1.54      ratchov   292:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.51      ratchov   293:        struct file *f = p->u.io.file;
                    294:        unsigned char *data;
                    295:        unsigned n, count, off;
                    296:
                    297:        off = p->u.io.partial;
                    298:        data = abuf_wgetblk(obuf, &count, 0);
                    299:        if (count > todo)
                    300:                count = todo;
                    301:        n = file_read(f, data + off, count * obuf->bpf - off);
                    302:        if (n == 0)
                    303:                return 0;
                    304:        n += off;
                    305:        p->u.io.partial = n % obuf->bpf;
                    306:        count = n / obuf->bpf;
                    307:        if (count > 0)
                    308:                abuf_wcommit(obuf, count);
                    309:        if (done)
                    310:                *done = count;
                    311:        return 1;
                    312: }
                    313:
                    314: int
1.50      ratchov   315: rfile_in(struct aproc *p, struct abuf *ibuf_dummy)
1.49      ratchov   316: {
1.54      ratchov   317:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.49      ratchov   318:        struct file *f = p->u.io.file;
                    319:
1.51      ratchov   320:        if (!ABUF_WOK(obuf) || !(f->state & FILE_ROK))
1.50      ratchov   321:                return 0;
1.51      ratchov   322:        if (!rfile_do(p, obuf->len, NULL))
1.12      ratchov   323:                return 0;
                    324:        if (!abuf_flush(obuf))
                    325:                return 0;
                    326:        return 1;
1.1       ratchov   327: }
                    328:
                    329: int
1.41      ratchov   330: rfile_out(struct aproc *p, struct abuf *obuf)
1.1       ratchov   331: {
                    332:        struct file *f = p->u.io.file;
                    333:
1.32      ratchov   334:        if (f->state & FILE_RINUSE)
1.12      ratchov   335:                return 0;
1.51      ratchov   336:        if (!ABUF_WOK(obuf) || !(f->state & FILE_ROK))
1.1       ratchov   337:                return 0;
1.51      ratchov   338:        if (!rfile_do(p, obuf->len, NULL))
1.12      ratchov   339:                return 0;
                    340:        return 1;
1.1       ratchov   341: }
                    342:
                    343: void
1.41      ratchov   344: rfile_done(struct aproc *p)
1.1       ratchov   345: {
                    346:        struct file *f = p->u.io.file;
1.39      ratchov   347:        struct abuf *obuf;
1.1       ratchov   348:
1.30      ratchov   349:        if (f == NULL)
                    350:                return;
1.39      ratchov   351:        /*
1.51      ratchov   352:         * disconnect from file structure
                    353:         */
                    354:        f->rproc = NULL;
                    355:        p->u.io.file = NULL;
                    356:
                    357:        /*
1.39      ratchov   358:         * all buffers must be detached before deleting f->wproc,
                    359:         * because otherwise it could trigger this code again
                    360:         */
1.54      ratchov   361:        obuf = LIST_FIRST(&p->outs);
1.39      ratchov   362:        if (obuf)
                    363:                abuf_eof(obuf);
1.37      ratchov   364:        if (f->wproc) {
                    365:                aproc_del(f->wproc);
                    366:        } else
1.12      ratchov   367:                file_del(f);
1.51      ratchov   368:
                    369: #ifdef DEBUG
                    370:        if (debug_level >= 2 && p->u.io.partial > 0) {
                    371:                aproc_dbg(p);
                    372:                dbg_puts(": ");
                    373:                dbg_putu(p->u.io.partial);
                    374:                dbg_puts(" bytes lost in partial read\n");
                    375:        }
                    376: #endif
1.1       ratchov   377: }
                    378:
                    379: void
1.41      ratchov   380: rfile_eof(struct aproc *p, struct abuf *ibuf_dummy)
1.1       ratchov   381: {
1.8       ratchov   382:        aproc_del(p);
1.1       ratchov   383: }
                    384:
                    385: void
1.41      ratchov   386: rfile_hup(struct aproc *p, struct abuf *obuf)
1.1       ratchov   387: {
1.8       ratchov   388:        aproc_del(p);
1.1       ratchov   389: }
                    390:
1.41      ratchov   391: struct aproc_ops rfile_ops = {
1.43      ratchov   392:        "rfile",
1.41      ratchov   393:        rfile_in,
                    394:        rfile_out,
                    395:        rfile_eof,
                    396:        rfile_hup,
1.12      ratchov   397:        NULL, /* newin */
                    398:        NULL, /* newout */
                    399:        aproc_ipos,
                    400:        aproc_opos,
1.41      ratchov   401:        rfile_done
1.1       ratchov   402: };
                    403:
                    404: struct aproc *
1.41      ratchov   405: rfile_new(struct file *f)
1.1       ratchov   406: {
                    407:        struct aproc *p;
                    408:
1.41      ratchov   409:        p = aproc_new(&rfile_ops, f->name);
1.1       ratchov   410:        p->u.io.file = f;
1.51      ratchov   411:        p->u.io.partial = 0;
1.31      ratchov   412:        f->rproc = p;
1.1       ratchov   413:        return p;
                    414: }
                    415:
                    416: void
1.41      ratchov   417: wfile_done(struct aproc *p)
1.1       ratchov   418: {
                    419:        struct file *f = p->u.io.file;
1.39      ratchov   420:        struct abuf *ibuf;
1.1       ratchov   421:
1.30      ratchov   422:        if (f == NULL)
                    423:                return;
1.39      ratchov   424:        /*
1.51      ratchov   425:         * disconnect from file structure
                    426:         */
                    427:        f->wproc = NULL;
                    428:        p->u.io.file = NULL;
                    429:
                    430:        /*
1.39      ratchov   431:         * all buffers must be detached before deleting f->rproc,
                    432:         * because otherwise it could trigger this code again
                    433:         */
1.54      ratchov   434:        ibuf = LIST_FIRST(&p->ins);
1.39      ratchov   435:        if (ibuf)
                    436:                abuf_hup(ibuf);
1.37      ratchov   437:        if (f->rproc) {
                    438:                aproc_del(f->rproc);
                    439:        } else
1.12      ratchov   440:                file_del(f);
1.51      ratchov   441: #ifdef DEBUG
                    442:        if (debug_level >= 2 && p->u.io.partial > 0) {
                    443:                aproc_dbg(p);
                    444:                dbg_puts(": ");
                    445:                dbg_putu(p->u.io.partial);
                    446:                dbg_puts(" bytes lost in partial write\n");
                    447:        }
                    448: #endif
1.1       ratchov   449: }
                    450:
                    451: int
1.51      ratchov   452: wfile_do(struct aproc *p, unsigned todo, unsigned *done)
                    453: {
1.54      ratchov   454:        struct abuf *ibuf = LIST_FIRST(&p->ins);
1.51      ratchov   455:        struct file *f = p->u.io.file;
                    456:        unsigned char *data;
                    457:        unsigned n, count, off;
                    458:
                    459:        off = p->u.io.partial;
                    460:        data = abuf_rgetblk(ibuf, &count, 0);
                    461:        if (count > todo)
                    462:                count = todo;
                    463:        n = file_write(f, data + off, count * ibuf->bpf - off);
                    464:        if (n == 0)
                    465:                return 0;
                    466:        n += off;
                    467:        p->u.io.partial = n % ibuf->bpf;
                    468:        count = n / ibuf->bpf;
                    469:        if (count > 0)
                    470:                abuf_rdiscard(ibuf, count);
                    471:        if (done)
                    472:                *done = count;
                    473:        return 1;
                    474: }
                    475: int
1.50      ratchov   476: wfile_in(struct aproc *p, struct abuf *ibuf)
1.49      ratchov   477: {
                    478:        struct file *f = p->u.io.file;
1.1       ratchov   479:
1.32      ratchov   480:        if (f->state & FILE_WINUSE)
1.12      ratchov   481:                return 0;
1.51      ratchov   482:        if (!ABUF_ROK(ibuf) || !(f->state & FILE_WOK))
1.1       ratchov   483:                return 0;
1.51      ratchov   484:        if (!wfile_do(p, ibuf->len, NULL))
1.12      ratchov   485:                return 0;
                    486:        return 1;
1.1       ratchov   487: }
                    488:
                    489: int
1.41      ratchov   490: wfile_out(struct aproc *p, struct abuf *obuf_dummy)
1.1       ratchov   491: {
1.54      ratchov   492:        struct abuf *ibuf = LIST_FIRST(&p->ins);
1.1       ratchov   493:        struct file *f = p->u.io.file;
                    494:
1.34      ratchov   495:        if (!abuf_fill(ibuf))
1.12      ratchov   496:                return 0;
1.51      ratchov   497:        if (!ABUF_ROK(ibuf) || !(f->state & FILE_WOK))
1.12      ratchov   498:                return 0;
1.51      ratchov   499:        if (!wfile_do(p, ibuf->len, NULL))
1.12      ratchov   500:                return 0;
1.1       ratchov   501:        return 1;
                    502: }
                    503:
                    504: void
1.41      ratchov   505: wfile_eof(struct aproc *p, struct abuf *ibuf)
1.1       ratchov   506: {
1.8       ratchov   507:        aproc_del(p);
1.1       ratchov   508: }
                    509:
                    510: void
1.41      ratchov   511: wfile_hup(struct aproc *p, struct abuf *obuf_dummy)
1.1       ratchov   512: {
1.8       ratchov   513:        aproc_del(p);
1.1       ratchov   514: }
                    515:
1.41      ratchov   516: struct aproc_ops wfile_ops = {
1.43      ratchov   517:        "wfile",
1.41      ratchov   518:        wfile_in,
                    519:        wfile_out,
                    520:        wfile_eof,
                    521:        wfile_hup,
1.12      ratchov   522:        NULL, /* newin */
                    523:        NULL, /* newout */
                    524:        aproc_ipos,
                    525:        aproc_opos,
1.41      ratchov   526:        wfile_done
1.1       ratchov   527: };
                    528:
                    529: struct aproc *
1.41      ratchov   530: wfile_new(struct file *f)
1.1       ratchov   531: {
                    532:        struct aproc *p;
                    533:
1.41      ratchov   534:        p = aproc_new(&wfile_ops, f->name);
1.1       ratchov   535:        p->u.io.file = f;
1.51      ratchov   536:        p->u.io.partial = 0;
1.1       ratchov   537:        f->wproc = p;
                    538:        return p;
                    539: }
                    540:
                    541: /*
1.51      ratchov   542:  * Drop as much as possible samples from the reader end,
                    543:  * negative values mean ``insert silence''.
                    544:  */
                    545: void
                    546: mix_drop(struct abuf *buf, int extra)
                    547: {
                    548:        unsigned count;
                    549:
                    550:        buf->r.mix.drop += extra;
                    551:        while (buf->r.mix.drop > 0) {
                    552:                count = buf->r.mix.drop;
                    553:                if (count > buf->used)
                    554:                        count = buf->used;
                    555:                if (count == 0) {
                    556: #ifdef DEBUG
                    557:                        if (debug_level >= 4) {
                    558:                                abuf_dbg(buf);
                    559:                                dbg_puts(": drop: no data\n");
                    560:                        }
                    561: #endif
                    562:                        return;
                    563:                }
                    564:                abuf_rdiscard(buf, count);
                    565:                buf->r.mix.drop -= count;
                    566: #ifdef DEBUG
                    567:                if (debug_level >= 4) {
                    568:                        abuf_dbg(buf);
                    569:                        dbg_puts(": dropped ");
                    570:                        dbg_putu(count);
                    571:                        dbg_puts(", to drop = ");
                    572:                        dbg_putu(buf->r.mix.drop);
                    573:                        dbg_puts("\n");
                    574:                }
                    575: #endif
                    576:        }
                    577: }
                    578:
                    579: /*
1.57      ratchov   580:  * Append the necessary amount of silence, in a way
                    581:  * obuf->w.mix.todo doesn't exceed the given value
1.1       ratchov   582:  */
                    583: void
1.57      ratchov   584: mix_bzero(struct abuf *obuf, unsigned maxtodo)
1.1       ratchov   585: {
1.63    ! ratchov   586:        adata_t *odata;
1.57      ratchov   587:        unsigned ocount, todo;
1.1       ratchov   588:
1.57      ratchov   589:        if (obuf->w.mix.todo >= maxtodo)
                    590:                return;
                    591:        todo = maxtodo - obuf->w.mix.todo;
1.63    ! ratchov   592:        odata = (adata_t *)abuf_wgetblk(obuf, &ocount, obuf->w.mix.todo);
1.57      ratchov   593:        if (ocount > todo)
                    594:                ocount = todo;
1.51      ratchov   595:        if (ocount == 0)
                    596:                return;
                    597:        memset(odata, 0, ocount * obuf->bpf);
                    598:        obuf->w.mix.todo += ocount;
1.42      ratchov   599: #ifdef DEBUG
                    600:        if (debug_level >= 4) {
                    601:                abuf_dbg(obuf);
                    602:                dbg_puts(": bzero(");
1.51      ratchov   603:                dbg_putu(obuf->w.mix.todo);
1.42      ratchov   604:                dbg_puts(")\n");
                    605:        }
                    606: #endif
1.1       ratchov   607: }
                    608:
                    609: /*
                    610:  * Mix an input block over an output block.
                    611:  */
1.51      ratchov   612: unsigned
1.1       ratchov   613: mix_badd(struct abuf *ibuf, struct abuf *obuf)
                    614: {
1.63    ! ratchov   615:        adata_t *idata, *odata;
1.52      ratchov   616:        unsigned cmin, cmax;
                    617:        unsigned i, j, cc, istart, inext, onext, ostart;
1.51      ratchov   618:        unsigned scount, icount, ocount;
1.23      ratchov   619:        int vol;
1.1       ratchov   620:
1.42      ratchov   621: #ifdef DEBUG
                    622:        if (debug_level >= 4) {
                    623:                abuf_dbg(ibuf);
1.43      ratchov   624:                dbg_puts(": badd: done = ");
                    625:                dbg_putu(ibuf->r.mix.done);
                    626:                dbg_puts("/");
1.42      ratchov   627:                dbg_putu(obuf->w.mix.todo);
1.51      ratchov   628:                dbg_puts(", drop = ");
                    629:                dbg_puti(ibuf->r.mix.drop);
1.42      ratchov   630:                dbg_puts("\n");
                    631:        }
                    632: #endif
1.28      ratchov   633:        /*
1.51      ratchov   634:         * Insert silence for xrun correction
                    635:         */
1.57      ratchov   636:        while (ibuf->r.mix.drop < 0) {
1.51      ratchov   637:                icount = -ibuf->r.mix.drop;
1.57      ratchov   638:                mix_bzero(obuf, ibuf->r.mix.done + icount);
                    639:                ocount = obuf->w.mix.todo - ibuf->r.mix.done;
                    640:                if (ocount == 0)
                    641:                        return 0;
1.51      ratchov   642:                scount = (icount < ocount) ? icount : ocount;
                    643:                ibuf->r.mix.done += scount;
                    644:                ibuf->r.mix.drop += scount;
                    645:        }
                    646:
                    647:        /*
1.50      ratchov   648:         * Calculate the maximum we can read.
1.49      ratchov   649:         */
1.63    ! ratchov   650:        idata = (adata_t *)abuf_rgetblk(ibuf, &icount, 0);
1.50      ratchov   651:        if (icount == 0)
1.51      ratchov   652:                return 0;
1.28      ratchov   653:
                    654:        /*
1.33      ratchov   655:         * Calculate the maximum we can write.
1.28      ratchov   656:         */
1.63    ! ratchov   657:        odata = (adata_t *)abuf_wgetblk(obuf, &ocount, ibuf->r.mix.done);
1.1       ratchov   658:        if (ocount == 0)
1.51      ratchov   659:                return 0;
1.1       ratchov   660:
1.57      ratchov   661:        scount = (icount < ocount) ? icount : ocount;
                    662:        mix_bzero(obuf, scount + ibuf->r.mix.done);
                    663:
1.63    ! ratchov   664:        vol = ADATA_MUL(ibuf->r.mix.weight, ibuf->r.mix.vol);
1.52      ratchov   665:        cmin = obuf->cmin > ibuf->cmin ? obuf->cmin : ibuf->cmin;
                    666:        cmax = obuf->cmax < ibuf->cmax ? obuf->cmax : ibuf->cmax;
                    667:        ostart = cmin - obuf->cmin;
                    668:        istart = cmin - ibuf->cmin;
                    669:        onext = obuf->cmax - cmax + ostart;
                    670:        inext = ibuf->cmax - cmax + istart;
                    671:        cc = cmax - cmin + 1;
1.13      ratchov   672:        odata += ostart;
1.52      ratchov   673:        idata += istart;
1.13      ratchov   674:        for (i = scount; i > 0; i--) {
1.52      ratchov   675:                for (j = cc; j > 0; j--) {
1.63    ! ratchov   676:                        *odata += ADATA_MUL(*idata, vol);
1.13      ratchov   677:                        idata++;
                    678:                        odata++;
                    679:                }
                    680:                odata += onext;
1.52      ratchov   681:                idata += inext;
1.13      ratchov   682:        }
1.51      ratchov   683:        abuf_rdiscard(ibuf, scount);
                    684:        ibuf->r.mix.done += scount;
1.1       ratchov   685:
1.42      ratchov   686: #ifdef DEBUG
                    687:        if (debug_level >= 4) {
                    688:                abuf_dbg(ibuf);
                    689:                dbg_puts(": badd: done = ");
                    690:                dbg_putu(ibuf->r.mix.done);
                    691:                dbg_puts("/");
                    692:                dbg_putu(obuf->w.mix.todo);
                    693:                dbg_puts("\n");
                    694:        }
                    695: #endif
1.51      ratchov   696:        return scount;
1.1       ratchov   697: }
                    698:
1.28      ratchov   699: /*
                    700:  * Handle buffer underrun, return 0 if stream died.
                    701:  */
                    702: int
1.51      ratchov   703: mix_xrun(struct aproc *p, struct abuf *i)
1.28      ratchov   704: {
1.54      ratchov   705:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.51      ratchov   706:        unsigned fdrop, remain;
1.31      ratchov   707:
1.36      ratchov   708:        if (i->r.mix.done > 0)
1.28      ratchov   709:                return 1;
1.36      ratchov   710:        if (i->r.mix.xrun == XRUN_ERROR) {
1.28      ratchov   711:                abuf_hup(i);
                    712:                return 0;
                    713:        }
1.51      ratchov   714:        fdrop = obuf->w.mix.todo;
1.42      ratchov   715: #ifdef DEBUG
                    716:        if (debug_level >= 3) {
                    717:                abuf_dbg(i);
                    718:                dbg_puts(": underrun, dropping ");
                    719:                dbg_putu(fdrop);
                    720:                dbg_puts(" + ");
1.51      ratchov   721:                dbg_putu(i->r.mix.drop);
1.42      ratchov   722:                dbg_puts("\n");
                    723:        }
                    724: #endif
1.51      ratchov   725:        i->r.mix.done += fdrop;
1.36      ratchov   726:        if (i->r.mix.xrun == XRUN_SYNC)
1.51      ratchov   727:                mix_drop(i, fdrop);
1.28      ratchov   728:        else {
1.51      ratchov   729:                remain = fdrop % p->u.mix.round;
                    730:                if (remain)
                    731:                        remain = p->u.mix.round - remain;
                    732:                mix_drop(i, -(int)remain);
                    733:                fdrop += remain;
                    734: #ifdef DEBUG
                    735:                if (debug_level >= 3) {
                    736:                        abuf_dbg(i);
                    737:                        dbg_puts(": underrun, adding ");
                    738:                        dbg_putu(remain);
                    739:                        dbg_puts("\n");
                    740:                }
                    741: #endif
1.28      ratchov   742:                abuf_opos(i, -(int)fdrop);
                    743:                if (i->duplex) {
1.42      ratchov   744: #ifdef DEBUG
                    745:                        if (debug_level >= 3) {
                    746:                                abuf_dbg(i->duplex);
                    747:                                dbg_puts(": full-duplex resync\n");
                    748:                        }
                    749: #endif
1.51      ratchov   750:                        sub_silence(i->duplex, -(int)fdrop);
1.28      ratchov   751:                        abuf_ipos(i->duplex, -(int)fdrop);
                    752:                }
                    753:        }
                    754:        return 1;
                    755: }
                    756:
1.1       ratchov   757: int
                    758: mix_in(struct aproc *p, struct abuf *ibuf)
                    759: {
1.54      ratchov   760:        struct abuf *i, *inext, *obuf = LIST_FIRST(&p->outs);
1.28      ratchov   761:        unsigned odone;
1.51      ratchov   762:        unsigned maxwrite;
                    763:        unsigned scount;
1.1       ratchov   764:
1.42      ratchov   765: #ifdef DEBUG
                    766:        if (debug_level >= 4) {
                    767:                aproc_dbg(p);
                    768:                dbg_puts(": used = ");
                    769:                dbg_putu(ibuf->used);
                    770:                dbg_puts("/");
                    771:                dbg_putu(ibuf->len);
1.43      ratchov   772:                dbg_puts(", done = ");
                    773:                dbg_putu(ibuf->r.mix.done);
                    774:                dbg_puts("/");
1.42      ratchov   775:                dbg_putu(obuf->w.mix.todo);
                    776:                dbg_puts("\n");
                    777:        }
                    778: #endif
1.51      ratchov   779:        if (!MIX_ROK(ibuf))
1.1       ratchov   780:                return 0;
1.51      ratchov   781:        scount = 0;
1.58      ratchov   782:        odone = obuf->len;
1.54      ratchov   783:        for (i = LIST_FIRST(&p->ins); i != NULL; i = inext) {
1.28      ratchov   784:                inext = LIST_NEXT(i, ient);
1.51      ratchov   785:                if (i->r.mix.drop >= 0 && !abuf_fill(i))
1.28      ratchov   786:                        continue; /* eof */
1.51      ratchov   787:                mix_drop(i, 0);
                    788:                scount += mix_badd(i, obuf);
1.36      ratchov   789:                if (odone > i->r.mix.done)
                    790:                        odone = i->r.mix.done;
1.28      ratchov   791:        }
1.54      ratchov   792:        if (LIST_EMPTY(&p->ins) || scount == 0)
1.28      ratchov   793:                return 0;
1.51      ratchov   794: #ifdef DEBUG
                    795:        if (debug_level >= 4) {
                    796:                aproc_dbg(p);
                    797:                dbg_puts(": maxwrite = ");
                    798:                dbg_putu(p->u.mix.maxlat);
                    799:                dbg_puts(" - ");
                    800:                dbg_putu(p->u.mix.lat);
                    801:                dbg_puts(" = ");
                    802:                dbg_putu(p->u.mix.maxlat - p->u.mix.lat);
                    803:                dbg_puts("\n");
                    804:        }
                    805: #endif
                    806:        maxwrite = p->u.mix.maxlat - p->u.mix.lat;
                    807:        if (maxwrite > 0) {
                    808:                if (odone > maxwrite)
                    809:                        odone = maxwrite;
                    810:                p->u.mix.lat += odone;
1.54      ratchov   811:                LIST_FOREACH(i, &p->ins, ient) {
1.51      ratchov   812:                        i->r.mix.done -= odone;
                    813:                }
                    814:                abuf_wcommit(obuf, odone);
                    815:                obuf->w.mix.todo -= odone;
                    816:                if (APROC_OK(p->u.mix.mon))
                    817:                        mon_snoop(p->u.mix.mon, obuf, obuf->used - odone, odone);
                    818:                if (!abuf_flush(obuf))
                    819:                        return 0; /* hup */
1.1       ratchov   820:        }
                    821:        return 1;
                    822: }
                    823:
                    824: int
                    825: mix_out(struct aproc *p, struct abuf *obuf)
                    826: {
                    827:        struct abuf *i, *inext;
1.28      ratchov   828:        unsigned odone;
1.51      ratchov   829:        unsigned maxwrite;
                    830:        unsigned scount;
1.1       ratchov   831:
1.42      ratchov   832: #ifdef DEBUG
                    833:        if (debug_level >= 4) {
                    834:                aproc_dbg(p);
                    835:                dbg_puts(": used = ");
                    836:                dbg_putu(obuf->used);
                    837:                dbg_puts("/");
                    838:                dbg_putu(obuf->len);
                    839:                dbg_puts(", todo = ");
                    840:                dbg_putu(obuf->w.mix.todo);
                    841:                dbg_puts("/");
                    842:                dbg_putu(obuf->len);
                    843:                dbg_puts("\n");
                    844:        }
                    845: #endif
1.12      ratchov   846:        if (!ABUF_WOK(obuf))
1.31      ratchov   847:                return 0;
1.51      ratchov   848: #ifdef DEBUG
                    849:        if (debug_level >= 4) {
                    850:                aproc_dbg(p);
                    851:                dbg_puts(": maxwrite = ");
                    852:                dbg_putu(p->u.mix.maxlat);
                    853:                dbg_puts(" - ");
                    854:                dbg_putu(p->u.mix.lat);
                    855:                dbg_puts(" = ");
                    856:                dbg_putu(p->u.mix.maxlat - p->u.mix.lat);
                    857:                dbg_puts("\n");
                    858:        }
                    859: #endif
                    860:        maxwrite = p->u.mix.maxlat - p->u.mix.lat;
1.57      ratchov   861:        if (maxwrite > obuf->w.mix.todo) {
                    862:                if ((p->flags & (APROC_QUIT | APROC_DROP)) == APROC_DROP)
                    863:                        mix_bzero(obuf, maxwrite);
                    864:        }
1.51      ratchov   865:        scount = 0;
1.28      ratchov   866:        odone = obuf->len;
1.54      ratchov   867:        for (i = LIST_FIRST(&p->ins); i != NULL; i = inext) {
1.1       ratchov   868:                inext = LIST_NEXT(i, ient);
1.51      ratchov   869:                if (i->r.mix.drop >= 0 && !abuf_fill(i))
1.28      ratchov   870:                        continue; /* eof */
1.51      ratchov   871:                mix_drop(i, 0);
                    872:                if (maxwrite > 0 && !MIX_ROK(i)) {
1.38      ratchov   873:                        if (p->flags & APROC_DROP) {
1.51      ratchov   874:                                if (!mix_xrun(p, i))
1.5       ratchov   875:                                        continue;
                    876:                        }
1.3       ratchov   877:                } else
1.51      ratchov   878:                        scount += mix_badd(i, obuf);
1.36      ratchov   879:                if (odone > i->r.mix.done)
                    880:                        odone = i->r.mix.done;
1.28      ratchov   881:        }
1.57      ratchov   882:        if (LIST_EMPTY(&p->ins) && obuf->w.mix.todo == 0) {
1.38      ratchov   883:                if (p->flags & APROC_QUIT) {
1.28      ratchov   884:                        aproc_del(p);
                    885:                        return 0;
                    886:                }
1.38      ratchov   887:                if (!(p->flags & APROC_DROP))
1.28      ratchov   888:                        return 0;
1.57      ratchov   889:        }
                    890:        if (odone > obuf->w.mix.todo)
1.36      ratchov   891:                odone = obuf->w.mix.todo;
1.57      ratchov   892:        if (odone > maxwrite)
                    893:                odone = maxwrite;
                    894:        if (odone > 0) {
1.51      ratchov   895:                p->u.mix.lat += odone;
1.54      ratchov   896:                LIST_FOREACH(i, &p->ins, ient) {
1.51      ratchov   897:                        i->r.mix.done -= odone;
                    898:                }
                    899:                abuf_wcommit(obuf, odone);
                    900:                obuf->w.mix.todo -= odone;
                    901:                if (APROC_OK(p->u.mix.mon))
                    902:                        mon_snoop(p->u.mix.mon, obuf, obuf->used - odone, odone);
1.49      ratchov   903:        }
1.55      ratchov   904:        if (LIST_EMPTY(&p->ins))
                    905:                p->u.mix.idle += odone;
1.51      ratchov   906:        if (scount == 0)
1.50      ratchov   907:                return 0;
1.1       ratchov   908:        return 1;
                    909: }
                    910:
                    911: void
                    912: mix_eof(struct aproc *p, struct abuf *ibuf)
                    913: {
1.62      ratchov   914:        struct abuf *i, *obuf = LIST_FIRST(&p->outs);
1.28      ratchov   915:        unsigned odone;
1.1       ratchov   916:
1.12      ratchov   917:        mix_setmaster(p);
                    918:
1.28      ratchov   919:        if (!aproc_inuse(p)) {
1.42      ratchov   920: #ifdef DEBUG
                    921:                if (debug_level >= 3) {
                    922:                        aproc_dbg(p);
                    923:                        dbg_puts(": running other streams\n");
                    924:                }
                    925: #endif
1.28      ratchov   926:                /*
1.33      ratchov   927:                 * Find a blocked input.
1.28      ratchov   928:                 */
                    929:                odone = obuf->len;
1.62      ratchov   930:                LIST_FOREACH(i, &p->ins, ient) {
                    931:                        /*
                    932:                         * abuf_fill() may trigger mix_eof(), do the job
                    933:                         * and possibly reorder the list
                    934:                         */
1.51      ratchov   935:                        if (!abuf_fill(i))
1.62      ratchov   936:                                return;
1.51      ratchov   937:                        if (MIX_ROK(i) && i->r.mix.done < obuf->w.mix.todo) {
1.28      ratchov   938:                                abuf_run(i);
                    939:                                return;
                    940:                        }
1.36      ratchov   941:                        if (odone > i->r.mix.done)
                    942:                                odone = i->r.mix.done;
1.28      ratchov   943:                }
                    944:                /*
1.33      ratchov   945:                 * No blocked inputs. Check if output is blocked.
1.28      ratchov   946:                 */
1.54      ratchov   947:                if (LIST_EMPTY(&p->ins) || odone == obuf->w.mix.todo)
1.28      ratchov   948:                        abuf_run(obuf);
                    949:        }
1.1       ratchov   950: }
                    951:
                    952: void
                    953: mix_hup(struct aproc *p, struct abuf *obuf)
                    954: {
                    955:        aproc_del(p);
                    956: }
                    957:
                    958: void
                    959: mix_newin(struct aproc *p, struct abuf *ibuf)
                    960: {
1.22      ratchov   961:        p->u.mix.idle = 0;
1.36      ratchov   962:        ibuf->r.mix.done = 0;
                    963:        ibuf->r.mix.vol = ADATA_UNIT;
                    964:        ibuf->r.mix.weight = ADATA_UNIT;
                    965:        ibuf->r.mix.maxweight = ADATA_UNIT;
                    966:        ibuf->r.mix.xrun = XRUN_IGNORE;
1.51      ratchov   967:        ibuf->r.mix.drop = 0;
1.1       ratchov   968: }
                    969:
                    970: void
                    971: mix_newout(struct aproc *p, struct abuf *obuf)
                    972: {
1.42      ratchov   973: #ifdef DEBUG
                    974:        if (debug_level >= 3) {
                    975:                aproc_dbg(p);
1.61      ratchov   976:                dbg_puts(": newout, will use ");
1.51      ratchov   977:                dbg_putu(obuf->len);
1.61      ratchov   978:                dbg_puts(" fr\n");
1.42      ratchov   979:        }
                    980: #endif
1.36      ratchov   981:        obuf->w.mix.todo = 0;
1.1       ratchov   982: }
                    983:
1.12      ratchov   984: void
                    985: mix_opos(struct aproc *p, struct abuf *obuf, int delta)
                    986: {
1.51      ratchov   987:        p->u.mix.lat -= delta;
1.42      ratchov   988: #ifdef DEBUG
                    989:        if (debug_level >= 4) {
                    990:                aproc_dbg(p);
                    991:                dbg_puts(": opos: lat = ");
                    992:                dbg_puti(p->u.mix.lat);
                    993:                dbg_puts("/");
                    994:                dbg_puti(p->u.mix.maxlat);
1.51      ratchov   995:                dbg_puts("\n");
1.42      ratchov   996:        }
                    997: #endif
1.51      ratchov   998:        if (APROC_OK(p->u.mix.ctl))
1.38      ratchov   999:                ctl_ontick(p->u.mix.ctl, delta);
1.12      ratchov  1000:        aproc_opos(p, obuf, delta);
1.51      ratchov  1001:        if (APROC_OK(p->u.mix.mon))
                   1002:                p->u.mix.mon->ops->ipos(p->u.mix.mon, NULL, delta);
1.12      ratchov  1003: }
                   1004:
1.1       ratchov  1005: struct aproc_ops mix_ops = {
1.12      ratchov  1006:        "mix",
                   1007:        mix_in,
                   1008:        mix_out,
                   1009:        mix_eof,
                   1010:        mix_hup,
                   1011:        mix_newin,
                   1012:        mix_newout,
                   1013:        aproc_ipos,
                   1014:        mix_opos,
                   1015:        NULL
1.1       ratchov  1016: };
                   1017:
                   1018: struct aproc *
1.57      ratchov  1019: mix_new(char *name, int maxlat, unsigned round)
1.1       ratchov  1020: {
                   1021:        struct aproc *p;
                   1022:
1.12      ratchov  1023:        p = aproc_new(&mix_ops, name);
1.22      ratchov  1024:        p->u.mix.idle = 0;
1.12      ratchov  1025:        p->u.mix.lat = 0;
1.51      ratchov  1026:        p->u.mix.round = round;
1.12      ratchov  1027:        p->u.mix.maxlat = maxlat;
1.57      ratchov  1028:        p->u.mix.ctl = NULL;
                   1029:        p->u.mix.mon = NULL;
1.1       ratchov  1030:        return p;
1.10      ratchov  1031: }
                   1032:
                   1033: /*
1.33      ratchov  1034:  * Normalize input levels.
1.10      ratchov  1035:  */
                   1036: void
                   1037: mix_setmaster(struct aproc *p)
                   1038: {
                   1039:        unsigned n;
1.40      ratchov  1040:        struct abuf *i, *j;
1.24      ratchov  1041:        int weight;
1.10      ratchov  1042:
1.40      ratchov  1043:        /*
                   1044:         * count the number of inputs. If a set of inputs
                   1045:         * uses channels that have no intersection, they are
                   1046:         * counted only once because they don't need to
                   1047:         * share their volume
1.60      ratchov  1048:         *
                   1049:         * XXX: this is wrong, this is not optimal if we have two
                   1050:         *      buckets of N and N' clients, in which case we should
                   1051:         *      get 1/N and 1/N' respectively
1.40      ratchov  1052:         */
1.10      ratchov  1053:        n = 0;
1.54      ratchov  1054:        LIST_FOREACH(i, &p->ins, ient) {
1.40      ratchov  1055:                j = LIST_NEXT(i, ient);
                   1056:                for (;;) {
                   1057:                        if (j == NULL) {
                   1058:                                n++;
                   1059:                                break;
                   1060:                        }
                   1061:                        if (i->cmin > j->cmax || i->cmax < j->cmin)
                   1062:                                break;
                   1063:                        j = LIST_NEXT(j, ient);
                   1064:                }
1.24      ratchov  1065:        }
1.54      ratchov  1066:        LIST_FOREACH(i, &p->ins, ient) {
1.24      ratchov  1067:                weight = ADATA_UNIT / n;
1.40      ratchov  1068:                if (weight > i->r.mix.maxweight)
                   1069:                        weight = i->r.mix.maxweight;
                   1070:                i->r.mix.weight = weight;
1.42      ratchov  1071: #ifdef DEBUG
                   1072:                if (debug_level >= 3) {
                   1073:                        abuf_dbg(i);
                   1074:                        dbg_puts(": setmaster: ");
                   1075:                        dbg_puti(i->r.mix.weight);
                   1076:                        dbg_puts("/");
                   1077:                        dbg_puti(i->r.mix.maxweight);
                   1078:                        dbg_puts("\n");
                   1079:                }
                   1080: #endif
1.24      ratchov  1081:        }
1.1       ratchov  1082: }
                   1083:
1.22      ratchov  1084: void
                   1085: mix_clear(struct aproc *p)
                   1086: {
1.54      ratchov  1087:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.22      ratchov  1088:
                   1089:        p->u.mix.lat = 0;
1.36      ratchov  1090:        obuf->w.mix.todo = 0;
1.22      ratchov  1091: }
                   1092:
1.1       ratchov  1093: /*
1.57      ratchov  1094:  * Gracefully terminate the mixer: raise the APROC_QUIT flag
                   1095:  * and let the rest of the code do the job. If there are neither
                   1096:  * inputs nor uncommited data, then terminate right away
                   1097:  */
                   1098: void
                   1099: mix_quit(struct aproc *p)
                   1100: {
                   1101:        struct abuf *obuf = LIST_FIRST(&p->outs);
                   1102:
                   1103:        p->flags |= APROC_QUIT;
                   1104:
                   1105:        /*
                   1106:         * eof the last input will trigger aproc_del()
                   1107:         */
                   1108:        if (!LIST_EMPTY(&p->ins) || obuf->w.mix.todo > 0)
                   1109:                return;
                   1110:        aproc_del(p);
                   1111: }
                   1112:
                   1113: /*
1.51      ratchov  1114:  * Append as much as possible silence on the writer end
                   1115:  */
                   1116: void
                   1117: sub_silence(struct abuf *buf, int extra)
                   1118: {
                   1119:        unsigned char *data;
                   1120:        unsigned count;
                   1121:
                   1122:        buf->w.sub.silence += extra;
                   1123:        if (buf->w.sub.silence > 0) {
                   1124:                data = abuf_wgetblk(buf, &count, 0);
                   1125:                if (count >= buf->w.sub.silence)
                   1126:                        count = buf->w.sub.silence;
                   1127:                if (count == 0) {
                   1128: #ifdef DEBUG
                   1129:                        if (debug_level >= 4) {
                   1130:                                abuf_dbg(buf);
                   1131:                                dbg_puts(": no space for silence\n");
                   1132:                        }
                   1133: #endif
                   1134:                        return;
                   1135:                }
                   1136:                memset(data, 0, count * buf->bpf);
                   1137:                abuf_wcommit(buf, count);
                   1138:                buf->w.sub.silence -= count;
                   1139: #ifdef DEBUG
                   1140:                if (debug_level >= 4) {
                   1141:                        abuf_dbg(buf);
                   1142:                        dbg_puts(": appended ");
                   1143:                        dbg_putu(count);
                   1144:                        dbg_puts(", remaining silence = ");
                   1145:                        dbg_putu(buf->w.sub.silence);
                   1146:                        dbg_puts("\n");
                   1147:                }
                   1148: #endif
                   1149:        }
                   1150: }
                   1151:
                   1152: /*
1.1       ratchov  1153:  * Copy data from ibuf to obuf.
                   1154:  */
                   1155: void
                   1156: sub_bcopy(struct abuf *ibuf, struct abuf *obuf)
                   1157: {
1.63    ! ratchov  1158:        adata_t *idata, *odata;
1.52      ratchov  1159:        unsigned cmin, cmax;
                   1160:        unsigned i, j, cc, istart, inext, onext, ostart;
1.1       ratchov  1161:        unsigned icount, ocount, scount;
                   1162:
1.51      ratchov  1163:        /*
                   1164:         * Drop samples for xrun correction
                   1165:         */
                   1166:        if (obuf->w.sub.silence < 0) {
                   1167:                scount = -obuf->w.sub.silence;
                   1168:                if (scount > ibuf->used)
                   1169:                        scount = ibuf->used;
                   1170:                obuf->w.sub.done += scount;
                   1171:                obuf->w.sub.silence += scount;
                   1172:        }
                   1173:
1.63    ! ratchov  1174:        idata = (adata_t *)abuf_rgetblk(ibuf, &icount, obuf->w.sub.done);
1.1       ratchov  1175:        if (icount == 0)
                   1176:                return;
1.63    ! ratchov  1177:        odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
1.1       ratchov  1178:        if (ocount == 0)
                   1179:                return;
1.52      ratchov  1180:        cmin = obuf->cmin > ibuf->cmin ? obuf->cmin : ibuf->cmin;
                   1181:        cmax = obuf->cmax < ibuf->cmax ? obuf->cmax : ibuf->cmax;
                   1182:        ostart = cmin - obuf->cmin;
                   1183:        istart = cmin - ibuf->cmin;
                   1184:        onext = obuf->cmax - cmax;
                   1185:        inext = ibuf->cmax - cmax + istart;
                   1186:        cc = cmax - cmin + 1;
                   1187:        idata += istart;
1.1       ratchov  1188:        scount = (icount < ocount) ? icount : ocount;
1.13      ratchov  1189:        for (i = scount; i > 0; i--) {
1.52      ratchov  1190:                for (j = ostart; j > 0; j--)
                   1191:                        *odata++ = 0x1111;
                   1192:                for (j = cc; j > 0; j--) {
1.13      ratchov  1193:                        *odata = *idata;
                   1194:                        odata++;
                   1195:                        idata++;
                   1196:                }
1.52      ratchov  1197:                for (j = onext; j > 0; j--)
                   1198:                        *odata++ = 0x2222;
1.13      ratchov  1199:                idata += inext;
                   1200:        }
1.51      ratchov  1201:        abuf_wcommit(obuf, scount);
                   1202:        obuf->w.sub.done += scount;
1.42      ratchov  1203: #ifdef DEBUG
                   1204:        if (debug_level >= 4) {
                   1205:                abuf_dbg(obuf);
                   1206:                dbg_puts(": bcopy ");
                   1207:                dbg_putu(scount);
1.51      ratchov  1208:                dbg_puts("\n");
1.42      ratchov  1209:        }
                   1210: #endif
1.1       ratchov  1211: }
                   1212:
1.28      ratchov  1213: /*
1.33      ratchov  1214:  * Handle buffer overruns. Return 0 if the stream died.
1.28      ratchov  1215:  */
                   1216: int
1.51      ratchov  1217: sub_xrun(struct aproc *p, struct abuf *i)
1.28      ratchov  1218: {
1.54      ratchov  1219:        struct abuf *ibuf = LIST_FIRST(&p->ins);
1.51      ratchov  1220:        unsigned fdrop, remain;
1.28      ratchov  1221:
1.36      ratchov  1222:        if (i->w.sub.done > 0)
1.28      ratchov  1223:                return 1;
1.36      ratchov  1224:        if (i->w.sub.xrun == XRUN_ERROR) {
1.28      ratchov  1225:                abuf_eof(i);
                   1226:                return 0;
                   1227:        }
1.51      ratchov  1228:        fdrop = ibuf->used;
1.42      ratchov  1229: #ifdef DEBUG
                   1230:        if (debug_level >= 3) {
                   1231:                abuf_dbg(i);
                   1232:                dbg_puts(": overrun, silence ");
                   1233:                dbg_putu(fdrop);
                   1234:                dbg_puts(" + ");
1.51      ratchov  1235:                dbg_putu(i->w.sub.silence);
1.42      ratchov  1236:                dbg_puts("\n");
                   1237:        }
                   1238: #endif
1.51      ratchov  1239:        i->w.sub.done += fdrop;
1.36      ratchov  1240:        if (i->w.sub.xrun == XRUN_SYNC)
1.51      ratchov  1241:                sub_silence(i, fdrop);
1.28      ratchov  1242:        else {
1.51      ratchov  1243:                remain = fdrop % p->u.sub.round;
                   1244:                if (remain)
                   1245:                        remain = p->u.sub.round - remain;
                   1246:                sub_silence(i, -(int)remain);
                   1247:                fdrop += remain;
                   1248: #ifdef DEBUG
                   1249:                if (debug_level >= 3) {
                   1250:                        abuf_dbg(i);
                   1251:                        dbg_puts(": overrun, adding ");
                   1252:                        dbg_putu(remain);
                   1253:                        dbg_puts("\n");
                   1254:                }
                   1255: #endif
                   1256:
1.28      ratchov  1257:                abuf_ipos(i, -(int)fdrop);
                   1258:                if (i->duplex) {
1.42      ratchov  1259: #ifdef DEBUG
                   1260:                        if (debug_level >= 3) {
                   1261:                                abuf_dbg(i->duplex);
                   1262:                                dbg_puts(": full-duplex resync\n");
                   1263:                        }
                   1264: #endif
1.51      ratchov  1265:                        mix_drop(i->duplex, -(int)fdrop);
1.28      ratchov  1266:                        abuf_opos(i->duplex, -(int)fdrop);
                   1267:                }
                   1268:        }
                   1269:        return 1;
                   1270: }
                   1271:
1.1       ratchov  1272: int
                   1273: sub_in(struct aproc *p, struct abuf *ibuf)
                   1274: {
                   1275:        struct abuf *i, *inext;
1.28      ratchov  1276:        unsigned idone;
1.31      ratchov  1277:
1.12      ratchov  1278:        if (!ABUF_ROK(ibuf))
                   1279:                return 0;
1.28      ratchov  1280:        idone = ibuf->len;
1.54      ratchov  1281:        for (i = LIST_FIRST(&p->outs); i != NULL; i = inext) {
1.1       ratchov  1282:                inext = LIST_NEXT(i, oent);
1.51      ratchov  1283:                sub_silence(i, 0);
                   1284:                if (!SUB_WOK(i)) {
1.38      ratchov  1285:                        if (p->flags & APROC_DROP) {
1.51      ratchov  1286:                                if (!sub_xrun(p, i))
1.5       ratchov  1287:                                        continue;
                   1288:                        }
1.12      ratchov  1289:                } else
1.1       ratchov  1290:                        sub_bcopy(ibuf, i);
1.36      ratchov  1291:                if (idone > i->w.sub.done)
                   1292:                        idone = i->w.sub.done;
1.12      ratchov  1293:                if (!abuf_flush(i))
                   1294:                        continue;
1.1       ratchov  1295:        }
1.54      ratchov  1296:        if (LIST_EMPTY(&p->outs)) {
1.38      ratchov  1297:                if (p->flags & APROC_QUIT) {
1.28      ratchov  1298:                        aproc_del(p);
                   1299:                        return 0;
                   1300:                }
1.38      ratchov  1301:                if (!(p->flags & APROC_DROP))
1.28      ratchov  1302:                        return 0;
                   1303:                idone = ibuf->used;
1.51      ratchov  1304:                p->u.sub.idle += idone;
1.28      ratchov  1305:        }
                   1306:        if (idone == 0)
                   1307:                return 0;
1.54      ratchov  1308:        LIST_FOREACH(i, &p->outs, oent) {
1.36      ratchov  1309:                i->w.sub.done -= idone;
1.1       ratchov  1310:        }
1.28      ratchov  1311:        abuf_rdiscard(ibuf, idone);
1.51      ratchov  1312:        abuf_opos(ibuf, idone);
                   1313:        p->u.sub.lat -= idone;
1.12      ratchov  1314:        return 1;
1.1       ratchov  1315: }
                   1316:
                   1317: int
                   1318: sub_out(struct aproc *p, struct abuf *obuf)
                   1319: {
1.54      ratchov  1320:        struct abuf *ibuf = LIST_FIRST(&p->ins);
1.1       ratchov  1321:        struct abuf *i, *inext;
1.28      ratchov  1322:        unsigned idone;
1.1       ratchov  1323:
1.51      ratchov  1324:        if (!SUB_WOK(obuf))
1.12      ratchov  1325:                return 0;
1.28      ratchov  1326:        if (!abuf_fill(ibuf))
                   1327:                return 0; /* eof */
                   1328:        idone = ibuf->len;
1.54      ratchov  1329:        for (i = LIST_FIRST(&p->outs); i != NULL; i = inext) {
1.12      ratchov  1330:                inext = LIST_NEXT(i, oent);
1.51      ratchov  1331:                sub_silence(i, 0);
1.28      ratchov  1332:                sub_bcopy(ibuf, i);
1.36      ratchov  1333:                if (idone > i->w.sub.done)
                   1334:                        idone = i->w.sub.done;
1.12      ratchov  1335:                if (!abuf_flush(i))
                   1336:                        continue;
1.1       ratchov  1337:        }
1.54      ratchov  1338:        if (LIST_EMPTY(&p->outs) || idone == 0)
1.28      ratchov  1339:                return 0;
1.54      ratchov  1340:        LIST_FOREACH(i, &p->outs, oent) {
1.36      ratchov  1341:                i->w.sub.done -= idone;
1.1       ratchov  1342:        }
1.28      ratchov  1343:        abuf_rdiscard(ibuf, idone);
1.51      ratchov  1344:        abuf_opos(ibuf, idone);
                   1345:        p->u.sub.lat -= idone;
1.1       ratchov  1346:        return 1;
                   1347: }
                   1348:
                   1349: void
                   1350: sub_eof(struct aproc *p, struct abuf *ibuf)
                   1351: {
1.8       ratchov  1352:        aproc_del(p);
1.1       ratchov  1353: }
                   1354:
                   1355: void
                   1356: sub_hup(struct aproc *p, struct abuf *obuf)
                   1357: {
1.62      ratchov  1358:        struct abuf *i, *ibuf = LIST_FIRST(&p->ins);
1.28      ratchov  1359:        unsigned idone;
1.1       ratchov  1360:
1.28      ratchov  1361:        if (!aproc_inuse(p)) {
1.42      ratchov  1362: #ifdef DEBUG
                   1363:                if (debug_level >= 3) {
                   1364:                        aproc_dbg(p);
                   1365:                        dbg_puts(": running other streams\n");
                   1366:                }
                   1367: #endif
1.28      ratchov  1368:                /*
1.33      ratchov  1369:                 * Find a blocked output.
1.28      ratchov  1370:                 */
                   1371:                idone = ibuf->len;
1.62      ratchov  1372:                LIST_FOREACH(i, &p->outs, oent) {
                   1373:                        /*
                   1374:                         * abuf_flush() may trigger sub_hup(), do the job
                   1375:                         * and possibly reorder the list
                   1376:                         */
1.51      ratchov  1377:                        if (!abuf_flush(i))
1.62      ratchov  1378:                                return;
1.51      ratchov  1379:                        if (SUB_WOK(i) && i->w.sub.done < ibuf->used) {
1.28      ratchov  1380:                                abuf_run(i);
                   1381:                                return;
                   1382:                        }
1.36      ratchov  1383:                        if (idone > i->w.sub.done)
                   1384:                                idone = i->w.sub.done;
1.28      ratchov  1385:                }
                   1386:                /*
1.33      ratchov  1387:                 * No blocked outputs. Check if input is blocked.
1.28      ratchov  1388:                 */
1.54      ratchov  1389:                if (LIST_EMPTY(&p->outs) || idone == ibuf->used)
1.28      ratchov  1390:                        abuf_run(ibuf);
                   1391:        }
1.1       ratchov  1392: }
                   1393:
                   1394: void
                   1395: sub_newout(struct aproc *p, struct abuf *obuf)
                   1396: {
1.22      ratchov  1397:        p->u.sub.idle = 0;
1.36      ratchov  1398:        obuf->w.sub.done = 0;
                   1399:        obuf->w.sub.xrun = XRUN_IGNORE;
1.51      ratchov  1400:        obuf->w.sub.silence = 0;
1.1       ratchov  1401: }
                   1402:
1.12      ratchov  1403: void
                   1404: sub_ipos(struct aproc *p, struct abuf *ibuf, int delta)
                   1405: {
                   1406:        p->u.sub.lat += delta;
1.42      ratchov  1407: #ifdef DEBUG
                   1408:        if (debug_level >= 4) {
                   1409:                aproc_dbg(p);
                   1410:                dbg_puts(": ipos: lat = ");
                   1411:                dbg_puti(p->u.sub.lat);
                   1412:                dbg_puts("/");
                   1413:                dbg_puti(p->u.sub.maxlat);
1.51      ratchov  1414:                dbg_puts("\n");
1.42      ratchov  1415:        }
                   1416: #endif
1.51      ratchov  1417:        if (APROC_OK(p->u.sub.ctl))
1.38      ratchov  1418:                ctl_ontick(p->u.sub.ctl, delta);
1.12      ratchov  1419:        aproc_ipos(p, ibuf, delta);
                   1420: }
                   1421:
1.1       ratchov  1422: struct aproc_ops sub_ops = {
1.12      ratchov  1423:        "sub",
                   1424:        sub_in,
                   1425:        sub_out,
                   1426:        sub_eof,
                   1427:        sub_hup,
                   1428:        NULL,
                   1429:        sub_newout,
                   1430:        sub_ipos,
                   1431:        aproc_opos,
                   1432:        NULL
1.1       ratchov  1433: };
                   1434:
                   1435: struct aproc *
1.57      ratchov  1436: sub_new(char *name, int maxlat, unsigned round)
1.1       ratchov  1437: {
                   1438:        struct aproc *p;
                   1439:
1.12      ratchov  1440:        p = aproc_new(&sub_ops, name);
1.22      ratchov  1441:        p->u.sub.idle = 0;
1.12      ratchov  1442:        p->u.sub.lat = 0;
1.51      ratchov  1443:        p->u.sub.round = round;
1.12      ratchov  1444:        p->u.sub.maxlat = maxlat;
1.57      ratchov  1445:        p->u.sub.ctl = NULL;
1.1       ratchov  1446:        return p;
1.22      ratchov  1447: }
                   1448:
                   1449: void
                   1450: sub_clear(struct aproc *p)
                   1451: {
1.51      ratchov  1452:        p->u.sub.lat = 0;
1.1       ratchov  1453: }
                   1454:
                   1455: /*
                   1456:  * Convert one block.
                   1457:  */
                   1458: void
1.15      ratchov  1459: resamp_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
                   1460: {
                   1461:        unsigned inch;
1.63    ! ratchov  1462:        adata_t *idata;
1.27      ratchov  1463:        unsigned oblksz;
1.15      ratchov  1464:        unsigned ifr;
                   1465:        unsigned onch;
1.63    ! ratchov  1466:        int s, ds, diff;
        !          1467:        adata_t *odata;
1.27      ratchov  1468:        unsigned iblksz;
1.15      ratchov  1469:        unsigned ofr;
                   1470:        unsigned c;
1.63    ! ratchov  1471:        adata_t *ctxbuf, *ctx;
1.27      ratchov  1472:        unsigned ctx_start;
1.15      ratchov  1473:        unsigned icount, ocount;
                   1474:
                   1475:        /*
                   1476:         * Calculate max frames readable at once from the input buffer.
                   1477:         */
1.63    ! ratchov  1478:        idata = (adata_t *)abuf_rgetblk(ibuf, &icount, 0);
1.51      ratchov  1479:        ifr = icount;
1.15      ratchov  1480:
1.63    ! ratchov  1481:        odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
1.51      ratchov  1482:        ofr = ocount;
1.15      ratchov  1483:
                   1484:        /*
                   1485:         * Partially copy structures into local variables, to avoid
                   1486:         * unnecessary indirections; this also allows the compiler to
                   1487:         * order local variables more "cache-friendly".
                   1488:         */
1.27      ratchov  1489:        diff = p->u.resamp.diff;
1.15      ratchov  1490:        inch = ibuf->cmax - ibuf->cmin + 1;
1.26      ratchov  1491:        iblksz = p->u.resamp.iblksz;
1.15      ratchov  1492:        onch = obuf->cmax - obuf->cmin + 1;
1.26      ratchov  1493:        oblksz = p->u.resamp.oblksz;
1.15      ratchov  1494:        ctxbuf = p->u.resamp.ctx;
1.27      ratchov  1495:        ctx_start = p->u.resamp.ctx_start;
1.15      ratchov  1496:
                   1497:        /*
                   1498:         * Start conversion.
                   1499:         */
1.42      ratchov  1500: #ifdef DEBUG
                   1501:        if (debug_level >= 4) {
                   1502:                aproc_dbg(p);
1.61      ratchov  1503:                dbg_puts(": starting diff = ");
1.46      ratchov  1504:                dbg_puti(diff);
                   1505:                dbg_puts(", ifr = ");
1.42      ratchov  1506:                dbg_putu(ifr);
                   1507:                dbg_puts(", ofr = ");
1.46      ratchov  1508:                dbg_putu(ofr);
1.42      ratchov  1509:                dbg_puts(" fr\n");
                   1510:        }
                   1511: #endif
1.15      ratchov  1512:        for (;;) {
1.27      ratchov  1513:                if (diff < 0) {
1.51      ratchov  1514:                        if (ifr == 0)
                   1515:                                break;
1.27      ratchov  1516:                        ctx_start ^= 1;
                   1517:                        ctx = ctxbuf + ctx_start;
1.15      ratchov  1518:                        for (c = inch; c > 0; c--) {
1.29      ratchov  1519:                                *ctx = *idata++;
1.27      ratchov  1520:                                ctx += RESAMP_NCTX;
1.15      ratchov  1521:                        }
1.27      ratchov  1522:                        diff += oblksz;
1.51      ratchov  1523:                        ifr--;
1.56      ratchov  1524:                } else if (diff > 0) {
1.51      ratchov  1525:                        if (ofr == 0)
1.50      ratchov  1526:                                break;
1.27      ratchov  1527:                        ctx = ctxbuf;
                   1528:                        for (c = onch; c > 0; c--) {
1.63    ! ratchov  1529:                                s = ctx[ctx_start];
        !          1530:                                ds = ctx[ctx_start ^ 1] - s;
1.27      ratchov  1531:                                ctx += RESAMP_NCTX;
1.63    ! ratchov  1532:                                *odata++ = s + ADATA_MULDIV(ds, diff, oblksz);
1.27      ratchov  1533:                        }
                   1534:                        diff -= iblksz;
1.56      ratchov  1535:                        ofr--;
                   1536:                } else {
                   1537:                        if (ifr == 0 || ofr == 0)
                   1538:                                break;
                   1539:                        ctx = ctxbuf + ctx_start;
                   1540:                        for (c = onch; c > 0; c--) {
                   1541:                                *odata++ = *ctx;
                   1542:                                ctx += RESAMP_NCTX;
                   1543:                        }
                   1544:                        ctx_start ^= 1;
                   1545:                        ctx = ctxbuf + ctx_start;
                   1546:                        for (c = inch; c > 0; c--) {
                   1547:                                *ctx = *idata++;
                   1548:                                ctx += RESAMP_NCTX;
                   1549:                        }
                   1550:                        diff -= iblksz;
                   1551:                        diff += oblksz;
                   1552:                        ifr--;
1.51      ratchov  1553:                        ofr--;
1.15      ratchov  1554:                }
                   1555:        }
1.27      ratchov  1556:        p->u.resamp.diff = diff;
                   1557:        p->u.resamp.ctx_start = ctx_start;
1.42      ratchov  1558: #ifdef DEBUG
                   1559:        if (debug_level >= 4) {
                   1560:                aproc_dbg(p);
1.61      ratchov  1561:                dbg_puts(": done delta = ");
1.46      ratchov  1562:                dbg_puti(diff);
                   1563:                dbg_puts(", ifr = ");
1.42      ratchov  1564:                dbg_putu(ifr);
                   1565:                dbg_puts(", ofr = ");
1.46      ratchov  1566:                dbg_putu(ofr);
1.42      ratchov  1567:                dbg_puts(" fr\n");
                   1568:        }
                   1569: #endif
1.15      ratchov  1570:        /*
                   1571:         * Update FIFO pointers.
                   1572:         */
1.51      ratchov  1573:        icount -= ifr;
                   1574:        ocount -= ofr;
1.15      ratchov  1575:        abuf_rdiscard(ibuf, icount);
                   1576:        abuf_wcommit(obuf, ocount);
                   1577: }
                   1578:
                   1579: int
                   1580: resamp_in(struct aproc *p, struct abuf *ibuf)
                   1581: {
1.54      ratchov  1582:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.15      ratchov  1583:
                   1584:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1585:                return 0;
                   1586:        resamp_bcopy(p, ibuf, obuf);
                   1587:        if (!abuf_flush(obuf))
                   1588:                return 0;
                   1589:        return 1;
                   1590: }
                   1591:
                   1592: int
                   1593: resamp_out(struct aproc *p, struct abuf *obuf)
                   1594: {
1.54      ratchov  1595:        struct abuf *ibuf = LIST_FIRST(&p->ins);
1.15      ratchov  1596:
                   1597:        if (!abuf_fill(ibuf))
                   1598:                return 0;
                   1599:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1600:                return 0;
                   1601:        resamp_bcopy(p, ibuf, obuf);
                   1602:        return 1;
                   1603: }
                   1604:
                   1605: void
                   1606: resamp_eof(struct aproc *p, struct abuf *ibuf)
                   1607: {
                   1608:        aproc_del(p);
                   1609: }
                   1610:
                   1611: void
                   1612: resamp_hup(struct aproc *p, struct abuf *obuf)
                   1613: {
                   1614:        aproc_del(p);
                   1615: }
                   1616:
                   1617: void
                   1618: resamp_ipos(struct aproc *p, struct abuf *ibuf, int delta)
                   1619: {
1.54      ratchov  1620:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.15      ratchov  1621:        long long ipos;
1.45      ratchov  1622:
                   1623:        ipos = (long long)delta * p->u.resamp.oblksz + p->u.resamp.idelta;
                   1624:        p->u.resamp.idelta = ipos % p->u.resamp.iblksz;
                   1625:        abuf_ipos(obuf, ipos / (int)p->u.resamp.iblksz);
1.15      ratchov  1626: }
                   1627:
                   1628: void
                   1629: resamp_opos(struct aproc *p, struct abuf *obuf, int delta)
                   1630: {
1.54      ratchov  1631:        struct abuf *ibuf = LIST_FIRST(&p->ins);
1.15      ratchov  1632:        long long opos;
                   1633:
1.45      ratchov  1634:        opos = (long long)delta * p->u.resamp.iblksz + p->u.resamp.odelta;
                   1635:        p->u.resamp.odelta = opos % p->u.resamp.oblksz;
                   1636:        abuf_opos(ibuf, opos / p->u.resamp.oblksz);
1.15      ratchov  1637: }
                   1638:
                   1639: struct aproc_ops resamp_ops = {
                   1640:        "resamp",
                   1641:        resamp_in,
                   1642:        resamp_out,
                   1643:        resamp_eof,
                   1644:        resamp_hup,
                   1645:        NULL,
                   1646:        NULL,
                   1647:        resamp_ipos,
                   1648:        resamp_opos,
                   1649:        NULL
                   1650: };
                   1651:
                   1652: struct aproc *
1.26      ratchov  1653: resamp_new(char *name, unsigned iblksz, unsigned oblksz)
1.15      ratchov  1654: {
                   1655:        struct aproc *p;
1.16      ratchov  1656:        unsigned i;
1.15      ratchov  1657:
                   1658:        p = aproc_new(&resamp_ops, name);
1.26      ratchov  1659:        p->u.resamp.iblksz = iblksz;
                   1660:        p->u.resamp.oblksz = oblksz;
1.48      ratchov  1661:        p->u.resamp.diff = 0;
1.15      ratchov  1662:        p->u.resamp.idelta = 0;
                   1663:        p->u.resamp.odelta = 0;
1.27      ratchov  1664:        p->u.resamp.ctx_start = 0;
                   1665:        for (i = 0; i < NCHAN_MAX * RESAMP_NCTX; i++)
1.16      ratchov  1666:                p->u.resamp.ctx[i] = 0;
1.42      ratchov  1667: #ifdef DEBUG
                   1668:        if (debug_level >= 3) {
                   1669:                aproc_dbg(p);
                   1670:                dbg_puts(": new ");
                   1671:                dbg_putu(iblksz);
                   1672:                dbg_puts("/");
                   1673:                dbg_putu(oblksz);
                   1674:                dbg_puts("\n");
                   1675:        }
                   1676: #endif
1.19      ratchov  1677:        return p;
                   1678: }
                   1679:
                   1680: /*
                   1681:  * Convert one block.
                   1682:  */
                   1683: void
                   1684: enc_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
                   1685: {
                   1686:        unsigned nch, scount, icount, ocount;
                   1687:        unsigned f;
1.63    ! ratchov  1688:        adata_t *idata;
1.19      ratchov  1689:        int s;
                   1690:        unsigned oshift;
                   1691:        int osigbit;
                   1692:        unsigned obps;
                   1693:        unsigned i;
                   1694:        unsigned char *odata;
                   1695:        int obnext;
                   1696:        int osnext;
                   1697:
                   1698:        /*
                   1699:         * Calculate max frames readable at once from the input buffer.
                   1700:         */
1.63    ! ratchov  1701:        idata = (adata_t *)abuf_rgetblk(ibuf, &icount, 0);
1.19      ratchov  1702:        if (icount == 0)
                   1703:                return;
                   1704:        odata = abuf_wgetblk(obuf, &ocount, 0);
                   1705:        if (ocount == 0)
                   1706:                return;
                   1707:        scount = (icount < ocount) ? icount : ocount;
                   1708:        nch = ibuf->cmax - ibuf->cmin + 1;
1.42      ratchov  1709: #ifdef DEBUG
                   1710:        if (debug_level >= 4) {
                   1711:                aproc_dbg(p);
                   1712:                dbg_puts(": bcopy ");
                   1713:                dbg_putu(scount);
1.51      ratchov  1714:                dbg_puts(" fr / ");
1.42      ratchov  1715:                dbg_putu(nch);
                   1716:                dbg_puts(" ch\n");
                   1717:        }
                   1718: #endif
1.19      ratchov  1719:        /*
                   1720:         * Partially copy structures into local variables, to avoid
                   1721:         * unnecessary indirections; this also allows the compiler to
                   1722:         * order local variables more "cache-friendly".
                   1723:         */
                   1724:        oshift = p->u.conv.shift;
                   1725:        osigbit = p->u.conv.sigbit;
                   1726:        obps = p->u.conv.bps;
                   1727:        obnext = p->u.conv.bnext;
                   1728:        osnext = p->u.conv.snext;
                   1729:
                   1730:        /*
                   1731:         * Start conversion.
                   1732:         */
                   1733:        odata += p->u.conv.bfirst;
                   1734:        for (f = scount * nch; f > 0; f--) {
                   1735:                s = *idata++;
1.63    ! ratchov  1736:                s <<= 32 - ADATA_BITS;
1.19      ratchov  1737:                s >>= oshift;
                   1738:                s ^= osigbit;
                   1739:                for (i = obps; i > 0; i--) {
                   1740:                        *odata = (unsigned char)s;
                   1741:                        s >>= 8;
                   1742:                        odata += obnext;
                   1743:                }
                   1744:                odata += osnext;
                   1745:        }
                   1746:
                   1747:        /*
                   1748:         * Update FIFO pointers.
                   1749:         */
1.51      ratchov  1750:        abuf_rdiscard(ibuf, scount);
                   1751:        abuf_wcommit(obuf, scount);
1.19      ratchov  1752: }
                   1753:
                   1754: int
                   1755: enc_in(struct aproc *p, struct abuf *ibuf)
                   1756: {
1.54      ratchov  1757:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.19      ratchov  1758:
                   1759:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1760:                return 0;
                   1761:        enc_bcopy(p, ibuf, obuf);
                   1762:        if (!abuf_flush(obuf))
                   1763:                return 0;
                   1764:        return 1;
                   1765: }
                   1766:
                   1767: int
                   1768: enc_out(struct aproc *p, struct abuf *obuf)
                   1769: {
1.54      ratchov  1770:        struct abuf *ibuf = LIST_FIRST(&p->ins);
1.19      ratchov  1771:
                   1772:        if (!abuf_fill(ibuf))
                   1773:                return 0;
                   1774:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1775:                return 0;
                   1776:        enc_bcopy(p, ibuf, obuf);
                   1777:        return 1;
                   1778: }
                   1779:
                   1780: void
                   1781: enc_eof(struct aproc *p, struct abuf *ibuf)
                   1782: {
                   1783:        aproc_del(p);
                   1784: }
                   1785:
                   1786: void
                   1787: enc_hup(struct aproc *p, struct abuf *obuf)
                   1788: {
                   1789:        aproc_del(p);
                   1790: }
                   1791:
                   1792: struct aproc_ops enc_ops = {
                   1793:        "enc",
                   1794:        enc_in,
                   1795:        enc_out,
                   1796:        enc_eof,
                   1797:        enc_hup,
                   1798:        NULL,
                   1799:        NULL,
                   1800:        aproc_ipos,
                   1801:        aproc_opos,
                   1802:        NULL
                   1803: };
                   1804:
                   1805: struct aproc *
                   1806: enc_new(char *name, struct aparams *par)
                   1807: {
                   1808:        struct aproc *p;
                   1809:
                   1810:        p = aproc_new(&enc_ops, name);
                   1811:        p->u.conv.bps = par->bps;
                   1812:        p->u.conv.sigbit = par->sig ? 0 : 1 << (par->bits - 1);
                   1813:        if (par->msb) {
                   1814:                p->u.conv.shift = 32 - par->bps * 8;
                   1815:        } else {
                   1816:                p->u.conv.shift = 32 - par->bits;
                   1817:        }
                   1818:        if (!par->le) {
                   1819:                p->u.conv.bfirst = par->bps - 1;
                   1820:                p->u.conv.bnext = -1;
                   1821:                p->u.conv.snext = 2 * par->bps;
                   1822:        } else {
                   1823:                p->u.conv.bfirst = 0;
                   1824:                p->u.conv.bnext = 1;
                   1825:                p->u.conv.snext = 0;
                   1826:        }
1.42      ratchov  1827: #ifdef DEBUG
                   1828:        if (debug_level >= 3) {
                   1829:                aproc_dbg(p);
                   1830:                dbg_puts(": new ");
                   1831:                aparams_dbg(par);
                   1832:                dbg_puts("\n");
                   1833:        }
                   1834: #endif
1.19      ratchov  1835:        return p;
                   1836: }
                   1837:
                   1838: /*
                   1839:  * Convert one block.
                   1840:  */
                   1841: void
                   1842: dec_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
                   1843: {
                   1844:        unsigned nch, scount, icount, ocount;
                   1845:        unsigned f;
                   1846:        unsigned ibps;
                   1847:        unsigned i;
                   1848:        int s = 0xdeadbeef;
                   1849:        unsigned char *idata;
                   1850:        int ibnext;
                   1851:        int isnext;
                   1852:        int isigbit;
                   1853:        unsigned ishift;
1.63    ! ratchov  1854:        adata_t *odata;
1.19      ratchov  1855:
                   1856:        /*
                   1857:         * Calculate max frames readable at once from the input buffer.
                   1858:         */
                   1859:        idata = abuf_rgetblk(ibuf, &icount, 0);
                   1860:        if (icount == 0)
                   1861:                return;
1.63    ! ratchov  1862:        odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
1.19      ratchov  1863:        if (ocount == 0)
                   1864:                return;
                   1865:        scount = (icount < ocount) ? icount : ocount;
                   1866:        nch = obuf->cmax - obuf->cmin + 1;
1.42      ratchov  1867: #ifdef DEBUG
                   1868:        if (debug_level >= 4) {
                   1869:                aproc_dbg(p);
                   1870:                dbg_puts(": bcopy ");
                   1871:                dbg_putu(scount);
1.51      ratchov  1872:                dbg_puts(" fr / ");
1.42      ratchov  1873:                dbg_putu(nch);
                   1874:                dbg_puts(" ch\n");
                   1875:        }
                   1876: #endif
1.19      ratchov  1877:        /*
                   1878:         * Partially copy structures into local variables, to avoid
                   1879:         * unnecessary indirections; this also allows the compiler to
                   1880:         * order local variables more "cache-friendly".
                   1881:         */
                   1882:        ibps = p->u.conv.bps;
                   1883:        ibnext = p->u.conv.bnext;
                   1884:        isigbit = p->u.conv.sigbit;
                   1885:        ishift = p->u.conv.shift;
                   1886:        isnext = p->u.conv.snext;
                   1887:
                   1888:        /*
                   1889:         * Start conversion.
                   1890:         */
                   1891:        idata += p->u.conv.bfirst;
                   1892:        for (f = scount * nch; f > 0; f--) {
                   1893:                for (i = ibps; i > 0; i--) {
                   1894:                        s <<= 8;
                   1895:                        s |= *idata;
                   1896:                        idata += ibnext;
                   1897:                }
                   1898:                idata += isnext;
                   1899:                s ^= isigbit;
                   1900:                s <<= ishift;
1.63    ! ratchov  1901:                s >>= 32 - ADATA_BITS;
1.19      ratchov  1902:                *odata++ = s;
                   1903:        }
                   1904:
                   1905:        /*
                   1906:         * Update FIFO pointers.
                   1907:         */
1.51      ratchov  1908:        abuf_rdiscard(ibuf, scount);
                   1909:        abuf_wcommit(obuf, scount);
1.19      ratchov  1910: }
                   1911:
                   1912: int
                   1913: dec_in(struct aproc *p, struct abuf *ibuf)
                   1914: {
1.54      ratchov  1915:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.19      ratchov  1916:
                   1917:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1918:                return 0;
                   1919:        dec_bcopy(p, ibuf, obuf);
                   1920:        if (!abuf_flush(obuf))
                   1921:                return 0;
                   1922:        return 1;
                   1923: }
                   1924:
                   1925: int
                   1926: dec_out(struct aproc *p, struct abuf *obuf)
                   1927: {
1.54      ratchov  1928:        struct abuf *ibuf = LIST_FIRST(&p->ins);
1.19      ratchov  1929:
                   1930:        if (!abuf_fill(ibuf))
                   1931:                return 0;
                   1932:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   1933:                return 0;
                   1934:        dec_bcopy(p, ibuf, obuf);
                   1935:        return 1;
                   1936: }
                   1937:
                   1938: void
                   1939: dec_eof(struct aproc *p, struct abuf *ibuf)
                   1940: {
                   1941:        aproc_del(p);
                   1942: }
                   1943:
                   1944: void
                   1945: dec_hup(struct aproc *p, struct abuf *obuf)
                   1946: {
                   1947:        aproc_del(p);
                   1948: }
                   1949:
                   1950: struct aproc_ops dec_ops = {
                   1951:        "dec",
                   1952:        dec_in,
                   1953:        dec_out,
                   1954:        dec_eof,
                   1955:        dec_hup,
                   1956:        NULL,
                   1957:        NULL,
                   1958:        aproc_ipos,
                   1959:        aproc_opos,
                   1960:        NULL
                   1961: };
                   1962:
                   1963: struct aproc *
                   1964: dec_new(char *name, struct aparams *par)
                   1965: {
                   1966:        struct aproc *p;
                   1967:
                   1968:        p = aproc_new(&dec_ops, name);
                   1969:        p->u.conv.bps = par->bps;
                   1970:        p->u.conv.sigbit = par->sig ? 0 : 1 << (par->bits - 1);
                   1971:        if (par->msb) {
                   1972:                p->u.conv.shift = 32 - par->bps * 8;
                   1973:        } else {
                   1974:                p->u.conv.shift = 32 - par->bits;
                   1975:        }
                   1976:        if (par->le) {
                   1977:                p->u.conv.bfirst = par->bps - 1;
                   1978:                p->u.conv.bnext = -1;
                   1979:                p->u.conv.snext = 2 * par->bps;
                   1980:        } else {
                   1981:                p->u.conv.bfirst = 0;
                   1982:                p->u.conv.bnext = 1;
                   1983:                p->u.conv.snext = 0;
                   1984:        }
1.42      ratchov  1985: #ifdef DEBUG
                   1986:        if (debug_level >= 3) {
                   1987:                aproc_dbg(p);
                   1988:                dbg_puts(": new ");
                   1989:                aparams_dbg(par);
1.53      ratchov  1990:                dbg_puts("\n");
                   1991:        }
                   1992: #endif
                   1993:        return p;
                   1994: }
                   1995:
                   1996: /*
                   1997:  * Convert one block.
                   1998:  */
                   1999: void
                   2000: join_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
                   2001: {
                   2002:        unsigned h, hops;
                   2003:        unsigned inch, inext;
1.63    ! ratchov  2004:        adata_t *idata;
1.53      ratchov  2005:        unsigned onch, onext;
1.63    ! ratchov  2006:        adata_t *odata;
1.53      ratchov  2007:        int scale;
                   2008:        unsigned c, f, scount, icount, ocount;
                   2009:
                   2010:        /*
                   2011:         * Calculate max frames readable at once from the input buffer.
                   2012:         */
1.63    ! ratchov  2013:        idata = (adata_t *)abuf_rgetblk(ibuf, &icount, 0);
1.53      ratchov  2014:        if (icount == 0)
                   2015:                return;
1.63    ! ratchov  2016:        odata = (adata_t *)abuf_wgetblk(obuf, &ocount, 0);
1.53      ratchov  2017:        if (ocount == 0)
                   2018:                return;
                   2019:        scount = icount < ocount ? icount : ocount;
                   2020:        inch = ibuf->cmax - ibuf->cmin + 1;
                   2021:        onch = obuf->cmax - obuf->cmin + 1;
                   2022:        if (2 * inch <= onch) {
                   2023:                hops = onch / inch;
                   2024:                inext = inch * hops;
                   2025:                onext = onch - inext;
                   2026:                for (f = scount; f > 0; f--) {
                   2027:                        h = hops;
                   2028:                        for (;;) {
                   2029:                                for (c = inch; c > 0; c--)
                   2030:                                        *odata++ = *idata++;
                   2031:                                if (--h == 0)
                   2032:                                        break;
                   2033:                                idata -= inch;
                   2034:                        }
                   2035:                        for (c = onext; c > 0; c--)
                   2036:                                *odata++ = 0;
                   2037:                }
                   2038:        } else if (inch >= 2 * onch) {
                   2039:                hops = inch / onch;
                   2040:                inext = inch - onch * hops;
                   2041:                scale = ADATA_UNIT / hops;
                   2042:                inch -= onch + inext;
                   2043:                hops--;
                   2044:                for (f = scount; f > 0; f--) {
                   2045:                        for (c = onch; c > 0; c--)
1.63    ! ratchov  2046:                                *odata++ = ADATA_MUL(*idata++, scale);
1.53      ratchov  2047:                        for (h = hops; h > 0; h--) {
                   2048:                                odata -= onch;
                   2049:                                for (c = onch; c > 0; c--)
1.63    ! ratchov  2050:                                        *odata++ += ADATA_MUL(*idata++, scale);
1.53      ratchov  2051:                        }
                   2052:                        idata += inext;
                   2053:                }
                   2054:        } else {
                   2055: #ifdef DEBUG
                   2056:                aproc_dbg(p);
                   2057:                dbg_puts(": nothing to do\n");
                   2058:                dbg_panic();
                   2059: #endif
                   2060:        }
                   2061: #ifdef DEBUG
                   2062:        if (debug_level >= 4) {
                   2063:                aproc_dbg(p);
                   2064:                dbg_puts(": bcopy ");
                   2065:                dbg_putu(scount);
                   2066:                dbg_puts(" fr\n");
                   2067:        }
                   2068: #endif
                   2069:        abuf_rdiscard(ibuf, scount);
                   2070:        abuf_wcommit(obuf, scount);
                   2071: }
                   2072:
                   2073: int
                   2074: join_in(struct aproc *p, struct abuf *ibuf)
                   2075: {
1.54      ratchov  2076:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.53      ratchov  2077:
                   2078:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   2079:                return 0;
                   2080:        join_bcopy(p, ibuf, obuf);
                   2081:        if (!abuf_flush(obuf))
                   2082:                return 0;
                   2083:        return 1;
                   2084: }
                   2085:
                   2086: int
                   2087: join_out(struct aproc *p, struct abuf *obuf)
                   2088: {
1.54      ratchov  2089:        struct abuf *ibuf = LIST_FIRST(&p->ins);
1.53      ratchov  2090:
                   2091:        if (!abuf_fill(ibuf))
                   2092:                return 0;
                   2093:        if (!ABUF_WOK(obuf) || !ABUF_ROK(ibuf))
                   2094:                return 0;
                   2095:        join_bcopy(p, ibuf, obuf);
                   2096:        return 1;
                   2097: }
                   2098:
                   2099: void
                   2100: join_eof(struct aproc *p, struct abuf *ibuf)
                   2101: {
                   2102:        aproc_del(p);
                   2103: }
                   2104:
                   2105: void
                   2106: join_hup(struct aproc *p, struct abuf *obuf)
                   2107: {
                   2108:        aproc_del(p);
                   2109: }
                   2110:
                   2111: struct aproc_ops join_ops = {
                   2112:        "join",
                   2113:        join_in,
                   2114:        join_out,
                   2115:        join_eof,
                   2116:        join_hup,
                   2117:        NULL,
                   2118:        NULL,
                   2119:        aproc_ipos,
                   2120:        aproc_opos,
                   2121:        NULL
                   2122: };
                   2123:
                   2124: struct aproc *
                   2125: join_new(char *name)
                   2126: {
                   2127:        struct aproc *p;
                   2128:
                   2129:        p = aproc_new(&join_ops, name);
                   2130: #ifdef DEBUG
                   2131:        if (debug_level >= 3) {
                   2132:                aproc_dbg(p);
1.61      ratchov  2133:                dbg_puts(": new\n");
1.51      ratchov  2134:        }
                   2135: #endif
                   2136:        return p;
                   2137: }
                   2138:
                   2139: /*
                   2140:  * Commit and flush part of the output buffer
                   2141:  */
                   2142: void
                   2143: mon_flush(struct aproc *p)
                   2144: {
1.54      ratchov  2145:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.51      ratchov  2146:        unsigned count;
                   2147:
                   2148: #ifdef DEBUG
                   2149:        if (debug_level >= 4) {
                   2150:                aproc_dbg(p);
                   2151:                dbg_puts(": delta = ");
                   2152:                dbg_puti(p->u.mon.delta);
                   2153:                dbg_puts("/");
                   2154:                dbg_putu(p->u.mon.bufsz);
                   2155:                dbg_puts(" pending = ");
                   2156:                dbg_puti(p->u.mon.pending);
                   2157:                dbg_puts("\n");
                   2158:        }
                   2159: #endif
                   2160:        if (p->u.mon.delta <= 0 || p->u.mon.pending == 0)
                   2161:                return;
                   2162:        count = p->u.mon.delta;
                   2163:        if (count > p->u.mon.pending)
                   2164:                count = p->u.mon.pending;
                   2165:        abuf_wcommit(obuf, count);
                   2166:        p->u.mon.pending -= count;
                   2167:        p->u.mon.delta -= count;
                   2168:        abuf_flush(obuf);
                   2169: }
                   2170:
                   2171: /*
                   2172:  * Copy one block.
                   2173:  */
                   2174: void
                   2175: mon_snoop(struct aproc *p, struct abuf *ibuf, unsigned pos, unsigned todo)
                   2176: {
1.54      ratchov  2177:        struct abuf *obuf = LIST_FIRST(&p->outs);
1.51      ratchov  2178:        unsigned scount, icount, ocount;
1.63    ! ratchov  2179:        adata_t *idata, *odata;
1.51      ratchov  2180:
                   2181: #ifdef DEBUG
                   2182:        if (debug_level >= 4) {
                   2183:                aproc_dbg(p);
1.61      ratchov  2184:                dbg_puts(": snooping ");
1.51      ratchov  2185:                dbg_putu(pos);
                   2186:                dbg_puts("..");
                   2187:                dbg_putu(todo);
                   2188:                dbg_puts("\n");
                   2189:        }
                   2190: #endif
                   2191:        if (!abuf_flush(obuf))
                   2192:                return;
                   2193:
                   2194:        while (todo > 0) {
                   2195:                /*
                   2196:                 * Calculate max frames readable at once from the input buffer.
                   2197:                 */
1.63    ! ratchov  2198:                idata = (adata_t *)abuf_rgetblk(ibuf, &icount, pos);
        !          2199:                odata = (adata_t *)abuf_wgetblk(obuf, &ocount, p->u.mon.pending);
1.51      ratchov  2200:                scount = (icount < ocount) ? icount : ocount;
                   2201: #ifdef DEBUG
                   2202:                if (debug_level >= 4) {
                   2203:                        aproc_dbg(p);
                   2204:                        dbg_puts(": snooping ");
                   2205:                        dbg_putu(scount);
                   2206:                        dbg_puts(" fr\n");
                   2207:                }
                   2208:                if (scount == 0) {
                   2209:                        dbg_puts("monitor xrun, not allowed\n");
                   2210:                        dbg_panic();
                   2211:                }
                   2212: #endif
                   2213:                memcpy(odata, idata, scount * obuf->bpf);
                   2214:                p->u.mon.pending += scount;
                   2215:                todo -= scount;
                   2216:                pos += scount;
                   2217:        }
                   2218:        mon_flush(p);
                   2219: }
                   2220:
                   2221: int
                   2222: mon_in(struct aproc *p, struct abuf *ibuf)
                   2223: {
                   2224: #ifdef DEBUG
                   2225:        dbg_puts("monitor can't have inputs to read\n");
                   2226:        dbg_panic();
                   2227: #endif
                   2228:        return 0;
                   2229: }
                   2230:
                   2231: /*
                   2232:  * put the monitor into ``empty'' state
                   2233:  */
                   2234: void
                   2235: mon_clear(struct aproc *p)
                   2236: {
                   2237:        p->u.mon.pending = 0;
                   2238:        p->u.mon.delta = 0;
                   2239: }
                   2240:
                   2241: int
                   2242: mon_out(struct aproc *p, struct abuf *obuf)
                   2243: {
                   2244:        /*
                   2245:         * can't trigger monitored stream to produce data
                   2246:         */
                   2247:        return 0;
                   2248: }
                   2249:
                   2250: void
                   2251: mon_eof(struct aproc *p, struct abuf *ibuf)
                   2252: {
                   2253: #ifdef DEBUG
                   2254:        dbg_puts("monitor can't have inputs to eof\n");
                   2255:        dbg_panic();
                   2256: #endif
                   2257: }
                   2258:
                   2259: void
                   2260: mon_hup(struct aproc *p, struct abuf *obuf)
                   2261: {
                   2262:        aproc_del(p);
                   2263: }
                   2264:
                   2265: void
                   2266: mon_ipos(struct aproc *p, struct abuf *ibuf, int delta)
                   2267: {
                   2268:        aproc_ipos(p, ibuf, delta);
                   2269:        p->u.mon.delta += delta;
                   2270:        mon_flush(p);
                   2271: }
                   2272:
                   2273: struct aproc_ops mon_ops = {
                   2274:        "mon",
                   2275:        mon_in,
                   2276:        mon_out,
                   2277:        mon_eof,
                   2278:        mon_hup,
                   2279:        NULL,
                   2280:        NULL,
                   2281:        mon_ipos,
                   2282:        aproc_opos,
                   2283:        NULL
                   2284: };
                   2285:
                   2286: struct aproc *
                   2287: mon_new(char *name, unsigned bufsz)
                   2288: {
                   2289:        struct aproc *p;
                   2290:
                   2291:        p = aproc_new(&mon_ops, name);
                   2292:        p->u.mon.pending = 0;
                   2293:        p->u.mon.delta = 0;
                   2294:        p->u.mon.bufsz = bufsz;
                   2295: #ifdef DEBUG
                   2296:        if (debug_level >= 3) {
                   2297:                aproc_dbg(p);
                   2298:                dbg_puts(": new\n");
1.42      ratchov  2299:        }
                   2300: #endif
1.1       ratchov  2301:        return p;
                   2302: }