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

1.76    ! ratchov     1: /*     $OpenBSD: dev.c,v 1.75 2020/06/28 05:21:39 ratchov Exp $        */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #include <stdio.h>
                     18: #include <string.h>
                     19:
                     20: #include "abuf.h"
                     21: #include "defs.h"
                     22: #include "dev.h"
                     23: #include "dsp.h"
                     24: #include "siofile.h"
                     25: #include "midi.h"
1.36      ratchov    26: #include "opt.h"
1.1       ratchov    27: #include "sysex.h"
                     28: #include "utils.h"
                     29:
1.14      ratchov    30: void zomb_onmove(void *);
1.25      ratchov    31: void zomb_onvol(void *);
1.1       ratchov    32: void zomb_fill(void *);
                     33: void zomb_flush(void *);
                     34: void zomb_eof(void *);
                     35: void zomb_exit(void *);
                     36:
1.7       ratchov    37: void dev_log(struct dev *);
                     38: void dev_midi_qfr(struct dev *, int);
                     39: void dev_midi_full(struct dev *);
                     40: void dev_midi_vol(struct dev *, struct slot *);
                     41: void dev_midi_master(struct dev *);
                     42: void dev_midi_slotdesc(struct dev *, struct slot *);
                     43: void dev_midi_dump(struct dev *);
1.1       ratchov    44: void dev_midi_imsg(void *, unsigned char *, int);
                     45: void dev_midi_omsg(void *, unsigned char *, int);
                     46: void dev_midi_fill(void *, int);
                     47: void dev_midi_exit(void *);
1.7       ratchov    48:
                     49: void dev_mix_badd(struct dev *, struct slot *);
                     50: void dev_mix_adjvol(struct dev *);
                     51: void dev_sub_bcopy(struct dev *, struct slot *);
                     52:
                     53: void dev_onmove(struct dev *, int);
                     54: void dev_master(struct dev *, unsigned int);
                     55: void dev_cycle(struct dev *);
                     56: int dev_getpos(struct dev *);
                     57: struct dev *dev_new(char *, struct aparams *, unsigned int, unsigned int,
                     58:     unsigned int, unsigned int, unsigned int, unsigned int);
1.25      ratchov    59: void dev_adjpar(struct dev *, int, int, int);
1.59      ratchov    60: int dev_allocbufs(struct dev *);
1.7       ratchov    61: int dev_open(struct dev *);
1.59      ratchov    62: void dev_freebufs(struct dev *);
1.7       ratchov    63: void dev_close(struct dev *);
                     64: int dev_ref(struct dev *);
                     65: void dev_unref(struct dev *);
                     66: int dev_init(struct dev *);
                     67: void dev_done(struct dev *);
                     68: struct dev *dev_bynum(int);
                     69: void dev_del(struct dev *);
1.75      ratchov    70: void dev_setalt(struct dev *, unsigned int);
1.7       ratchov    71: unsigned int dev_roundof(struct dev *, unsigned int);
                     72: void dev_wakeup(struct dev *);
                     73: void dev_sync_attach(struct dev *);
                     74: void dev_mmcstart(struct dev *);
                     75: void dev_mmcstop(struct dev *);
                     76: void dev_mmcloc(struct dev *, unsigned int);
                     77:
1.64      ratchov    78: void slot_ctlname(struct slot *, char *, size_t);
1.7       ratchov    79: void slot_log(struct slot *);
                     80: void slot_del(struct slot *);
                     81: void slot_setvol(struct slot *, unsigned int);
                     82: void slot_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.73      ratchov  1021:        d->alt_list = NULL;
                   1022:        dev_addname(d,path);
1.1       ratchov  1023:        d->num = dev_sndnum++;
1.36      ratchov  1024:        d->opt_list = NULL;
1.76    ! ratchov  1025:        d->alt_num = -1;
1.1       ratchov  1026:
                   1027:        /*
                   1028:         * XXX: below, we allocate a midi input buffer, since we don't
                   1029:         *      receive raw midi data, so no need to allocate a input
                   1030:         *      ibuf.  Possibly set imsg & fill callbacks to NULL and
                   1031:         *      use this to in midi_new() to check if buffers need to be
                   1032:         *      allocated
                   1033:         */
                   1034:        d->midi = midi_new(&dev_midiops, d, MODE_MIDIIN | MODE_MIDIOUT);
                   1035:        midi_tag(d->midi, d->num);
                   1036:        d->reqpar = *par;
                   1037:        d->reqmode = mode;
                   1038:        d->reqpchan = d->reqrchan = 0;
                   1039:        d->reqbufsz = bufsz;
                   1040:        d->reqround = round;
                   1041:        d->reqrate = rate;
                   1042:        d->hold = hold;
                   1043:        d->autovol = autovol;
                   1044:        d->refcnt = 0;
                   1045:        d->pstate = DEV_CFG;
                   1046:        d->serial = 0;
                   1047:        for (i = 0; i < DEV_NSLOT; i++) {
                   1048:                d->slot[i].unit = i;
                   1049:                d->slot[i].ops = NULL;
                   1050:                d->slot[i].vol = MIDI_MAXCTL;
                   1051:                d->slot[i].serial = d->serial++;
1.69      ratchov  1052:                memset(d->slot[i].name, 0, SLOT_NAMEMAX);
1.1       ratchov  1053:        }
1.64      ratchov  1054:        for (i = 0; i < DEV_NCTLSLOT; i++) {
                   1055:                d->ctlslot[i].ops = NULL;
                   1056:                d->ctlslot[i].dev = d;
                   1057:                d->ctlslot[i].mask = 0;
                   1058:                d->ctlslot[i].mode = 0;
                   1059:        }
1.1       ratchov  1060:        d->slot_list = NULL;
                   1061:        d->master = MIDI_MAXCTL;
                   1062:        d->mtc.origin = 0;
                   1063:        d->tstate = MMC_STOP;
1.64      ratchov  1064:        d->ctl_list = NULL;
1.1       ratchov  1065:        d->next = dev_list;
                   1066:        dev_list = d;
                   1067:        return d;
                   1068: }
                   1069:
                   1070: /*
1.73      ratchov  1071:  * add a alternate name
                   1072:  */
                   1073: int
                   1074: dev_addname(struct dev *d, char *name)
                   1075: {
                   1076:        struct dev_alt *a;
                   1077:
                   1078:        if (d->alt_list != NULL && d->alt_list->idx == DEV_NMAX - 1) {
                   1079:                log_puts(name);
                   1080:                log_puts(": too many alternate names\n");
                   1081:                return 0;
                   1082:        }
                   1083:        a = xmalloc(sizeof(struct dev_alt));
                   1084:        a->name = name;
                   1085:        a->idx = (d->alt_list == NULL) ? 0 : d->alt_list->idx + 1;
                   1086:        a->next = d->alt_list;
                   1087:        d->alt_list = a;
                   1088:        return 1;
                   1089: }
                   1090:
                   1091: /*
1.75      ratchov  1092:  * set prefered alt device name
                   1093:  */
                   1094: void
                   1095: dev_setalt(struct dev *d, unsigned int idx)
                   1096: {
                   1097:        struct dev_alt **pa, *a;
                   1098:
                   1099:        /* find alt with given index */
                   1100:        for (pa = &d->alt_list; (a = *pa)->idx != idx; pa = &a->next)
                   1101:                ;
                   1102:
                   1103:        /* detach from list */
                   1104:        *pa = a->next;
                   1105:
                   1106:        /* attach at head */
                   1107:        a->next = d->alt_list;
                   1108:        d->alt_list = a;
                   1109:
                   1110:        /* reopen device with the new alt */
                   1111:        if (idx != d->alt_num)
                   1112:                dev_reopen(d);
                   1113: }
                   1114:
                   1115: /*
1.1       ratchov  1116:  * adjust device parameters and mode
                   1117:  */
                   1118: void
                   1119: dev_adjpar(struct dev *d, int mode,
1.25      ratchov  1120:     int pmax, int rmax)
1.1       ratchov  1121: {
                   1122:        d->reqmode |= mode & MODE_AUDIOMASK;
                   1123:        if (mode & MODE_PLAY) {
                   1124:                if (d->reqpchan < pmax + 1)
                   1125:                        d->reqpchan = pmax + 1;
                   1126:        }
                   1127:        if (mode & MODE_REC) {
                   1128:                if (d->reqrchan < rmax + 1)
                   1129:                        d->reqrchan = rmax + 1;
                   1130:        }
                   1131: }
                   1132:
                   1133: /*
                   1134:  * Open the device with the dev_reqxxx capabilities. Setup a mixer, demuxer,
                   1135:  * monitor, midi control, and any necessary conversions.
                   1136:  */
                   1137: int
1.59      ratchov  1138: dev_allocbufs(struct dev *d)
1.1       ratchov  1139: {
                   1140:        if (d->mode & MODE_REC) {
                   1141:                /*
                   1142:                 * Create device <-> demuxer buffer
                   1143:                 */
                   1144:                d->rbuf = xmalloc(d->round * d->rchan * sizeof(adata_t));
                   1145:
                   1146:                /*
                   1147:                 * Insert a converter, if needed.
                   1148:                 */
                   1149:                if (!aparams_native(&d->par)) {
                   1150:                        dec_init(&d->dec, &d->par, d->rchan);
                   1151:                        d->decbuf = xmalloc(d->round * d->rchan * d->par.bps);
                   1152:                } else
                   1153:                        d->decbuf = NULL;
                   1154:        }
                   1155:        if (d->mode & MODE_PLAY) {
                   1156:                /*
                   1157:                 * Create device <-> mixer buffer
                   1158:                 */
                   1159:                d->poffs = 0;
1.15      ratchov  1160:                d->psize = d->bufsz + d->round;
                   1161:                d->pbuf = xmalloc(d->psize * d->pchan * sizeof(adata_t));
1.1       ratchov  1162:                d->mode |= MODE_MON;
                   1163:
                   1164:                /*
                   1165:                 * Append a converter, if needed.
                   1166:                 */
                   1167:                if (!aparams_native(&d->par)) {
                   1168:                        enc_init(&d->enc, &d->par, d->pchan);
                   1169:                        d->encbuf = xmalloc(d->round * d->pchan * d->par.bps);
                   1170:                } else
                   1171:                        d->encbuf = NULL;
                   1172:        }
                   1173:        if (log_level >= 2) {
                   1174:                dev_log(d);
                   1175:                log_puts(": ");
                   1176:                log_putu(d->rate);
                   1177:                log_puts("Hz, ");
                   1178:                aparams_log(&d->par);
                   1179:                if (d->mode & MODE_PLAY) {
                   1180:                        log_puts(", play 0:");
                   1181:                        log_puti(d->pchan - 1);
                   1182:                }
                   1183:                if (d->mode & MODE_REC) {
                   1184:                        log_puts(", rec 0:");
                   1185:                        log_puti(d->rchan - 1);
                   1186:                }
                   1187:                log_puts(", ");
                   1188:                log_putu(d->bufsz / d->round);
                   1189:                log_puts(" blocks of ");
                   1190:                log_putu(d->round);
                   1191:                log_puts(" frames\n");
                   1192:        }
1.57      ratchov  1193:        return 1;
                   1194: }
                   1195:
                   1196: /*
                   1197:  * Reset parameters and open the device.
                   1198:  */
                   1199: int
                   1200: dev_open(struct dev *d)
                   1201: {
1.64      ratchov  1202:        int i;
                   1203:        char name[CTL_NAMEMAX];
1.75      ratchov  1204:        struct dev_alt *a;
1.64      ratchov  1205:
1.70      ratchov  1206:        d->master_enabled = 0;
1.57      ratchov  1207:        d->mode = d->reqmode;
                   1208:        d->round = d->reqround;
                   1209:        d->bufsz = d->reqbufsz;
                   1210:        d->rate = d->reqrate;
                   1211:        d->pchan = d->reqpchan;
                   1212:        d->rchan = d->reqrchan;
                   1213:        d->par = d->reqpar;
                   1214:        if (d->pchan == 0)
                   1215:                d->pchan = 2;
                   1216:        if (d->rchan == 0)
                   1217:                d->rchan = 2;
1.59      ratchov  1218:        if (!dev_sio_open(d)) {
                   1219:                if (log_level >= 1) {
                   1220:                        dev_log(d);
                   1221:                        log_puts(": failed to open audio device\n");
                   1222:                }
                   1223:                return 0;
                   1224:        }
                   1225:        if (!dev_allocbufs(d))
1.57      ratchov  1226:                return 0;
1.64      ratchov  1227:
                   1228:        for (i = 0; i < DEV_NSLOT; i++) {
1.69      ratchov  1229:                if (d->slot[i].name[0] == 0)
                   1230:                        continue;
1.64      ratchov  1231:                slot_ctlname(&d->slot[i], name, CTL_NAMEMAX);
                   1232:                dev_addctl(d, "app", CTL_NUM,
                   1233:                    CTLADDR_SLOT_LEVEL(i),
                   1234:                    name, -1, "level",
                   1235:                    NULL, -1, 127, d->slot[i].vol);
                   1236:        }
                   1237:
1.75      ratchov  1238:        for (a = d->alt_list; a != NULL; a = a->next) {
                   1239:                snprintf(name, sizeof(name), "%d", a->idx);
                   1240:                dev_addctl(d, "", CTL_SEL,
                   1241:                    CTLADDR_ALT_SEL + a->idx,
                   1242:                    "server", -1, "device",
                   1243:                    name, -1, 1, a->idx == d->alt_num);
                   1244:        }
                   1245:
1.59      ratchov  1246:        d->pstate = DEV_INIT;
1.1       ratchov  1247:        return 1;
                   1248: }
                   1249:
                   1250: /*
1.72      ratchov  1251:  * Force all slots to exit and close device, called after an error
1.55      ratchov  1252:  */
                   1253: void
1.72      ratchov  1254: dev_abort(struct dev *d)
1.55      ratchov  1255: {
                   1256:        int i;
                   1257:        struct slot *s;
1.64      ratchov  1258:        struct ctlslot *c;
1.55      ratchov  1259:
                   1260:        for (s = d->slot, i = DEV_NSLOT; i > 0; i--, s++) {
                   1261:                if (s->ops)
                   1262:                        s->ops->exit(s->arg);
                   1263:                s->ops = NULL;
                   1264:        }
                   1265:        d->slot_list = NULL;
1.64      ratchov  1266:
                   1267:        for (c = d->ctlslot, i = DEV_NCTLSLOT; i > 0; i--, c++) {
                   1268:                if (c->ops)
                   1269:                        c->ops->exit(c->arg);
                   1270:                c->ops = NULL;
                   1271:        }
1.72      ratchov  1272:
                   1273:        if (d->pstate != DEV_CFG)
                   1274:                dev_close(d);
1.55      ratchov  1275: }
                   1276:
                   1277: /*
1.1       ratchov  1278:  * force the device to go in DEV_CFG state, the caller is supposed to
                   1279:  * ensure buffers are drained
                   1280:  */
                   1281: void
1.59      ratchov  1282: dev_freebufs(struct dev *d)
1.1       ratchov  1283: {
                   1284: #ifdef DEBUG
                   1285:        if (log_level >= 3) {
                   1286:                dev_log(d);
                   1287:                log_puts(": closing\n");
                   1288:        }
                   1289: #endif
                   1290:        if (d->mode & MODE_PLAY) {
                   1291:                if (d->encbuf != NULL)
                   1292:                        xfree(d->encbuf);
                   1293:                xfree(d->pbuf);
                   1294:        }
                   1295:        if (d->mode & MODE_REC) {
                   1296:                if (d->decbuf != NULL)
                   1297:                        xfree(d->decbuf);
                   1298:                xfree(d->rbuf);
                   1299:        }
1.58      ratchov  1300: }
                   1301:
                   1302: /*
                   1303:  * Close the device and exit all slots
                   1304:  */
                   1305: void
                   1306: dev_close(struct dev *d)
                   1307: {
1.64      ratchov  1308:        struct ctl *c;
                   1309:
1.59      ratchov  1310:        d->pstate = DEV_CFG;
                   1311:        dev_sio_close(d);
                   1312:        dev_freebufs(d);
1.64      ratchov  1313:
                   1314:        /* there are no clients, just free remaining local controls */
                   1315:        while ((c = d->ctl_list) != NULL) {
                   1316:                d->ctl_list = c->next;
                   1317:                xfree(c);
                   1318:        }
1.1       ratchov  1319: }
                   1320:
1.62      ratchov  1321: /*
                   1322:  * Close the device, but attempt to migrate everything to a new sndio
                   1323:  * device.
                   1324:  */
                   1325: int
                   1326: dev_reopen(struct dev *d)
                   1327: {
                   1328:        struct slot *s;
                   1329:        long long pos;
                   1330:        unsigned int pstate;
                   1331:        int delta;
                   1332:
                   1333:        /* not opened */
                   1334:        if (d->pstate == DEV_CFG)
                   1335:                return 1;
                   1336:
                   1337:        /* save state */
                   1338:        delta = d->delta;
                   1339:        pstate = d->pstate;
                   1340:
                   1341:        if (!dev_sio_reopen(d))
                   1342:                return 0;
                   1343:
                   1344:        /* reopen returns a stopped device */
                   1345:        d->pstate = DEV_INIT;
                   1346:
                   1347:        /* reallocate new buffers, with new parameters */
                   1348:        dev_freebufs(d);
                   1349:        dev_allocbufs(d);
                   1350:
                   1351:        /*
                   1352:         * adjust time positions, make anything go back delta ticks, so
                   1353:         * that the new device can start at zero
                   1354:         */
                   1355:        for (s = d->slot_list; s != NULL; s = s->next) {
                   1356:                pos = (long long)s->delta * d->round + s->delta_rem;
                   1357:                pos -= (long long)delta * s->round;
                   1358:                s->delta_rem = pos % (int)d->round;
                   1359:                s->delta = pos / (int)d->round;
                   1360:                if (log_level >= 3) {
                   1361:                        slot_log(s);
                   1362:                        log_puts(": adjusted: delta -> ");
                   1363:                        log_puti(s->delta);
                   1364:                        log_puts(", delta_rem -> ");
                   1365:                        log_puti(s->delta_rem);
                   1366:                        log_puts("\n");
                   1367:                }
                   1368:
                   1369:                /* reinitilize the format conversion chain */
                   1370:                slot_initconv(s);
                   1371:        }
                   1372:        if (d->tstate == MMC_RUN) {
                   1373:                d->mtc.delta -= delta * MTC_SEC;
                   1374:                if (log_level >= 2) {
                   1375:                        dev_log(d);
                   1376:                        log_puts(": adjusted mtc: delta ->");
                   1377:                        log_puti(d->mtc.delta);
                   1378:                        log_puts("\n");
                   1379:                }
                   1380:        }
                   1381:
1.71      ratchov  1382:        /* remove old controls and add new ones */
                   1383:        dev_sioctl_close(d);
1.64      ratchov  1384:        dev_sioctl_open(d);
                   1385:
1.62      ratchov  1386:        /* start the device if needed */
                   1387:        if (pstate == DEV_RUN)
                   1388:                dev_wakeup(d);
                   1389:
                   1390:        return 1;
                   1391: }
                   1392:
1.1       ratchov  1393: int
                   1394: dev_ref(struct dev *d)
                   1395: {
                   1396: #ifdef DEBUG
                   1397:        if (log_level >= 3) {
                   1398:                dev_log(d);
                   1399:                log_puts(": device requested\n");
                   1400:        }
                   1401: #endif
                   1402:        if (d->pstate == DEV_CFG && !dev_open(d))
                   1403:                return 0;
                   1404:        d->refcnt++;
                   1405:        return 1;
                   1406: }
                   1407:
                   1408: void
                   1409: dev_unref(struct dev *d)
                   1410: {
                   1411: #ifdef DEBUG
                   1412:        if (log_level >= 3) {
                   1413:                dev_log(d);
                   1414:                log_puts(": device released\n");
                   1415:        }
                   1416: #endif
                   1417:        d->refcnt--;
                   1418:        if (d->refcnt == 0 && d->pstate == DEV_INIT)
                   1419:                dev_close(d);
                   1420: }
                   1421:
                   1422: /*
                   1423:  * initialize the device with the current parameters
                   1424:  */
                   1425: int
                   1426: dev_init(struct dev *d)
                   1427: {
                   1428:        if ((d->reqmode & MODE_AUDIOMASK) == 0) {
                   1429: #ifdef DEBUG
                   1430:                    dev_log(d);
                   1431:                    log_puts(": has no streams\n");
                   1432: #endif
                   1433:                    return 0;
                   1434:        }
                   1435:        if (d->hold && !dev_ref(d))
                   1436:                return 0;
                   1437:        return 1;
                   1438: }
                   1439:
                   1440: /*
                   1441:  * Unless the device is already in process of closing, request it to close
                   1442:  */
                   1443: void
                   1444: dev_done(struct dev *d)
                   1445: {
                   1446: #ifdef DEBUG
                   1447:        if (log_level >= 3) {
                   1448:                dev_log(d);
                   1449:                log_puts(": draining\n");
                   1450:        }
                   1451: #endif
1.20      ratchov  1452:        if (d->tstate != MMC_STOP)
                   1453:                dev_mmcstop(d);
1.1       ratchov  1454:        if (d->hold)
                   1455:                dev_unref(d);
                   1456: }
                   1457:
                   1458: struct dev *
                   1459: dev_bynum(int num)
                   1460: {
                   1461:        struct dev *d;
                   1462:
                   1463:        for (d = dev_list; d != NULL; d = d->next) {
1.19      ratchov  1464:                if (d->num == num)
1.1       ratchov  1465:                        return d;
                   1466:        }
                   1467:        return NULL;
                   1468: }
                   1469:
                   1470: /*
                   1471:  * Free the device
                   1472:  */
                   1473: void
                   1474: dev_del(struct dev *d)
                   1475: {
                   1476:        struct dev **p;
1.73      ratchov  1477:        struct dev_alt *a;
1.1       ratchov  1478:
                   1479: #ifdef DEBUG
                   1480:        if (log_level >= 3) {
                   1481:                dev_log(d);
                   1482:                log_puts(": deleting\n");
                   1483:        }
                   1484: #endif
1.36      ratchov  1485:        while (d->opt_list != NULL)
                   1486:                opt_del(d, d->opt_list);
1.1       ratchov  1487:        if (d->pstate != DEV_CFG)
                   1488:                dev_close(d);
                   1489:        for (p = &dev_list; *p != d; p = &(*p)->next) {
                   1490: #ifdef DEBUG
                   1491:                if (*p == NULL) {
                   1492:                        dev_log(d);
                   1493:                        log_puts(": device to delete not on the list\n");
                   1494:                        panic();
                   1495:                }
                   1496: #endif
                   1497:        }
                   1498:        midi_del(d->midi);
                   1499:        *p = d->next;
1.73      ratchov  1500:        while ((a = d->alt_list) != NULL) {
                   1501:                d->alt_list = a->next;
                   1502:                xfree(a);
                   1503:        }
1.1       ratchov  1504:        xfree(d);
                   1505: }
                   1506:
                   1507: unsigned int
                   1508: dev_roundof(struct dev *d, unsigned int newrate)
                   1509: {
                   1510:        return (d->round * newrate + d->rate / 2) / d->rate;
                   1511: }
                   1512:
                   1513: /*
                   1514:  * If the device is paused, then resume it.
                   1515:  */
                   1516: void
                   1517: dev_wakeup(struct dev *d)
                   1518: {
                   1519:        if (d->pstate == DEV_INIT) {
                   1520:                if (log_level >= 2) {
                   1521:                        dev_log(d);
                   1522:                        log_puts(": device started\n");
                   1523:                }
                   1524:                if (d->mode & MODE_PLAY) {
                   1525:                        d->prime = d->bufsz;
                   1526:                } else {
                   1527:                        d->prime = 0;
                   1528:                }
1.16      ratchov  1529:                d->poffs = 0;
1.12      ratchov  1530:
1.23      ratchov  1531:                /*
1.16      ratchov  1532:                 * empty cycles don't increment delta, so it's ok to
                   1533:                 * start at 0
                   1534:                 **/
1.23      ratchov  1535:                d->delta = 0;
1.12      ratchov  1536:
1.1       ratchov  1537:                d->pstate = DEV_RUN;
                   1538:                dev_sio_start(d);
                   1539:        }
                   1540: }
                   1541:
                   1542: /*
                   1543:  * check that all clients controlled by MMC are ready to start, if so,
                   1544:  * attach them all at the same position
                   1545:  */
                   1546: void
                   1547: dev_sync_attach(struct dev *d)
                   1548: {
                   1549:        int i;
                   1550:        struct slot *s;
                   1551:
                   1552:        if (d->tstate != MMC_START) {
                   1553:                if (log_level >= 2) {
                   1554:                        dev_log(d);
                   1555:                        log_puts(": not started by mmc yet, waiting...\n");
                   1556:                }
                   1557:                return;
                   1558:        }
                   1559:        for (i = 0; i < DEV_NSLOT; i++) {
                   1560:                s = d->slot + i;
1.46      ratchov  1561:                if (!s->ops || !s->opt->mmc)
1.1       ratchov  1562:                        continue;
1.46      ratchov  1563:                if (s->pstate != SLOT_READY) {
1.1       ratchov  1564: #ifdef DEBUG
                   1565:                        if (log_level >= 3) {
                   1566:                                slot_log(s);
                   1567:                                log_puts(": not ready, start delayed\n");
                   1568:                        }
                   1569: #endif
                   1570:                        return;
                   1571:                }
                   1572:        }
                   1573:        if (!dev_ref(d))
                   1574:                return;
                   1575:        for (i = 0; i < DEV_NSLOT; i++) {
                   1576:                s = d->slot + i;
1.46      ratchov  1577:                if (!s->ops || !s->opt->mmc)
1.1       ratchov  1578:                        continue;
1.46      ratchov  1579:                slot_attach(s);
1.1       ratchov  1580:        }
                   1581:        d->tstate = MMC_RUN;
                   1582:        dev_midi_full(d);
                   1583:        dev_wakeup(d);
                   1584: }
                   1585:
                   1586: /*
                   1587:  * start all slots simultaneously
                   1588:  */
                   1589: void
                   1590: dev_mmcstart(struct dev *d)
                   1591: {
                   1592:        if (d->tstate == MMC_STOP) {
                   1593:                d->tstate = MMC_START;
                   1594:                dev_sync_attach(d);
                   1595: #ifdef DEBUG
                   1596:        } else {
                   1597:                if (log_level >= 3) {
                   1598:                        dev_log(d);
                   1599:                        log_puts(": ignoring mmc start\n");
                   1600:                }
                   1601: #endif
                   1602:        }
                   1603: }
                   1604:
                   1605: /*
                   1606:  * stop all slots simultaneously
                   1607:  */
                   1608: void
                   1609: dev_mmcstop(struct dev *d)
                   1610: {
                   1611:        switch (d->tstate) {
                   1612:        case MMC_START:
                   1613:                d->tstate = MMC_STOP;
                   1614:                return;
                   1615:        case MMC_RUN:
                   1616:                d->tstate = MMC_STOP;
                   1617:                dev_unref(d);
                   1618:                break;
                   1619:        default:
                   1620: #ifdef DEBUG
                   1621:                if (log_level >= 3) {
                   1622:                        dev_log(d);
                   1623:                        log_puts(": ignored mmc stop\n");
                   1624:                }
                   1625: #endif
                   1626:                return;
                   1627:        }
                   1628: }
                   1629:
                   1630: /*
                   1631:  * relocate all slots simultaneously
                   1632:  */
                   1633: void
                   1634: dev_mmcloc(struct dev *d, unsigned int origin)
                   1635: {
                   1636:        if (log_level >= 2) {
                   1637:                dev_log(d);
                   1638:                log_puts(": relocated to ");
                   1639:                log_putu(origin);
                   1640:                log_puts("\n");
                   1641:        }
                   1642:        if (d->tstate == MMC_RUN)
                   1643:                dev_mmcstop(d);
                   1644:        d->mtc.origin = origin;
                   1645:        if (d->tstate == MMC_RUN)
                   1646:                dev_mmcstart(d);
                   1647: }
                   1648:
1.35      ratchov  1649: /*
                   1650:  * allocate buffers & conversion chain
                   1651:  */
                   1652: void
1.60      ratchov  1653: slot_initconv(struct slot *s)
1.35      ratchov  1654: {
1.63      ratchov  1655:        unsigned int dev_nch;
1.35      ratchov  1656:        struct dev *d = s->dev;
                   1657:
                   1658:        if (s->mode & MODE_PLAY) {
                   1659:                cmap_init(&s->mix.cmap,
1.42      ratchov  1660:                    s->opt->pmin, s->opt->pmin + s->mix.nch - 1,
                   1661:                    s->opt->pmin, s->opt->pmin + s->mix.nch - 1,
1.35      ratchov  1662:                    0, d->pchan - 1,
1.40      ratchov  1663:                    s->opt->pmin, s->opt->pmax);
1.35      ratchov  1664:                if (!aparams_native(&s->par)) {
1.42      ratchov  1665:                        dec_init(&s->mix.dec, &s->par, s->mix.nch);
1.35      ratchov  1666:                }
                   1667:                if (s->rate != d->rate) {
                   1668:                        resamp_init(&s->mix.resamp, s->round, d->round,
1.42      ratchov  1669:                            s->mix.nch);
1.35      ratchov  1670:                }
1.61      ratchov  1671:                s->mix.join = 1;
                   1672:                s->mix.expand = 1;
1.63      ratchov  1673:                if (s->opt->dup && s->mix.cmap.nch > 0) {
                   1674:                        dev_nch = d->pchan < (s->opt->pmax + 1) ?
                   1675:                            d->pchan - s->opt->pmin :
                   1676:                            s->opt->pmax - s->opt->pmin + 1;
                   1677:                        if (dev_nch > s->mix.nch)
                   1678:                                s->mix.expand = dev_nch / s->mix.nch;
                   1679:                        else if (s->mix.nch > dev_nch)
                   1680:                                s->mix.join = s->mix.nch / dev_nch;
1.61      ratchov  1681:                }
1.35      ratchov  1682:        }
                   1683:
                   1684:        if (s->mode & MODE_RECMASK) {
1.63      ratchov  1685:                unsigned int outchan = (s->mode & MODE_MON) ?
                   1686:                    d->pchan : d->rchan;
                   1687:
1.35      ratchov  1688:                cmap_init(&s->sub.cmap,
1.63      ratchov  1689:                    0, outchan - 1,
1.40      ratchov  1690:                    s->opt->rmin, s->opt->rmax,
1.42      ratchov  1691:                    s->opt->rmin, s->opt->rmin + s->sub.nch - 1,
                   1692:                    s->opt->rmin, s->opt->rmin + s->sub.nch - 1);
1.35      ratchov  1693:                if (s->rate != d->rate) {
                   1694:                        resamp_init(&s->sub.resamp, d->round, s->round,
1.42      ratchov  1695:                            s->sub.nch);
1.35      ratchov  1696:                }
                   1697:                if (!aparams_native(&s->par)) {
1.42      ratchov  1698:                        enc_init(&s->sub.enc, &s->par, s->sub.nch);
1.61      ratchov  1699:                }
                   1700:                s->sub.join = 1;
                   1701:                s->sub.expand = 1;
1.63      ratchov  1702:                if (s->opt->dup && s->sub.cmap.nch > 0) {
                   1703:                        dev_nch = outchan < (s->opt->rmax + 1) ?
                   1704:                            outchan - s->opt->rmin :
                   1705:                            s->opt->rmax - s->opt->rmin + 1;
                   1706:                        if (dev_nch > s->sub.nch)
                   1707:                                s->sub.join = dev_nch / s->sub.nch;
                   1708:                        else if (s->sub.nch > dev_nch)
                   1709:                                s->sub.expand = s->sub.nch / dev_nch;
1.35      ratchov  1710:                }
                   1711:
                   1712:                /*
                   1713:                 * cmap_copy() doesn't write samples in all channels,
                   1714:                 * for instance when mono->stereo conversion is
                   1715:                 * disabled. So we have to prefill cmap_copy() output
                   1716:                 * with silence.
                   1717:                 */
                   1718:                if (s->sub.resampbuf) {
                   1719:                        memset(s->sub.resampbuf, 0,
1.42      ratchov  1720:                            d->round * s->sub.nch * sizeof(adata_t));
1.35      ratchov  1721:                } else if (s->sub.encbuf) {
                   1722:                        memset(s->sub.encbuf, 0,
1.42      ratchov  1723:                            s->round * s->sub.nch * sizeof(adata_t));
1.35      ratchov  1724:                } else {
                   1725:                        memset(s->sub.buf.data, 0,
1.42      ratchov  1726:                            s->appbufsz * s->sub.nch * sizeof(adata_t));
1.35      ratchov  1727:                }
                   1728:        }
1.60      ratchov  1729: }
                   1730:
                   1731: /*
                   1732:  * allocate buffers & conversion chain
                   1733:  */
                   1734: void
                   1735: slot_allocbufs(struct slot *s)
                   1736: {
                   1737:        struct dev *d = s->dev;
                   1738:
                   1739:        if (s->mode & MODE_PLAY) {
                   1740:                s->mix.bpf = s->par.bps * s->mix.nch;
                   1741:                abuf_init(&s->mix.buf, s->appbufsz * s->mix.bpf);
                   1742:
                   1743:                s->mix.decbuf = NULL;
                   1744:                s->mix.resampbuf = NULL;
                   1745:                if (!aparams_native(&s->par)) {
                   1746:                        s->mix.decbuf =
                   1747:                            xmalloc(s->round * s->mix.nch * sizeof(adata_t));
                   1748:                }
                   1749:                if (s->rate != d->rate) {
                   1750:                        s->mix.resampbuf =
                   1751:                            xmalloc(d->round * s->mix.nch * sizeof(adata_t));
                   1752:                }
                   1753:        }
                   1754:
                   1755:        if (s->mode & MODE_RECMASK) {
                   1756:                s->sub.bpf = s->par.bps * s->sub.nch;
                   1757:                abuf_init(&s->sub.buf, s->appbufsz * s->sub.bpf);
                   1758:
                   1759:                s->sub.encbuf = NULL;
                   1760:                s->sub.resampbuf = NULL;
                   1761:                if (s->rate != d->rate) {
                   1762:                        s->sub.resampbuf =
                   1763:                            xmalloc(d->round * s->sub.nch * sizeof(adata_t));
                   1764:                }
                   1765:                if (!aparams_native(&s->par)) {
                   1766:                        s->sub.encbuf =
                   1767:                            xmalloc(s->round * s->sub.nch * sizeof(adata_t));
                   1768:                }
                   1769:        }
                   1770:
                   1771:        slot_initconv(s);
1.35      ratchov  1772:
                   1773: #ifdef DEBUG
                   1774:        if (log_level >= 3) {
                   1775:                slot_log(s);
                   1776:                log_puts(": allocated ");
                   1777:                log_putu(s->appbufsz);
                   1778:                log_puts("/");
                   1779:                log_putu(SLOT_BUFSZ(s));
                   1780:                log_puts(" fr buffers\n");
                   1781:        }
                   1782: #endif
                   1783: }
                   1784:
                   1785: /*
                   1786:  * free buffers & conversion chain
                   1787:  */
                   1788: void
                   1789: slot_freebufs(struct slot *s)
                   1790: {
                   1791:        if (s->mode & MODE_RECMASK) {
                   1792:                abuf_done(&s->sub.buf);
                   1793:                if (s->sub.encbuf)
                   1794:                        xfree(s->sub.encbuf);
                   1795:                if (s->sub.resampbuf)
                   1796:                        xfree(s->sub.resampbuf);
                   1797:        }
                   1798:
                   1799:        if (s->mode & MODE_PLAY) {
                   1800:                abuf_done(&s->mix.buf);
                   1801:                if (s->mix.decbuf)
                   1802:                        xfree(s->mix.decbuf);
                   1803:                if (s->mix.resampbuf)
                   1804:                        xfree(s->mix.resampbuf);
                   1805:        }
                   1806: }
                   1807:
1.1       ratchov  1808: /*
                   1809:  * allocate a new slot and register the given call-backs
                   1810:  */
                   1811: struct slot *
1.54      ratchov  1812: slot_new(struct dev *d, struct opt *opt, unsigned int id, char *who,
1.37      ratchov  1813:     struct slotops *ops, void *arg, int mode)
1.1       ratchov  1814: {
                   1815:        char *p;
                   1816:        char name[SLOT_NAMEMAX];
1.52      ratchov  1817:        unsigned int i, ser, bestser, bestidx;
                   1818:        struct slot *unit[DEV_NSLOT];
1.1       ratchov  1819:        struct slot *s;
                   1820:
                   1821:        /*
1.27      ratchov  1822:         * create a ``valid'' control name (lowcase, remove [^a-z], truncate)
1.1       ratchov  1823:         */
                   1824:        for (i = 0, p = who; ; p++) {
                   1825:                if (i == SLOT_NAMEMAX - 1 || *p == '\0') {
                   1826:                        name[i] = '\0';
                   1827:                        break;
                   1828:                } else if (*p >= 'A' && *p <= 'Z') {
                   1829:                        name[i++] = *p + 'a' - 'A';
                   1830:                } else if (*p >= 'a' && *p <= 'z')
                   1831:                        name[i++] = *p;
                   1832:        }
                   1833:        if (i == 0)
                   1834:                strlcpy(name, "noname", SLOT_NAMEMAX);
                   1835:
                   1836:        /*
1.52      ratchov  1837:         * build a unit-to-slot map for this name
1.1       ratchov  1838:         */
1.52      ratchov  1839:        for (i = 0; i < DEV_NSLOT; i++)
                   1840:                unit[i] = NULL;
                   1841:        for (i = 0; i < DEV_NSLOT; i++) {
                   1842:                s = d->slot + i;
1.1       ratchov  1843:                if (strcmp(s->name, name) == 0)
1.52      ratchov  1844:                        unit[s->unit] = s;
1.1       ratchov  1845:        }
                   1846:
                   1847:        /*
1.54      ratchov  1848:         * find the free slot with the least unit number and same id
                   1849:         */
                   1850:        for (i = 0; i < DEV_NSLOT; i++) {
                   1851:                s = unit[i];
                   1852:                if (s != NULL && s->ops == NULL && s->id == id)
                   1853:                        goto found;
                   1854:        }
                   1855:
                   1856:        /*
1.52      ratchov  1857:         * find the free slot with the least unit number
1.1       ratchov  1858:         */
1.52      ratchov  1859:        for (i = 0; i < DEV_NSLOT; i++) {
                   1860:                s = unit[i];
1.54      ratchov  1861:                if (s != NULL && s->ops == NULL) {
                   1862:                        s->id = id;
1.1       ratchov  1863:                        goto found;
1.54      ratchov  1864:                }
1.1       ratchov  1865:        }
                   1866:
                   1867:        /*
1.18      ratchov  1868:         * couldn't find a matching slot, pick oldest free slot
1.1       ratchov  1869:         * and set its name/unit
                   1870:         */
                   1871:        bestser = 0;
                   1872:        bestidx = DEV_NSLOT;
                   1873:        for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) {
                   1874:                if (s->ops != NULL)
                   1875:                        continue;
                   1876:                ser = d->serial - s->serial;
                   1877:                if (ser > bestser) {
                   1878:                        bestser = ser;
                   1879:                        bestidx = i;
                   1880:                }
                   1881:        }
1.51      ratchov  1882:        if (bestidx != DEV_NSLOT) {
                   1883:                s = d->slot + bestidx;
                   1884:                s->vol = MIDI_MAXCTL;
                   1885:                strlcpy(s->name, name, SLOT_NAMEMAX);
                   1886:                s->serial = d->serial++;
1.52      ratchov  1887:                for (i = 0; unit[i] != NULL; i++)
                   1888:                        ; /* nothing */
                   1889:                s->unit = i;
1.54      ratchov  1890:                s->id = id;
1.51      ratchov  1891:                goto found;
1.1       ratchov  1892:        }
1.53      ratchov  1893:
1.51      ratchov  1894:        if (log_level >= 1) {
1.1       ratchov  1895:                log_puts(name);
1.51      ratchov  1896:                log_puts(": out of sub-device slots\n");
1.1       ratchov  1897:        }
1.51      ratchov  1898:        return NULL;
1.1       ratchov  1899:
                   1900: found:
1.37      ratchov  1901:        if ((mode & MODE_REC) && (opt->mode & MODE_MON)) {
                   1902:                mode |= MODE_MON;
                   1903:                mode &= ~MODE_REC;
                   1904:        }
                   1905:        if ((mode & opt->mode) != mode) {
                   1906:                if (log_level >= 1) {
                   1907:                        slot_log(s);
                   1908:                        log_puts(": requested mode not allowed\n");
                   1909:                }
                   1910:                return 0;
                   1911:        }
1.1       ratchov  1912:        if (!dev_ref(d))
                   1913:                return NULL;
1.64      ratchov  1914:        dev_label(d, s - d->slot);
1.31      ratchov  1915:        if ((mode & d->mode) != mode) {
1.1       ratchov  1916:                if (log_level >= 1) {
                   1917:                        slot_log(s);
                   1918:                        log_puts(": requested mode not supported\n");
                   1919:                }
1.31      ratchov  1920:                dev_unref(d);
1.49      ratchov  1921:                return NULL;
1.1       ratchov  1922:        }
1.31      ratchov  1923:        s->dev = d;
1.37      ratchov  1924:        s->opt = opt;
1.31      ratchov  1925:        s->ops = ops;
                   1926:        s->arg = arg;
                   1927:        s->pstate = SLOT_INIT;
1.1       ratchov  1928:        s->mode = mode;
1.6       ratchov  1929:        aparams_init(&s->par);
1.41      ratchov  1930:        if (s->mode & MODE_PLAY)
1.42      ratchov  1931:                s->mix.nch = s->opt->pmax - s->opt->pmin + 1;
1.41      ratchov  1932:        if (s->mode & MODE_RECMASK)
1.42      ratchov  1933:                s->sub.nch = s->opt->rmax - s->opt->rmin + 1;
1.46      ratchov  1934:        s->xrun = s->opt->mmc ? XRUN_SYNC : XRUN_IGNORE;
1.1       ratchov  1935:        s->appbufsz = d->bufsz;
                   1936:        s->round = d->round;
1.5       ratchov  1937:        s->rate = d->rate;
1.1       ratchov  1938:        dev_midi_slotdesc(d, s);
                   1939:        dev_midi_vol(d, s);
1.43      ratchov  1940: #ifdef DEBUG
                   1941:        if (log_level >= 3) {
                   1942:                slot_log(s);
                   1943:                log_puts(": using ");
                   1944:                dev_log(d);
                   1945:                log_puts(".");
                   1946:                log_puts(opt->name);
                   1947:                log_puts(", mode = ");
                   1948:                log_putx(mode);
                   1949:                log_puts("\n");
                   1950:        }
                   1951: #endif
1.1       ratchov  1952:        return s;
                   1953: }
                   1954:
                   1955: /*
                   1956:  * release the given slot
                   1957:  */
                   1958: void
                   1959: slot_del(struct slot *s)
                   1960: {
                   1961:        s->arg = s;
                   1962:        s->ops = &zomb_slotops;
                   1963:        switch (s->pstate) {
                   1964:        case SLOT_INIT:
                   1965:                s->ops = NULL;
                   1966:                break;
                   1967:        case SLOT_START:
                   1968:        case SLOT_READY:
                   1969:        case SLOT_RUN:
                   1970:                slot_stop(s);
                   1971:                /* PASSTHROUGH */
                   1972:        case SLOT_STOP:
                   1973:                break;
                   1974:        }
                   1975:        dev_unref(s->dev);
                   1976:        s->dev = NULL;
                   1977: }
                   1978:
                   1979: /*
                   1980:  * change the slot play volume; called either by the slot or by MIDI
                   1981:  */
                   1982: void
                   1983: slot_setvol(struct slot *s, unsigned int vol)
                   1984: {
                   1985: #ifdef DEBUG
                   1986:        if (log_level >= 3) {
                   1987:                slot_log(s);
                   1988:                log_puts(": setting volume ");
                   1989:                log_putu(vol);
                   1990:                log_puts("\n");
                   1991:        }
                   1992: #endif
                   1993:        s->vol = vol;
                   1994:        s->mix.vol = MIDI_TO_ADATA(s->vol);
                   1995: }
                   1996:
                   1997: /*
                   1998:  * attach the slot to the device (ie start playing & recording
                   1999:  */
                   2000: void
                   2001: slot_attach(struct slot *s)
                   2002: {
                   2003:        struct dev *d = s->dev;
1.12      ratchov  2004:        long long pos;
1.1       ratchov  2005:        int startpos;
                   2006:
                   2007:        /*
                   2008:         * start the device if not started
                   2009:         */
                   2010:        dev_wakeup(d);
1.23      ratchov  2011:
1.1       ratchov  2012:        /*
                   2013:         * get the current position, the origin is when the first sample
                   2014:         * played and/or recorded
                   2015:         */
                   2016:        startpos = dev_getpos(d) * (int)s->round / (int)d->round;
1.12      ratchov  2017:
                   2018:        /*
                   2019:         * adjust initial clock
                   2020:         */
                   2021:        pos = (long long)d->delta * s->round;
                   2022:        s->delta = startpos + pos / (int)d->round;
                   2023:        s->delta_rem = pos % d->round;
                   2024:
1.1       ratchov  2025:        s->pstate = SLOT_RUN;
                   2026: #ifdef DEBUG
1.17      ratchov  2027:        if (log_level >= 2) {
1.1       ratchov  2028:                slot_log(s);
                   2029:                log_puts(": attached at ");
                   2030:                log_puti(startpos);
1.12      ratchov  2031:                log_puts(", delta = ");
                   2032:                log_puti(d->delta);
1.1       ratchov  2033:                log_puts("\n");
                   2034:        }
                   2035: #endif
                   2036:
                   2037:        /*
                   2038:         * We dont check whether the device is dying,
                   2039:         * because dev_xxx() functions are supposed to
                   2040:         * work (i.e., not to crash)
                   2041:         */
                   2042: #ifdef DEBUG
                   2043:        if ((s->mode & d->mode) != s->mode) {
                   2044:                slot_log(s);
1.24      ratchov  2045:                log_puts(": mode beyond device mode, not attaching\n");
1.1       ratchov  2046:                panic();
                   2047:        }
                   2048: #endif
                   2049:        s->next = d->slot_list;
                   2050:        d->slot_list = s;
                   2051:        if (s->mode & MODE_PLAY) {
                   2052:                s->mix.vol = MIDI_TO_ADATA(s->vol);
                   2053:                dev_mix_adjvol(d);
                   2054:        }
                   2055: }
                   2056:
                   2057: /*
                   2058:  * if MMC is enabled, and try to attach all slots synchronously, else
                   2059:  * simply attach the slot
                   2060:  */
                   2061: void
                   2062: slot_ready(struct slot *s)
                   2063: {
1.3       ratchov  2064:        /*
                   2065:         * device may be disconnected, and if so we're called from
                   2066:         * slot->ops->exit() on a closed device
1.23      ratchov  2067:         */
1.3       ratchov  2068:        if (s->dev->pstate == DEV_CFG)
                   2069:                return;
1.46      ratchov  2070:        if (!s->opt->mmc)
1.1       ratchov  2071:                slot_attach(s);
1.46      ratchov  2072:        else
1.1       ratchov  2073:                dev_sync_attach(s->dev);
                   2074: }
                   2075:
                   2076: /*
                   2077:  * setup buffers & conversion layers, prepare the slot to receive data
                   2078:  * (for playback) or start (recording).
                   2079:  */
                   2080: void
                   2081: slot_start(struct slot *s)
                   2082: {
                   2083: #ifdef DEBUG
                   2084:        if (s->pstate != SLOT_INIT) {
                   2085:                slot_log(s);
                   2086:                log_puts(": slot_start: wrong state\n");
                   2087:                panic();
                   2088:        }
                   2089:        if (s->mode & MODE_PLAY) {
                   2090:                if (log_level >= 3) {
                   2091:                        slot_log(s);
                   2092:                        log_puts(": playing ");
                   2093:                        aparams_log(&s->par);
                   2094:                        log_puts(" -> ");
1.35      ratchov  2095:                        aparams_log(&s->dev->par);
1.1       ratchov  2096:                        log_puts("\n");
                   2097:                }
                   2098:        }
                   2099:        if (s->mode & MODE_RECMASK) {
                   2100:                if (log_level >= 3) {
                   2101:                        slot_log(s);
                   2102:                        log_puts(": recording ");
                   2103:                        aparams_log(&s->par);
                   2104:                        log_puts(" <- ");
1.35      ratchov  2105:                        aparams_log(&s->dev->par);
1.1       ratchov  2106:                        log_puts("\n");
1.35      ratchov  2107:                }
1.1       ratchov  2108:        }
                   2109: #endif
1.35      ratchov  2110:        slot_allocbufs(s);
1.47      ratchov  2111:
                   2112:        if (s->mode & MODE_RECMASK) {
                   2113:                /*
                   2114:                 * N-th recorded block is the N-th played block
                   2115:                 */
                   2116:                s->sub.prime = -dev_getpos(s->dev) / s->dev->round;
                   2117:        }
                   2118:        s->skip = 0;
                   2119:
1.1       ratchov  2120:        if (s->mode & MODE_PLAY) {
                   2121:                s->pstate = SLOT_START;
                   2122:        } else {
                   2123:                s->pstate = SLOT_READY;
                   2124:                slot_ready(s);
                   2125:        }
                   2126: }
                   2127:
                   2128: /*
                   2129:  * stop playback and recording, and free conversion layers
                   2130:  */
                   2131: void
                   2132: slot_detach(struct slot *s)
                   2133: {
                   2134:        struct slot **ps;
                   2135:
                   2136: #ifdef DEBUG
                   2137:        if (log_level >= 3) {
                   2138:                slot_log(s);
                   2139:                log_puts(": detaching\n");
                   2140:        }
                   2141: #endif
                   2142:        for (ps = &s->dev->slot_list; *ps != s; ps = &(*ps)->next) {
                   2143: #ifdef DEBUG
1.28      ratchov  2144:                if (*ps == NULL) {
1.1       ratchov  2145:                        slot_log(s);
                   2146:                        log_puts(": can't detach, not on list\n");
                   2147:                        panic();
                   2148:                }
                   2149: #endif
1.23      ratchov  2150:        }
1.1       ratchov  2151:        *ps = s->next;
1.35      ratchov  2152:        if (s->mode & MODE_PLAY)
1.1       ratchov  2153:                dev_mix_adjvol(s->dev);
                   2154: }
                   2155:
                   2156: /*
                   2157:  * put the slot in stopping state (draining play buffers) or
                   2158:  * stop & detach if no data to drain.
                   2159:  */
                   2160: void
                   2161: slot_stop(struct slot *s)
                   2162: {
                   2163: #ifdef DEBUG
                   2164:        if (log_level >= 3) {
                   2165:                slot_log(s);
                   2166:                log_puts(": stopping\n");
                   2167:        }
                   2168: #endif
                   2169:        if (s->pstate == SLOT_START) {
1.33      ratchov  2170:                /*
                   2171:                 * If in rec-only mode, we're already in the READY or
                   2172:                 * RUN states. We're here because the play buffer was
                   2173:                 * not full enough, try to start so it's drained.
                   2174:                 */
                   2175:                s->pstate = SLOT_READY;
                   2176:                slot_ready(s);
1.1       ratchov  2177:        }
1.34      ratchov  2178:
                   2179:        if (s->pstate == SLOT_RUN) {
                   2180:                if (s->mode & MODE_PLAY) {
                   2181:                        /*
                   2182:                         * Don't detach, dev_cycle() will do it for us
                   2183:                         * when the buffer is drained.
                   2184:                         */
                   2185:                        s->pstate = SLOT_STOP;
                   2186:                        return;
                   2187:                }
                   2188:                slot_detach(s);
                   2189:        } else {
1.1       ratchov  2190: #ifdef DEBUG
                   2191:                if (log_level >= 3) {
                   2192:                        slot_log(s);
                   2193:                        log_puts(": not drained (blocked by mmc)\n");
                   2194:                }
                   2195: #endif
                   2196:        }
1.35      ratchov  2197:
1.34      ratchov  2198:        s->pstate = SLOT_INIT;
                   2199:        s->ops->eof(s->arg);
1.35      ratchov  2200:        slot_freebufs(s);
1.1       ratchov  2201: }
                   2202:
1.12      ratchov  2203: void
                   2204: slot_skip_update(struct slot *s)
                   2205: {
                   2206:        int skip;
                   2207:
                   2208:        skip = slot_skip(s);
                   2209:        while (skip > 0) {
                   2210: #ifdef DEBUG
                   2211:                if (log_level >= 4) {
                   2212:                        slot_log(s);
                   2213:                        log_puts(": catching skipped block\n");
                   2214:                }
                   2215: #endif
                   2216:                if (s->mode & MODE_RECMASK)
                   2217:                        s->ops->flush(s->arg);
                   2218:                if (s->mode & MODE_PLAY)
                   2219:                        s->ops->fill(s->arg);
                   2220:                skip--;
                   2221:        }
                   2222: }
                   2223:
1.1       ratchov  2224: /*
                   2225:  * notify the slot that we just wrote in the play buffer, must be called
                   2226:  * after each write
                   2227:  */
                   2228: void
                   2229: slot_write(struct slot *s)
                   2230: {
                   2231:        if (s->pstate == SLOT_START && s->mix.buf.used == s->mix.buf.len) {
                   2232: #ifdef DEBUG
                   2233:                if (log_level >= 4) {
                   2234:                        slot_log(s);
                   2235:                        log_puts(": switching to READY state\n");
                   2236:                }
                   2237: #endif
                   2238:                s->pstate = SLOT_READY;
                   2239:                slot_ready(s);
                   2240:        }
1.12      ratchov  2241:        slot_skip_update(s);
1.1       ratchov  2242: }
                   2243:
                   2244: /*
                   2245:  * notify the slot that we freed some space in the rec buffer
                   2246:  */
                   2247: void
                   2248: slot_read(struct slot *s)
                   2249: {
1.12      ratchov  2250:        slot_skip_update(s);
1.64      ratchov  2251: }
                   2252:
                   2253: /*
                   2254:  * allocate at control slot
                   2255:  */
                   2256: struct ctlslot *
                   2257: ctlslot_new(struct dev *d, struct ctlops *ops, void *arg)
                   2258: {
                   2259:        struct ctlslot *s;
                   2260:        struct ctl *c;
                   2261:        int i;
                   2262:
                   2263:        i = 0;
                   2264:        for (;;) {
                   2265:                if (i == DEV_NCTLSLOT)
                   2266:                        return NULL;
                   2267:                s = d->ctlslot + i;
                   2268:                if (s->ops == NULL)
                   2269:                        break;
                   2270:                i++;
                   2271:        }
                   2272:        s->dev = d;
                   2273:        s->mask = 1 << i;
                   2274:        if (!dev_ref(d))
                   2275:                return NULL;
                   2276:        s->ops = ops;
                   2277:        s->arg = arg;
                   2278:        for (c = d->ctl_list; c != NULL; c = c->next)
                   2279:                c->refs_mask |= s->mask;
                   2280:        return s;
                   2281: }
                   2282:
                   2283: /*
                   2284:  * free control slot
                   2285:  */
                   2286: void
                   2287: ctlslot_del(struct ctlslot *s)
                   2288: {
                   2289:        struct ctl *c, **pc;
                   2290:
                   2291:        pc = &s->dev->ctl_list;
                   2292:        while ((c = *pc) != NULL) {
                   2293:                c->refs_mask &= ~s->mask;
                   2294:                if (c->refs_mask == 0) {
                   2295:                        *pc = c->next;
                   2296:                        xfree(c);
                   2297:                } else
                   2298:                        pc = &c->next;
                   2299:        }
                   2300:        s->ops = NULL;
                   2301:        dev_unref(s->dev);
                   2302: }
                   2303:
                   2304: void
                   2305: ctl_node_log(struct ctl_node *c)
                   2306: {
                   2307:        log_puts(c->name);
                   2308:        if (c->unit >= 0)
                   2309:                log_putu(c->unit);
                   2310: }
                   2311:
                   2312: void
                   2313: ctl_log(struct ctl *c)
                   2314: {
                   2315:        if (c->group[0] != 0) {
                   2316:                log_puts(c->group);
                   2317:                log_puts("/");
                   2318:        }
                   2319:        ctl_node_log(&c->node0);
                   2320:        log_puts(".");
                   2321:        log_puts(c->func);
                   2322:        log_puts("=");
                   2323:        switch (c->type) {
1.67      ratchov  2324:        case CTL_NONE:
                   2325:                log_puts("none");
                   2326:                break;
1.64      ratchov  2327:        case CTL_NUM:
                   2328:        case CTL_SW:
                   2329:                log_putu(c->curval);
                   2330:                break;
                   2331:        case CTL_VEC:
                   2332:        case CTL_LIST:
1.74      ratchov  2333:        case CTL_SEL:
1.64      ratchov  2334:                ctl_node_log(&c->node1);
                   2335:                log_puts(":");
                   2336:                log_putu(c->curval);
                   2337:        }
                   2338:        log_puts(" at ");
                   2339:        log_putu(c->addr);
                   2340: }
                   2341:
                   2342: /*
                   2343:  * add a ctl
                   2344:  */
                   2345: struct ctl *
                   2346: dev_addctl(struct dev *d, char *gstr, int type, int addr,
                   2347:     char *str0, int unit0, char *func, char *str1, int unit1, int maxval, int val)
                   2348: {
                   2349:        struct ctl *c, **pc;
                   2350:        int i;
                   2351:
                   2352:        c = xmalloc(sizeof(struct ctl));
                   2353:        c->type = type;
                   2354:        strlcpy(c->func, func, CTL_NAMEMAX);
                   2355:        strlcpy(c->group, gstr, CTL_NAMEMAX);
                   2356:        strlcpy(c->node0.name, str0, CTL_NAMEMAX);
                   2357:        c->node0.unit = unit0;
1.74      ratchov  2358:        if (c->type == CTL_VEC || c->type == CTL_LIST || c->type == CTL_SEL) {
1.64      ratchov  2359:                strlcpy(c->node1.name, str1, CTL_NAMEMAX);
                   2360:                c->node1.unit = unit1;
                   2361:        } else
                   2362:                memset(&c->node1, 0, sizeof(struct ctl_node));
                   2363:        c->addr = addr;
                   2364:        c->maxval = maxval;
                   2365:        c->val_mask = ~0;
                   2366:        c->desc_mask = ~0;
                   2367:        c->curval = val;
                   2368:        c->dirty = 0;
                   2369:        c->refs_mask = 0;
                   2370:        for (i = 0; i < DEV_NCTLSLOT; i++) {
                   2371:                c->refs_mask |= CTL_DEVMASK;
                   2372:                if (d->ctlslot[i].ops != NULL)
                   2373:                        c->refs_mask |= 1 << i;
                   2374:        }
                   2375:        for (pc = &d->ctl_list; *pc != NULL; pc = &(*pc)->next)
                   2376:                ; /* nothing */
                   2377:        c->next = NULL;
                   2378:        *pc = c;
                   2379: #ifdef DEBUG
                   2380:        if (log_level >= 3) {
                   2381:                dev_log(d);
                   2382:                log_puts(": adding ");
                   2383:                ctl_log(c);
                   2384:                log_puts("\n");
                   2385:        }
                   2386: #endif
                   2387:        return c;
                   2388: }
                   2389:
                   2390: void
                   2391: dev_rmctl(struct dev *d, int addr)
                   2392: {
                   2393:        struct ctl *c, **pc;
                   2394:
                   2395:        pc = &d->ctl_list;
                   2396:        for (;;) {
                   2397:                c = *pc;
                   2398:                if (c == NULL)
                   2399:                        return;
                   2400:                if (c->type != CTL_NONE && c->addr == addr)
                   2401:                        break;
                   2402:                pc = &c->next;
                   2403:        }
                   2404:        c->type = CTL_NONE;
                   2405: #ifdef DEBUG
                   2406:        if (log_level >= 3) {
                   2407:                dev_log(d);
                   2408:                log_puts(": removing ");
                   2409:                ctl_log(c);
                   2410:                log_puts(", refs_mask = 0x");
                   2411:                log_putx(c->refs_mask);
                   2412:                log_puts("\n");
                   2413:        }
                   2414: #endif
                   2415:        c->refs_mask &= ~CTL_DEVMASK;
1.68      ratchov  2416:        if (c->refs_mask == 0) {
                   2417:                *pc = c->next;
                   2418:                xfree(c);
1.64      ratchov  2419:                return;
1.68      ratchov  2420:        }
                   2421:        c->desc_mask = ~0;
1.65      ratchov  2422: }
                   2423:
                   2424: void
                   2425: dev_ctlsync(struct dev *d)
                   2426: {
1.70      ratchov  2427:        struct ctl *c;
1.65      ratchov  2428:        struct ctlslot *s;
1.70      ratchov  2429:        int found, i;
                   2430:
                   2431:        found = 0;
                   2432:        for (c = d->ctl_list; c != NULL; c = c->next) {
                   2433:                if (c->addr != CTLADDR_MASTER &&
                   2434:                    c->type == CTL_NUM &&
                   2435:                    strcmp(c->group, "") == 0 &&
                   2436:                    strcmp(c->node0.name, "output") == 0 &&
                   2437:                    strcmp(c->func, "level") == 0)
                   2438:                        found = 1;
                   2439:        }
                   2440:
                   2441:        if (d->master_enabled && found) {
                   2442:                if (log_level >= 2) {
                   2443:                        dev_log(d);
                   2444:                        log_puts(": software master level control disabled\n");
                   2445:                }
                   2446:                d->master_enabled = 0;
                   2447:                dev_rmctl(d, CTLADDR_MASTER);
                   2448:        } else if (!d->master_enabled && !found) {
                   2449:                if (log_level >= 2) {
                   2450:                        dev_log(d);
                   2451:                        log_puts(": software master level control enabled\n");
                   2452:                }
                   2453:                d->master_enabled = 1;
                   2454:                dev_addctl(d, "", CTL_NUM, CTLADDR_MASTER,
                   2455:                    "output", -1, "level", NULL, -1, 127, d->master);
                   2456:        }
1.65      ratchov  2457:
                   2458:        for (s = d->ctlslot, i = DEV_NCTLSLOT; i > 0; i--, s++) {
                   2459:                if (s->ops)
                   2460:                        s->ops->sync(s->arg);
                   2461:        }
1.64      ratchov  2462: }
                   2463:
                   2464: int
                   2465: dev_setctl(struct dev *d, int addr, int val)
                   2466: {
                   2467:        struct ctl *c;
                   2468:        int num;
                   2469:
                   2470:        c = d->ctl_list;
                   2471:        for (;;) {
                   2472:                if (c == NULL) {
                   2473:                        if (log_level >= 3) {
                   2474:                                dev_log(d);
                   2475:                                log_puts(": ");
                   2476:                                log_putu(addr);
                   2477:                                log_puts(": no such ctl address\n");
                   2478:                        }
                   2479:                        return 0;
                   2480:                }
                   2481:                if (c->type != CTL_NONE && c->addr == addr)
                   2482:                        break;
                   2483:                c = c->next;
                   2484:        }
                   2485:        if (c->curval == val) {
                   2486:                if (log_level >= 3) {
                   2487:                        ctl_log(c);
                   2488:                        log_puts(": already set\n");
                   2489:                }
                   2490:                return 1;
                   2491:        }
                   2492:        if (val < 0 || val > c->maxval) {
                   2493:                if (log_level >= 3) {
                   2494:                        dev_log(d);
                   2495:                        log_puts(": ");
                   2496:                        log_putu(val);
                   2497:                        log_puts(": ctl val out of bounds\n");
                   2498:                }
                   2499:                return 0;
                   2500:        }
                   2501:        if (addr >= CTLADDR_END) {
                   2502:                if (log_level >= 3) {
                   2503:                        ctl_log(c);
                   2504:                        log_puts(": marked as dirty\n");
                   2505:                }
                   2506:                c->dirty = 1;
                   2507:                dev_ref(d);
                   2508:        } else {
1.75      ratchov  2509:                if (addr >= CTLADDR_ALT_SEL) {
                   2510:                        if (val) {
                   2511:                                num = addr - CTLADDR_ALT_SEL;
                   2512:                                dev_setalt(d, num);
                   2513:                        }
                   2514:                        return 1;
                   2515:                } else if (addr == CTLADDR_MASTER) {
1.70      ratchov  2516:                        if (d->master_enabled) {
                   2517:                                dev_master(d, val);
                   2518:                                dev_midi_master(d);
                   2519:                        }
1.64      ratchov  2520:                } else {
                   2521:                        num = addr - CTLADDR_SLOT_LEVEL(0);
                   2522:                        slot_setvol(d->slot + num, val);
                   2523:                        dev_midi_vol(d, d->slot + num);
                   2524:                }
1.66      ratchov  2525:                c->val_mask = ~0U;
1.64      ratchov  2526:        }
                   2527:        c->curval = val;
                   2528:        return 1;
                   2529: }
                   2530:
                   2531: int
                   2532: dev_onval(struct dev *d, int addr, int val)
                   2533: {
                   2534:        struct ctl *c;
                   2535:
                   2536:        c = d->ctl_list;
                   2537:        for (;;) {
                   2538:                if (c == NULL)
                   2539:                        return 0;
                   2540:                if (c->type != CTL_NONE && c->addr == addr)
                   2541:                        break;
                   2542:                c = c->next;
                   2543:        }
                   2544:        c->curval = val;
                   2545:        c->val_mask = ~0U;
                   2546:        return 1;
                   2547: }
                   2548:
                   2549: void
                   2550: dev_label(struct dev *d, int i)
                   2551: {
                   2552:        struct ctl *c;
                   2553:        char name[CTL_NAMEMAX];
                   2554:
1.69      ratchov  2555:        slot_ctlname(&d->slot[i], name, CTL_NAMEMAX);
                   2556:
1.64      ratchov  2557:        c = d->ctl_list;
                   2558:        for (;;) {
1.69      ratchov  2559:                if (c == NULL) {
                   2560:                        dev_addctl(d, "app", CTL_NUM,
                   2561:                            CTLADDR_SLOT_LEVEL(i),
                   2562:                            name, -1, "level",
                   2563:                            NULL, -1, 127, d->slot[i].vol);
1.64      ratchov  2564:                        return;
1.69      ratchov  2565:                }
1.64      ratchov  2566:                if (c->addr == CTLADDR_SLOT_LEVEL(i))
                   2567:                        break;
                   2568:                c = c->next;
                   2569:        }
                   2570:        if (strcmp(c->node0.name, name) == 0)
                   2571:                return;
                   2572:        strlcpy(c->node0.name, name, CTL_NAMEMAX);
                   2573:        c->desc_mask = ~0;
                   2574: }
                   2575:
                   2576: int
                   2577: dev_nctl(struct dev *d)
                   2578: {
                   2579:        struct ctl *c;
                   2580:        int n;
                   2581:
                   2582:        n = 0;
                   2583:        for (c = d->ctl_list; c != NULL; c = c->next)
                   2584:                n++;
                   2585:        return n;
1.1       ratchov  2586: }