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

Annotation of src/usr.bin/sndiod/midi.c, Revision 1.17

1.17    ! ratchov     1: /*     $OpenBSD: midi.c,v 1.16 2017/01/03 06:53:20 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 <stdlib.h>
                     19: #include <string.h>
                     20:
                     21: #include "abuf.h"
                     22: #include "defs.h"
                     23: #include "dev.h"
                     24: #include "file.h"
                     25: #include "midi.h"
                     26: #include "miofile.h"
                     27: #include "sysex.h"
                     28: #include "utils.h"
                     29:
                     30: int  port_open(struct port *);
                     31: void port_imsg(void *, unsigned char *, int);
                     32: void port_omsg(void *, unsigned char *, int);
                     33: void port_fill(void *, int);
                     34: void port_exit(void *);
                     35:
                     36: struct midiops port_midiops = {
                     37:        port_imsg,
                     38:        port_omsg,
                     39:        port_fill,
                     40:        port_exit
                     41: };
                     42:
                     43: #define MIDI_NEP 32
                     44: struct midi midi_ep[MIDI_NEP];
                     45: struct port *port_list = NULL;
                     46: unsigned int midi_portnum = 0;
                     47:
                     48: struct midithru {
1.2       ratchov    49:        unsigned int txmask, rxmask;
1.1       ratchov    50: #define MIDITHRU_NMAX 32
                     51: } midithru[MIDITHRU_NMAX];
                     52:
                     53: /*
                     54:  * length of voice and common messages (status byte included)
                     55:  */
                     56: unsigned int voice_len[] = { 3, 3, 3, 3, 2, 2, 3 };
                     57: unsigned int common_len[] = { 0, 2, 3, 2, 0, 0, 1, 1 };
                     58:
                     59: void
                     60: midi_log(struct midi *ep)
                     61: {
                     62:        log_puts("midi");
                     63:        log_putu(ep - midi_ep);
                     64: }
                     65:
                     66: void
                     67: midi_init(void)
                     68: {
                     69: }
                     70:
                     71: void
                     72: midi_done(void)
                     73: {
                     74: }
                     75:
                     76: struct midi *
                     77: midi_new(struct midiops *ops, void *arg, int mode)
                     78: {
                     79:        int i;
                     80:        struct midi *ep;
                     81:
                     82:        for (i = 0, ep = midi_ep;; i++, ep++) {
                     83:                if (i == MIDI_NEP)
                     84:                        return NULL;
                     85:                if (ep->ops == NULL)
                     86:                        break;
                     87:        }
                     88:        ep->ops = ops;
                     89:        ep->arg = arg;
                     90:        ep->used = 0;
                     91:        ep->len = 0;
                     92:        ep->idx = 0;
                     93:        ep->st = 0;
                     94:        ep->txmask = 0;
1.2       ratchov    95:        ep->self = 1 << i;
                     96:        ep->tickets = 0;
1.1       ratchov    97:        ep->mode = mode;
1.2       ratchov    98:
1.1       ratchov    99:        /*
1.16      ratchov   100:         * the output buffer is the client input
1.1       ratchov   101:         */
1.2       ratchov   102:        if (ep->mode & MODE_MIDIIN)
1.1       ratchov   103:                abuf_init(&ep->obuf, MIDI_BUFSZ);
1.2       ratchov   104:        midi_tickets(ep);
1.1       ratchov   105:        return ep;
                    106: }
                    107:
                    108: void
                    109: midi_del(struct midi *ep)
                    110: {
                    111:        int i;
1.2       ratchov   112:        struct midi *peer;
1.1       ratchov   113:
1.2       ratchov   114:        ep->txmask = 0;
                    115:        for (i = 0; i < MIDI_NEP; i++) {
                    116:                peer = midi_ep + i;
                    117:                if (peer->txmask & ep->self) {
                    118:                        peer->txmask &= ~ep->self;
                    119:                        midi_tickets(peer);
                    120:                }
                    121:        }
                    122:        for (i = 0; i < MIDITHRU_NMAX; i++) {
                    123:                midithru[i].txmask &= ~ep->self;
                    124:                midithru[i].rxmask &= ~ep->self;
                    125:        }
1.1       ratchov   126:        ep->ops = NULL;
                    127:        if (ep->mode & MODE_MIDIIN) {
                    128:                abuf_done(&ep->obuf);
                    129:        }
                    130: }
                    131:
                    132: /*
1.2       ratchov   133:  * connect two midi endpoints
1.1       ratchov   134:  */
                    135: void
1.2       ratchov   136: midi_link(struct midi *ep, struct midi *peer)
1.1       ratchov   137: {
1.2       ratchov   138:        if (ep->mode & MODE_MIDIOUT) {
                    139:                ep->txmask |= peer->self;
                    140:                midi_tickets(ep);
                    141:        }
                    142:        if (ep->mode & MODE_MIDIIN) {
                    143: #ifdef DEBUG
                    144:                if (ep->obuf.used > 0) {
                    145:                        midi_log(ep);
                    146:                        log_puts(": linked with non-empty buffer\n");
                    147:                        panic();
                    148:                }
                    149: #endif
1.16      ratchov   150:                /* ep has empty buffer, so no need to call midi_tickets() */
1.2       ratchov   151:                peer->txmask |= ep->self;
1.1       ratchov   152:        }
                    153: }
                    154:
                    155: /*
1.2       ratchov   156:  * add the midi endpoint in the ``tag'' midi thru box
1.1       ratchov   157:  */
                    158: void
1.2       ratchov   159: midi_tag(struct midi *ep, unsigned int tag)
1.1       ratchov   160: {
1.2       ratchov   161:        struct midi *peer;
                    162:        struct midithru *t = midithru + tag;
1.1       ratchov   163:        int i;
                    164:
1.2       ratchov   165:        if (ep->mode & MODE_MIDIOUT) {
                    166:                ep->txmask |= t->txmask;
                    167:                midi_tickets(ep);
                    168:        }
                    169:        if (ep->mode & MODE_MIDIIN) {
                    170: #ifdef DEBUG
                    171:                if (ep->obuf.used > 0) {
                    172:                        midi_log(ep);
                    173:                        log_puts(": tagged with non-empty buffer\n");
                    174:                        panic();
                    175:                }
                    176: #endif
                    177:                for (i = 0; i < MIDI_NEP; i++) {
                    178:                        if (!(t->rxmask & (1 << i)))
                    179:                                continue;
                    180:                        peer = midi_ep + i;
                    181:                        peer->txmask |= ep->self;
                    182:                }
1.1       ratchov   183:        }
1.2       ratchov   184:        if (ep->mode & MODE_MIDIOUT)
                    185:                t->rxmask |= ep->self;
                    186:        if (ep->mode & MODE_MIDIIN)
                    187:                t->txmask |= ep->self;
1.1       ratchov   188: }
                    189:
                    190: /*
1.6       ratchov   191:  * broadcast the given message to other endpoints
1.1       ratchov   192:  */
                    193: void
                    194: midi_send(struct midi *iep, unsigned char *msg, int size)
                    195: {
                    196:        struct midi *oep;
                    197:        int i;
                    198:
                    199: #ifdef DEBUG
                    200:        if (log_level >= 4) {
                    201:                midi_log(iep);
                    202:                log_puts(": sending:");
                    203:                for (i = 0; i < size; i++) {
                    204:                        log_puts(" ");
                    205:                        log_putx(msg[i]);
                    206:                }
                    207:                log_puts("\n");
                    208:        }
                    209: #endif
                    210:        for (i = 0; i < MIDI_NEP ; i++) {
                    211:                if ((iep->txmask & (1 << i)) == 0)
                    212:                        continue;
                    213:                oep = midi_ep + i;
                    214:                if (msg[0] <= 0x7f) {
                    215:                        if (oep->owner != iep)
                    216:                                continue;
                    217:                } else if (msg[0] <= 0xf7)
                    218:                        oep->owner = iep;
                    219: #ifdef DEBUG
                    220:                if (log_level >= 4) {
                    221:                        midi_log(iep);
                    222:                        log_puts(" -> ");
                    223:                        midi_log(oep);
                    224:                        log_puts("\n");
                    225:                }
                    226: #endif
                    227:                oep->ops->omsg(oep->arg, msg, size);
                    228:        }
                    229: }
                    230:
1.2       ratchov   231: /*
                    232:  * determine if we have gained more input tickets, and if so call the
                    233:  * fill() call-back to notify the i/o layer that it can send more data
                    234:  */
                    235: void
                    236: midi_tickets(struct midi *iep)
                    237: {
                    238:        int i, tickets, avail, maxavail;
                    239:        struct midi *oep;
1.17    ! ratchov   240:
        !           241:        /*
        !           242:         * don't request iep->ops->fill() too often as it generates
        !           243:         * useless network traffic: wait until we reach half of the
        !           244:         * max tickets count. As in the worst case (see comment below)
        !           245:         * one ticket may consume two bytes, the max ticket count is
        !           246:         * BUFSZ / 2 and halt of it is simply BUFSZ / 4.
        !           247:         */
        !           248:        if (iep->tickets >= MIDI_BUFSZ / 4)
        !           249:                return;
1.2       ratchov   250:
                    251:        maxavail = MIDI_BUFSZ;
                    252:        for (i = 0; i < MIDI_NEP ; i++) {
                    253:                if ((iep->txmask & (1 << i)) == 0)
                    254:                        continue;
                    255:                oep = midi_ep + i;
                    256:                avail = oep->obuf.len - oep->obuf.used;
                    257:                if (maxavail > avail)
                    258:                        maxavail = avail;
                    259:        }
                    260:
                    261:        /*
1.15      ratchov   262:         * in the worst case output message is twice the
1.2       ratchov   263:         * input message (2-byte messages with running status)
                    264:         */
                    265:        tickets = maxavail / 2 - iep->tickets;
                    266:        if (tickets > 0) {
                    267:                iep->tickets += tickets;
                    268:                iep->ops->fill(iep->arg, tickets);
                    269:        }
                    270: }
                    271:
                    272: /*
                    273:  * recalculate tickets of endpoints sending data to this one
                    274:  */
1.1       ratchov   275: void
                    276: midi_fill(struct midi *oep)
                    277: {
1.2       ratchov   278:        int i;
1.1       ratchov   279:        struct midi *iep;
                    280:
1.2       ratchov   281:        for (i = 0; i < MIDI_NEP; i++) {
1.1       ratchov   282:                iep = midi_ep + i;
1.2       ratchov   283:                if (iep->txmask & oep->self)
                    284:                        midi_tickets(iep);
1.1       ratchov   285:        }
                    286: }
                    287:
                    288: /*
1.2       ratchov   289:  * parse then give data chunk, and calling imsg() for each message
1.1       ratchov   290:  */
                    291: void
1.2       ratchov   292: midi_in(struct midi *iep, unsigned char *idata, int icount)
1.1       ratchov   293: {
                    294:        int i;
                    295:        unsigned char c;
                    296:
                    297:        for (i = 0; i < icount; i++) {
                    298:                c = *idata++;
                    299:                if (c >= 0xf8) {
                    300:                        if (c != MIDI_ACK)
                    301:                                iep->ops->imsg(iep->arg, &c, 1);
                    302:                } else if (c == SYSEX_END) {
                    303:                        if (iep->st == SYSEX_START) {
                    304:                                iep->msg[iep->idx++] = c;
                    305:                                iep->ops->imsg(iep->arg, iep->msg, iep->idx);
                    306:                        }
                    307:                        iep->st = 0;
                    308:                        iep->idx = 0;
                    309:                } else if (c >= 0xf0) {
                    310:                        iep->msg[0] = c;
                    311:                        iep->len = common_len[c & 7];
                    312:                        iep->st = c;
                    313:                        iep->idx = 1;
                    314:                } else if (c >= 0x80) {
                    315:                        iep->msg[0] = c;
                    316:                        iep->len = voice_len[(c >> 4) & 7];
                    317:                        iep->st = c;
                    318:                        iep->idx = 1;
                    319:                } else if (iep->st) {
                    320:                        if (iep->idx == 0 && iep->st != SYSEX_START)
                    321:                                iep->msg[iep->idx++] = iep->st;
                    322:                        iep->msg[iep->idx++] = c;
                    323:                        if (iep->idx == iep->len) {
                    324:                                iep->ops->imsg(iep->arg, iep->msg, iep->idx);
                    325:                                if (iep->st >= 0xf0)
                    326:                                        iep->st = 0;
                    327:                                iep->idx = 0;
                    328:                        } else if (iep->idx == MIDI_MSGMAX) {
                    329:                                /* sysex continued */
                    330:                                iep->ops->imsg(iep->arg, iep->msg, iep->idx);
                    331:                                iep->idx = 0;
                    332:                        }
                    333:                }
                    334:        }
1.2       ratchov   335:        iep->tickets -= icount;
                    336:        if (iep->tickets < 0)
                    337:                iep->tickets = 0;
1.7       ratchov   338:        midi_tickets(iep);
1.1       ratchov   339: }
                    340:
                    341: /*
                    342:  * store the given message in the output buffer
                    343:  */
                    344: void
1.15      ratchov   345: midi_out(struct midi *oep, unsigned char *idata, int icount)
1.1       ratchov   346: {
                    347:        unsigned char *odata;
                    348:        int ocount;
                    349: #ifdef DEBUG
                    350:        int i;
                    351: #endif
1.15      ratchov   352:
1.1       ratchov   353:        while (icount > 0) {
                    354:                if (oep->obuf.used == oep->obuf.len) {
                    355: #ifdef DEBUG
                    356:                        if (log_level >= 2) {
                    357:                                midi_log(oep);
1.2       ratchov   358:                                log_puts(": too slow, discarding ");
1.1       ratchov   359:                                log_putu(oep->obuf.used);
                    360:                                log_puts(" bytes\n");
                    361:                        }
                    362: #endif
                    363:                        abuf_rdiscard(&oep->obuf, oep->obuf.used);
                    364:                        oep->owner = NULL;
                    365:                        return;
                    366:                }
                    367:                odata = abuf_wgetblk(&oep->obuf, &ocount);
                    368:                if (ocount > icount)
                    369:                        ocount = icount;
                    370:                memcpy(odata, idata, ocount);
                    371: #ifdef DEBUG
                    372:                if (log_level >= 4) {
                    373:                        midi_log(oep);
                    374:                        log_puts(": out: ");
                    375:                        for (i = 0; i < ocount; i++) {
                    376:                                log_puts(" ");
                    377:                                log_putx(odata[i]);
                    378:                        }
                    379:                        log_puts("\n");
                    380:                }
                    381: #endif
                    382:                abuf_wcommit(&oep->obuf, ocount);
                    383:                icount -= ocount;
                    384:                idata += ocount;
                    385:        }
                    386: }
                    387:
                    388: void
                    389: port_log(struct port *p)
                    390: {
                    391:        midi_log(p->midi);
                    392: }
                    393:
                    394: void
                    395: port_imsg(void *arg, unsigned char *msg, int size)
                    396: {
                    397:        struct port *p = arg;
                    398:
                    399:        midi_send(p->midi, msg, size);
                    400: }
                    401:
                    402:
                    403: void
                    404: port_omsg(void *arg, unsigned char *msg, int size)
                    405: {
                    406:        struct port *p = arg;
                    407:
                    408:        midi_out(p->midi, msg, size);
                    409: }
                    410:
                    411: void
                    412: port_fill(void *arg, int count)
                    413: {
                    414:        /* no flow control */
                    415: }
                    416:
                    417: void
                    418: port_exit(void *arg)
                    419: {
                    420: #ifdef DEBUG
                    421:        struct port *p = arg;
                    422:
                    423:        if (log_level >= 3) {
                    424:                port_log(p);
1.3       ratchov   425:                log_puts(": port exit\n");
                    426:                panic();
1.1       ratchov   427:        }
                    428: #endif
                    429: }
                    430:
                    431: /*
                    432:  * create a new midi port
                    433:  */
                    434: struct port *
1.4       ratchov   435: port_new(char *path, unsigned int mode, int hold)
1.1       ratchov   436: {
1.12      ratchov   437:        struct port *c;
1.1       ratchov   438:
                    439:        c = xmalloc(sizeof(struct port));
1.14      ratchov   440:        c->path = xstrdup(path);
1.1       ratchov   441:        c->state = PORT_CFG;
1.4       ratchov   442:        c->hold = hold;
1.1       ratchov   443:        c->midi = midi_new(&port_midiops, c, mode);
1.11      ratchov   444:        c->num = midi_portnum++;
1.12      ratchov   445:        c->next = port_list;
                    446:        port_list = c;
1.1       ratchov   447:        return c;
                    448: }
                    449:
                    450: /*
                    451:  * destroy the given midi port
                    452:  */
                    453: void
                    454: port_del(struct port *c)
                    455: {
                    456:        struct port **p;
                    457:
                    458:        if (c->state != PORT_CFG)
                    459:                port_close(c);
                    460:        midi_del(c->midi);
                    461:        for (p = &port_list; *p != c; p = &(*p)->next) {
                    462: #ifdef DEBUG
                    463:                if (*p == NULL) {
                    464:                        log_puts("port to delete not on list\n");
                    465:                        panic();
                    466:                }
                    467: #endif
                    468:        }
                    469:        *p = c->next;
1.14      ratchov   470:        xfree(c->path);
1.1       ratchov   471:        xfree(c);
                    472: }
                    473:
1.3       ratchov   474: int
                    475: port_ref(struct port *c)
                    476: {
                    477: #ifdef DEBUG
                    478:        if (log_level >= 3) {
                    479:                port_log(c);
                    480:                log_puts(": port requested\n");
                    481:        }
                    482: #endif
                    483:        if (c->state == PORT_CFG && !port_open(c))
                    484:                return 0;
                    485:        return 1;
                    486: }
                    487:
                    488: void
                    489: port_unref(struct port *c)
                    490: {
                    491:        int i, rxmask;
                    492:
                    493: #ifdef DEBUG
                    494:        if (log_level >= 3) {
                    495:                port_log(c);
                    496:                log_puts(": port released\n");
                    497:        }
                    498: #endif
                    499:        for (rxmask = 0, i = 0; i < MIDI_NEP; i++)
                    500:                rxmask |= midi_ep[i].txmask;
1.10      ratchov   501:        if ((rxmask & c->midi->self) == 0 && c->midi->txmask == 0 &&
                    502:            c->state == PORT_INIT && !c->hold)
1.5       ratchov   503:                port_drain(c);
1.3       ratchov   504: }
                    505:
1.1       ratchov   506: struct port *
                    507: port_bynum(int num)
                    508: {
                    509:        struct port *p;
                    510:
                    511:        for (p = port_list; p != NULL; p = p->next) {
1.12      ratchov   512:                if (p->num == num)
1.1       ratchov   513:                        return p;
                    514:        }
                    515:        return NULL;
                    516: }
                    517:
                    518: int
                    519: port_open(struct port *c)
                    520: {
                    521:        if (!port_mio_open(c)) {
                    522:                if (log_level >= 1) {
                    523:                        log_puts(c->path);
                    524:                        log_puts(": failed to open midi port\n");
                    525:                }
                    526:                return 0;
                    527:        }
                    528:        c->state = PORT_INIT;
                    529:        return 1;
                    530: }
                    531:
                    532: int
                    533: port_close(struct port *c)
                    534: {
1.3       ratchov   535:        int i;
                    536:        struct midi *ep;
1.1       ratchov   537: #ifdef DEBUG
                    538:        if (c->state == PORT_CFG) {
                    539:                port_log(c);
                    540:                log_puts(": can't close port (not opened)\n");
1.3       ratchov   541:                panic();
1.1       ratchov   542:        }
                    543: #endif
1.15      ratchov   544:        c->state = PORT_CFG;
1.1       ratchov   545:        port_mio_close(c);
1.15      ratchov   546:
1.3       ratchov   547:        for (i = 0; i < MIDI_NEP; i++) {
                    548:                ep = midi_ep + i;
                    549:                if ((ep->txmask & c->midi->self) ||
                    550:                    (c->midi->txmask & ep->self))
                    551:                        ep->ops->exit(ep->arg);
                    552:        }
1.1       ratchov   553:        return 1;
                    554: }
                    555:
1.5       ratchov   556: void
                    557: port_drain(struct port *c)
                    558: {
                    559:        struct midi *ep = c->midi;
                    560:
                    561:        if (!(ep->mode & MODE_MIDIOUT) || ep->obuf.used == 0)
                    562:                port_close(c);
                    563:        else {
                    564:                c->state = PORT_DRAIN;
                    565: #ifdef DEBUG
                    566:                if (log_level >= 3) {
                    567:                        port_log(c);
                    568:                        log_puts(": draining\n");
                    569:                }
                    570: #endif
                    571:        }
                    572: }
                    573:
1.1       ratchov   574: int
                    575: port_init(struct port *c)
                    576: {
1.4       ratchov   577:        if (c->hold)
                    578:                return port_open(c);
                    579:        return 1;
1.1       ratchov   580: }
                    581:
                    582: void
                    583: port_done(struct port *c)
                    584: {
1.5       ratchov   585:        if (c->state == PORT_INIT)
                    586:                port_drain(c);
1.1       ratchov   587: }