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

Annotation of src/usr.bin/sndiod/dev.c, Revision 1.79

1.79    ! ratchov     1: /*     $OpenBSD: dev.c,v 1.78 2021/01/28 11:02:28 ratchov Exp $        */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008-2012 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: #include <stdio.h>
                     18: #include <string.h>
                     19:
                     20: #include "abuf.h"
                     21: #include "defs.h"
                     22: #include "dev.h"
                     23: #include "dsp.h"
                     24: #include "siofile.h"
                     25: #include "midi.h"
1.36      ratchov    26: #include "opt.h"
1.1       ratchov    27: #include "sysex.h"
                     28: #include "utils.h"
                     29:
1.14      ratchov    30: void zomb_onmove(void *);
1.25      ratchov    31: void zomb_onvol(void *);
1.1       ratchov    32: void zomb_fill(void *);
                     33: void zomb_flush(void *);
                     34: void zomb_eof(void *);
                     35: void zomb_exit(void *);
                     36:
1.7       ratchov    37: void dev_log(struct dev *);
                     38: void dev_midi_qfr(struct dev *, int);
                     39: void dev_midi_full(struct dev *);
                     40: void dev_midi_vol(struct dev *, struct slot *);
                     41: void dev_midi_master(struct dev *);
                     42: void dev_midi_slotdesc(struct dev *, struct slot *);
                     43: void dev_midi_dump(struct dev *);
1.1       ratchov    44: void dev_midi_imsg(void *, unsigned char *, int);
                     45: void dev_midi_omsg(void *, unsigned char *, int);
                     46: void dev_midi_fill(void *, int);
                     47: void dev_midi_exit(void *);
1.7       ratchov    48:
                     49: void dev_mix_badd(struct dev *, struct slot *);
                     50: void dev_mix_adjvol(struct dev *);
                     51: void dev_sub_bcopy(struct dev *, struct slot *);
                     52:
                     53: void dev_onmove(struct dev *, int);
                     54: void dev_master(struct dev *, unsigned int);
                     55: void dev_cycle(struct dev *);
                     56: int dev_getpos(struct dev *);
                     57: struct dev *dev_new(char *, struct aparams *, unsigned int, unsigned int,
                     58:     unsigned int, unsigned int, unsigned int, unsigned int);
1.25      ratchov    59: void dev_adjpar(struct dev *, int, int, int);
1.59      ratchov    60: int dev_allocbufs(struct dev *);
1.7       ratchov    61: int dev_open(struct dev *);
1.59      ratchov    62: void dev_freebufs(struct dev *);
1.7       ratchov    63: void dev_close(struct dev *);
                     64: int dev_ref(struct dev *);
                     65: void dev_unref(struct dev *);
                     66: int dev_init(struct dev *);
                     67: void dev_done(struct dev *);
                     68: struct dev *dev_bynum(int);
                     69: void dev_del(struct dev *);
1.75      ratchov    70: void dev_setalt(struct dev *, unsigned int);
1.7       ratchov    71: unsigned int dev_roundof(struct dev *, unsigned int);
                     72: void dev_wakeup(struct dev *);
                     73: void dev_sync_attach(struct dev *);
                     74: void dev_mmcstart(struct dev *);
                     75: void dev_mmcstop(struct dev *);
                     76: void dev_mmcloc(struct dev *, unsigned int);
                     77:
1.64      ratchov    78: void slot_ctlname(struct slot *, char *, size_t);
1.7       ratchov    79: void slot_log(struct slot *);
                     80: void slot_del(struct slot *);
                     81: void slot_setvol(struct slot *, unsigned int);
                     82: void slot_ready(struct slot *);
1.35      ratchov    83: void slot_allocbufs(struct slot *);
                     84: void slot_freebufs(struct slot *);
1.12      ratchov    85: void slot_skip_update(struct slot *);
1.7       ratchov    86: void slot_write(struct slot *);
                     87: void slot_read(struct slot *);
1.12      ratchov    88: int slot_skip(struct slot *);
1.1       ratchov    89:
1.64      ratchov    90: void ctl_node_log(struct ctl_node *);
                     91: void ctl_log(struct ctl *);
                     92:
1.1       ratchov    93: struct midiops dev_midiops = {
                     94:        dev_midi_imsg,
                     95:        dev_midi_omsg,
                     96:        dev_midi_fill,
                     97:        dev_midi_exit
                     98: };
                     99:
                    100: struct slotops zomb_slotops = {
                    101:        zomb_onmove,
                    102:        zomb_onvol,
                    103:        zomb_fill,
                    104:        zomb_flush,
                    105:        zomb_eof,
                    106:        zomb_exit
                    107: };
                    108:
                    109: struct dev *dev_list = NULL;
                    110: unsigned int dev_sndnum = 0;
                    111:
                    112: void
                    113: dev_log(struct dev *d)
                    114: {
1.3       ratchov   115: #ifdef DEBUG
                    116:        static char *pstates[] = {
                    117:                "cfg", "ini", "run"
                    118:        };
                    119: #endif
1.1       ratchov   120:        log_puts("snd");
                    121:        log_putu(d->num);
1.3       ratchov   122: #ifdef DEBUG
                    123:        if (log_level >= 3) {
                    124:                log_puts(" pst=");
                    125:                log_puts(pstates[d->pstate]);
                    126:        }
                    127: #endif
1.1       ratchov   128: }
                    129:
                    130: void
1.64      ratchov   131: slot_ctlname(struct slot *s, char *name, size_t size)
                    132: {
                    133:        snprintf(name, size, "%s%u", s->name, s->unit);
                    134: }
                    135:
                    136: void
1.1       ratchov   137: slot_log(struct slot *s)
1.3       ratchov   138: {
1.64      ratchov   139:        char name[CTL_NAMEMAX];
1.1       ratchov   140: #ifdef DEBUG
                    141:        static char *pstates[] = {
                    142:                "ini", "sta", "rdy", "run", "stp", "mid"
                    143:        };
                    144: #endif
1.64      ratchov   145:        slot_ctlname(s, name, CTL_NAMEMAX);
                    146:        log_puts(name);
1.1       ratchov   147: #ifdef DEBUG
                    148:        if (log_level >= 3) {
                    149:                log_puts(" vol=");
                    150:                log_putu(s->vol);
                    151:                if (s->ops) {
                    152:                        log_puts(",pst=");
                    153:                        log_puts(pstates[s->pstate]);
                    154:                }
                    155:        }
                    156: #endif
                    157: }
                    158:
                    159: void
1.14      ratchov   160: zomb_onmove(void *arg)
1.1       ratchov   161: {
                    162: }
                    163:
                    164: void
1.25      ratchov   165: zomb_onvol(void *arg)
1.1       ratchov   166: {
                    167: }
                    168:
                    169: void
                    170: zomb_fill(void *arg)
                    171: {
                    172: }
                    173:
                    174: void
                    175: zomb_flush(void *arg)
                    176: {
                    177: }
                    178:
                    179: void
                    180: zomb_eof(void *arg)
                    181: {
                    182:        struct slot *s = arg;
                    183:
                    184: #ifdef DEBUG
                    185:        if (log_level >= 3) {
                    186:                slot_log(s);
                    187:                log_puts(": zomb_eof\n");
                    188:        }
                    189: #endif
                    190:        s->ops = NULL;
                    191: }
                    192:
                    193: void
                    194: zomb_exit(void *arg)
                    195: {
                    196: #ifdef DEBUG
                    197:        struct slot *s = arg;
                    198:
                    199:        if (log_level >= 3) {
                    200:                slot_log(s);
                    201:                log_puts(": zomb_exit\n");
                    202:        }
                    203: #endif
                    204: }
                    205:
                    206: /*
                    207:  * send a quarter frame MTC message
                    208:  */
                    209: void
                    210: dev_midi_qfr(struct dev *d, int delta)
                    211: {
                    212:        unsigned char buf[2];
                    213:        unsigned int data;
                    214:        int qfrlen;
                    215:
                    216:        d->mtc.delta += delta * MTC_SEC;
                    217:        qfrlen = d->rate * (MTC_SEC / (4 * d->mtc.fps));
                    218:        while (d->mtc.delta >= qfrlen) {
                    219:                switch (d->mtc.qfr) {
                    220:                case 0:
                    221:                        data = d->mtc.fr & 0xf;
                    222:                        break;
                    223:                case 1:
                    224:                        data = d->mtc.fr >> 4;
                    225:                        break;
                    226:                case 2:
                    227:                        data = d->mtc.sec & 0xf;
                    228:                        break;
                    229:                case 3:
                    230:                        data = d->mtc.sec >> 4;
                    231:                        break;
                    232:                case 4:
                    233:                        data = d->mtc.min & 0xf;
                    234:                        break;
                    235:                case 5:
                    236:                        data = d->mtc.min >> 4;
                    237:                        break;
                    238:                case 6:
                    239:                        data = d->mtc.hr & 0xf;
                    240:                        break;
                    241:                case 7:
                    242:                        data = (d->mtc.hr >> 4) | (d->mtc.fps_id << 1);
                    243:                        /*
                    244:                         * tick messages are sent 2 frames ahead
                    245:                         */
                    246:                        d->mtc.fr += 2;
                    247:                        if (d->mtc.fr < d->mtc.fps)
                    248:                                break;
                    249:                        d->mtc.fr -= d->mtc.fps;
                    250:                        d->mtc.sec++;
                    251:                        if (d->mtc.sec < 60)
                    252:                                break;
                    253:                        d->mtc.sec = 0;
                    254:                        d->mtc.min++;
                    255:                        if (d->mtc.min < 60)
                    256:                                break;
                    257:                        d->mtc.min = 0;
                    258:                        d->mtc.hr++;
                    259:                        if (d->mtc.hr < 24)
                    260:                                break;
                    261:                        d->mtc.hr = 0;
                    262:                        break;
                    263:                default:
                    264:                        /* NOTREACHED */
                    265:                        data = 0;
                    266:                }
                    267:                buf[0] = 0xf1;
                    268:                buf[1] = (d->mtc.qfr << 4) | data;
                    269:                d->mtc.qfr++;
                    270:                d->mtc.qfr &= 7;
                    271:                midi_send(d->midi, buf, 2);
                    272:                d->mtc.delta -= qfrlen;
                    273:        }
                    274: }
                    275:
                    276: /*
                    277:  * send a full frame MTC message
                    278:  */
                    279: void
                    280: dev_midi_full(struct dev *d)
                    281: {
                    282:        struct sysex x;
                    283:        unsigned int fps;
                    284:
                    285:        d->mtc.delta = MTC_SEC * dev_getpos(d);
                    286:        if (d->rate % (30 * 4 * d->round) == 0) {
                    287:                d->mtc.fps_id = MTC_FPS_30;
                    288:                d->mtc.fps = 30;
                    289:        } else if (d->rate % (25 * 4 * d->round) == 0) {
                    290:                d->mtc.fps_id = MTC_FPS_25;
                    291:                d->mtc.fps = 25;
                    292:        } else {
                    293:                d->mtc.fps_id = MTC_FPS_24;
                    294:                d->mtc.fps = 24;
                    295:        }
                    296: #ifdef DEBUG
                    297:        if (log_level >= 3) {
                    298:                dev_log(d);
                    299:                log_puts(": mtc full frame at ");
                    300:                log_puti(d->mtc.delta);
                    301:                log_puts(", ");
                    302:                log_puti(d->mtc.fps);
                    303:                log_puts(" fps\n");
                    304:        }
                    305: #endif
                    306:        fps = d->mtc.fps;
                    307:        d->mtc.hr =  (d->mtc.origin / (MTC_SEC * 3600)) % 24;
                    308:        d->mtc.min = (d->mtc.origin / (MTC_SEC * 60))   % 60;
                    309:        d->mtc.sec = (d->mtc.origin / (MTC_SEC))        % 60;
                    310:        d->mtc.fr =  (d->mtc.origin / (MTC_SEC / fps))  % fps;
                    311:
                    312:        x.start = SYSEX_START;
                    313:        x.type = SYSEX_TYPE_RT;
1.8       ratchov   314:        x.dev = SYSEX_DEV_ANY;
1.1       ratchov   315:        x.id0 = SYSEX_MTC;
                    316:        x.id1 = SYSEX_MTC_FULL;
                    317:        x.u.full.hr = d->mtc.hr | (d->mtc.fps_id << 5);
                    318:        x.u.full.min = d->mtc.min;
                    319:        x.u.full.sec = d->mtc.sec;
                    320:        x.u.full.fr = d->mtc.fr;
                    321:        x.u.full.end = SYSEX_END;
                    322:        d->mtc.qfr = 0;
                    323:        midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(full));
                    324: }
                    325:
                    326: /*
                    327:  * send a volume change MIDI message
                    328:  */
                    329: void
                    330: dev_midi_vol(struct dev *d, struct slot *s)
                    331: {
                    332:        unsigned char msg[3];
                    333:
                    334:        msg[0] = MIDI_CTL | (s - d->slot);
                    335:        msg[1] = MIDI_CTL_VOL;
                    336:        msg[2] = s->vol;
                    337:        midi_send(d->midi, msg, 3);
                    338: }
                    339:
                    340: /*
                    341:  * send a master volume MIDI message
                    342:  */
                    343: void
                    344: dev_midi_master(struct dev *d)
                    345: {
1.70      ratchov   346:        struct ctl *c;
                    347:        unsigned int master, v;
1.1       ratchov   348:        struct sysex x;
                    349:
1.70      ratchov   350:        if (d->master_enabled)
                    351:                master = d->master;
                    352:        else {
                    353:                master = 0;
                    354:                for (c = d->ctl_list; c != NULL; c = c->next) {
                    355:                        if (c->type != CTL_NUM ||
                    356:                            strcmp(c->group, "") != 0 ||
                    357:                            strcmp(c->node0.name, "output") != 0 ||
                    358:                            strcmp(c->func, "level") != 0)
                    359:                                continue;
                    360:                        v = (c->curval * 127 + c->maxval / 2) / c->maxval;
                    361:                        if (master < v)
                    362:                                master = v;
                    363:                }
                    364:        }
                    365:
1.1       ratchov   366:        memset(&x, 0, sizeof(struct sysex));
                    367:        x.start = SYSEX_START;
                    368:        x.type = SYSEX_TYPE_RT;
1.8       ratchov   369:        x.dev = SYSEX_DEV_ANY;
1.1       ratchov   370:        x.id0 = SYSEX_CONTROL;
                    371:        x.id1 = SYSEX_MASTER;
                    372:        x.u.master.fine = 0;
1.70      ratchov   373:        x.u.master.coarse = master;
1.1       ratchov   374:        x.u.master.end = SYSEX_END;
                    375:        midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(master));
                    376: }
                    377:
                    378: /*
                    379:  * send a sndiod-specific slot description MIDI message
                    380:  */
                    381: void
                    382: dev_midi_slotdesc(struct dev *d, struct slot *s)
                    383: {
                    384:        struct sysex x;
                    385:
                    386:        memset(&x, 0, sizeof(struct sysex));
                    387:        x.start = SYSEX_START;
                    388:        x.type = SYSEX_TYPE_EDU;
1.8       ratchov   389:        x.dev = SYSEX_DEV_ANY;
1.1       ratchov   390:        x.id0 = SYSEX_AUCAT;
                    391:        x.id1 = SYSEX_AUCAT_SLOTDESC;
1.64      ratchov   392:        if (*s->name != '\0')
                    393:                slot_ctlname(s, (char *)x.u.slotdesc.name, SYSEX_NAMELEN);
1.1       ratchov   394:        x.u.slotdesc.chan = s - d->slot;
                    395:        x.u.slotdesc.end = SYSEX_END;
                    396:        midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(slotdesc));
                    397: }
                    398:
                    399: void
                    400: dev_midi_dump(struct dev *d)
                    401: {
                    402:        struct sysex x;
                    403:        struct slot *s;
                    404:        int i;
                    405:
                    406:        dev_midi_master(d);
                    407:        for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) {
                    408:                dev_midi_slotdesc(d, s);
                    409:                dev_midi_vol(d, s);
                    410:        }
                    411:        x.start = SYSEX_START;
                    412:        x.type = SYSEX_TYPE_EDU;
1.8       ratchov   413:        x.dev = SYSEX_DEV_ANY;
1.1       ratchov   414:        x.id0 = SYSEX_AUCAT;
                    415:        x.id1 = SYSEX_AUCAT_DUMPEND;
                    416:        x.u.dumpend.end = SYSEX_END;
                    417:        midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(dumpend));
                    418: }
                    419:
                    420: void
                    421: dev_midi_imsg(void *arg, unsigned char *msg, int len)
                    422: {
                    423: #ifdef DEBUG
                    424:        struct dev *d = arg;
                    425:
                    426:        dev_log(d);
                    427:        log_puts(": can't receive midi messages\n");
                    428:        panic();
                    429: #endif
                    430: }
                    431:
                    432: void
                    433: dev_midi_omsg(void *arg, unsigned char *msg, int len)
                    434: {
                    435:        struct dev *d = arg;
                    436:        struct sysex *x;
                    437:        unsigned int fps, chan;
                    438:
                    439:        if ((msg[0] & MIDI_CMDMASK) == MIDI_CTL && msg[1] == MIDI_CTL_VOL) {
                    440:                chan = msg[0] & MIDI_CHANMASK;
                    441:                if (chan >= DEV_NSLOT)
                    442:                        return;
                    443:                slot_setvol(d->slot + chan, msg[2]);
1.64      ratchov   444:                dev_onval(d, CTLADDR_SLOT_LEVEL(chan), msg[2]);
1.1       ratchov   445:                return;
                    446:        }
                    447:        x = (struct sysex *)msg;
                    448:        if (x->start != SYSEX_START)
                    449:                return;
                    450:        if (len < SYSEX_SIZE(empty))
                    451:                return;
                    452:        switch (x->type) {
                    453:        case SYSEX_TYPE_RT:
                    454:                if (x->id0 == SYSEX_CONTROL && x->id1 == SYSEX_MASTER) {
1.64      ratchov   455:                        if (len == SYSEX_SIZE(master)) {
1.1       ratchov   456:                                dev_master(d, x->u.master.coarse);
1.70      ratchov   457:                                if (d->master_enabled) {
                    458:                                        dev_onval(d, CTLADDR_MASTER,
                    459:                                           x->u.master.coarse);
                    460:                                }
1.64      ratchov   461:                        }
1.1       ratchov   462:                        return;
                    463:                }
                    464:                if (x->id0 != SYSEX_MMC)
                    465:                        return;
                    466:                switch (x->id1) {
                    467:                case SYSEX_MMC_STOP:
                    468:                        if (len != SYSEX_SIZE(stop))
                    469:                                return;
                    470:                        if (log_level >= 2) {
                    471:                                dev_log(d);
                    472:                                log_puts(": mmc stop\n");
                    473:                        }
                    474:                        dev_mmcstop(d);
                    475:                        break;
                    476:                case SYSEX_MMC_START:
                    477:                        if (len != SYSEX_SIZE(start))
                    478:                                return;
                    479:                        if (log_level >= 2) {
                    480:                                dev_log(d);
                    481:                                log_puts(": mmc start\n");
                    482:                        }
                    483:                        dev_mmcstart(d);
                    484:                        break;
                    485:                case SYSEX_MMC_LOC:
                    486:                        if (len != SYSEX_SIZE(loc) ||
                    487:                            x->u.loc.len != SYSEX_MMC_LOC_LEN ||
                    488:                            x->u.loc.cmd != SYSEX_MMC_LOC_CMD)
                    489:                                return;
                    490:                        switch (x->u.loc.hr >> 5) {
                    491:                        case MTC_FPS_24:
                    492:                                fps = 24;
                    493:                                break;
                    494:                        case MTC_FPS_25:
                    495:                                fps = 25;
                    496:                                break;
                    497:                        case MTC_FPS_30:
                    498:                                fps = 30;
                    499:                                break;
                    500:                        default:
                    501:                                dev_mmcstop(d);
                    502:                                return;
                    503:                        }
                    504:                        dev_mmcloc(d,
                    505:                            (x->u.loc.hr & 0x1f) * 3600 * MTC_SEC +
                    506:                             x->u.loc.min * 60 * MTC_SEC +
                    507:                             x->u.loc.sec * MTC_SEC +
1.48      ratchov   508:                             x->u.loc.fr * (MTC_SEC / fps));
1.1       ratchov   509:                        break;
                    510:                }
                    511:                break;
                    512:        case SYSEX_TYPE_EDU:
                    513:                if (x->id0 != SYSEX_AUCAT || x->id1 != SYSEX_AUCAT_DUMPREQ)
                    514:                        return;
                    515:                if (len != SYSEX_SIZE(dumpreq))
                    516:                        return;
                    517:                dev_midi_dump(d);
                    518:                break;
                    519:        }
                    520: }
                    521:
                    522: void
                    523: dev_midi_fill(void *arg, int count)
                    524: {
1.2       ratchov   525:        /* nothing to do */
1.1       ratchov   526: }
                    527:
                    528: void
                    529: dev_midi_exit(void *arg)
                    530: {
                    531:        struct dev *d = arg;
                    532:
                    533:        if (log_level >= 1) {
                    534:                dev_log(d);
                    535:                log_puts(": midi end point died\n");
                    536:        }
                    537:        if (d->pstate != DEV_CFG)
                    538:                dev_close(d);
                    539: }
                    540:
1.12      ratchov   541: int
                    542: slot_skip(struct slot *s)
1.1       ratchov   543: {
1.12      ratchov   544:        unsigned char *data = (unsigned char *)0xdeadbeef; /* please gcc */
                    545:        int max, count;
                    546:
                    547:        max = s->skip;
                    548:        while (s->skip > 0) {
                    549:                if (s->pstate != SLOT_STOP && (s->mode & MODE_RECMASK)) {
                    550:                        data = abuf_wgetblk(&s->sub.buf, &count);
                    551:                        if (count < s->round * s->sub.bpf)
                    552:                                break;
                    553:                }
                    554:                if (s->mode & MODE_PLAY) {
                    555:                        if (s->mix.buf.used < s->round * s->mix.bpf)
                    556:                                break;
                    557:                }
1.1       ratchov   558: #ifdef DEBUG
                    559:                if (log_level >= 4) {
                    560:                        slot_log(s);
1.12      ratchov   561:                        log_puts(": skipped a cycle\n");
1.1       ratchov   562:                }
                    563: #endif
1.12      ratchov   564:                if (s->pstate != SLOT_STOP && (s->mode & MODE_RECMASK)) {
                    565:                        if (s->sub.encbuf)
                    566:                                enc_sil_do(&s->sub.enc, data, s->round);
                    567:                        else
                    568:                                memset(data, 0, s->round * s->sub.bpf);
                    569:                        abuf_wcommit(&s->sub.buf, s->round * s->sub.bpf);
                    570:                }
                    571:                if (s->mode & MODE_PLAY) {
                    572:                        abuf_rdiscard(&s->mix.buf, s->round * s->mix.bpf);
1.1       ratchov   573:                }
1.23      ratchov   574:                s->skip--;
1.1       ratchov   575:        }
1.12      ratchov   576:        return max - s->skip;
1.1       ratchov   577: }
                    578:
1.32      ratchov   579: /*
                    580:  * Mix the slot input block over the output block
                    581:  */
                    582: void
                    583: dev_mix_badd(struct dev *d, struct slot *s)
1.1       ratchov   584: {
1.32      ratchov   585:        adata_t *idata, *odata, *in;
                    586:        int icount, i, offs, vol, nch;
                    587:
                    588:        odata = DEV_PBUF(d);
                    589:        idata = (adata_t *)abuf_rgetblk(&s->mix.buf, &icount);
                    590: #ifdef DEBUG
                    591:        if (icount < s->round * s->mix.bpf) {
                    592:                slot_log(s);
                    593:                log_puts(": not enough data to mix (");
                    594:                log_putu(icount);
                    595:                log_puts("bytes)\n");
                    596:                panic();
                    597:        }
                    598: #endif
                    599:
                    600:        /*
                    601:         * Apply the following processing chain:
                    602:         *
                    603:         *      dec -> resamp-> cmap
                    604:         *
                    605:         * where the first two are optional.
                    606:         */
                    607:
                    608:        in = idata;
                    609:
                    610:        if (s->mix.decbuf) {
                    611:                dec_do(&s->mix.dec, (void *)in, s->mix.decbuf, s->round);
                    612:                in = s->mix.decbuf;
                    613:        }
1.1       ratchov   614:
                    615:        if (s->mix.resampbuf) {
1.32      ratchov   616:                resamp_do(&s->mix.resamp, in, s->mix.resampbuf, s->round);
1.1       ratchov   617:                in = s->mix.resampbuf;
1.32      ratchov   618:        }
1.1       ratchov   619:
1.10      ratchov   620:        nch = s->mix.cmap.nch;
1.1       ratchov   621:        vol = ADATA_MUL(s->mix.weight, s->mix.vol) / s->mix.join;
1.32      ratchov   622:        cmap_add(&s->mix.cmap, in, odata, vol, d->round);
1.1       ratchov   623:
                    624:        offs = 0;
                    625:        for (i = s->mix.join - 1; i > 0; i--) {
                    626:                offs += nch;
1.32      ratchov   627:                cmap_add(&s->mix.cmap, in + offs, odata, vol, d->round);
1.1       ratchov   628:        }
1.32      ratchov   629:
1.1       ratchov   630:        offs = 0;
                    631:        for (i = s->mix.expand - 1; i > 0; i--) {
                    632:                offs += nch;
1.32      ratchov   633:                cmap_add(&s->mix.cmap, in, odata + offs, vol, d->round);
1.1       ratchov   634:        }
                    635:
                    636:        abuf_rdiscard(&s->mix.buf, s->round * s->mix.bpf);
                    637: }
                    638:
                    639: /*
                    640:  * Normalize input levels.
                    641:  */
                    642: void
                    643: dev_mix_adjvol(struct dev *d)
                    644: {
                    645:        unsigned int n;
                    646:        struct slot *i, *j;
1.42      ratchov   647:        int jcmax, icmax, weight;
1.1       ratchov   648:
                    649:        for (i = d->slot_list; i != NULL; i = i->next) {
                    650:                if (!(i->mode & MODE_PLAY))
                    651:                        continue;
1.42      ratchov   652:                icmax = i->opt->pmin + i->mix.nch - 1;
1.1       ratchov   653:                weight = ADATA_UNIT;
                    654:                if (d->autovol) {
                    655:                        /*
                    656:                         * count the number of inputs that have
                    657:                         * overlapping channel sets
                    658:                         */
                    659:                        n = 0;
                    660:                        for (j = d->slot_list; j != NULL; j = j->next) {
                    661:                                if (!(j->mode & MODE_PLAY))
                    662:                                        continue;
1.42      ratchov   663:                                jcmax = j->opt->pmin + j->mix.nch - 1;
                    664:                                if (i->opt->pmin <= jcmax &&
                    665:                                    icmax >= j->opt->pmin)
1.1       ratchov   666:                                        n++;
                    667:                        }
                    668:                        weight /= n;
                    669:                }
1.38      ratchov   670:                if (weight > i->opt->maxweight)
                    671:                        weight = i->opt->maxweight;
1.70      ratchov   672:                i->mix.weight = d->master_enabled ?
                    673:                    ADATA_MUL(weight, MIDI_TO_ADATA(d->master)) : weight;
1.1       ratchov   674: #ifdef DEBUG
                    675:                if (log_level >= 3) {
                    676:                        slot_log(i);
                    677:                        log_puts(": set weight: ");
                    678:                        log_puti(i->mix.weight);
                    679:                        log_puts("/");
1.38      ratchov   680:                        log_puti(i->opt->maxweight);
1.1       ratchov   681:                        log_puts("\n");
                    682:                }
                    683: #endif
                    684:        }
                    685: }
                    686:
                    687: /*
                    688:  * Copy data from slot to device
                    689:  */
                    690: void
                    691: dev_sub_bcopy(struct dev *d, struct slot *s)
                    692: {
1.32      ratchov   693:        adata_t *idata, *enc_out, *resamp_out, *cmap_out;
                    694:        void *odata;
1.15      ratchov   695:        int ocount, moffs;
1.1       ratchov   696:
1.32      ratchov   697:        int i, vol, offs, nch;
                    698:
                    699:
1.15      ratchov   700:        if (s->mode & MODE_MON) {
                    701:                moffs = d->poffs + d->round;
                    702:                if (moffs == d->psize)
                    703:                        moffs = 0;
                    704:                idata = d->pbuf + moffs * d->pchan;
                    705:        } else
                    706:                idata = d->rbuf;
1.1       ratchov   707:        odata = (adata_t *)abuf_wgetblk(&s->sub.buf, &ocount);
                    708: #ifdef DEBUG
                    709:        if (ocount < s->round * s->sub.bpf) {
                    710:                log_puts("dev_sub_bcopy: not enough space\n");
                    711:                panic();
                    712:        }
                    713: #endif
1.32      ratchov   714:
                    715:        /*
                    716:         * Apply the following processing chain:
                    717:         *
                    718:         *      cmap -> resamp -> enc
                    719:         *
                    720:         * where the last two are optional.
                    721:         */
                    722:
                    723:        enc_out = odata;
                    724:        resamp_out = s->sub.encbuf ? s->sub.encbuf : enc_out;
                    725:        cmap_out = s->sub.resampbuf ? s->sub.resampbuf : resamp_out;
                    726:
                    727:        nch = s->sub.cmap.nch;
                    728:        vol = ADATA_UNIT / s->sub.join;
                    729:        cmap_copy(&s->sub.cmap, idata, cmap_out, vol, d->round);
                    730:
                    731:        offs = 0;
                    732:        for (i = s->sub.join - 1; i > 0; i--) {
                    733:                offs += nch;
                    734:                cmap_add(&s->sub.cmap, idata + offs, cmap_out, vol, d->round);
                    735:        }
                    736:
                    737:        offs = 0;
                    738:        for (i = s->sub.expand - 1; i > 0; i--) {
                    739:                offs += nch;
                    740:                cmap_copy(&s->sub.cmap, idata, cmap_out + offs, vol, d->round);
                    741:        }
                    742:
                    743:        if (s->sub.resampbuf) {
                    744:                resamp_do(&s->sub.resamp,
                    745:                    s->sub.resampbuf, resamp_out, d->round);
                    746:        }
                    747:
                    748:        if (s->sub.encbuf)
                    749:                enc_do(&s->sub.enc, s->sub.encbuf, (void *)enc_out, s->round);
                    750:
                    751:        abuf_wcommit(&s->sub.buf, s->round * s->sub.bpf);
1.1       ratchov   752: }
                    753:
1.16      ratchov   754: /*
                    755:  * run a one block cycle: consume one recorded block from
                    756:  * rbuf and produce one play block in pbuf
                    757:  */
1.1       ratchov   758: void
1.16      ratchov   759: dev_cycle(struct dev *d)
1.1       ratchov   760: {
                    761:        struct slot *s, **ps;
1.12      ratchov   762:        unsigned char *base;
                    763:        int nsamp;
1.1       ratchov   764:
1.16      ratchov   765:        /*
                    766:         * check if the device is actually used. If it isn't,
                    767:         * then close it
                    768:         */
                    769:        if (d->slot_list == NULL && d->tstate != MMC_RUN) {
                    770:                if (log_level >= 2) {
                    771:                        dev_log(d);
                    772:                        log_puts(": device stopped\n");
                    773:                }
                    774:                dev_sio_stop(d);
                    775:                d->pstate = DEV_INIT;
                    776:                if (d->refcnt == 0)
                    777:                        dev_close(d);
                    778:                return;
                    779:        }
                    780:
                    781:        if (d->prime > 0) {
                    782: #ifdef DEBUG
                    783:                if (log_level >= 4) {
                    784:                        dev_log(d);
                    785:                        log_puts(": empty cycle, prime = ");
                    786:                        log_putu(d->prime);
                    787:                        log_puts("\n");
                    788:                }
                    789: #endif
                    790:                base = (unsigned char *)DEV_PBUF(d);
                    791:                nsamp = d->round * d->pchan;
                    792:                memset(base, 0, nsamp * sizeof(adata_t));
                    793:                if (d->encbuf) {
                    794:                        enc_do(&d->enc, (unsigned char *)DEV_PBUF(d),
                    795:                            d->encbuf, d->round);
                    796:                }
                    797:                d->prime -= d->round;
                    798:                return;
                    799:        }
                    800:
1.12      ratchov   801:        d->delta -= d->round;
1.1       ratchov   802: #ifdef DEBUG
                    803:        if (log_level >= 4) {
                    804:                dev_log(d);
1.16      ratchov   805:                log_puts(": full cycle: delta = ");
1.12      ratchov   806:                log_puti(d->delta);
                    807:                if (d->mode & MODE_PLAY) {
                    808:                        log_puts(", poffs = ");
                    809:                        log_puti(d->poffs);
                    810:                }
                    811:                log_puts("\n");
1.1       ratchov   812:        }
                    813: #endif
1.12      ratchov   814:        if (d->mode & MODE_PLAY) {
                    815:                base = (unsigned char *)DEV_PBUF(d);
                    816:                nsamp = d->round * d->pchan;
                    817:                memset(base, 0, nsamp * sizeof(adata_t));
                    818:        }
1.1       ratchov   819:        if ((d->mode & MODE_REC) && d->decbuf)
                    820:                dec_do(&d->dec, d->decbuf, (unsigned char *)d->rbuf, d->round);
                    821:        ps = &d->slot_list;
                    822:        while ((s = *ps) != NULL) {
1.12      ratchov   823: #ifdef DEBUG
                    824:                if (log_level >= 4) {
                    825:                        slot_log(s);
                    826:                        log_puts(": running");
                    827:                        log_puts(", skip = ");
                    828:                        log_puti(s->skip);
                    829:                        log_puts("\n");
                    830:                }
                    831: #endif
                    832:                /*
                    833:                 * skip cycles for XRUN_SYNC correction
                    834:                 */
                    835:                slot_skip(s);
                    836:                if (s->skip < 0) {
                    837:                        s->skip++;
1.1       ratchov   838:                        ps = &s->next;
                    839:                        continue;
                    840:                }
1.12      ratchov   841:
                    842: #ifdef DEBUG
                    843:                if (s->pstate == SLOT_STOP && !(s->mode & MODE_PLAY)) {
                    844:                        slot_log(s);
                    845:                        log_puts(": rec-only slots can't be drained\n");
                    846:                        panic();
                    847:                }
                    848: #endif
                    849:                /*
                    850:                 * check if stopped stream finished draining
                    851:                 */
                    852:                if (s->pstate == SLOT_STOP &&
                    853:                    s->mix.buf.used < s->round * s->mix.bpf) {
                    854:                        /*
                    855:                         * partial blocks are zero-filled by socket
                    856:                         * layer, so s->mix.buf.used == 0 and we can
                    857:                         * destroy the buffer
                    858:                         */
1.35      ratchov   859:                        *ps = s->next;
1.12      ratchov   860:                        s->pstate = SLOT_INIT;
                    861:                        s->ops->eof(s->arg);
1.35      ratchov   862:                        slot_freebufs(s);
1.12      ratchov   863:                        dev_mix_adjvol(d);
1.35      ratchov   864: #ifdef DEBUG
                    865:                        if (log_level >= 3) {
                    866:                                slot_log(s);
                    867:                                log_puts(": drained\n");
                    868:                        }
                    869: #endif
1.1       ratchov   870:                        continue;
                    871:                }
1.23      ratchov   872:
1.12      ratchov   873:                /*
                    874:                 * check for xruns
                    875:                 */
1.23      ratchov   876:                if (((s->mode & MODE_PLAY) &&
1.12      ratchov   877:                        s->mix.buf.used < s->round * s->mix.bpf) ||
                    878:                    ((s->mode & MODE_RECMASK) &&
                    879:                        s->sub.buf.len - s->sub.buf.used <
                    880:                        s->round * s->sub.bpf)) {
                    881:
                    882: #ifdef DEBUG
                    883:                        if (log_level >= 3) {
                    884:                                slot_log(s);
                    885:                                log_puts(": xrun, pause cycle\n");
                    886:                        }
                    887: #endif
1.1       ratchov   888:                        if (s->xrun == XRUN_IGNORE) {
                    889:                                s->delta -= s->round;
1.12      ratchov   890:                                ps = &s->next;
                    891:                        } else if (s->xrun == XRUN_SYNC) {
                    892:                                s->skip++;
                    893:                                ps = &s->next;
                    894:                        } else if (s->xrun == XRUN_ERROR) {
                    895:                                s->ops->exit(s->arg);
                    896:                                *ps = s->next;
                    897:                        } else {
                    898: #ifdef DEBUG
                    899:                                slot_log(s);
                    900:                                log_puts(": bad xrun mode\n");
                    901:                                panic();
                    902: #endif
                    903:                        }
                    904:                        continue;
                    905:                }
                    906:                if ((s->mode & MODE_RECMASK) && !(s->pstate == SLOT_STOP)) {
                    907:                        if (s->sub.prime == 0) {
                    908:                                dev_sub_bcopy(d, s);
                    909:                                s->ops->flush(s->arg);
                    910:                        } else {
1.1       ratchov   911: #ifdef DEBUG
                    912:                                if (log_level >= 3) {
                    913:                                        slot_log(s);
1.12      ratchov   914:                                        log_puts(": prime = ");
                    915:                                        log_puti(s->sub.prime);
                    916:                                        log_puts("\n");
1.1       ratchov   917:                                }
                    918: #endif
1.12      ratchov   919:                                s->sub.prime--;
1.1       ratchov   920:                        }
                    921:                }
1.15      ratchov   922:                if (s->mode & MODE_PLAY) {
                    923:                        dev_mix_badd(d, s);
                    924:                        if (s->pstate != SLOT_STOP)
                    925:                                s->ops->fill(s->arg);
                    926:                }
1.1       ratchov   927:                ps = &s->next;
                    928:        }
1.12      ratchov   929:        if ((d->mode & MODE_PLAY) && d->encbuf) {
                    930:                enc_do(&d->enc, (unsigned char *)DEV_PBUF(d),
                    931:                    d->encbuf, d->round);
                    932:        }
1.1       ratchov   933: }
                    934:
                    935: /*
                    936:  * called at every clock tick by the device
                    937:  */
                    938: void
                    939: dev_onmove(struct dev *d, int delta)
                    940: {
                    941:        long long pos;
1.23      ratchov   942:        struct slot *s, *snext;
1.12      ratchov   943:
                    944:        d->delta += delta;
1.1       ratchov   945:
                    946:        for (s = d->slot_list; s != NULL; s = snext) {
1.12      ratchov   947:                /*
                    948:                 * s->ops->onmove() may remove the slot
                    949:                 */
1.1       ratchov   950:                snext = s->next;
                    951:                pos = (long long)delta * s->round + s->delta_rem;
                    952:                s->delta_rem = pos % d->round;
                    953:                s->delta += pos / (int)d->round;
                    954:                if (s->delta >= 0)
1.14      ratchov   955:                        s->ops->onmove(s->arg);
1.1       ratchov   956:        }
                    957:        if (d->tstate == MMC_RUN)
                    958:                dev_midi_qfr(d, delta);
                    959: }
                    960:
                    961: void
                    962: dev_master(struct dev *d, unsigned int master)
                    963: {
1.70      ratchov   964:        struct ctl *c;
                    965:        unsigned int v;
                    966:
1.1       ratchov   967:        if (log_level >= 2) {
                    968:                dev_log(d);
                    969:                log_puts(": master volume set to ");
                    970:                log_putu(master);
                    971:                log_puts("\n");
                    972:        }
1.70      ratchov   973:        if (d->master_enabled) {
                    974:                d->master = master;
                    975:                if (d->mode & MODE_PLAY)
                    976:                        dev_mix_adjvol(d);
                    977:        } else {
                    978:                for (c = d->ctl_list; c != NULL; c = c->next) {
                    979:                        if (c->type != CTL_NUM ||
                    980:                            strcmp(c->group, "") != 0 ||
                    981:                            strcmp(c->node0.name, "output") != 0 ||
                    982:                            strcmp(c->func, "level") != 0)
                    983:                                continue;
                    984:                        v = (master * c->maxval + 64) / 127;
                    985:                        dev_setctl(d, c->addr, v);
                    986:                }
                    987:        }
1.1       ratchov   988: }
                    989:
                    990: /*
                    991:  * return the latency that a stream would have if it's attached
                    992:  */
                    993: int
                    994: dev_getpos(struct dev *d)
                    995: {
                    996:        return (d->mode & MODE_PLAY) ? -d->bufsz : 0;
                    997: }
                    998:
                    999: /*
                   1000:  * Create a sndio device
                   1001:  */
                   1002: struct dev *
                   1003: dev_new(char *path, struct aparams *par,
                   1004:     unsigned int mode, unsigned int bufsz, unsigned int round,
                   1005:     unsigned int rate, unsigned int hold, unsigned int autovol)
                   1006: {
                   1007:        struct dev *d;
                   1008:        unsigned int i;
                   1009:
                   1010:        if (dev_sndnum == DEV_NMAX) {
                   1011:                if (log_level >= 1)
                   1012:                        log_puts("too many devices\n");
                   1013:                return NULL;
                   1014:        }
                   1015:        d = xmalloc(sizeof(struct dev));
1.73      ratchov  1016:        d->alt_list = NULL;
                   1017:        dev_addname(d,path);
1.1       ratchov  1018:        d->num = dev_sndnum++;
1.36      ratchov  1019:        d->opt_list = NULL;
1.76      ratchov  1020:        d->alt_num = -1;
1.1       ratchov  1021:
                   1022:        /*
                   1023:         * XXX: below, we allocate a midi input buffer, since we don't
                   1024:         *      receive raw midi data, so no need to allocate a input
                   1025:         *      ibuf.  Possibly set imsg & fill callbacks to NULL and
                   1026:         *      use this to in midi_new() to check if buffers need to be
                   1027:         *      allocated
                   1028:         */
                   1029:        d->midi = midi_new(&dev_midiops, d, MODE_MIDIIN | MODE_MIDIOUT);
                   1030:        midi_tag(d->midi, d->num);
                   1031:        d->reqpar = *par;
                   1032:        d->reqmode = mode;
                   1033:        d->reqpchan = d->reqrchan = 0;
                   1034:        d->reqbufsz = bufsz;
                   1035:        d->reqround = round;
                   1036:        d->reqrate = rate;
                   1037:        d->hold = hold;
                   1038:        d->autovol = autovol;
                   1039:        d->refcnt = 0;
                   1040:        d->pstate = DEV_CFG;
                   1041:        d->serial = 0;
                   1042:        for (i = 0; i < DEV_NSLOT; i++) {
                   1043:                d->slot[i].unit = i;
                   1044:                d->slot[i].ops = NULL;
                   1045:                d->slot[i].vol = MIDI_MAXCTL;
                   1046:                d->slot[i].serial = d->serial++;
1.69      ratchov  1047:                memset(d->slot[i].name, 0, SLOT_NAMEMAX);
1.1       ratchov  1048:        }
1.64      ratchov  1049:        for (i = 0; i < DEV_NCTLSLOT; i++) {
                   1050:                d->ctlslot[i].ops = NULL;
                   1051:                d->ctlslot[i].dev = d;
                   1052:                d->ctlslot[i].mask = 0;
                   1053:                d->ctlslot[i].mode = 0;
                   1054:        }
1.1       ratchov  1055:        d->slot_list = NULL;
                   1056:        d->master = MIDI_MAXCTL;
                   1057:        d->mtc.origin = 0;
                   1058:        d->tstate = MMC_STOP;
1.64      ratchov  1059:        d->ctl_list = NULL;
1.1       ratchov  1060:        d->next = dev_list;
                   1061:        dev_list = d;
                   1062:        return d;
                   1063: }
                   1064:
                   1065: /*
1.73      ratchov  1066:  * add a alternate name
                   1067:  */
                   1068: int
                   1069: dev_addname(struct dev *d, char *name)
                   1070: {
                   1071:        struct dev_alt *a;
                   1072:
                   1073:        if (d->alt_list != NULL && d->alt_list->idx == DEV_NMAX - 1) {
                   1074:                log_puts(name);
                   1075:                log_puts(": too many alternate names\n");
                   1076:                return 0;
                   1077:        }
                   1078:        a = xmalloc(sizeof(struct dev_alt));
                   1079:        a->name = name;
                   1080:        a->idx = (d->alt_list == NULL) ? 0 : d->alt_list->idx + 1;
                   1081:        a->next = d->alt_list;
                   1082:        d->alt_list = a;
                   1083:        return 1;
                   1084: }
                   1085:
                   1086: /*
1.75      ratchov  1087:  * set prefered alt device name
                   1088:  */
                   1089: void
                   1090: dev_setalt(struct dev *d, unsigned int idx)
                   1091: {
                   1092:        struct dev_alt **pa, *a;
                   1093:
                   1094:        /* find alt with given index */
                   1095:        for (pa = &d->alt_list; (a = *pa)->idx != idx; pa = &a->next)
                   1096:                ;
                   1097:
                   1098:        /* detach from list */
                   1099:        *pa = a->next;
                   1100:
                   1101:        /* attach at head */
                   1102:        a->next = d->alt_list;
                   1103:        d->alt_list = a;
                   1104:
                   1105:        /* reopen device with the new alt */
                   1106:        if (idx != d->alt_num)
                   1107:                dev_reopen(d);
                   1108: }
                   1109:
                   1110: /*
1.1       ratchov  1111:  * adjust device parameters and mode
                   1112:  */
                   1113: void
                   1114: dev_adjpar(struct dev *d, int mode,
1.25      ratchov  1115:     int pmax, int rmax)
1.1       ratchov  1116: {
                   1117:        d->reqmode |= mode & MODE_AUDIOMASK;
                   1118:        if (mode & MODE_PLAY) {
                   1119:                if (d->reqpchan < pmax + 1)
                   1120:                        d->reqpchan = pmax + 1;
                   1121:        }
                   1122:        if (mode & MODE_REC) {
                   1123:                if (d->reqrchan < rmax + 1)
                   1124:                        d->reqrchan = rmax + 1;
                   1125:        }
                   1126: }
                   1127:
                   1128: /*
                   1129:  * Open the device with the dev_reqxxx capabilities. Setup a mixer, demuxer,
                   1130:  * monitor, midi control, and any necessary conversions.
                   1131:  */
                   1132: int
1.59      ratchov  1133: dev_allocbufs(struct dev *d)
1.1       ratchov  1134: {
                   1135:        if (d->mode & MODE_REC) {
                   1136:                /*
                   1137:                 * Create device <-> demuxer buffer
                   1138:                 */
                   1139:                d->rbuf = xmalloc(d->round * d->rchan * sizeof(adata_t));
                   1140:
                   1141:                /*
                   1142:                 * Insert a converter, if needed.
                   1143:                 */
                   1144:                if (!aparams_native(&d->par)) {
                   1145:                        dec_init(&d->dec, &d->par, d->rchan);
                   1146:                        d->decbuf = xmalloc(d->round * d->rchan * d->par.bps);
                   1147:                } else
                   1148:                        d->decbuf = NULL;
                   1149:        }
                   1150:        if (d->mode & MODE_PLAY) {
                   1151:                /*
                   1152:                 * Create device <-> mixer buffer
                   1153:                 */
                   1154:                d->poffs = 0;
1.15      ratchov  1155:                d->psize = d->bufsz + d->round;
                   1156:                d->pbuf = xmalloc(d->psize * d->pchan * sizeof(adata_t));
1.1       ratchov  1157:                d->mode |= MODE_MON;
                   1158:
                   1159:                /*
                   1160:                 * Append a converter, if needed.
                   1161:                 */
                   1162:                if (!aparams_native(&d->par)) {
                   1163:                        enc_init(&d->enc, &d->par, d->pchan);
                   1164:                        d->encbuf = xmalloc(d->round * d->pchan * d->par.bps);
                   1165:                } else
                   1166:                        d->encbuf = NULL;
                   1167:        }
                   1168:        if (log_level >= 2) {
                   1169:                dev_log(d);
                   1170:                log_puts(": ");
                   1171:                log_putu(d->rate);
                   1172:                log_puts("Hz, ");
                   1173:                aparams_log(&d->par);
                   1174:                if (d->mode & MODE_PLAY) {
                   1175:                        log_puts(", play 0:");
                   1176:                        log_puti(d->pchan - 1);
                   1177:                }
                   1178:                if (d->mode & MODE_REC) {
                   1179:                        log_puts(", rec 0:");
                   1180:                        log_puti(d->rchan - 1);
                   1181:                }
                   1182:                log_puts(", ");
                   1183:                log_putu(d->bufsz / d->round);
                   1184:                log_puts(" blocks of ");
                   1185:                log_putu(d->round);
                   1186:                log_puts(" frames\n");
                   1187:        }
1.57      ratchov  1188:        return 1;
                   1189: }
                   1190:
                   1191: /*
                   1192:  * Reset parameters and open the device.
                   1193:  */
                   1194: int
                   1195: dev_open(struct dev *d)
                   1196: {
1.64      ratchov  1197:        int i;
                   1198:        char name[CTL_NAMEMAX];
1.75      ratchov  1199:        struct dev_alt *a;
1.64      ratchov  1200:
1.70      ratchov  1201:        d->master_enabled = 0;
1.57      ratchov  1202:        d->mode = d->reqmode;
                   1203:        d->round = d->reqround;
                   1204:        d->bufsz = d->reqbufsz;
                   1205:        d->rate = d->reqrate;
                   1206:        d->pchan = d->reqpchan;
                   1207:        d->rchan = d->reqrchan;
                   1208:        d->par = d->reqpar;
                   1209:        if (d->pchan == 0)
                   1210:                d->pchan = 2;
                   1211:        if (d->rchan == 0)
                   1212:                d->rchan = 2;
1.59      ratchov  1213:        if (!dev_sio_open(d)) {
                   1214:                if (log_level >= 1) {
                   1215:                        dev_log(d);
                   1216:                        log_puts(": failed to open audio device\n");
                   1217:                }
                   1218:                return 0;
                   1219:        }
                   1220:        if (!dev_allocbufs(d))
1.57      ratchov  1221:                return 0;
1.64      ratchov  1222:
                   1223:        for (i = 0; i < DEV_NSLOT; i++) {
1.69      ratchov  1224:                if (d->slot[i].name[0] == 0)
                   1225:                        continue;
1.64      ratchov  1226:                slot_ctlname(&d->slot[i], name, CTL_NAMEMAX);
                   1227:                dev_addctl(d, "app", CTL_NUM,
                   1228:                    CTLADDR_SLOT_LEVEL(i),
                   1229:                    name, -1, "level",
                   1230:                    NULL, -1, 127, d->slot[i].vol);
                   1231:        }
                   1232:
1.77      ratchov  1233:        /* if there are multiple alt devs, add server.device knob */
                   1234:        if (d->alt_list->next != NULL) {
                   1235:                for (a = d->alt_list; a != NULL; a = a->next) {
                   1236:                        snprintf(name, sizeof(name), "%d", a->idx);
                   1237:                        dev_addctl(d, "", CTL_SEL,
                   1238:                            CTLADDR_ALT_SEL + a->idx,
                   1239:                            "server", -1, "device",
                   1240:                            name, -1, 1, a->idx == d->alt_num);
                   1241:                }
1.75      ratchov  1242:        }
                   1243:
1.59      ratchov  1244:        d->pstate = DEV_INIT;
1.1       ratchov  1245:        return 1;
                   1246: }
                   1247:
                   1248: /*
1.72      ratchov  1249:  * Force all slots to exit and close device, called after an error
1.55      ratchov  1250:  */
                   1251: void
1.72      ratchov  1252: dev_abort(struct dev *d)
1.55      ratchov  1253: {
                   1254:        int i;
                   1255:        struct slot *s;
1.64      ratchov  1256:        struct ctlslot *c;
1.55      ratchov  1257:
                   1258:        for (s = d->slot, i = DEV_NSLOT; i > 0; i--, s++) {
                   1259:                if (s->ops)
                   1260:                        s->ops->exit(s->arg);
                   1261:                s->ops = NULL;
                   1262:        }
                   1263:        d->slot_list = NULL;
1.64      ratchov  1264:
                   1265:        for (c = d->ctlslot, i = DEV_NCTLSLOT; i > 0; i--, c++) {
                   1266:                if (c->ops)
                   1267:                        c->ops->exit(c->arg);
                   1268:                c->ops = NULL;
                   1269:        }
1.72      ratchov  1270:
                   1271:        if (d->pstate != DEV_CFG)
                   1272:                dev_close(d);
1.55      ratchov  1273: }
                   1274:
                   1275: /*
1.1       ratchov  1276:  * force the device to go in DEV_CFG state, the caller is supposed to
                   1277:  * ensure buffers are drained
                   1278:  */
                   1279: void
1.59      ratchov  1280: dev_freebufs(struct dev *d)
1.1       ratchov  1281: {
                   1282: #ifdef DEBUG
                   1283:        if (log_level >= 3) {
                   1284:                dev_log(d);
                   1285:                log_puts(": closing\n");
                   1286:        }
                   1287: #endif
                   1288:        if (d->mode & MODE_PLAY) {
                   1289:                if (d->encbuf != NULL)
                   1290:                        xfree(d->encbuf);
                   1291:                xfree(d->pbuf);
                   1292:        }
                   1293:        if (d->mode & MODE_REC) {
                   1294:                if (d->decbuf != NULL)
                   1295:                        xfree(d->decbuf);
                   1296:                xfree(d->rbuf);
                   1297:        }
1.58      ratchov  1298: }
                   1299:
                   1300: /*
                   1301:  * Close the device and exit all slots
                   1302:  */
                   1303: void
                   1304: dev_close(struct dev *d)
                   1305: {
1.64      ratchov  1306:        struct ctl *c;
                   1307:
1.59      ratchov  1308:        d->pstate = DEV_CFG;
                   1309:        dev_sio_close(d);
                   1310:        dev_freebufs(d);
1.64      ratchov  1311:
                   1312:        /* there are no clients, just free remaining local controls */
                   1313:        while ((c = d->ctl_list) != NULL) {
                   1314:                d->ctl_list = c->next;
                   1315:                xfree(c);
                   1316:        }
1.1       ratchov  1317: }
                   1318:
1.62      ratchov  1319: /*
                   1320:  * Close the device, but attempt to migrate everything to a new sndio
                   1321:  * device.
                   1322:  */
                   1323: int
                   1324: dev_reopen(struct dev *d)
                   1325: {
                   1326:        struct slot *s;
                   1327:        long long pos;
                   1328:        unsigned int pstate;
                   1329:        int delta;
                   1330:
                   1331:        /* not opened */
                   1332:        if (d->pstate == DEV_CFG)
                   1333:                return 1;
                   1334:
                   1335:        /* save state */
                   1336:        delta = d->delta;
                   1337:        pstate = d->pstate;
                   1338:
                   1339:        if (!dev_sio_reopen(d))
                   1340:                return 0;
                   1341:
                   1342:        /* reopen returns a stopped device */
                   1343:        d->pstate = DEV_INIT;
                   1344:
                   1345:        /* reallocate new buffers, with new parameters */
                   1346:        dev_freebufs(d);
                   1347:        dev_allocbufs(d);
                   1348:
                   1349:        /*
                   1350:         * adjust time positions, make anything go back delta ticks, so
                   1351:         * that the new device can start at zero
                   1352:         */
                   1353:        for (s = d->slot_list; s != NULL; s = s->next) {
                   1354:                pos = (long long)s->delta * d->round + s->delta_rem;
                   1355:                pos -= (long long)delta * s->round;
                   1356:                s->delta_rem = pos % (int)d->round;
                   1357:                s->delta = pos / (int)d->round;
                   1358:                if (log_level >= 3) {
                   1359:                        slot_log(s);
                   1360:                        log_puts(": adjusted: delta -> ");
                   1361:                        log_puti(s->delta);
                   1362:                        log_puts(", delta_rem -> ");
                   1363:                        log_puti(s->delta_rem);
                   1364:                        log_puts("\n");
                   1365:                }
                   1366:
                   1367:                /* reinitilize the format conversion chain */
                   1368:                slot_initconv(s);
                   1369:        }
                   1370:        if (d->tstate == MMC_RUN) {
                   1371:                d->mtc.delta -= delta * MTC_SEC;
                   1372:                if (log_level >= 2) {
                   1373:                        dev_log(d);
                   1374:                        log_puts(": adjusted mtc: delta ->");
                   1375:                        log_puti(d->mtc.delta);
                   1376:                        log_puts("\n");
                   1377:                }
                   1378:        }
                   1379:
1.71      ratchov  1380:        /* remove old controls and add new ones */
                   1381:        dev_sioctl_close(d);
1.64      ratchov  1382:        dev_sioctl_open(d);
                   1383:
1.62      ratchov  1384:        /* start the device if needed */
                   1385:        if (pstate == DEV_RUN)
                   1386:                dev_wakeup(d);
                   1387:
                   1388:        return 1;
                   1389: }
                   1390:
1.1       ratchov  1391: int
                   1392: dev_ref(struct dev *d)
                   1393: {
                   1394: #ifdef DEBUG
                   1395:        if (log_level >= 3) {
                   1396:                dev_log(d);
                   1397:                log_puts(": device requested\n");
                   1398:        }
                   1399: #endif
                   1400:        if (d->pstate == DEV_CFG && !dev_open(d))
                   1401:                return 0;
                   1402:        d->refcnt++;
                   1403:        return 1;
                   1404: }
                   1405:
                   1406: void
                   1407: dev_unref(struct dev *d)
                   1408: {
                   1409: #ifdef DEBUG
                   1410:        if (log_level >= 3) {
                   1411:                dev_log(d);
                   1412:                log_puts(": device released\n");
                   1413:        }
                   1414: #endif
                   1415:        d->refcnt--;
                   1416:        if (d->refcnt == 0 && d->pstate == DEV_INIT)
                   1417:                dev_close(d);
                   1418: }
                   1419:
                   1420: /*
                   1421:  * initialize the device with the current parameters
                   1422:  */
                   1423: int
                   1424: dev_init(struct dev *d)
                   1425: {
                   1426:        if ((d->reqmode & MODE_AUDIOMASK) == 0) {
                   1427: #ifdef DEBUG
                   1428:                    dev_log(d);
                   1429:                    log_puts(": has no streams\n");
                   1430: #endif
                   1431:                    return 0;
                   1432:        }
                   1433:        if (d->hold && !dev_ref(d))
                   1434:                return 0;
                   1435:        return 1;
                   1436: }
                   1437:
                   1438: /*
                   1439:  * Unless the device is already in process of closing, request it to close
                   1440:  */
                   1441: void
                   1442: dev_done(struct dev *d)
                   1443: {
                   1444: #ifdef DEBUG
                   1445:        if (log_level >= 3) {
                   1446:                dev_log(d);
                   1447:                log_puts(": draining\n");
                   1448:        }
                   1449: #endif
1.20      ratchov  1450:        if (d->tstate != MMC_STOP)
                   1451:                dev_mmcstop(d);
1.1       ratchov  1452:        if (d->hold)
                   1453:                dev_unref(d);
                   1454: }
                   1455:
                   1456: struct dev *
                   1457: dev_bynum(int num)
                   1458: {
                   1459:        struct dev *d;
                   1460:
                   1461:        for (d = dev_list; d != NULL; d = d->next) {
1.19      ratchov  1462:                if (d->num == num)
1.1       ratchov  1463:                        return d;
                   1464:        }
                   1465:        return NULL;
                   1466: }
                   1467:
                   1468: /*
                   1469:  * Free the device
                   1470:  */
                   1471: void
                   1472: dev_del(struct dev *d)
                   1473: {
                   1474:        struct dev **p;
1.73      ratchov  1475:        struct dev_alt *a;
1.1       ratchov  1476:
                   1477: #ifdef DEBUG
                   1478:        if (log_level >= 3) {
                   1479:                dev_log(d);
                   1480:                log_puts(": deleting\n");
                   1481:        }
                   1482: #endif
1.36      ratchov  1483:        while (d->opt_list != NULL)
                   1484:                opt_del(d, d->opt_list);
1.1       ratchov  1485:        if (d->pstate != DEV_CFG)
                   1486:                dev_close(d);
                   1487:        for (p = &dev_list; *p != d; p = &(*p)->next) {
                   1488: #ifdef DEBUG
                   1489:                if (*p == NULL) {
                   1490:                        dev_log(d);
                   1491:                        log_puts(": device to delete not on the list\n");
                   1492:                        panic();
                   1493:                }
                   1494: #endif
                   1495:        }
                   1496:        midi_del(d->midi);
                   1497:        *p = d->next;
1.73      ratchov  1498:        while ((a = d->alt_list) != NULL) {
                   1499:                d->alt_list = a->next;
                   1500:                xfree(a);
                   1501:        }
1.1       ratchov  1502:        xfree(d);
                   1503: }
                   1504:
                   1505: unsigned int
                   1506: dev_roundof(struct dev *d, unsigned int newrate)
                   1507: {
                   1508:        return (d->round * newrate + d->rate / 2) / d->rate;
                   1509: }
                   1510:
                   1511: /*
                   1512:  * If the device is paused, then resume it.
                   1513:  */
                   1514: void
                   1515: dev_wakeup(struct dev *d)
                   1516: {
                   1517:        if (d->pstate == DEV_INIT) {
                   1518:                if (log_level >= 2) {
                   1519:                        dev_log(d);
                   1520:                        log_puts(": device started\n");
                   1521:                }
                   1522:                if (d->mode & MODE_PLAY) {
                   1523:                        d->prime = d->bufsz;
                   1524:                } else {
                   1525:                        d->prime = 0;
                   1526:                }
1.16      ratchov  1527:                d->poffs = 0;
1.12      ratchov  1528:
1.23      ratchov  1529:                /*
1.16      ratchov  1530:                 * empty cycles don't increment delta, so it's ok to
                   1531:                 * start at 0
                   1532:                 **/
1.23      ratchov  1533:                d->delta = 0;
1.12      ratchov  1534:
1.1       ratchov  1535:                d->pstate = DEV_RUN;
                   1536:                dev_sio_start(d);
                   1537:        }
                   1538: }
                   1539:
                   1540: /*
                   1541:  * check that all clients controlled by MMC are ready to start, if so,
                   1542:  * attach them all at the same position
                   1543:  */
                   1544: void
                   1545: dev_sync_attach(struct dev *d)
                   1546: {
                   1547:        int i;
                   1548:        struct slot *s;
                   1549:
                   1550:        if (d->tstate != MMC_START) {
                   1551:                if (log_level >= 2) {
                   1552:                        dev_log(d);
                   1553:                        log_puts(": not started by mmc yet, waiting...\n");
                   1554:                }
                   1555:                return;
                   1556:        }
                   1557:        for (i = 0; i < DEV_NSLOT; i++) {
                   1558:                s = d->slot + i;
1.46      ratchov  1559:                if (!s->ops || !s->opt->mmc)
1.1       ratchov  1560:                        continue;
1.46      ratchov  1561:                if (s->pstate != SLOT_READY) {
1.1       ratchov  1562: #ifdef DEBUG
                   1563:                        if (log_level >= 3) {
                   1564:                                slot_log(s);
                   1565:                                log_puts(": not ready, start delayed\n");
                   1566:                        }
                   1567: #endif
                   1568:                        return;
                   1569:                }
                   1570:        }
                   1571:        if (!dev_ref(d))
                   1572:                return;
                   1573:        for (i = 0; i < DEV_NSLOT; i++) {
                   1574:                s = d->slot + i;
1.46      ratchov  1575:                if (!s->ops || !s->opt->mmc)
1.1       ratchov  1576:                        continue;
1.46      ratchov  1577:                slot_attach(s);
1.79    ! ratchov  1578:                s->pstate = SLOT_RUN;
1.1       ratchov  1579:        }
                   1580:        d->tstate = MMC_RUN;
                   1581:        dev_midi_full(d);
                   1582:        dev_wakeup(d);
                   1583: }
                   1584:
                   1585: /*
                   1586:  * start all slots simultaneously
                   1587:  */
                   1588: void
                   1589: dev_mmcstart(struct dev *d)
                   1590: {
                   1591:        if (d->tstate == MMC_STOP) {
                   1592:                d->tstate = MMC_START;
                   1593:                dev_sync_attach(d);
                   1594: #ifdef DEBUG
                   1595:        } else {
                   1596:                if (log_level >= 3) {
                   1597:                        dev_log(d);
                   1598:                        log_puts(": ignoring mmc start\n");
                   1599:                }
                   1600: #endif
                   1601:        }
                   1602: }
                   1603:
                   1604: /*
                   1605:  * stop all slots simultaneously
                   1606:  */
                   1607: void
                   1608: dev_mmcstop(struct dev *d)
                   1609: {
                   1610:        switch (d->tstate) {
                   1611:        case MMC_START:
                   1612:                d->tstate = MMC_STOP;
                   1613:                return;
                   1614:        case MMC_RUN:
                   1615:                d->tstate = MMC_STOP;
                   1616:                dev_unref(d);
                   1617:                break;
                   1618:        default:
                   1619: #ifdef DEBUG
                   1620:                if (log_level >= 3) {
                   1621:                        dev_log(d);
                   1622:                        log_puts(": ignored mmc stop\n");
                   1623:                }
                   1624: #endif
                   1625:                return;
                   1626:        }
                   1627: }
                   1628:
                   1629: /*
                   1630:  * relocate all slots simultaneously
                   1631:  */
                   1632: void
                   1633: dev_mmcloc(struct dev *d, unsigned int origin)
                   1634: {
                   1635:        if (log_level >= 2) {
                   1636:                dev_log(d);
                   1637:                log_puts(": relocated to ");
                   1638:                log_putu(origin);
                   1639:                log_puts("\n");
                   1640:        }
                   1641:        if (d->tstate == MMC_RUN)
                   1642:                dev_mmcstop(d);
                   1643:        d->mtc.origin = origin;
                   1644:        if (d->tstate == MMC_RUN)
                   1645:                dev_mmcstart(d);
                   1646: }
                   1647:
1.35      ratchov  1648: /*
                   1649:  * allocate buffers & conversion chain
                   1650:  */
                   1651: void
1.60      ratchov  1652: slot_initconv(struct slot *s)
1.35      ratchov  1653: {
1.63      ratchov  1654:        unsigned int dev_nch;
1.35      ratchov  1655:        struct dev *d = s->dev;
                   1656:
                   1657:        if (s->mode & MODE_PLAY) {
                   1658:                cmap_init(&s->mix.cmap,
1.42      ratchov  1659:                    s->opt->pmin, s->opt->pmin + s->mix.nch - 1,
                   1660:                    s->opt->pmin, s->opt->pmin + s->mix.nch - 1,
1.35      ratchov  1661:                    0, d->pchan - 1,
1.40      ratchov  1662:                    s->opt->pmin, s->opt->pmax);
1.35      ratchov  1663:                if (!aparams_native(&s->par)) {
1.42      ratchov  1664:                        dec_init(&s->mix.dec, &s->par, s->mix.nch);
1.35      ratchov  1665:                }
                   1666:                if (s->rate != d->rate) {
                   1667:                        resamp_init(&s->mix.resamp, s->round, d->round,
1.42      ratchov  1668:                            s->mix.nch);
1.35      ratchov  1669:                }
1.61      ratchov  1670:                s->mix.join = 1;
                   1671:                s->mix.expand = 1;
1.63      ratchov  1672:                if (s->opt->dup && s->mix.cmap.nch > 0) {
                   1673:                        dev_nch = d->pchan < (s->opt->pmax + 1) ?
                   1674:                            d->pchan - s->opt->pmin :
                   1675:                            s->opt->pmax - s->opt->pmin + 1;
                   1676:                        if (dev_nch > s->mix.nch)
                   1677:                                s->mix.expand = dev_nch / s->mix.nch;
                   1678:                        else if (s->mix.nch > dev_nch)
                   1679:                                s->mix.join = s->mix.nch / dev_nch;
1.61      ratchov  1680:                }
1.35      ratchov  1681:        }
                   1682:
                   1683:        if (s->mode & MODE_RECMASK) {
1.63      ratchov  1684:                unsigned int outchan = (s->mode & MODE_MON) ?
                   1685:                    d->pchan : d->rchan;
                   1686:
1.35      ratchov  1687:                cmap_init(&s->sub.cmap,
1.63      ratchov  1688:                    0, outchan - 1,
1.40      ratchov  1689:                    s->opt->rmin, s->opt->rmax,
1.42      ratchov  1690:                    s->opt->rmin, s->opt->rmin + s->sub.nch - 1,
                   1691:                    s->opt->rmin, s->opt->rmin + s->sub.nch - 1);
1.35      ratchov  1692:                if (s->rate != d->rate) {
                   1693:                        resamp_init(&s->sub.resamp, d->round, s->round,
1.42      ratchov  1694:                            s->sub.nch);
1.35      ratchov  1695:                }
                   1696:                if (!aparams_native(&s->par)) {
1.42      ratchov  1697:                        enc_init(&s->sub.enc, &s->par, s->sub.nch);
1.61      ratchov  1698:                }
                   1699:                s->sub.join = 1;
                   1700:                s->sub.expand = 1;
1.63      ratchov  1701:                if (s->opt->dup && s->sub.cmap.nch > 0) {
                   1702:                        dev_nch = outchan < (s->opt->rmax + 1) ?
                   1703:                            outchan - s->opt->rmin :
                   1704:                            s->opt->rmax - s->opt->rmin + 1;
                   1705:                        if (dev_nch > s->sub.nch)
                   1706:                                s->sub.join = dev_nch / s->sub.nch;
                   1707:                        else if (s->sub.nch > dev_nch)
                   1708:                                s->sub.expand = s->sub.nch / dev_nch;
1.35      ratchov  1709:                }
                   1710:
                   1711:                /*
                   1712:                 * cmap_copy() doesn't write samples in all channels,
                   1713:                 * for instance when mono->stereo conversion is
                   1714:                 * disabled. So we have to prefill cmap_copy() output
                   1715:                 * with silence.
                   1716:                 */
                   1717:                if (s->sub.resampbuf) {
                   1718:                        memset(s->sub.resampbuf, 0,
1.42      ratchov  1719:                            d->round * s->sub.nch * sizeof(adata_t));
1.35      ratchov  1720:                } else if (s->sub.encbuf) {
                   1721:                        memset(s->sub.encbuf, 0,
1.42      ratchov  1722:                            s->round * s->sub.nch * sizeof(adata_t));
1.35      ratchov  1723:                } else {
                   1724:                        memset(s->sub.buf.data, 0,
1.42      ratchov  1725:                            s->appbufsz * s->sub.nch * sizeof(adata_t));
1.35      ratchov  1726:                }
                   1727:        }
1.60      ratchov  1728: }
                   1729:
                   1730: /*
                   1731:  * allocate buffers & conversion chain
                   1732:  */
                   1733: void
                   1734: slot_allocbufs(struct slot *s)
                   1735: {
                   1736:        struct dev *d = s->dev;
                   1737:
                   1738:        if (s->mode & MODE_PLAY) {
                   1739:                s->mix.bpf = s->par.bps * s->mix.nch;
                   1740:                abuf_init(&s->mix.buf, s->appbufsz * s->mix.bpf);
                   1741:
                   1742:                s->mix.decbuf = NULL;
                   1743:                s->mix.resampbuf = NULL;
                   1744:                if (!aparams_native(&s->par)) {
                   1745:                        s->mix.decbuf =
                   1746:                            xmalloc(s->round * s->mix.nch * sizeof(adata_t));
                   1747:                }
                   1748:                if (s->rate != d->rate) {
                   1749:                        s->mix.resampbuf =
                   1750:                            xmalloc(d->round * s->mix.nch * sizeof(adata_t));
                   1751:                }
                   1752:        }
                   1753:
                   1754:        if (s->mode & MODE_RECMASK) {
                   1755:                s->sub.bpf = s->par.bps * s->sub.nch;
                   1756:                abuf_init(&s->sub.buf, s->appbufsz * s->sub.bpf);
                   1757:
                   1758:                s->sub.encbuf = NULL;
                   1759:                s->sub.resampbuf = NULL;
                   1760:                if (s->rate != d->rate) {
                   1761:                        s->sub.resampbuf =
                   1762:                            xmalloc(d->round * s->sub.nch * sizeof(adata_t));
                   1763:                }
                   1764:                if (!aparams_native(&s->par)) {
                   1765:                        s->sub.encbuf =
                   1766:                            xmalloc(s->round * s->sub.nch * sizeof(adata_t));
                   1767:                }
                   1768:        }
                   1769:
                   1770:        slot_initconv(s);
1.35      ratchov  1771:
                   1772: #ifdef DEBUG
                   1773:        if (log_level >= 3) {
                   1774:                slot_log(s);
                   1775:                log_puts(": allocated ");
                   1776:                log_putu(s->appbufsz);
                   1777:                log_puts("/");
                   1778:                log_putu(SLOT_BUFSZ(s));
                   1779:                log_puts(" fr buffers\n");
                   1780:        }
                   1781: #endif
                   1782: }
                   1783:
                   1784: /*
                   1785:  * free buffers & conversion chain
                   1786:  */
                   1787: void
                   1788: slot_freebufs(struct slot *s)
                   1789: {
                   1790:        if (s->mode & MODE_RECMASK) {
                   1791:                abuf_done(&s->sub.buf);
                   1792:                if (s->sub.encbuf)
                   1793:                        xfree(s->sub.encbuf);
                   1794:                if (s->sub.resampbuf)
                   1795:                        xfree(s->sub.resampbuf);
                   1796:        }
                   1797:
                   1798:        if (s->mode & MODE_PLAY) {
                   1799:                abuf_done(&s->mix.buf);
                   1800:                if (s->mix.decbuf)
                   1801:                        xfree(s->mix.decbuf);
                   1802:                if (s->mix.resampbuf)
                   1803:                        xfree(s->mix.resampbuf);
                   1804:        }
                   1805: }
                   1806:
1.1       ratchov  1807: /*
                   1808:  * allocate a new slot and register the given call-backs
                   1809:  */
                   1810: struct slot *
1.54      ratchov  1811: slot_new(struct dev *d, struct opt *opt, unsigned int id, char *who,
1.37      ratchov  1812:     struct slotops *ops, void *arg, int mode)
1.1       ratchov  1813: {
                   1814:        char *p;
                   1815:        char name[SLOT_NAMEMAX];
1.52      ratchov  1816:        unsigned int i, ser, bestser, bestidx;
                   1817:        struct slot *unit[DEV_NSLOT];
1.1       ratchov  1818:        struct slot *s;
                   1819:
                   1820:        /*
1.27      ratchov  1821:         * create a ``valid'' control name (lowcase, remove [^a-z], truncate)
1.1       ratchov  1822:         */
                   1823:        for (i = 0, p = who; ; p++) {
                   1824:                if (i == SLOT_NAMEMAX - 1 || *p == '\0') {
                   1825:                        name[i] = '\0';
                   1826:                        break;
                   1827:                } else if (*p >= 'A' && *p <= 'Z') {
                   1828:                        name[i++] = *p + 'a' - 'A';
                   1829:                } else if (*p >= 'a' && *p <= 'z')
                   1830:                        name[i++] = *p;
                   1831:        }
                   1832:        if (i == 0)
                   1833:                strlcpy(name, "noname", SLOT_NAMEMAX);
                   1834:
                   1835:        /*
1.52      ratchov  1836:         * build a unit-to-slot map for this name
1.1       ratchov  1837:         */
1.52      ratchov  1838:        for (i = 0; i < DEV_NSLOT; i++)
                   1839:                unit[i] = NULL;
                   1840:        for (i = 0; i < DEV_NSLOT; i++) {
                   1841:                s = d->slot + i;
1.1       ratchov  1842:                if (strcmp(s->name, name) == 0)
1.52      ratchov  1843:                        unit[s->unit] = s;
1.1       ratchov  1844:        }
                   1845:
                   1846:        /*
1.54      ratchov  1847:         * find the free slot with the least unit number and same id
                   1848:         */
                   1849:        for (i = 0; i < DEV_NSLOT; i++) {
                   1850:                s = unit[i];
                   1851:                if (s != NULL && s->ops == NULL && s->id == id)
                   1852:                        goto found;
                   1853:        }
                   1854:
                   1855:        /*
1.52      ratchov  1856:         * find the free slot with the least unit number
1.1       ratchov  1857:         */
1.52      ratchov  1858:        for (i = 0; i < DEV_NSLOT; i++) {
                   1859:                s = unit[i];
1.54      ratchov  1860:                if (s != NULL && s->ops == NULL) {
                   1861:                        s->id = id;
1.1       ratchov  1862:                        goto found;
1.54      ratchov  1863:                }
1.1       ratchov  1864:        }
                   1865:
                   1866:        /*
1.18      ratchov  1867:         * couldn't find a matching slot, pick oldest free slot
1.1       ratchov  1868:         * and set its name/unit
                   1869:         */
                   1870:        bestser = 0;
                   1871:        bestidx = DEV_NSLOT;
                   1872:        for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) {
                   1873:                if (s->ops != NULL)
                   1874:                        continue;
                   1875:                ser = d->serial - s->serial;
                   1876:                if (ser > bestser) {
                   1877:                        bestser = ser;
                   1878:                        bestidx = i;
                   1879:                }
                   1880:        }
1.51      ratchov  1881:        if (bestidx != DEV_NSLOT) {
                   1882:                s = d->slot + bestidx;
                   1883:                s->vol = MIDI_MAXCTL;
                   1884:                strlcpy(s->name, name, SLOT_NAMEMAX);
                   1885:                s->serial = d->serial++;
1.52      ratchov  1886:                for (i = 0; unit[i] != NULL; i++)
                   1887:                        ; /* nothing */
                   1888:                s->unit = i;
1.54      ratchov  1889:                s->id = id;
1.51      ratchov  1890:                goto found;
1.1       ratchov  1891:        }
1.53      ratchov  1892:
1.51      ratchov  1893:        if (log_level >= 1) {
1.1       ratchov  1894:                log_puts(name);
1.51      ratchov  1895:                log_puts(": out of sub-device slots\n");
1.1       ratchov  1896:        }
1.51      ratchov  1897:        return NULL;
1.1       ratchov  1898:
                   1899: found:
1.37      ratchov  1900:        if ((mode & MODE_REC) && (opt->mode & MODE_MON)) {
                   1901:                mode |= MODE_MON;
                   1902:                mode &= ~MODE_REC;
                   1903:        }
                   1904:        if ((mode & opt->mode) != mode) {
                   1905:                if (log_level >= 1) {
                   1906:                        slot_log(s);
                   1907:                        log_puts(": requested mode not allowed\n");
                   1908:                }
                   1909:                return 0;
                   1910:        }
1.1       ratchov  1911:        if (!dev_ref(d))
                   1912:                return NULL;
1.64      ratchov  1913:        dev_label(d, s - d->slot);
1.31      ratchov  1914:        if ((mode & d->mode) != mode) {
1.1       ratchov  1915:                if (log_level >= 1) {
                   1916:                        slot_log(s);
                   1917:                        log_puts(": requested mode not supported\n");
                   1918:                }
1.31      ratchov  1919:                dev_unref(d);
1.49      ratchov  1920:                return NULL;
1.1       ratchov  1921:        }
1.31      ratchov  1922:        s->dev = d;
1.37      ratchov  1923:        s->opt = opt;
1.31      ratchov  1924:        s->ops = ops;
                   1925:        s->arg = arg;
                   1926:        s->pstate = SLOT_INIT;
1.1       ratchov  1927:        s->mode = mode;
1.6       ratchov  1928:        aparams_init(&s->par);
1.41      ratchov  1929:        if (s->mode & MODE_PLAY)
1.42      ratchov  1930:                s->mix.nch = s->opt->pmax - s->opt->pmin + 1;
1.41      ratchov  1931:        if (s->mode & MODE_RECMASK)
1.42      ratchov  1932:                s->sub.nch = s->opt->rmax - s->opt->rmin + 1;
1.46      ratchov  1933:        s->xrun = s->opt->mmc ? XRUN_SYNC : XRUN_IGNORE;
1.1       ratchov  1934:        s->appbufsz = d->bufsz;
                   1935:        s->round = d->round;
1.5       ratchov  1936:        s->rate = d->rate;
1.1       ratchov  1937:        dev_midi_slotdesc(d, s);
                   1938:        dev_midi_vol(d, s);
1.43      ratchov  1939: #ifdef DEBUG
                   1940:        if (log_level >= 3) {
                   1941:                slot_log(s);
                   1942:                log_puts(": using ");
                   1943:                dev_log(d);
                   1944:                log_puts(".");
                   1945:                log_puts(opt->name);
                   1946:                log_puts(", mode = ");
                   1947:                log_putx(mode);
                   1948:                log_puts("\n");
                   1949:        }
                   1950: #endif
1.1       ratchov  1951:        return s;
                   1952: }
                   1953:
                   1954: /*
                   1955:  * release the given slot
                   1956:  */
                   1957: void
                   1958: slot_del(struct slot *s)
                   1959: {
                   1960:        s->arg = s;
                   1961:        s->ops = &zomb_slotops;
                   1962:        switch (s->pstate) {
                   1963:        case SLOT_INIT:
                   1964:                s->ops = NULL;
                   1965:                break;
                   1966:        case SLOT_START:
                   1967:        case SLOT_READY:
                   1968:        case SLOT_RUN:
                   1969:                slot_stop(s);
                   1970:                /* PASSTHROUGH */
                   1971:        case SLOT_STOP:
                   1972:                break;
                   1973:        }
                   1974:        dev_unref(s->dev);
                   1975:        s->dev = NULL;
                   1976: }
                   1977:
                   1978: /*
                   1979:  * change the slot play volume; called either by the slot or by MIDI
                   1980:  */
                   1981: void
                   1982: slot_setvol(struct slot *s, unsigned int vol)
                   1983: {
                   1984: #ifdef DEBUG
                   1985:        if (log_level >= 3) {
                   1986:                slot_log(s);
                   1987:                log_puts(": setting volume ");
                   1988:                log_putu(vol);
                   1989:                log_puts("\n");
                   1990:        }
                   1991: #endif
                   1992:        s->vol = vol;
                   1993:        s->mix.vol = MIDI_TO_ADATA(s->vol);
                   1994: }
                   1995:
                   1996: /*
                   1997:  * attach the slot to the device (ie start playing & recording
                   1998:  */
                   1999: void
                   2000: slot_attach(struct slot *s)
                   2001: {
                   2002:        struct dev *d = s->dev;
1.12      ratchov  2003:        long long pos;
1.1       ratchov  2004:
                   2005:        /*
                   2006:         * start the device if not started
                   2007:         */
                   2008:        dev_wakeup(d);
1.23      ratchov  2009:
1.1       ratchov  2010:        /*
1.12      ratchov  2011:         * adjust initial clock
                   2012:         */
1.79    ! ratchov  2013:        pos = s->delta_rem +
        !          2014:            (long long)s->delta * d->round +
        !          2015:            (long long)d->delta * s->round;
        !          2016:        s->delta = pos / (int)d->round;
1.12      ratchov  2017:        s->delta_rem = pos % d->round;
1.79    ! ratchov  2018:        if (s->delta_rem < 0) {
        !          2019:                s->delta_rem += d->round;
        !          2020:                s->delta--;
        !          2021:        }
1.12      ratchov  2022:
1.1       ratchov  2023: #ifdef DEBUG
1.17      ratchov  2024:        if (log_level >= 2) {
1.1       ratchov  2025:                slot_log(s);
                   2026:                log_puts(": attached at ");
1.79    ! ratchov  2027:                log_puti(s->delta);
        !          2028:                log_puts(" + ");
        !          2029:                log_puti(s->delta_rem);
        !          2030:                log_puts("/");
        !          2031:                log_puti(s->round);
1.1       ratchov  2032:                log_puts("\n");
                   2033:        }
                   2034: #endif
                   2035:
                   2036:        /*
                   2037:         * We dont check whether the device is dying,
                   2038:         * because dev_xxx() functions are supposed to
                   2039:         * work (i.e., not to crash)
                   2040:         */
                   2041: #ifdef DEBUG
                   2042:        if ((s->mode & d->mode) != s->mode) {
                   2043:                slot_log(s);
1.24      ratchov  2044:                log_puts(": mode beyond device mode, not attaching\n");
1.1       ratchov  2045:                panic();
                   2046:        }
                   2047: #endif
                   2048:        s->next = d->slot_list;
                   2049:        d->slot_list = s;
                   2050:        if (s->mode & MODE_PLAY) {
                   2051:                s->mix.vol = MIDI_TO_ADATA(s->vol);
                   2052:                dev_mix_adjvol(d);
                   2053:        }
                   2054: }
                   2055:
                   2056: /*
                   2057:  * if MMC is enabled, and try to attach all slots synchronously, else
                   2058:  * simply attach the slot
                   2059:  */
                   2060: void
                   2061: slot_ready(struct slot *s)
                   2062: {
1.3       ratchov  2063:        /*
                   2064:         * device may be disconnected, and if so we're called from
                   2065:         * slot->ops->exit() on a closed device
1.23      ratchov  2066:         */
1.3       ratchov  2067:        if (s->dev->pstate == DEV_CFG)
                   2068:                return;
1.79    ! ratchov  2069:        if (!s->opt->mmc) {
1.1       ratchov  2070:                slot_attach(s);
1.79    ! ratchov  2071:                s->pstate = SLOT_RUN;
        !          2072:        } else
1.1       ratchov  2073:                dev_sync_attach(s->dev);
                   2074: }
                   2075:
                   2076: /*
                   2077:  * setup buffers & conversion layers, prepare the slot to receive data
                   2078:  * (for playback) or start (recording).
                   2079:  */
                   2080: void
                   2081: slot_start(struct slot *s)
                   2082: {
                   2083: #ifdef DEBUG
                   2084:        if (s->pstate != SLOT_INIT) {
                   2085:                slot_log(s);
                   2086:                log_puts(": slot_start: wrong state\n");
                   2087:                panic();
                   2088:        }
                   2089:        if (s->mode & MODE_PLAY) {
                   2090:                if (log_level >= 3) {
                   2091:                        slot_log(s);
                   2092:                        log_puts(": playing ");
                   2093:                        aparams_log(&s->par);
                   2094:                        log_puts(" -> ");
1.35      ratchov  2095:                        aparams_log(&s->dev->par);
1.1       ratchov  2096:                        log_puts("\n");
                   2097:                }
                   2098:        }
                   2099:        if (s->mode & MODE_RECMASK) {
                   2100:                if (log_level >= 3) {
                   2101:                        slot_log(s);
                   2102:                        log_puts(": recording ");
                   2103:                        aparams_log(&s->par);
                   2104:                        log_puts(" <- ");
1.35      ratchov  2105:                        aparams_log(&s->dev->par);
1.1       ratchov  2106:                        log_puts("\n");
1.35      ratchov  2107:                }
1.1       ratchov  2108:        }
                   2109: #endif
1.35      ratchov  2110:        slot_allocbufs(s);
1.47      ratchov  2111:
                   2112:        if (s->mode & MODE_RECMASK) {
                   2113:                /*
                   2114:                 * N-th recorded block is the N-th played block
                   2115:                 */
                   2116:                s->sub.prime = -dev_getpos(s->dev) / s->dev->round;
                   2117:        }
                   2118:        s->skip = 0;
                   2119:
1.79    ! ratchov  2120:        /*
        !          2121:         * get the current position, the origin is when the first sample
        !          2122:         * played and/or recorded
        !          2123:         */
        !          2124:        s->delta = dev_getpos(s->dev) * (int)s->round / (int)s->dev->round;
        !          2125:        s->delta_rem = 0;
        !          2126:
1.1       ratchov  2127:        if (s->mode & MODE_PLAY) {
                   2128:                s->pstate = SLOT_START;
                   2129:        } else {
                   2130:                s->pstate = SLOT_READY;
                   2131:                slot_ready(s);
                   2132:        }
                   2133: }
                   2134:
                   2135: /*
                   2136:  * stop playback and recording, and free conversion layers
                   2137:  */
                   2138: void
                   2139: slot_detach(struct slot *s)
                   2140: {
                   2141:        struct slot **ps;
1.79    ! ratchov  2142:        struct dev *d;
        !          2143:        long long pos;
1.1       ratchov  2144:
                   2145: #ifdef DEBUG
                   2146:        if (log_level >= 3) {
                   2147:                slot_log(s);
                   2148:                log_puts(": detaching\n");
                   2149:        }
                   2150: #endif
                   2151:        for (ps = &s->dev->slot_list; *ps != s; ps = &(*ps)->next) {
                   2152: #ifdef DEBUG
1.28      ratchov  2153:                if (*ps == NULL) {
1.1       ratchov  2154:                        slot_log(s);
                   2155:                        log_puts(": can't detach, not on list\n");
                   2156:                        panic();
                   2157:                }
                   2158: #endif
1.23      ratchov  2159:        }
1.1       ratchov  2160:        *ps = s->next;
1.79    ! ratchov  2161:
        !          2162:        d = s->dev;
        !          2163:
        !          2164:        /*
        !          2165:         * adjust clock, go back d->delta ticks so that slot_attach()
        !          2166:         * could be called with the resulting state
        !          2167:         */
        !          2168:        pos = s->delta_rem +
        !          2169:            (long long)s->delta * d->round -
        !          2170:            (long long)d->delta * s->round;
        !          2171:        s->delta = pos / (int)d->round;
        !          2172:        s->delta_rem = pos % d->round;
        !          2173:        if (s->delta_rem < 0) {
        !          2174:                s->delta_rem += d->round;
        !          2175:                s->delta--;
        !          2176:        }
        !          2177:
        !          2178: #ifdef DEBUG
        !          2179:        if (log_level >= 2) {
        !          2180:                slot_log(s);
        !          2181:                log_puts(": detached at ");
        !          2182:                log_puti(s->delta);
        !          2183:                log_puts(" + ");
        !          2184:                log_puti(s->delta_rem);
        !          2185:                log_puts("/");
        !          2186:                log_puti(d->round);
        !          2187:                log_puts("\n");
        !          2188:        }
        !          2189: #endif
1.35      ratchov  2190:        if (s->mode & MODE_PLAY)
1.1       ratchov  2191:                dev_mix_adjvol(s->dev);
                   2192: }
                   2193:
                   2194: /*
                   2195:  * put the slot in stopping state (draining play buffers) or
                   2196:  * stop & detach if no data to drain.
                   2197:  */
                   2198: void
                   2199: slot_stop(struct slot *s)
                   2200: {
                   2201: #ifdef DEBUG
                   2202:        if (log_level >= 3) {
                   2203:                slot_log(s);
                   2204:                log_puts(": stopping\n");
                   2205:        }
                   2206: #endif
                   2207:        if (s->pstate == SLOT_START) {
1.33      ratchov  2208:                /*
                   2209:                 * If in rec-only mode, we're already in the READY or
                   2210:                 * RUN states. We're here because the play buffer was
                   2211:                 * not full enough, try to start so it's drained.
                   2212:                 */
                   2213:                s->pstate = SLOT_READY;
                   2214:                slot_ready(s);
1.1       ratchov  2215:        }
1.34      ratchov  2216:
                   2217:        if (s->pstate == SLOT_RUN) {
                   2218:                if (s->mode & MODE_PLAY) {
                   2219:                        /*
                   2220:                         * Don't detach, dev_cycle() will do it for us
                   2221:                         * when the buffer is drained.
                   2222:                         */
                   2223:                        s->pstate = SLOT_STOP;
                   2224:                        return;
                   2225:                }
                   2226:                slot_detach(s);
                   2227:        } else {
1.1       ratchov  2228: #ifdef DEBUG
                   2229:                if (log_level >= 3) {
                   2230:                        slot_log(s);
                   2231:                        log_puts(": not drained (blocked by mmc)\n");
                   2232:                }
                   2233: #endif
                   2234:        }
1.35      ratchov  2235:
1.34      ratchov  2236:        s->pstate = SLOT_INIT;
                   2237:        s->ops->eof(s->arg);
1.35      ratchov  2238:        slot_freebufs(s);
1.1       ratchov  2239: }
                   2240:
1.12      ratchov  2241: void
                   2242: slot_skip_update(struct slot *s)
                   2243: {
                   2244:        int skip;
                   2245:
                   2246:        skip = slot_skip(s);
                   2247:        while (skip > 0) {
                   2248: #ifdef DEBUG
                   2249:                if (log_level >= 4) {
                   2250:                        slot_log(s);
                   2251:                        log_puts(": catching skipped block\n");
                   2252:                }
                   2253: #endif
                   2254:                if (s->mode & MODE_RECMASK)
                   2255:                        s->ops->flush(s->arg);
                   2256:                if (s->mode & MODE_PLAY)
                   2257:                        s->ops->fill(s->arg);
                   2258:                skip--;
                   2259:        }
                   2260: }
                   2261:
1.1       ratchov  2262: /*
                   2263:  * notify the slot that we just wrote in the play buffer, must be called
                   2264:  * after each write
                   2265:  */
                   2266: void
                   2267: slot_write(struct slot *s)
                   2268: {
                   2269:        if (s->pstate == SLOT_START && s->mix.buf.used == s->mix.buf.len) {
                   2270: #ifdef DEBUG
                   2271:                if (log_level >= 4) {
                   2272:                        slot_log(s);
                   2273:                        log_puts(": switching to READY state\n");
                   2274:                }
                   2275: #endif
                   2276:                s->pstate = SLOT_READY;
                   2277:                slot_ready(s);
                   2278:        }
1.12      ratchov  2279:        slot_skip_update(s);
1.1       ratchov  2280: }
                   2281:
                   2282: /*
                   2283:  * notify the slot that we freed some space in the rec buffer
                   2284:  */
                   2285: void
                   2286: slot_read(struct slot *s)
                   2287: {
1.12      ratchov  2288:        slot_skip_update(s);
1.64      ratchov  2289: }
                   2290:
                   2291: /*
                   2292:  * allocate at control slot
                   2293:  */
                   2294: struct ctlslot *
                   2295: ctlslot_new(struct dev *d, struct ctlops *ops, void *arg)
                   2296: {
                   2297:        struct ctlslot *s;
                   2298:        struct ctl *c;
                   2299:        int i;
                   2300:
                   2301:        i = 0;
                   2302:        for (;;) {
                   2303:                if (i == DEV_NCTLSLOT)
                   2304:                        return NULL;
                   2305:                s = d->ctlslot + i;
                   2306:                if (s->ops == NULL)
                   2307:                        break;
                   2308:                i++;
                   2309:        }
                   2310:        s->dev = d;
                   2311:        s->mask = 1 << i;
                   2312:        if (!dev_ref(d))
                   2313:                return NULL;
                   2314:        s->ops = ops;
                   2315:        s->arg = arg;
                   2316:        for (c = d->ctl_list; c != NULL; c = c->next)
                   2317:                c->refs_mask |= s->mask;
                   2318:        return s;
                   2319: }
                   2320:
                   2321: /*
                   2322:  * free control slot
                   2323:  */
                   2324: void
                   2325: ctlslot_del(struct ctlslot *s)
                   2326: {
                   2327:        struct ctl *c, **pc;
                   2328:
                   2329:        pc = &s->dev->ctl_list;
                   2330:        while ((c = *pc) != NULL) {
                   2331:                c->refs_mask &= ~s->mask;
                   2332:                if (c->refs_mask == 0) {
                   2333:                        *pc = c->next;
                   2334:                        xfree(c);
                   2335:                } else
                   2336:                        pc = &c->next;
                   2337:        }
                   2338:        s->ops = NULL;
                   2339:        dev_unref(s->dev);
                   2340: }
                   2341:
                   2342: void
                   2343: ctl_node_log(struct ctl_node *c)
                   2344: {
                   2345:        log_puts(c->name);
                   2346:        if (c->unit >= 0)
                   2347:                log_putu(c->unit);
                   2348: }
                   2349:
                   2350: void
                   2351: ctl_log(struct ctl *c)
                   2352: {
                   2353:        if (c->group[0] != 0) {
                   2354:                log_puts(c->group);
                   2355:                log_puts("/");
                   2356:        }
                   2357:        ctl_node_log(&c->node0);
                   2358:        log_puts(".");
                   2359:        log_puts(c->func);
                   2360:        log_puts("=");
                   2361:        switch (c->type) {
1.67      ratchov  2362:        case CTL_NONE:
                   2363:                log_puts("none");
                   2364:                break;
1.64      ratchov  2365:        case CTL_NUM:
                   2366:        case CTL_SW:
                   2367:                log_putu(c->curval);
                   2368:                break;
                   2369:        case CTL_VEC:
                   2370:        case CTL_LIST:
1.74      ratchov  2371:        case CTL_SEL:
1.64      ratchov  2372:                ctl_node_log(&c->node1);
                   2373:                log_puts(":");
                   2374:                log_putu(c->curval);
                   2375:        }
                   2376:        log_puts(" at ");
                   2377:        log_putu(c->addr);
                   2378: }
                   2379:
                   2380: /*
                   2381:  * add a ctl
                   2382:  */
                   2383: struct ctl *
                   2384: dev_addctl(struct dev *d, char *gstr, int type, int addr,
                   2385:     char *str0, int unit0, char *func, char *str1, int unit1, int maxval, int val)
                   2386: {
                   2387:        struct ctl *c, **pc;
                   2388:        int i;
                   2389:
                   2390:        c = xmalloc(sizeof(struct ctl));
                   2391:        c->type = type;
                   2392:        strlcpy(c->func, func, CTL_NAMEMAX);
                   2393:        strlcpy(c->group, gstr, CTL_NAMEMAX);
                   2394:        strlcpy(c->node0.name, str0, CTL_NAMEMAX);
                   2395:        c->node0.unit = unit0;
1.74      ratchov  2396:        if (c->type == CTL_VEC || c->type == CTL_LIST || c->type == CTL_SEL) {
1.64      ratchov  2397:                strlcpy(c->node1.name, str1, CTL_NAMEMAX);
                   2398:                c->node1.unit = unit1;
                   2399:        } else
                   2400:                memset(&c->node1, 0, sizeof(struct ctl_node));
                   2401:        c->addr = addr;
                   2402:        c->maxval = maxval;
                   2403:        c->val_mask = ~0;
                   2404:        c->desc_mask = ~0;
                   2405:        c->curval = val;
                   2406:        c->dirty = 0;
                   2407:        c->refs_mask = 0;
                   2408:        for (i = 0; i < DEV_NCTLSLOT; i++) {
                   2409:                c->refs_mask |= CTL_DEVMASK;
                   2410:                if (d->ctlslot[i].ops != NULL)
                   2411:                        c->refs_mask |= 1 << i;
                   2412:        }
                   2413:        for (pc = &d->ctl_list; *pc != NULL; pc = &(*pc)->next)
                   2414:                ; /* nothing */
                   2415:        c->next = NULL;
                   2416:        *pc = c;
                   2417: #ifdef DEBUG
                   2418:        if (log_level >= 3) {
                   2419:                dev_log(d);
                   2420:                log_puts(": adding ");
                   2421:                ctl_log(c);
                   2422:                log_puts("\n");
                   2423:        }
                   2424: #endif
                   2425:        return c;
                   2426: }
                   2427:
                   2428: void
                   2429: dev_rmctl(struct dev *d, int addr)
                   2430: {
                   2431:        struct ctl *c, **pc;
                   2432:
                   2433:        pc = &d->ctl_list;
                   2434:        for (;;) {
                   2435:                c = *pc;
                   2436:                if (c == NULL)
                   2437:                        return;
                   2438:                if (c->type != CTL_NONE && c->addr == addr)
                   2439:                        break;
                   2440:                pc = &c->next;
                   2441:        }
                   2442:        c->type = CTL_NONE;
                   2443: #ifdef DEBUG
                   2444:        if (log_level >= 3) {
                   2445:                dev_log(d);
                   2446:                log_puts(": removing ");
                   2447:                ctl_log(c);
                   2448:                log_puts(", refs_mask = 0x");
                   2449:                log_putx(c->refs_mask);
                   2450:                log_puts("\n");
                   2451:        }
                   2452: #endif
                   2453:        c->refs_mask &= ~CTL_DEVMASK;
1.68      ratchov  2454:        if (c->refs_mask == 0) {
                   2455:                *pc = c->next;
                   2456:                xfree(c);
1.64      ratchov  2457:                return;
1.68      ratchov  2458:        }
                   2459:        c->desc_mask = ~0;
1.65      ratchov  2460: }
                   2461:
                   2462: void
                   2463: dev_ctlsync(struct dev *d)
                   2464: {
1.70      ratchov  2465:        struct ctl *c;
1.65      ratchov  2466:        struct ctlslot *s;
1.70      ratchov  2467:        int found, i;
                   2468:
                   2469:        found = 0;
                   2470:        for (c = d->ctl_list; c != NULL; c = c->next) {
                   2471:                if (c->addr != CTLADDR_MASTER &&
                   2472:                    c->type == CTL_NUM &&
                   2473:                    strcmp(c->group, "") == 0 &&
                   2474:                    strcmp(c->node0.name, "output") == 0 &&
                   2475:                    strcmp(c->func, "level") == 0)
                   2476:                        found = 1;
                   2477:        }
                   2478:
                   2479:        if (d->master_enabled && found) {
                   2480:                if (log_level >= 2) {
                   2481:                        dev_log(d);
                   2482:                        log_puts(": software master level control disabled\n");
                   2483:                }
                   2484:                d->master_enabled = 0;
                   2485:                dev_rmctl(d, CTLADDR_MASTER);
                   2486:        } else if (!d->master_enabled && !found) {
                   2487:                if (log_level >= 2) {
                   2488:                        dev_log(d);
                   2489:                        log_puts(": software master level control enabled\n");
                   2490:                }
                   2491:                d->master_enabled = 1;
                   2492:                dev_addctl(d, "", CTL_NUM, CTLADDR_MASTER,
                   2493:                    "output", -1, "level", NULL, -1, 127, d->master);
                   2494:        }
1.65      ratchov  2495:
                   2496:        for (s = d->ctlslot, i = DEV_NCTLSLOT; i > 0; i--, s++) {
                   2497:                if (s->ops)
                   2498:                        s->ops->sync(s->arg);
                   2499:        }
1.64      ratchov  2500: }
                   2501:
                   2502: int
                   2503: dev_setctl(struct dev *d, int addr, int val)
                   2504: {
                   2505:        struct ctl *c;
                   2506:        int num;
                   2507:
                   2508:        c = d->ctl_list;
                   2509:        for (;;) {
                   2510:                if (c == NULL) {
                   2511:                        if (log_level >= 3) {
                   2512:                                dev_log(d);
                   2513:                                log_puts(": ");
                   2514:                                log_putu(addr);
                   2515:                                log_puts(": no such ctl address\n");
                   2516:                        }
                   2517:                        return 0;
                   2518:                }
                   2519:                if (c->type != CTL_NONE && c->addr == addr)
                   2520:                        break;
                   2521:                c = c->next;
                   2522:        }
                   2523:        if (c->curval == val) {
                   2524:                if (log_level >= 3) {
                   2525:                        ctl_log(c);
                   2526:                        log_puts(": already set\n");
                   2527:                }
                   2528:                return 1;
                   2529:        }
                   2530:        if (val < 0 || val > c->maxval) {
                   2531:                if (log_level >= 3) {
                   2532:                        dev_log(d);
                   2533:                        log_puts(": ");
                   2534:                        log_putu(val);
                   2535:                        log_puts(": ctl val out of bounds\n");
                   2536:                }
                   2537:                return 0;
                   2538:        }
                   2539:        if (addr >= CTLADDR_END) {
                   2540:                if (log_level >= 3) {
                   2541:                        ctl_log(c);
                   2542:                        log_puts(": marked as dirty\n");
                   2543:                }
                   2544:                c->dirty = 1;
                   2545:                dev_ref(d);
                   2546:        } else {
1.75      ratchov  2547:                if (addr >= CTLADDR_ALT_SEL) {
                   2548:                        if (val) {
                   2549:                                num = addr - CTLADDR_ALT_SEL;
                   2550:                                dev_setalt(d, num);
                   2551:                        }
                   2552:                        return 1;
                   2553:                } else if (addr == CTLADDR_MASTER) {
1.70      ratchov  2554:                        if (d->master_enabled) {
                   2555:                                dev_master(d, val);
                   2556:                                dev_midi_master(d);
                   2557:                        }
1.64      ratchov  2558:                } else {
                   2559:                        num = addr - CTLADDR_SLOT_LEVEL(0);
                   2560:                        slot_setvol(d->slot + num, val);
                   2561:                        dev_midi_vol(d, d->slot + num);
                   2562:                }
1.66      ratchov  2563:                c->val_mask = ~0U;
1.64      ratchov  2564:        }
                   2565:        c->curval = val;
                   2566:        return 1;
                   2567: }
                   2568:
                   2569: int
                   2570: dev_onval(struct dev *d, int addr, int val)
                   2571: {
                   2572:        struct ctl *c;
                   2573:
                   2574:        c = d->ctl_list;
                   2575:        for (;;) {
                   2576:                if (c == NULL)
                   2577:                        return 0;
                   2578:                if (c->type != CTL_NONE && c->addr == addr)
                   2579:                        break;
                   2580:                c = c->next;
                   2581:        }
                   2582:        c->curval = val;
                   2583:        c->val_mask = ~0U;
                   2584:        return 1;
                   2585: }
                   2586:
                   2587: void
                   2588: dev_label(struct dev *d, int i)
                   2589: {
                   2590:        struct ctl *c;
                   2591:        char name[CTL_NAMEMAX];
                   2592:
1.69      ratchov  2593:        slot_ctlname(&d->slot[i], name, CTL_NAMEMAX);
                   2594:
1.64      ratchov  2595:        c = d->ctl_list;
                   2596:        for (;;) {
1.69      ratchov  2597:                if (c == NULL) {
                   2598:                        dev_addctl(d, "app", CTL_NUM,
                   2599:                            CTLADDR_SLOT_LEVEL(i),
                   2600:                            name, -1, "level",
                   2601:                            NULL, -1, 127, d->slot[i].vol);
1.64      ratchov  2602:                        return;
1.69      ratchov  2603:                }
1.64      ratchov  2604:                if (c->addr == CTLADDR_SLOT_LEVEL(i))
                   2605:                        break;
                   2606:                c = c->next;
                   2607:        }
                   2608:        if (strcmp(c->node0.name, name) == 0)
                   2609:                return;
                   2610:        strlcpy(c->node0.name, name, CTL_NAMEMAX);
                   2611:        c->desc_mask = ~0;
1.1       ratchov  2612: }