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

Annotation of src/usr.bin/aucat/midi.c, Revision 1.34

1.34    ! ratchov     1: /*     $OpenBSD: midi.c,v 1.33 2011/05/09 18:03:08 ratchov Exp $       */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: /*
                     18:  * TODO
                     19:  *
1.7       ratchov    20:  * use shadow variables (to save NRPNs, LSB of controller)
                     21:  * in the midi merger
1.1       ratchov    22:  *
                     23:  * make output and input identical when only one
                     24:  * input is used (fix running status)
                     25:  */
                     26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
                     29:
                     30: #include "abuf.h"
                     31: #include "aproc.h"
1.3       ratchov    32: #include "conf.h"
                     33: #include "dev.h"
1.1       ratchov    34: #include "midi.h"
1.33      ratchov    35: #include "sysex.h"
1.14      ratchov    36: #ifdef DEBUG
                     37: #include "dbg.h"
                     38: #endif
1.1       ratchov    39:
                     40: /*
                     41:  * input data rate is XFER / TIMO (in bytes per microsecond),
                     42:  * it must be slightly larger than the MIDI standard 3125 bytes/s
                     43:  */
                     44: #define MIDITHRU_XFER 340
                     45: #define MIDITHRU_TIMO 100000
                     46:
1.3       ratchov    47: /*
                     48:  * masks to extract command and channel of status byte
                     49:  */
                     50: #define MIDI_CMDMASK   0xf0
                     51: #define MIDI_CHANMASK  0x0f
                     52:
                     53: /*
                     54:  * MIDI status bytes of voice messages
                     55:  */
                     56: #define MIDI_NOFF      0x80            /* note off */
                     57: #define MIDI_NON       0x90            /* note on */
                     58: #define MIDI_KAT       0xa0            /* key after touch */
                     59: #define MIDI_CTL       0xb0            /* controller */
                     60: #define MIDI_PC                0xc0            /* program change */
                     61: #define MIDI_CAT       0xd0            /* channel after touch */
                     62: #define MIDI_BEND      0xe0            /* pitch bend */
1.16      ratchov    63: #define MIDI_ACK       0xfe            /* active sensing message */
1.3       ratchov    64:
                     65: /*
                     66:  * MIDI controller numbers
                     67:  */
                     68: #define MIDI_CTLVOL    7               /* volume */
                     69: #define MIDI_CTLPAN    11              /* pan */
                     70:
                     71: /*
                     72:  * length of voice and common messages (status byte included)
                     73:  */
1.1       ratchov    74: unsigned voice_len[] = { 3, 3, 3, 3, 2, 2, 3 };
                     75: unsigned common_len[] = { 0, 2, 3, 2, 0, 0, 1, 1 };
                     76:
1.7       ratchov    77: /*
1.10      ratchov    78:  * send the message stored in of ibuf->r.midi.msg to obuf
1.7       ratchov    79:  */
1.1       ratchov    80: void
                     81: thru_flush(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
                     82: {
                     83:        unsigned ocount, itodo;
                     84:        unsigned char *odata, *idata;
                     85:
1.10      ratchov    86:        itodo = ibuf->r.midi.used;
                     87:        idata = ibuf->r.midi.msg;
1.14      ratchov    88: #ifdef DEBUG
                     89:        if (debug_level >= 4) {
                     90:                aproc_dbg(p);
                     91:                dbg_puts(": flushing ");
                     92:                dbg_putu(itodo);
                     93:                dbg_puts(" byte message\n");
                     94:        }
                     95: #endif
1.1       ratchov    96:        while (itodo > 0) {
                     97:                if (!ABUF_WOK(obuf)) {
1.14      ratchov    98: #ifdef DEBUG
1.28      ratchov    99:                        if (debug_level >= 3) {
1.14      ratchov   100:                                aproc_dbg(p);
                    101:                                dbg_puts(": overrun, discarding ");
                    102:                                dbg_putu(obuf->used);
                    103:                                dbg_puts(" bytes\n");
                    104:                        }
                    105: #endif
1.1       ratchov   106:                        abuf_rdiscard(obuf, obuf->used);
                    107:                        if (p->u.thru.owner == ibuf)
                    108:                                p->u.thru.owner = NULL;
                    109:                        return;
                    110:                }
                    111:                odata = abuf_wgetblk(obuf, &ocount, 0);
                    112:                if (ocount > itodo)
                    113:                        ocount = itodo;
                    114:                memcpy(odata, idata, ocount);
                    115:                abuf_wcommit(obuf, ocount);
                    116:                itodo -= ocount;
                    117:                idata += ocount;
                    118:        }
1.10      ratchov   119:        ibuf->r.midi.used = 0;
1.1       ratchov   120:        p->u.thru.owner = ibuf;
                    121: }
                    122:
1.7       ratchov   123: /*
                    124:  * send the real-time message (one byte) to obuf, similar to thrui_flush()
                    125:  */
1.1       ratchov   126: void
                    127: thru_rt(struct aproc *p, struct abuf *ibuf, struct abuf *obuf, unsigned c)
                    128: {
                    129:        unsigned ocount;
                    130:        unsigned char *odata;
                    131:
1.14      ratchov   132: #ifdef DEBUG
                    133:        if (debug_level >= 4) {
                    134:                aproc_dbg(p);
                    135:                dbg_puts(": ");
1.19      ratchov   136:                dbg_putx(c);
1.14      ratchov   137:                dbg_puts(": flushing realtime message\n");
                    138:        }
                    139: #endif
1.16      ratchov   140:        if (c == MIDI_ACK)
                    141:                return;
1.1       ratchov   142:        if (!ABUF_WOK(obuf)) {
1.14      ratchov   143: #ifdef DEBUG
1.28      ratchov   144:                if (debug_level >= 3) {
1.14      ratchov   145:                        aproc_dbg(p);
                    146:                        dbg_puts(": overrun, discarding ");
                    147:                        dbg_putu(obuf->used);
                    148:                        dbg_puts(" bytes\n");
                    149:                }
                    150: #endif
1.1       ratchov   151:                abuf_rdiscard(obuf, obuf->used);
                    152:                if (p->u.thru.owner == ibuf)
                    153:                        p->u.thru.owner = NULL;
                    154:        }
                    155:        odata = abuf_wgetblk(obuf, &ocount, 0);
                    156:        odata[0] = c;
                    157:        abuf_wcommit(obuf, 1);
                    158: }
                    159:
1.7       ratchov   160: /*
                    161:  * parse ibuf contents and store each message into obuf,
                    162:  * use at most ``todo'' bytes (for throttling)
                    163:  */
1.1       ratchov   164: void
                    165: thru_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf, unsigned todo)
                    166: {
                    167:        unsigned char *idata;
                    168:        unsigned c, icount, ioffs;
                    169:
                    170:        idata = NULL;
                    171:        icount = ioffs = 0;
                    172:        for (;;) {
                    173:                if (icount == 0) {
                    174:                        if (todo == 0)
                    175:                                break;
                    176:                        idata = abuf_rgetblk(ibuf, &icount, ioffs);
                    177:                        if (icount > todo)
                    178:                                icount = todo;
                    179:                        if (icount == 0)
                    180:                                break;
                    181:                        todo -= icount;
                    182:                        ioffs += icount;
                    183:                }
                    184:                c = *idata++;
                    185:                icount--;
                    186:                if (c < 0x80) {
1.10      ratchov   187:                        if (ibuf->r.midi.idx == 0 && ibuf->r.midi.st) {
                    188:                                ibuf->r.midi.msg[ibuf->r.midi.used++] = ibuf->r.midi.st;
                    189:                                ibuf->r.midi.idx++;
1.1       ratchov   190:                        }
1.10      ratchov   191:                        ibuf->r.midi.msg[ibuf->r.midi.used++] = c;
                    192:                        ibuf->r.midi.idx++;
                    193:                        if (ibuf->r.midi.idx == ibuf->r.midi.len) {
1.1       ratchov   194:                                thru_flush(p, ibuf, obuf);
1.10      ratchov   195:                                if (ibuf->r.midi.st >= 0xf0)
                    196:                                        ibuf->r.midi.st = 0;
                    197:                                ibuf->r.midi.idx = 0;
1.1       ratchov   198:                        }
1.10      ratchov   199:                        if (ibuf->r.midi.used == MIDI_MSGMAX) {
                    200:                                if (ibuf->r.midi.used == ibuf->r.midi.idx ||
1.1       ratchov   201:                                    p->u.thru.owner == ibuf)
                    202:                                        thru_flush(p, ibuf, obuf);
                    203:                                else
1.10      ratchov   204:                                        ibuf->r.midi.used = 0;
1.1       ratchov   205:                        }
                    206:                } else if (c < 0xf8) {
1.10      ratchov   207:                        if (ibuf->r.midi.used == ibuf->r.midi.idx ||
1.1       ratchov   208:                            p->u.thru.owner == ibuf) {
                    209:                                thru_flush(p, ibuf, obuf);
                    210:                        } else
1.10      ratchov   211:                                ibuf->r.midi.used = 0;
                    212:                        ibuf->r.midi.msg[0] = c;
                    213:                        ibuf->r.midi.used = 1;
                    214:                        ibuf->r.midi.len = (c >= 0xf0) ?
1.1       ratchov   215:                            common_len[c & 7] :
                    216:                            voice_len[(c >> 4) & 7];
1.10      ratchov   217:                        if (ibuf->r.midi.len == 1) {
1.1       ratchov   218:                                thru_flush(p, ibuf, obuf);
1.10      ratchov   219:                                ibuf->r.midi.idx = 0;
                    220:                                ibuf->r.midi.st = 0;
                    221:                                ibuf->r.midi.len = 0;
1.1       ratchov   222:                        } else {
1.10      ratchov   223:                                ibuf->r.midi.st = c;
                    224:                                ibuf->r.midi.idx = 1;
1.1       ratchov   225:                        }
                    226:                } else {
                    227:                        thru_rt(p, ibuf, obuf, c);
                    228:                }
                    229:        }
                    230: }
                    231:
                    232: int
                    233: thru_in(struct aproc *p, struct abuf *ibuf)
                    234: {
                    235:        struct abuf *i, *inext;
                    236:        unsigned todo;
                    237:
                    238:        if (!ABUF_ROK(ibuf))
                    239:                return 0;
1.10      ratchov   240:        if (ibuf->tickets == 0) {
1.14      ratchov   241: #ifdef DEBUG
                    242:                if (debug_level >= 4) {
                    243:                        abuf_dbg(ibuf);
                    244:                        dbg_puts(": out of tickets, blocking\n");
                    245:                }
                    246: #endif
1.1       ratchov   247:                return 0;
                    248:        }
                    249:        todo = ibuf->used;
1.10      ratchov   250:        if (todo > ibuf->tickets)
                    251:                todo = ibuf->tickets;
                    252:        ibuf->tickets -= todo;
1.20      ratchov   253:        for (i = LIST_FIRST(&p->outs); i != NULL; i = inext) {
1.1       ratchov   254:                inext = LIST_NEXT(i, oent);
                    255:                if (ibuf->duplex == i)
                    256:                        continue;
                    257:                thru_bcopy(p, ibuf, i, todo);
                    258:                (void)abuf_flush(i);
                    259:        }
                    260:        abuf_rdiscard(ibuf, todo);
                    261:        return 1;
                    262: }
                    263:
                    264: int
                    265: thru_out(struct aproc *p, struct abuf *obuf)
                    266: {
                    267:        return 0;
                    268: }
                    269:
                    270: void
                    271: thru_eof(struct aproc *p, struct abuf *ibuf)
                    272: {
1.13      ratchov   273:        if (!(p->flags & APROC_QUIT))
1.11      ratchov   274:                return;
1.34    ! ratchov   275:        if (LIST_EMPTY(&p->ins))
1.11      ratchov   276:                aproc_del(p);
1.1       ratchov   277: }
                    278:
                    279: void
                    280: thru_hup(struct aproc *p, struct abuf *obuf)
                    281: {
1.31      ratchov   282:        if (!(p->flags & APROC_QUIT))
                    283:                return;
1.34    ! ratchov   284:        if (LIST_EMPTY(&p->ins))
1.31      ratchov   285:                aproc_del(p);
1.1       ratchov   286: }
                    287:
                    288: void
                    289: thru_newin(struct aproc *p, struct abuf *ibuf)
                    290: {
1.10      ratchov   291:        ibuf->r.midi.used = 0;
                    292:        ibuf->r.midi.len = 0;
                    293:        ibuf->r.midi.idx = 0;
                    294:        ibuf->r.midi.st = 0;
                    295:        ibuf->tickets = MIDITHRU_XFER;
1.1       ratchov   296: }
                    297:
                    298: void
                    299: thru_done(struct aproc *p)
                    300: {
                    301:        timo_del(&p->u.thru.timo);
                    302: }
                    303:
                    304: struct aproc_ops thru_ops = {
                    305:        "thru",
                    306:        thru_in,
                    307:        thru_out,
                    308:        thru_eof,
                    309:        thru_hup,
                    310:        thru_newin,
                    311:        NULL, /* newout */
                    312:        NULL, /* ipos */
                    313:        NULL, /* opos */
                    314:        thru_done
                    315: };
                    316:
1.7       ratchov   317: /*
                    318:  * call-back invoked periodically to implement throttling at each invocation
                    319:  * gain more ``tickets'' for processing.  If one of the buffer was blocked by
                    320:  * the throttelling mechanism, then run it
                    321:  */
1.1       ratchov   322: void
                    323: thru_cb(void *addr)
                    324: {
                    325:        struct aproc *p = (struct aproc *)addr;
                    326:        struct abuf *i, *inext;
                    327:        unsigned tickets;
                    328:
                    329:        timo_add(&p->u.thru.timo, MIDITHRU_TIMO);
                    330:
1.20      ratchov   331:        for (i = LIST_FIRST(&p->ins); i != NULL; i = inext) {
1.1       ratchov   332:                inext = LIST_NEXT(i, ient);
1.10      ratchov   333:                tickets = i->tickets;
                    334:                i->tickets = MIDITHRU_XFER;
1.1       ratchov   335:                if (tickets == 0)
                    336:                        abuf_run(i);
                    337:        }
                    338: }
                    339:
                    340: struct aproc *
                    341: thru_new(char *name)
                    342: {
                    343:        struct aproc *p;
                    344:
                    345:        p = aproc_new(&thru_ops, name);
                    346:        p->u.thru.owner = NULL;
                    347:        timo_set(&p->u.thru.timo, thru_cb, p);
                    348:        timo_add(&p->u.thru.timo, MIDITHRU_TIMO);
1.3       ratchov   349:        return p;
                    350: }
                    351:
1.14      ratchov   352: #ifdef DEBUG
                    353: void
                    354: ctl_slotdbg(struct aproc *p, int slot)
                    355: {
                    356:        struct ctl_slot *s;
                    357:
                    358:        if (slot < 0) {
1.19      ratchov   359:                dbg_puts("none");
1.14      ratchov   360:        } else {
                    361:                s = p->u.ctl.slot + slot;
                    362:                dbg_puts(s->name);
                    363:                dbg_putu(s->unit);
1.19      ratchov   364:                dbg_puts("(");
1.14      ratchov   365:                dbg_putu(s->vol);
1.19      ratchov   366:                dbg_puts(")/");
1.14      ratchov   367:                switch (s->tstate) {
                    368:                case CTL_OFF:
                    369:                        dbg_puts("off");
                    370:                        break;
                    371:                case CTL_RUN:
                    372:                        dbg_puts("run");
                    373:                        break;
                    374:                case CTL_START:
                    375:                        dbg_puts("sta");
                    376:                        break;
                    377:                case CTL_STOP:
                    378:                        dbg_puts("stp");
                    379:                        break;
                    380:                default:
                    381:                        dbg_puts("unk");
                    382:                        break;
                    383:                }
                    384:        }
                    385: }
                    386: #endif
1.12      ratchov   387:
1.7       ratchov   388: /*
1.33      ratchov   389:  * send a message to the given output
                    390:  */
                    391: void
                    392: ctl_copymsg(struct abuf *obuf, unsigned char *msg, unsigned len)
                    393: {
                    394:        unsigned ocount, itodo;
                    395:        unsigned char *odata, *idata;
                    396:
                    397:        itodo = len;
                    398:        idata = msg;
                    399:        while (itodo > 0) {
                    400:                if (!ABUF_WOK(obuf)) {
                    401: #ifdef DEBUG
                    402:                        if (debug_level >= 3) {
                    403:                                abuf_dbg(obuf);
                    404:                                dbg_puts(": overrun, discarding ");
                    405:                                dbg_putu(obuf->used);
                    406:                                dbg_puts(" bytes\n");
                    407:                        }
                    408: #endif
                    409:                        abuf_rdiscard(obuf, obuf->used);
                    410:                }
                    411:                odata = abuf_wgetblk(obuf, &ocount, 0);
                    412:                if (ocount > itodo)
                    413:                        ocount = itodo;
                    414: #ifdef DEBUG
                    415:                if (debug_level >= 4) {
                    416:                        abuf_dbg(obuf);
                    417:                        dbg_puts(": stored ");
                    418:                        dbg_putu(ocount);
                    419:                        dbg_puts(" bytes\n");
                    420:                }
                    421: #endif
                    422:                memcpy(odata, idata, ocount);
                    423:                abuf_wcommit(obuf, ocount);
                    424:                itodo -= ocount;
                    425:                idata += ocount;
                    426:        }
                    427: }
                    428:
                    429: /*
1.7       ratchov   430:  * broadcast a message to all output buffers on the behalf of ibuf.
                    431:  * ie. don't sent back the message to the sender
                    432:  */
1.3       ratchov   433: void
                    434: ctl_sendmsg(struct aproc *p, struct abuf *ibuf, unsigned char *msg, unsigned len)
                    435: {
                    436:        struct abuf *i, *inext;
                    437:
1.20      ratchov   438:        for (i = LIST_FIRST(&p->outs); i != NULL; i = inext) {
1.3       ratchov   439:                inext = LIST_NEXT(i, oent);
1.19      ratchov   440:                if (i->duplex && i->duplex == ibuf)
1.3       ratchov   441:                        continue;
1.33      ratchov   442:                ctl_copymsg(i, msg, len);
1.3       ratchov   443:                (void)abuf_flush(i);
                    444:        }
                    445: }
                    446:
1.7       ratchov   447: /*
1.13      ratchov   448:  * send a quarter frame MTC message
                    449:  */
                    450: void
                    451: ctl_qfr(struct aproc *p)
                    452: {
                    453:        unsigned char buf[2];
                    454:        unsigned data;
                    455:
                    456:        switch (p->u.ctl.qfr) {
                    457:        case 0:
                    458:                data = p->u.ctl.fr & 0xf;
                    459:                break;
                    460:        case 1:
                    461:                data = p->u.ctl.fr >> 4;
                    462:                break;
                    463:        case 2:
                    464:                data = p->u.ctl.sec & 0xf;
                    465:                break;
                    466:        case 3:
                    467:                data = p->u.ctl.sec >> 4;
                    468:                break;
                    469:        case 4:
                    470:                data = p->u.ctl.min & 0xf;
                    471:                break;
                    472:        case 5:
                    473:                data = p->u.ctl.min >> 4;
                    474:                break;
                    475:        case 6:
                    476:                data = p->u.ctl.hr & 0xf;
                    477:                break;
                    478:        case 7:
                    479:                data = (p->u.ctl.hr >> 4) | (p->u.ctl.fps_id << 1);
                    480:                /*
                    481:                 * tick messages are sent 2 frames ahead
                    482:                 */
                    483:                p->u.ctl.fr += 2;
                    484:                if (p->u.ctl.fr < p->u.ctl.fps)
                    485:                        break;
                    486:                p->u.ctl.fr -= p->u.ctl.fps;
                    487:                p->u.ctl.sec++;
                    488:                if (p->u.ctl.sec < 60)
1.32      deraadt   489:                        break;
1.13      ratchov   490:                p->u.ctl.sec = 0;
                    491:                p->u.ctl.min++;
                    492:                if (p->u.ctl.min < 60)
                    493:                        break;
                    494:                p->u.ctl.min = 0;
                    495:                p->u.ctl.hr++;
                    496:                if (p->u.ctl.hr < 24)
                    497:                        break;
                    498:                p->u.ctl.hr = 0;
                    499:                break;
                    500:        default:
                    501:                /* NOTREACHED */
                    502:                data = 0;
                    503:        }
                    504:        buf[0] = 0xf1;
                    505:        buf[1] = (p->u.ctl.qfr << 4) | data;
                    506:        p->u.ctl.qfr++;
                    507:        p->u.ctl.qfr &= 7;
                    508:        ctl_sendmsg(p, NULL, buf, 2);
                    509: }
                    510:
                    511: /*
                    512:  * send a full frame MTC message
                    513:  */
                    514: void
                    515: ctl_full(struct aproc *p)
                    516: {
                    517:        unsigned char buf[10];
                    518:        unsigned origin = p->u.ctl.origin;
                    519:        unsigned fps = p->u.ctl.fps;
                    520:
                    521:        p->u.ctl.hr =  (origin / (3600 * MTC_SEC)) % 24;
                    522:        p->u.ctl.min = (origin / (60 * MTC_SEC))   % 60;
                    523:        p->u.ctl.sec = (origin / MTC_SEC)          % 60;
                    524:        p->u.ctl.fr =  (origin / (MTC_SEC / fps))  % fps;
                    525:
                    526:        buf[0] = 0xf0;
                    527:        buf[1] = 0x7f;
                    528:        buf[2] = 0x7f;
                    529:        buf[3] = 0x01;
                    530:        buf[4] = 0x01;
                    531:        buf[5] = p->u.ctl.hr | (p->u.ctl.fps_id << 5);
                    532:        buf[6] = p->u.ctl.min;
                    533:        buf[7] = p->u.ctl.sec;
                    534:        buf[8] = p->u.ctl.fr;
                    535:        buf[9] = 0xf7;
                    536:        p->u.ctl.qfr = 0;
                    537:        ctl_sendmsg(p, NULL, buf, 10);
                    538: }
                    539:
1.33      ratchov   540: void
                    541: ctl_msg_vol(struct aproc *p, int slot, char *msg)
                    542: {
                    543:        struct ctl_slot *s;
                    544:
                    545:        s = p->u.ctl.slot + slot;
                    546:        msg[0] = MIDI_CTL | slot;
                    547:        msg[1] = MIDI_CTLVOL;
                    548:        msg[2] = s->vol;
                    549: }
                    550:
1.13      ratchov   551: /*
1.12      ratchov   552:  * find the best matching free slot index (ie midi channel).
                    553:  * return -1, if there are no free slots anymore
1.7       ratchov   554:  */
1.3       ratchov   555: int
1.12      ratchov   556: ctl_getidx(struct aproc *p, char *who)
1.3       ratchov   557: {
                    558:        char *s;
                    559:        struct ctl_slot *slot;
1.4       ratchov   560:        char name[CTL_NAMEMAX];
                    561:        unsigned i, unit, umap = 0;
1.6       ratchov   562:        unsigned ser, bestser, bestidx;
1.3       ratchov   563:
1.4       ratchov   564:        /*
                    565:         * create a ``valid'' control name (lowcase, remove [^a-z], trucate)
                    566:         */
1.5       ratchov   567:        for (i = 0, s = who; ; s++) {
1.4       ratchov   568:                if (i == CTL_NAMEMAX - 1 || *s == '\0') {
                    569:                        name[i] = '\0';
                    570:                        break;
                    571:                } else if (*s >= 'A' && *s <= 'Z') {
                    572:                        name[i++] = *s + 'a' - 'A';
                    573:                } else if (*s >= 'a' && *s <= 'z')
                    574:                        name[i++] = *s;
                    575:        }
                    576:        if (i == 0)
                    577:                strlcpy(name, "noname", CTL_NAMEMAX);
                    578:
                    579:        /*
                    580:         * find the instance number of the control name
                    581:         */
                    582:        for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) {
1.12      ratchov   583:                if (slot->ops == NULL)
1.4       ratchov   584:                        continue;
                    585:                if (strcmp(slot->name, name) == 0)
                    586:                        umap |= (1 << i);
                    587:        }
1.12      ratchov   588:        for (unit = 0; ; unit++) {
1.29      ratchov   589:                if (unit == CTL_NSLOT) {
                    590: #ifdef DEBUG
                    591:                        if (debug_level >= 1) {
                    592:                                dbg_puts(name);
1.30      ratchov   593:                                dbg_puts(": too many instances\n");
1.29      ratchov   594:                        }
                    595: #endif
1.3       ratchov   596:                        return -1;
1.29      ratchov   597:                }
1.12      ratchov   598:                if ((umap & (1 << unit)) == 0)
1.3       ratchov   599:                        break;
                    600:        }
1.29      ratchov   601:
1.4       ratchov   602:        /*
                    603:         * find a free controller slot with the same name/unit
                    604:         */
                    605:        for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) {
1.12      ratchov   606:                if (slot->ops == NULL &&
1.4       ratchov   607:                    strcmp(slot->name, name) == 0 &&
                    608:                    slot->unit == unit) {
1.14      ratchov   609: #ifdef DEBUG
                    610:                        if (debug_level >= 3) {
1.29      ratchov   611:                                dbg_puts(name);
                    612:                                dbg_putu(unit);
1.14      ratchov   613:                                dbg_puts(": found slot ");
                    614:                                dbg_putu(i);
                    615:                                dbg_puts("\n");
                    616:                        }
                    617: #endif
1.4       ratchov   618:                        return i;
                    619:                }
                    620:        }
                    621:
                    622:        /*
1.6       ratchov   623:         * couldn't find a matching slot, pick oldest free slot
1.12      ratchov   624:         * and set its name/unit
1.4       ratchov   625:         */
1.6       ratchov   626:        bestser = 0;
                    627:        bestidx = CTL_NSLOT;
                    628:        for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) {
1.12      ratchov   629:                if (slot->ops != NULL)
1.6       ratchov   630:                        continue;
                    631:                ser = p->u.ctl.serial - slot->serial;
                    632:                if (ser > bestser) {
                    633:                        bestser = ser;
                    634:                        bestidx = i;
                    635:                }
1.3       ratchov   636:        }
1.29      ratchov   637:        if (bestidx == CTL_NSLOT) {
                    638: #ifdef DEBUG
                    639:                if (debug_level >= 1) {
                    640:                        dbg_puts(name);
                    641:                        dbg_putu(unit);
                    642:                        dbg_puts(": out of mixer slots\n");
                    643:                }
                    644: #endif
1.6       ratchov   645:                return -1;
1.29      ratchov   646:        }
1.6       ratchov   647:        slot = p->u.ctl.slot + bestidx;
1.22      ratchov   648:        if (slot->name[0] != '\0')
                    649:                slot->vol = MIDI_MAXCTL;
1.4       ratchov   650:        strlcpy(slot->name, name, CTL_NAMEMAX);
1.6       ratchov   651:        slot->serial = p->u.ctl.serial++;
1.4       ratchov   652:        slot->unit = unit;
1.14      ratchov   653: #ifdef DEBUG
                    654:        if (debug_level >= 3) {
1.29      ratchov   655:                dbg_puts(name);
                    656:                dbg_putu(unit);
1.14      ratchov   657:                dbg_puts(": overwritten slot ");
                    658:                dbg_putu(bestidx);
                    659:                dbg_puts("\n");
                    660:        }
                    661: #endif
1.6       ratchov   662:        return bestidx;
1.3       ratchov   663: }
                    664:
1.7       ratchov   665: /*
1.13      ratchov   666:  * check that all clients controlled by MMC are ready to start,
                    667:  * if so, start them all but the caller
                    668:  */
                    669: int
                    670: ctl_trystart(struct aproc *p, int caller)
                    671: {
                    672:        unsigned i;
                    673:        struct ctl_slot *s;
                    674:
                    675:        if (p->u.ctl.tstate != CTL_START) {
1.14      ratchov   676: #ifdef DEBUG
1.19      ratchov   677:                if (debug_level >= 3) {
                    678:                        ctl_slotdbg(p, caller);
                    679:                        dbg_puts(": server not started, delayd\n");
                    680:                }
1.14      ratchov   681: #endif
1.13      ratchov   682:                return 0;
                    683:        }
                    684:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    685:                if (!s->ops || i == caller)
                    686:                        continue;
                    687:                if (s->tstate != CTL_OFF && s->tstate != CTL_START) {
1.14      ratchov   688: #ifdef DEBUG
1.19      ratchov   689:                        if (debug_level >= 3) {
                    690:                                ctl_slotdbg(p, i);
                    691:                                dbg_puts(": not ready, server delayed\n");
                    692:                        }
1.14      ratchov   693: #endif
1.13      ratchov   694:                        return 0;
                    695:                }
                    696:        }
                    697:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    698:                if (!s->ops || i == caller)
                    699:                        continue;
                    700:                if (s->tstate == CTL_START) {
1.14      ratchov   701: #ifdef DEBUG
1.19      ratchov   702:                        if (debug_level >= 3) {
                    703:                                ctl_slotdbg(p, i);
                    704:                                dbg_puts(": started\n");
                    705:                        }
1.14      ratchov   706: #endif
1.13      ratchov   707:                        s->tstate = CTL_RUN;
                    708:                        s->ops->start(s->arg);
                    709:                }
                    710:        }
                    711:        if (caller >= 0)
                    712:                p->u.ctl.slot[caller].tstate = CTL_RUN;
                    713:        p->u.ctl.tstate = CTL_RUN;
1.25      ratchov   714:        p->u.ctl.delta = MTC_SEC * dev_getpos(p->u.ctl.dev);
                    715:        if (p->u.ctl.dev->rate % (30 * 4 * p->u.ctl.dev->round) == 0) {
1.13      ratchov   716:                p->u.ctl.fps_id = MTC_FPS_30;
                    717:                p->u.ctl.fps = 30;
1.25      ratchov   718:        } else if (p->u.ctl.dev->rate % (25 * 4 * p->u.ctl.dev->round) == 0) {
1.13      ratchov   719:                p->u.ctl.fps_id = MTC_FPS_25;
                    720:                p->u.ctl.fps = 25;
                    721:        } else {
                    722:                p->u.ctl.fps_id = MTC_FPS_24;
                    723:                p->u.ctl.fps = 24;
                    724:        }
1.14      ratchov   725: #ifdef DEBUG
1.19      ratchov   726:        if (debug_level >= 3) {
                    727:                ctl_slotdbg(p, caller);
                    728:                dbg_puts(": started server at ");
                    729:                dbg_puti(p->u.ctl.delta);
                    730:                dbg_puts(", ");
                    731:                dbg_puti(p->u.ctl.fps);
                    732:                dbg_puts(" mtc fps\n");
                    733:        }
1.14      ratchov   734: #endif
1.25      ratchov   735:        dev_wakeup(p->u.ctl.dev);
1.13      ratchov   736:        ctl_full(p);
                    737:        return 1;
                    738: }
                    739:
                    740: /*
1.12      ratchov   741:  * allocate a new slot and register the given call-backs
                    742:  */
                    743: int
1.13      ratchov   744: ctl_slotnew(struct aproc *p, char *who, struct ctl_ops *ops, void *arg, int tr)
1.12      ratchov   745: {
                    746:        int idx;
                    747:        struct ctl_slot *s;
1.33      ratchov   748:        unsigned char msg[sizeof(struct sysex)];
1.12      ratchov   749:
1.19      ratchov   750:        if (!APROC_OK(p)) {
                    751: #ifdef DEBUG
                    752:                if (debug_level >= 1) {
                    753:                        dbg_puts(who);
                    754:                        dbg_puts(": MIDI control not available\n");
                    755:                }
                    756: #endif
1.13      ratchov   757:                return -1;
1.19      ratchov   758:        }
1.12      ratchov   759:        idx = ctl_getidx(p, who);
                    760:        if (idx < 0)
                    761:                return -1;
                    762:
                    763:        s = p->u.ctl.slot + idx;
                    764:        s->ops = ops;
                    765:        s->arg = arg;
1.13      ratchov   766:        s->tstate = tr ? CTL_STOP : CTL_OFF;
1.12      ratchov   767:        s->ops->vol(s->arg, s->vol);
1.33      ratchov   768:        ctl_msg_vol(p, idx, msg);
                    769:        ctl_sendmsg(p, NULL, msg, 3);
1.12      ratchov   770:        return idx;
                    771: }
                    772:
                    773: /*
1.7       ratchov   774:  * release the given slot
                    775:  */
1.3       ratchov   776: void
                    777: ctl_slotdel(struct aproc *p, int index)
                    778: {
1.13      ratchov   779:        unsigned i;
                    780:        struct ctl_slot *s;
                    781:
1.19      ratchov   782:        if (!APROC_OK(p))
1.13      ratchov   783:                return;
1.12      ratchov   784:        p->u.ctl.slot[index].ops = NULL;
1.13      ratchov   785:        if (!(p->flags & APROC_QUIT))
                    786:                return;
                    787:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    788:                if (s->ops)
                    789:                        return;
                    790:        }
1.34    ! ratchov   791:        if (LIST_EMPTY(&p->ins))
1.13      ratchov   792:                aproc_del(p);
                    793: }
                    794:
                    795: /*
                    796:  * called at every clock tick by the mixer, delta is positive, unless
                    797:  * there's an overrun/underrun
                    798:  */
                    799: void
                    800: ctl_ontick(struct aproc *p, int delta)
                    801: {
                    802:        int qfrlen;
                    803:
                    804:        /*
                    805:         * don't send ticks before the start signal
                    806:         */
                    807:        if (p->u.ctl.tstate != CTL_RUN)
                    808:                return;
                    809:
                    810:        p->u.ctl.delta += delta * MTC_SEC;
                    811:
                    812:        /*
                    813:         * don't send ticks during the count-down
                    814:         */
                    815:        if (p->u.ctl.delta < 0)
                    816:                return;
                    817:
1.25      ratchov   818:        qfrlen = p->u.ctl.dev->rate * (MTC_SEC / (4 * p->u.ctl.fps));
1.13      ratchov   819:        while (p->u.ctl.delta >= qfrlen) {
                    820:                ctl_qfr(p);
                    821:                p->u.ctl.delta -= qfrlen;
                    822:        }
1.3       ratchov   823: }
                    824:
1.7       ratchov   825: /*
                    826:  * notifty the mixer that volume changed, called by whom allocad the slot using
                    827:  * ctl_slotnew(). Note: it doesn't make sens to call this from within the
                    828:  * call-back.
                    829:  */
1.3       ratchov   830: void
                    831: ctl_slotvol(struct aproc *p, int slot, unsigned vol)
                    832: {
                    833:        unsigned char msg[3];
                    834:
1.19      ratchov   835:        if (!APROC_OK(p))
1.13      ratchov   836:                return;
1.14      ratchov   837: #ifdef DEBUG
                    838:        if (debug_level >= 3) {
                    839:                ctl_slotdbg(p, slot);
                    840:                dbg_puts(": changing volume to ");
                    841:                dbg_putu(vol);
                    842:                dbg_puts("\n");
                    843:        }
                    844: #endif
1.7       ratchov   845:        p->u.ctl.slot[slot].vol = vol;
1.33      ratchov   846:        ctl_msg_vol(p, slot, msg);
1.3       ratchov   847:        ctl_sendmsg(p, NULL, msg, 3);
                    848: }
                    849:
1.7       ratchov   850: /*
1.13      ratchov   851:  * notify the MMC layer that the stream is attempting
                    852:  * to start. If other streams are not ready, 0 is returned meaning
                    853:  * that the stream should wait. If other streams are ready, they
                    854:  * are started, and the caller should start immediately.
                    855:  */
                    856: int
                    857: ctl_slotstart(struct aproc *p, int slot)
                    858: {
                    859:        struct ctl_slot *s = p->u.ctl.slot + slot;
                    860:
1.19      ratchov   861:        if (!APROC_OK(p))
1.13      ratchov   862:                return 1;
                    863:        if (s->tstate == CTL_OFF || p->u.ctl.tstate == CTL_OFF)
                    864:                return 1;
                    865:
                    866:        /*
                    867:         * if the server already started (the client missed the
                    868:         * start rendez-vous) or the server is stopped, then
                    869:         * tag the client as ``wanting to start''
                    870:         */
                    871:        s->tstate = CTL_START;
                    872:        return ctl_trystart(p, slot);
                    873: }
                    874:
                    875: /*
                    876:  * notify the MMC layer that the stream no longer is trying to
                    877:  * start (or that it just stopped), meaning that its ``start'' call-back
                    878:  * shouldn't be called anymore
                    879:  */
                    880: void
                    881: ctl_slotstop(struct aproc *p, int slot)
                    882: {
                    883:        struct ctl_slot *s = p->u.ctl.slot + slot;
                    884:
1.19      ratchov   885:        if (!APROC_OK(p))
1.13      ratchov   886:                return;
                    887:        /*
                    888:         * tag the stream as not trying to start,
                    889:         * unless MMC is turned off
                    890:         */
                    891:        if (s->tstate != CTL_OFF)
                    892:                s->tstate = CTL_STOP;
                    893: }
                    894:
                    895: /*
1.19      ratchov   896:  * start all slots simultaneously
                    897:  */
                    898: void
                    899: ctl_start(struct aproc *p)
                    900: {
                    901:        if (!APROC_OK(p))
                    902:                return;
                    903:        if (p->u.ctl.tstate == CTL_STOP) {
                    904:                p->u.ctl.tstate = CTL_START;
                    905:                (void)ctl_trystart(p, -1);
                    906: #ifdef DEBUG
                    907:        } else {
                    908:                if (debug_level >= 3) {
                    909:                        aproc_dbg(p);
                    910:                        dbg_puts(": ignoring mmc start\n");
                    911:                }
                    912: #endif
                    913:        }
                    914: }
                    915:
                    916: /*
                    917:  * stop all slots simultaneously
                    918:  */
                    919: void
                    920: ctl_stop(struct aproc *p)
                    921: {
                    922:        unsigned i;
                    923:        struct ctl_slot *s;
                    924:
                    925:        if (!APROC_OK(p))
                    926:                return;
                    927:        switch (p->u.ctl.tstate) {
                    928:        case CTL_START:
                    929:                p->u.ctl.tstate = CTL_STOP;
                    930:                return;
                    931:        case CTL_RUN:
                    932:                p->u.ctl.tstate = CTL_STOP;
                    933:                break;
                    934:        default:
                    935: #ifdef DEBUG
                    936:                if (debug_level >= 3) {
                    937:                        aproc_dbg(p);
                    938:                        dbg_puts(": ignored mmc stop\n");
                    939:                }
                    940: #endif
                    941:                return;
                    942:        }
                    943:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    944:                if (!s->ops)
                    945:                        continue;
                    946:                if (s->tstate == CTL_RUN) {
                    947: #ifdef DEBUG
                    948:                        if (debug_level >= 3) {
                    949:                                ctl_slotdbg(p, i);
                    950:                                dbg_puts(": requested to stop\n");
                    951:                        }
                    952: #endif
                    953:                        s->ops->stop(s->arg);
                    954:                }
                    955:        }
                    956: }
                    957:
                    958: /*
                    959:  * relocate all slots simultaneously
                    960:  */
                    961: void
                    962: ctl_loc(struct aproc *p, unsigned origin)
                    963: {
                    964:        unsigned i, tstate;
                    965:        struct ctl_slot *s;
                    966:
                    967:        if (!APROC_OK(p))
                    968:                return;
                    969: #ifdef DEBUG
                    970:        if (debug_level >= 2) {
                    971:                dbg_puts("server relocated to ");
                    972:                dbg_putu(origin);
                    973:                dbg_puts("\n");
                    974:        }
                    975: #endif
                    976:        tstate = p->u.ctl.tstate;
                    977:        if (tstate == CTL_RUN)
                    978:                ctl_stop(p);
                    979:        p->u.ctl.origin = origin;
                    980:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    981:                if (!s->ops)
                    982:                        continue;
                    983:                s->ops->loc(s->arg, p->u.ctl.origin);
                    984:        }
                    985:        if (tstate == CTL_RUN)
                    986:                ctl_start(p);
                    987: }
                    988:
                    989: /*
                    990:  * check if there are controlled streams
                    991:  */
                    992: int
                    993: ctl_idle(struct aproc *p)
                    994: {
                    995:        unsigned i;
                    996:        struct ctl_slot *s;
                    997:
                    998:        if (!APROC_OK(p))
                    999:                return 1;
                   1000:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                   1001:                if (s->ops)
                   1002:                        return 0;
                   1003:        }
                   1004:        return 1;
                   1005: }
                   1006:
                   1007: /*
1.7       ratchov  1008:  * handle a MIDI event received from ibuf
                   1009:  */
1.3       ratchov  1010: void
                   1011: ctl_ev(struct aproc *p, struct abuf *ibuf)
                   1012: {
                   1013:        unsigned chan;
1.5       ratchov  1014:        struct ctl_slot *slot;
1.33      ratchov  1015:        struct sysex *x;
                   1016:        unsigned fps, len;
1.14      ratchov  1017: #ifdef DEBUG
                   1018:        unsigned i;
                   1019:
                   1020:        if (debug_level >= 3) {
                   1021:                abuf_dbg(ibuf);
                   1022:                dbg_puts(": got event:");
                   1023:                for (i = 0; i < ibuf->r.midi.idx; i++) {
                   1024:                        dbg_puts(" ");
                   1025:                        dbg_putx(ibuf->r.midi.msg[i]);
                   1026:                }
                   1027:                dbg_puts("\n");
                   1028:        }
                   1029: #endif
1.10      ratchov  1030:        if ((ibuf->r.midi.msg[0] & MIDI_CMDMASK) == MIDI_CTL &&
1.33      ratchov  1031:            (ibuf->r.midi.msg[1] == MIDI_CTLVOL)) {
1.10      ratchov  1032:                chan = ibuf->r.midi.msg[0] & MIDI_CHANMASK;
1.3       ratchov  1033:                if (chan >= CTL_NSLOT)
                   1034:                        return;
1.5       ratchov  1035:                slot = p->u.ctl.slot + chan;
1.22      ratchov  1036:                slot->vol = ibuf->r.midi.msg[2];
1.12      ratchov  1037:                if (slot->ops == NULL)
1.3       ratchov  1038:                        return;
1.12      ratchov  1039:                slot->ops->vol(slot->arg, slot->vol);
1.10      ratchov  1040:                ctl_sendmsg(p, ibuf, ibuf->r.midi.msg, ibuf->r.midi.len);
1.3       ratchov  1041:        }
1.33      ratchov  1042:        x = (struct sysex *)ibuf->r.midi.msg;
                   1043:        len = ibuf->r.midi.idx;
                   1044:        if (x->start != SYSEX_START)
                   1045:                return;
                   1046:        if (len < SYSEX_SIZE(empty))
                   1047:                return;
                   1048:        switch (x->type) {
                   1049:        case SYSEX_TYPE_RT:
                   1050:                if (x->id0 != SYSEX_MMC)
                   1051:                        return;
                   1052:                switch (x->id1) {
                   1053:                case SYSEX_MMC_STOP:
                   1054:                        if (len != SYSEX_SIZE(stop))
                   1055:                                return;
1.14      ratchov  1056: #ifdef DEBUG
1.19      ratchov  1057:                        if (debug_level >= 3) {
1.14      ratchov  1058:                                abuf_dbg(ibuf);
                   1059:                                dbg_puts(": mmc stop\n");
                   1060:                        }
                   1061: #endif
1.19      ratchov  1062:                        ctl_stop(p);
1.13      ratchov  1063:                        break;
1.33      ratchov  1064:                case SYSEX_MMC_START:
                   1065:                        if (len != SYSEX_SIZE(start))
                   1066:                                return;
1.14      ratchov  1067: #ifdef DEBUG
1.19      ratchov  1068:                        if (debug_level >= 3) {
1.14      ratchov  1069:                                abuf_dbg(ibuf);
                   1070:                                dbg_puts(": mmc start\n");
                   1071:                        }
                   1072: #endif
1.19      ratchov  1073:                        ctl_start(p);
1.13      ratchov  1074:                        break;
1.33      ratchov  1075:                case SYSEX_MMC_LOC:
                   1076:                        if (len != SYSEX_SIZE(loc) ||
                   1077:                            x->u.loc.len != SYSEX_MMC_LOC_LEN ||
                   1078:                            x->u.loc.cmd != SYSEX_MMC_LOC_CMD)
                   1079:                                return;
                   1080:                        switch (x->u.loc.hr >> 5) {
                   1081:                        case MTC_FPS_24:
                   1082:                                fps = 24;
                   1083:                                break;
                   1084:                        case MTC_FPS_25:
                   1085:                                fps = 25;
                   1086:                                break;
                   1087:                        case MTC_FPS_30:
                   1088:                                fps = 30;
                   1089:                                break;
                   1090:                        default:
                   1091:                                p->u.ctl.origin = 0;
                   1092:                                return;
                   1093:                        }
                   1094:                        ctl_loc(p,
                   1095:                            (x->u.loc.hr & 0x1f) * 3600 * MTC_SEC +
                   1096:                             x->u.loc.min * 60 * MTC_SEC +
                   1097:                             x->u.loc.sec * MTC_SEC +
                   1098:                             x->u.loc.fr * (MTC_SEC / fps) +
                   1099:                             x->u.loc.cent * (MTC_SEC / 100 / fps));
1.13      ratchov  1100:                        break;
                   1101:                }
1.33      ratchov  1102:                break;
1.13      ratchov  1103:        }
1.3       ratchov  1104: }
                   1105:
                   1106: int
                   1107: ctl_in(struct aproc *p, struct abuf *ibuf)
                   1108: {
                   1109:        unsigned char *idata;
                   1110:        unsigned c, i, icount;
                   1111:
                   1112:        if (!ABUF_ROK(ibuf))
                   1113:                return 0;
                   1114:        idata = abuf_rgetblk(ibuf, &icount, 0);
                   1115:        for (i = 0; i < icount; i++) {
                   1116:                c = *idata++;
1.8       ratchov  1117:                if (c >= 0xf8) {
                   1118:                        /* clock events not used yet */
                   1119:                } else if (c >= 0xf0) {
1.10      ratchov  1120:                        if (ibuf->r.midi.st == 0xf0 && c == 0xf7 &&
                   1121:                            ibuf->r.midi.idx < MIDI_MSGMAX) {
                   1122:                                ibuf->r.midi.msg[ibuf->r.midi.idx++] = c;
1.8       ratchov  1123:                                ctl_ev(p, ibuf);
                   1124:                                continue;
                   1125:                        }
1.10      ratchov  1126:                        ibuf->r.midi.msg[0] = c;
                   1127:                        ibuf->r.midi.len = common_len[c & 7];
                   1128:                        ibuf->r.midi.st = c;
                   1129:                        ibuf->r.midi.idx = 1;
1.3       ratchov  1130:                } else if (c >= 0x80) {
1.10      ratchov  1131:                        ibuf->r.midi.msg[0] = c;
                   1132:                        ibuf->r.midi.len = voice_len[(c >> 4) & 7];
                   1133:                        ibuf->r.midi.st = c;
                   1134:                        ibuf->r.midi.idx = 1;
                   1135:                } else if (ibuf->r.midi.st) {
                   1136:                        if (ibuf->r.midi.idx == MIDI_MSGMAX)
1.8       ratchov  1137:                                continue;
1.10      ratchov  1138:                        if (ibuf->r.midi.idx == 0)
                   1139:                                ibuf->r.midi.msg[ibuf->r.midi.idx++] = ibuf->r.midi.st;
                   1140:                        ibuf->r.midi.msg[ibuf->r.midi.idx++] = c;
                   1141:                        if (ibuf->r.midi.idx == ibuf->r.midi.len) {
1.3       ratchov  1142:                                ctl_ev(p, ibuf);
1.10      ratchov  1143:                                ibuf->r.midi.idx = 0;
1.3       ratchov  1144:                        }
                   1145:                }
                   1146:        }
                   1147:        abuf_rdiscard(ibuf, icount);
                   1148:        return 1;
                   1149: }
                   1150:
                   1151: int
                   1152: ctl_out(struct aproc *p, struct abuf *obuf)
                   1153: {
                   1154:        return 0;
                   1155: }
                   1156:
                   1157: void
                   1158: ctl_eof(struct aproc *p, struct abuf *ibuf)
                   1159: {
1.13      ratchov  1160:        unsigned i;
                   1161:        struct ctl_slot *s;
                   1162:
                   1163:        if (!(p->flags & APROC_QUIT))
                   1164:                return;
                   1165:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
1.27      ratchov  1166:                if (s->ops != NULL)
                   1167:                        s->ops->quit(s->arg);
1.13      ratchov  1168:        }
1.34    ! ratchov  1169:        if (LIST_EMPTY(&p->ins))
1.13      ratchov  1170:                aproc_del(p);
1.3       ratchov  1171: }
                   1172:
                   1173: void
                   1174: ctl_hup(struct aproc *p, struct abuf *obuf)
                   1175: {
1.13      ratchov  1176:        unsigned i;
                   1177:        struct ctl_slot *s;
                   1178:
                   1179:        if (!(p->flags & APROC_QUIT))
                   1180:                return;
                   1181:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                   1182:                if (s->ops)
                   1183:                        return;
                   1184:        }
1.34    ! ratchov  1185:        if (LIST_EMPTY(&p->ins))
1.13      ratchov  1186:                aproc_del(p);
1.3       ratchov  1187: }
                   1188:
                   1189: void
                   1190: ctl_newin(struct aproc *p, struct abuf *ibuf)
                   1191: {
1.10      ratchov  1192:        ibuf->r.midi.used = 0;
                   1193:        ibuf->r.midi.len = 0;
                   1194:        ibuf->r.midi.idx = 0;
                   1195:        ibuf->r.midi.st = 0;
1.3       ratchov  1196: }
                   1197:
                   1198: void
                   1199: ctl_done(struct aproc *p)
                   1200: {
1.14      ratchov  1201:        unsigned i;
                   1202:        struct ctl_slot *s;
                   1203:
                   1204:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
1.25      ratchov  1205:                if (s->ops != NULL)
                   1206:                        s->ops->quit(s->arg);
1.14      ratchov  1207:        }
1.3       ratchov  1208: }
                   1209:
                   1210: struct aproc_ops ctl_ops = {
                   1211:        "ctl",
                   1212:        ctl_in,
                   1213:        ctl_out,
                   1214:        ctl_eof,
                   1215:        ctl_hup,
                   1216:        ctl_newin,
                   1217:        NULL, /* newout */
                   1218:        NULL, /* ipos */
                   1219:        NULL, /* opos */
                   1220:        ctl_done
                   1221: };
                   1222:
                   1223: struct aproc *
1.25      ratchov  1224: ctl_new(char *name, struct dev *dev)
1.3       ratchov  1225: {
                   1226:        struct aproc *p;
1.5       ratchov  1227:        struct ctl_slot *s;
1.3       ratchov  1228:        unsigned i;
                   1229:
                   1230:        p = aproc_new(&ctl_ops, name);
1.25      ratchov  1231:        p->u.ctl.dev = dev;
1.6       ratchov  1232:        p->u.ctl.serial = 0;
1.13      ratchov  1233:        p->u.ctl.tstate = CTL_STOP;
1.5       ratchov  1234:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
1.3       ratchov  1235:                p->u.ctl.slot[i].unit = i;
1.12      ratchov  1236:                p->u.ctl.slot[i].ops = NULL;
1.7       ratchov  1237:                p->u.ctl.slot[i].vol = MIDI_MAXCTL;
1.13      ratchov  1238:                p->u.ctl.slot[i].tstate = CTL_OFF;
1.6       ratchov  1239:                p->u.ctl.slot[i].serial = p->u.ctl.serial++;
1.22      ratchov  1240:                p->u.ctl.slot[i].name[0] = '\0';
1.3       ratchov  1241:        }
1.1       ratchov  1242:        return p;
                   1243: }