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

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