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

1.36    ! ratchov     1: /*     $OpenBSD: midi.c,v 1.35 2011/06/27 07:17:44 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
1.35      ratchov   541: ctl_msg_info(struct aproc *p, int slot, char *msg)
                    542: {
                    543:        struct ctl_slot *s;
                    544:        struct sysex *x = (struct sysex *)msg;
                    545:
                    546:        s = p->u.ctl.slot + slot;
                    547:        memset(x, 0, sizeof(struct sysex));
                    548:        x->start = SYSEX_START;
                    549:        x->type = SYSEX_TYPE_EDU;
                    550:        x->id0 = SYSEX_AUCAT;
                    551:        x->id1 = SYSEX_AUCAT_MIXINFO;
                    552:        if (*s->name != '\0') {
                    553:                snprintf(x->u.mixinfo.name,
                    554:                    SYSEX_NAMELEN, "%s%u", s->name, s->unit);
                    555:        }
                    556:        x->u.mixinfo.chan = slot;
                    557:        x->u.mixinfo.end = SYSEX_END;
                    558: }
                    559:
                    560: void
1.33      ratchov   561: ctl_msg_vol(struct aproc *p, int slot, char *msg)
                    562: {
                    563:        struct ctl_slot *s;
                    564:
                    565:        s = p->u.ctl.slot + slot;
                    566:        msg[0] = MIDI_CTL | slot;
                    567:        msg[1] = MIDI_CTLVOL;
                    568:        msg[2] = s->vol;
                    569: }
                    570:
1.35      ratchov   571: void
                    572: ctl_dump(struct aproc *p, struct abuf *obuf)
                    573: {
                    574:        unsigned i;
                    575:        unsigned char msg[sizeof(struct sysex)];
                    576:        struct ctl_slot *s;
                    577:
                    578:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    579:                ctl_msg_info(p, i, msg);
                    580:                ctl_copymsg(obuf, msg, SYSEX_SIZE(mixinfo));
                    581:                ctl_msg_vol(p, i, msg);
                    582:                ctl_copymsg(obuf, msg, 3);
                    583:        }
                    584:        msg[0] = SYSEX_START;
                    585:        msg[1] = SYSEX_TYPE_EDU;
                    586:        msg[2] = 0;
                    587:        msg[3] = SYSEX_AUCAT;
                    588:        msg[4] = SYSEX_AUCAT_DUMPEND;
                    589:        msg[5] = SYSEX_END;
                    590:        ctl_copymsg(obuf, msg, 6);
                    591:        abuf_flush(obuf);
                    592: }
                    593:
1.13      ratchov   594: /*
1.12      ratchov   595:  * find the best matching free slot index (ie midi channel).
                    596:  * return -1, if there are no free slots anymore
1.7       ratchov   597:  */
1.3       ratchov   598: int
1.12      ratchov   599: ctl_getidx(struct aproc *p, char *who)
1.3       ratchov   600: {
                    601:        char *s;
                    602:        struct ctl_slot *slot;
1.4       ratchov   603:        char name[CTL_NAMEMAX];
                    604:        unsigned i, unit, umap = 0;
1.6       ratchov   605:        unsigned ser, bestser, bestidx;
1.3       ratchov   606:
1.4       ratchov   607:        /*
                    608:         * create a ``valid'' control name (lowcase, remove [^a-z], trucate)
                    609:         */
1.5       ratchov   610:        for (i = 0, s = who; ; s++) {
1.4       ratchov   611:                if (i == CTL_NAMEMAX - 1 || *s == '\0') {
                    612:                        name[i] = '\0';
                    613:                        break;
                    614:                } else if (*s >= 'A' && *s <= 'Z') {
                    615:                        name[i++] = *s + 'a' - 'A';
                    616:                } else if (*s >= 'a' && *s <= 'z')
                    617:                        name[i++] = *s;
                    618:        }
                    619:        if (i == 0)
                    620:                strlcpy(name, "noname", CTL_NAMEMAX);
                    621:
                    622:        /*
                    623:         * find the instance number of the control name
                    624:         */
                    625:        for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) {
1.12      ratchov   626:                if (slot->ops == NULL)
1.4       ratchov   627:                        continue;
                    628:                if (strcmp(slot->name, name) == 0)
                    629:                        umap |= (1 << i);
                    630:        }
1.12      ratchov   631:        for (unit = 0; ; unit++) {
1.29      ratchov   632:                if (unit == CTL_NSLOT) {
                    633: #ifdef DEBUG
                    634:                        if (debug_level >= 1) {
                    635:                                dbg_puts(name);
1.30      ratchov   636:                                dbg_puts(": too many instances\n");
1.29      ratchov   637:                        }
                    638: #endif
1.3       ratchov   639:                        return -1;
1.29      ratchov   640:                }
1.12      ratchov   641:                if ((umap & (1 << unit)) == 0)
1.3       ratchov   642:                        break;
                    643:        }
1.29      ratchov   644:
1.4       ratchov   645:        /*
                    646:         * find a free controller slot with the same name/unit
                    647:         */
                    648:        for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) {
1.12      ratchov   649:                if (slot->ops == NULL &&
1.4       ratchov   650:                    strcmp(slot->name, name) == 0 &&
                    651:                    slot->unit == unit) {
1.14      ratchov   652: #ifdef DEBUG
                    653:                        if (debug_level >= 3) {
1.29      ratchov   654:                                dbg_puts(name);
                    655:                                dbg_putu(unit);
1.14      ratchov   656:                                dbg_puts(": found slot ");
                    657:                                dbg_putu(i);
                    658:                                dbg_puts("\n");
                    659:                        }
                    660: #endif
1.4       ratchov   661:                        return i;
                    662:                }
                    663:        }
                    664:
                    665:        /*
1.6       ratchov   666:         * couldn't find a matching slot, pick oldest free slot
1.12      ratchov   667:         * and set its name/unit
1.4       ratchov   668:         */
1.6       ratchov   669:        bestser = 0;
                    670:        bestidx = CTL_NSLOT;
                    671:        for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) {
1.12      ratchov   672:                if (slot->ops != NULL)
1.6       ratchov   673:                        continue;
                    674:                ser = p->u.ctl.serial - slot->serial;
                    675:                if (ser > bestser) {
                    676:                        bestser = ser;
                    677:                        bestidx = i;
                    678:                }
1.3       ratchov   679:        }
1.29      ratchov   680:        if (bestidx == CTL_NSLOT) {
                    681: #ifdef DEBUG
                    682:                if (debug_level >= 1) {
                    683:                        dbg_puts(name);
                    684:                        dbg_putu(unit);
                    685:                        dbg_puts(": out of mixer slots\n");
                    686:                }
                    687: #endif
1.6       ratchov   688:                return -1;
1.29      ratchov   689:        }
1.6       ratchov   690:        slot = p->u.ctl.slot + bestidx;
1.22      ratchov   691:        if (slot->name[0] != '\0')
                    692:                slot->vol = MIDI_MAXCTL;
1.4       ratchov   693:        strlcpy(slot->name, name, CTL_NAMEMAX);
1.6       ratchov   694:        slot->serial = p->u.ctl.serial++;
1.4       ratchov   695:        slot->unit = unit;
1.14      ratchov   696: #ifdef DEBUG
                    697:        if (debug_level >= 3) {
1.29      ratchov   698:                dbg_puts(name);
                    699:                dbg_putu(unit);
1.14      ratchov   700:                dbg_puts(": overwritten slot ");
                    701:                dbg_putu(bestidx);
                    702:                dbg_puts("\n");
                    703:        }
                    704: #endif
1.6       ratchov   705:        return bestidx;
1.3       ratchov   706: }
                    707:
1.7       ratchov   708: /*
1.13      ratchov   709:  * check that all clients controlled by MMC are ready to start,
                    710:  * if so, start them all but the caller
                    711:  */
                    712: int
                    713: ctl_trystart(struct aproc *p, int caller)
                    714: {
                    715:        unsigned i;
                    716:        struct ctl_slot *s;
                    717:
                    718:        if (p->u.ctl.tstate != CTL_START) {
1.14      ratchov   719: #ifdef DEBUG
1.19      ratchov   720:                if (debug_level >= 3) {
                    721:                        ctl_slotdbg(p, caller);
                    722:                        dbg_puts(": server not started, delayd\n");
                    723:                }
1.14      ratchov   724: #endif
1.13      ratchov   725:                return 0;
                    726:        }
                    727:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    728:                if (!s->ops || i == caller)
                    729:                        continue;
                    730:                if (s->tstate != CTL_OFF && s->tstate != CTL_START) {
1.14      ratchov   731: #ifdef DEBUG
1.19      ratchov   732:                        if (debug_level >= 3) {
                    733:                                ctl_slotdbg(p, i);
                    734:                                dbg_puts(": not ready, server delayed\n");
                    735:                        }
1.14      ratchov   736: #endif
1.13      ratchov   737:                        return 0;
                    738:                }
                    739:        }
                    740:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    741:                if (!s->ops || i == caller)
                    742:                        continue;
                    743:                if (s->tstate == CTL_START) {
1.14      ratchov   744: #ifdef DEBUG
1.19      ratchov   745:                        if (debug_level >= 3) {
                    746:                                ctl_slotdbg(p, i);
                    747:                                dbg_puts(": started\n");
                    748:                        }
1.14      ratchov   749: #endif
1.13      ratchov   750:                        s->tstate = CTL_RUN;
                    751:                        s->ops->start(s->arg);
                    752:                }
                    753:        }
                    754:        if (caller >= 0)
                    755:                p->u.ctl.slot[caller].tstate = CTL_RUN;
                    756:        p->u.ctl.tstate = CTL_RUN;
1.25      ratchov   757:        p->u.ctl.delta = MTC_SEC * dev_getpos(p->u.ctl.dev);
                    758:        if (p->u.ctl.dev->rate % (30 * 4 * p->u.ctl.dev->round) == 0) {
1.13      ratchov   759:                p->u.ctl.fps_id = MTC_FPS_30;
                    760:                p->u.ctl.fps = 30;
1.25      ratchov   761:        } else if (p->u.ctl.dev->rate % (25 * 4 * p->u.ctl.dev->round) == 0) {
1.13      ratchov   762:                p->u.ctl.fps_id = MTC_FPS_25;
                    763:                p->u.ctl.fps = 25;
                    764:        } else {
                    765:                p->u.ctl.fps_id = MTC_FPS_24;
                    766:                p->u.ctl.fps = 24;
                    767:        }
1.14      ratchov   768: #ifdef DEBUG
1.19      ratchov   769:        if (debug_level >= 3) {
                    770:                ctl_slotdbg(p, caller);
                    771:                dbg_puts(": started server at ");
                    772:                dbg_puti(p->u.ctl.delta);
                    773:                dbg_puts(", ");
                    774:                dbg_puti(p->u.ctl.fps);
                    775:                dbg_puts(" mtc fps\n");
                    776:        }
1.14      ratchov   777: #endif
1.25      ratchov   778:        dev_wakeup(p->u.ctl.dev);
1.13      ratchov   779:        ctl_full(p);
                    780:        return 1;
                    781: }
                    782:
                    783: /*
1.12      ratchov   784:  * allocate a new slot and register the given call-backs
                    785:  */
                    786: int
1.13      ratchov   787: ctl_slotnew(struct aproc *p, char *who, struct ctl_ops *ops, void *arg, int tr)
1.12      ratchov   788: {
                    789:        int idx;
                    790:        struct ctl_slot *s;
1.33      ratchov   791:        unsigned char msg[sizeof(struct sysex)];
1.12      ratchov   792:
1.19      ratchov   793:        if (!APROC_OK(p)) {
                    794: #ifdef DEBUG
                    795:                if (debug_level >= 1) {
                    796:                        dbg_puts(who);
                    797:                        dbg_puts(": MIDI control not available\n");
                    798:                }
                    799: #endif
1.13      ratchov   800:                return -1;
1.19      ratchov   801:        }
1.12      ratchov   802:        idx = ctl_getidx(p, who);
                    803:        if (idx < 0)
                    804:                return -1;
                    805:
                    806:        s = p->u.ctl.slot + idx;
                    807:        s->ops = ops;
                    808:        s->arg = arg;
1.13      ratchov   809:        s->tstate = tr ? CTL_STOP : CTL_OFF;
1.12      ratchov   810:        s->ops->vol(s->arg, s->vol);
1.35      ratchov   811:        ctl_msg_info(p, idx, msg);
                    812:        ctl_sendmsg(p, NULL, msg, SYSEX_SIZE(mixinfo));
1.33      ratchov   813:        ctl_msg_vol(p, idx, msg);
                    814:        ctl_sendmsg(p, NULL, msg, 3);
1.12      ratchov   815:        return idx;
                    816: }
                    817:
                    818: /*
1.7       ratchov   819:  * release the given slot
                    820:  */
1.3       ratchov   821: void
                    822: ctl_slotdel(struct aproc *p, int index)
                    823: {
1.13      ratchov   824:        unsigned i;
                    825:        struct ctl_slot *s;
                    826:
1.19      ratchov   827:        if (!APROC_OK(p))
1.13      ratchov   828:                return;
1.12      ratchov   829:        p->u.ctl.slot[index].ops = NULL;
1.13      ratchov   830:        if (!(p->flags & APROC_QUIT))
                    831:                return;
                    832:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    833:                if (s->ops)
                    834:                        return;
                    835:        }
1.34      ratchov   836:        if (LIST_EMPTY(&p->ins))
1.13      ratchov   837:                aproc_del(p);
                    838: }
                    839:
                    840: /*
                    841:  * called at every clock tick by the mixer, delta is positive, unless
                    842:  * there's an overrun/underrun
                    843:  */
                    844: void
                    845: ctl_ontick(struct aproc *p, int delta)
                    846: {
                    847:        int qfrlen;
                    848:
                    849:        /*
                    850:         * don't send ticks before the start signal
                    851:         */
                    852:        if (p->u.ctl.tstate != CTL_RUN)
                    853:                return;
                    854:
                    855:        p->u.ctl.delta += delta * MTC_SEC;
                    856:
                    857:        /*
                    858:         * don't send ticks during the count-down
                    859:         */
                    860:        if (p->u.ctl.delta < 0)
                    861:                return;
                    862:
1.25      ratchov   863:        qfrlen = p->u.ctl.dev->rate * (MTC_SEC / (4 * p->u.ctl.fps));
1.13      ratchov   864:        while (p->u.ctl.delta >= qfrlen) {
                    865:                ctl_qfr(p);
                    866:                p->u.ctl.delta -= qfrlen;
                    867:        }
1.3       ratchov   868: }
                    869:
1.7       ratchov   870: /*
                    871:  * notifty the mixer that volume changed, called by whom allocad the slot using
                    872:  * ctl_slotnew(). Note: it doesn't make sens to call this from within the
                    873:  * call-back.
                    874:  */
1.3       ratchov   875: void
                    876: ctl_slotvol(struct aproc *p, int slot, unsigned vol)
                    877: {
                    878:        unsigned char msg[3];
                    879:
1.19      ratchov   880:        if (!APROC_OK(p))
1.13      ratchov   881:                return;
1.14      ratchov   882: #ifdef DEBUG
                    883:        if (debug_level >= 3) {
                    884:                ctl_slotdbg(p, slot);
                    885:                dbg_puts(": changing volume to ");
                    886:                dbg_putu(vol);
                    887:                dbg_puts("\n");
                    888:        }
                    889: #endif
1.7       ratchov   890:        p->u.ctl.slot[slot].vol = vol;
1.33      ratchov   891:        ctl_msg_vol(p, slot, msg);
1.3       ratchov   892:        ctl_sendmsg(p, NULL, msg, 3);
                    893: }
                    894:
1.7       ratchov   895: /*
1.13      ratchov   896:  * notify the MMC layer that the stream is attempting
                    897:  * to start. If other streams are not ready, 0 is returned meaning
                    898:  * that the stream should wait. If other streams are ready, they
                    899:  * are started, and the caller should start immediately.
                    900:  */
                    901: int
                    902: ctl_slotstart(struct aproc *p, int slot)
                    903: {
                    904:        struct ctl_slot *s = p->u.ctl.slot + slot;
                    905:
1.19      ratchov   906:        if (!APROC_OK(p))
1.13      ratchov   907:                return 1;
                    908:        if (s->tstate == CTL_OFF || p->u.ctl.tstate == CTL_OFF)
                    909:                return 1;
                    910:
                    911:        /*
                    912:         * if the server already started (the client missed the
                    913:         * start rendez-vous) or the server is stopped, then
                    914:         * tag the client as ``wanting to start''
                    915:         */
                    916:        s->tstate = CTL_START;
                    917:        return ctl_trystart(p, slot);
                    918: }
                    919:
                    920: /*
                    921:  * notify the MMC layer that the stream no longer is trying to
                    922:  * start (or that it just stopped), meaning that its ``start'' call-back
                    923:  * shouldn't be called anymore
                    924:  */
                    925: void
                    926: ctl_slotstop(struct aproc *p, int slot)
                    927: {
                    928:        struct ctl_slot *s = p->u.ctl.slot + slot;
                    929:
1.19      ratchov   930:        if (!APROC_OK(p))
1.13      ratchov   931:                return;
                    932:        /*
                    933:         * tag the stream as not trying to start,
                    934:         * unless MMC is turned off
                    935:         */
                    936:        if (s->tstate != CTL_OFF)
                    937:                s->tstate = CTL_STOP;
                    938: }
                    939:
                    940: /*
1.19      ratchov   941:  * start all slots simultaneously
                    942:  */
                    943: void
                    944: ctl_start(struct aproc *p)
                    945: {
                    946:        if (!APROC_OK(p))
                    947:                return;
                    948:        if (p->u.ctl.tstate == CTL_STOP) {
                    949:                p->u.ctl.tstate = CTL_START;
                    950:                (void)ctl_trystart(p, -1);
                    951: #ifdef DEBUG
                    952:        } else {
                    953:                if (debug_level >= 3) {
                    954:                        aproc_dbg(p);
                    955:                        dbg_puts(": ignoring mmc start\n");
                    956:                }
                    957: #endif
                    958:        }
                    959: }
                    960:
                    961: /*
                    962:  * stop all slots simultaneously
                    963:  */
                    964: void
                    965: ctl_stop(struct aproc *p)
                    966: {
                    967:        unsigned i;
                    968:        struct ctl_slot *s;
                    969:
                    970:        if (!APROC_OK(p))
                    971:                return;
                    972:        switch (p->u.ctl.tstate) {
                    973:        case CTL_START:
                    974:                p->u.ctl.tstate = CTL_STOP;
                    975:                return;
                    976:        case CTL_RUN:
                    977:                p->u.ctl.tstate = CTL_STOP;
                    978:                break;
                    979:        default:
                    980: #ifdef DEBUG
                    981:                if (debug_level >= 3) {
                    982:                        aproc_dbg(p);
                    983:                        dbg_puts(": ignored mmc stop\n");
                    984:                }
                    985: #endif
                    986:                return;
                    987:        }
                    988:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    989:                if (!s->ops)
                    990:                        continue;
                    991:                if (s->tstate == CTL_RUN) {
                    992: #ifdef DEBUG
                    993:                        if (debug_level >= 3) {
                    994:                                ctl_slotdbg(p, i);
                    995:                                dbg_puts(": requested to stop\n");
                    996:                        }
                    997: #endif
                    998:                        s->ops->stop(s->arg);
                    999:                }
                   1000:        }
                   1001: }
                   1002:
                   1003: /*
                   1004:  * relocate all slots simultaneously
                   1005:  */
                   1006: void
                   1007: ctl_loc(struct aproc *p, unsigned origin)
                   1008: {
                   1009:        unsigned i, tstate;
                   1010:        struct ctl_slot *s;
                   1011:
                   1012:        if (!APROC_OK(p))
                   1013:                return;
                   1014: #ifdef DEBUG
                   1015:        if (debug_level >= 2) {
                   1016:                dbg_puts("server relocated to ");
                   1017:                dbg_putu(origin);
                   1018:                dbg_puts("\n");
                   1019:        }
                   1020: #endif
                   1021:        tstate = p->u.ctl.tstate;
                   1022:        if (tstate == CTL_RUN)
                   1023:                ctl_stop(p);
                   1024:        p->u.ctl.origin = origin;
                   1025:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                   1026:                if (!s->ops)
                   1027:                        continue;
                   1028:                s->ops->loc(s->arg, p->u.ctl.origin);
                   1029:        }
                   1030:        if (tstate == CTL_RUN)
                   1031:                ctl_start(p);
                   1032: }
                   1033:
                   1034: /*
                   1035:  * check if there are controlled streams
                   1036:  */
                   1037: int
                   1038: ctl_idle(struct aproc *p)
                   1039: {
                   1040:        unsigned i;
                   1041:        struct ctl_slot *s;
                   1042:
                   1043:        if (!APROC_OK(p))
                   1044:                return 1;
                   1045:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                   1046:                if (s->ops)
                   1047:                        return 0;
                   1048:        }
                   1049:        return 1;
                   1050: }
                   1051:
                   1052: /*
1.7       ratchov  1053:  * handle a MIDI event received from ibuf
                   1054:  */
1.3       ratchov  1055: void
                   1056: ctl_ev(struct aproc *p, struct abuf *ibuf)
                   1057: {
                   1058:        unsigned chan;
1.5       ratchov  1059:        struct ctl_slot *slot;
1.33      ratchov  1060:        struct sysex *x;
                   1061:        unsigned fps, len;
1.14      ratchov  1062: #ifdef DEBUG
                   1063:        unsigned i;
                   1064:
                   1065:        if (debug_level >= 3) {
                   1066:                abuf_dbg(ibuf);
                   1067:                dbg_puts(": got event:");
                   1068:                for (i = 0; i < ibuf->r.midi.idx; i++) {
                   1069:                        dbg_puts(" ");
                   1070:                        dbg_putx(ibuf->r.midi.msg[i]);
                   1071:                }
                   1072:                dbg_puts("\n");
                   1073:        }
                   1074: #endif
1.10      ratchov  1075:        if ((ibuf->r.midi.msg[0] & MIDI_CMDMASK) == MIDI_CTL &&
1.33      ratchov  1076:            (ibuf->r.midi.msg[1] == MIDI_CTLVOL)) {
1.10      ratchov  1077:                chan = ibuf->r.midi.msg[0] & MIDI_CHANMASK;
1.3       ratchov  1078:                if (chan >= CTL_NSLOT)
                   1079:                        return;
1.5       ratchov  1080:                slot = p->u.ctl.slot + chan;
1.22      ratchov  1081:                slot->vol = ibuf->r.midi.msg[2];
1.12      ratchov  1082:                if (slot->ops == NULL)
1.3       ratchov  1083:                        return;
1.12      ratchov  1084:                slot->ops->vol(slot->arg, slot->vol);
1.10      ratchov  1085:                ctl_sendmsg(p, ibuf, ibuf->r.midi.msg, ibuf->r.midi.len);
1.3       ratchov  1086:        }
1.33      ratchov  1087:        x = (struct sysex *)ibuf->r.midi.msg;
                   1088:        len = ibuf->r.midi.idx;
                   1089:        if (x->start != SYSEX_START)
                   1090:                return;
                   1091:        if (len < SYSEX_SIZE(empty))
                   1092:                return;
                   1093:        switch (x->type) {
                   1094:        case SYSEX_TYPE_RT:
                   1095:                if (x->id0 != SYSEX_MMC)
                   1096:                        return;
                   1097:                switch (x->id1) {
                   1098:                case SYSEX_MMC_STOP:
                   1099:                        if (len != SYSEX_SIZE(stop))
                   1100:                                return;
1.14      ratchov  1101: #ifdef DEBUG
1.19      ratchov  1102:                        if (debug_level >= 3) {
1.14      ratchov  1103:                                abuf_dbg(ibuf);
                   1104:                                dbg_puts(": mmc stop\n");
                   1105:                        }
                   1106: #endif
1.19      ratchov  1107:                        ctl_stop(p);
1.13      ratchov  1108:                        break;
1.33      ratchov  1109:                case SYSEX_MMC_START:
                   1110:                        if (len != SYSEX_SIZE(start))
                   1111:                                return;
1.14      ratchov  1112: #ifdef DEBUG
1.19      ratchov  1113:                        if (debug_level >= 3) {
1.14      ratchov  1114:                                abuf_dbg(ibuf);
                   1115:                                dbg_puts(": mmc start\n");
                   1116:                        }
                   1117: #endif
1.19      ratchov  1118:                        ctl_start(p);
1.13      ratchov  1119:                        break;
1.33      ratchov  1120:                case SYSEX_MMC_LOC:
                   1121:                        if (len != SYSEX_SIZE(loc) ||
                   1122:                            x->u.loc.len != SYSEX_MMC_LOC_LEN ||
                   1123:                            x->u.loc.cmd != SYSEX_MMC_LOC_CMD)
                   1124:                                return;
                   1125:                        switch (x->u.loc.hr >> 5) {
                   1126:                        case MTC_FPS_24:
                   1127:                                fps = 24;
                   1128:                                break;
                   1129:                        case MTC_FPS_25:
                   1130:                                fps = 25;
                   1131:                                break;
                   1132:                        case MTC_FPS_30:
                   1133:                                fps = 30;
                   1134:                                break;
                   1135:                        default:
                   1136:                                p->u.ctl.origin = 0;
                   1137:                                return;
                   1138:                        }
                   1139:                        ctl_loc(p,
                   1140:                            (x->u.loc.hr & 0x1f) * 3600 * MTC_SEC +
                   1141:                             x->u.loc.min * 60 * MTC_SEC +
                   1142:                             x->u.loc.sec * MTC_SEC +
                   1143:                             x->u.loc.fr * (MTC_SEC / fps) +
                   1144:                             x->u.loc.cent * (MTC_SEC / 100 / fps));
1.13      ratchov  1145:                        break;
                   1146:                }
1.35      ratchov  1147:                break;
                   1148:        case SYSEX_TYPE_EDU:
                   1149:                if (x->id0 != SYSEX_AUCAT || x->id1 != SYSEX_AUCAT_DUMPREQ)
                   1150:                        return;
                   1151:                if (len != SYSEX_SIZE(dumpreq))
                   1152:                        return;
                   1153:                if (ibuf->duplex)
                   1154:                        ctl_dump(p, ibuf->duplex);
1.33      ratchov  1155:                break;
1.13      ratchov  1156:        }
1.3       ratchov  1157: }
                   1158:
                   1159: int
                   1160: ctl_in(struct aproc *p, struct abuf *ibuf)
                   1161: {
                   1162:        unsigned char *idata;
                   1163:        unsigned c, i, icount;
                   1164:
                   1165:        if (!ABUF_ROK(ibuf))
                   1166:                return 0;
                   1167:        idata = abuf_rgetblk(ibuf, &icount, 0);
                   1168:        for (i = 0; i < icount; i++) {
                   1169:                c = *idata++;
1.8       ratchov  1170:                if (c >= 0xf8) {
                   1171:                        /* clock events not used yet */
                   1172:                } else if (c >= 0xf0) {
1.10      ratchov  1173:                        if (ibuf->r.midi.st == 0xf0 && c == 0xf7 &&
                   1174:                            ibuf->r.midi.idx < MIDI_MSGMAX) {
                   1175:                                ibuf->r.midi.msg[ibuf->r.midi.idx++] = c;
1.8       ratchov  1176:                                ctl_ev(p, ibuf);
                   1177:                                continue;
                   1178:                        }
1.10      ratchov  1179:                        ibuf->r.midi.msg[0] = c;
                   1180:                        ibuf->r.midi.len = common_len[c & 7];
                   1181:                        ibuf->r.midi.st = c;
                   1182:                        ibuf->r.midi.idx = 1;
1.3       ratchov  1183:                } else if (c >= 0x80) {
1.10      ratchov  1184:                        ibuf->r.midi.msg[0] = c;
                   1185:                        ibuf->r.midi.len = voice_len[(c >> 4) & 7];
                   1186:                        ibuf->r.midi.st = c;
                   1187:                        ibuf->r.midi.idx = 1;
                   1188:                } else if (ibuf->r.midi.st) {
                   1189:                        if (ibuf->r.midi.idx == MIDI_MSGMAX)
1.8       ratchov  1190:                                continue;
1.10      ratchov  1191:                        if (ibuf->r.midi.idx == 0)
                   1192:                                ibuf->r.midi.msg[ibuf->r.midi.idx++] = ibuf->r.midi.st;
                   1193:                        ibuf->r.midi.msg[ibuf->r.midi.idx++] = c;
                   1194:                        if (ibuf->r.midi.idx == ibuf->r.midi.len) {
1.3       ratchov  1195:                                ctl_ev(p, ibuf);
1.10      ratchov  1196:                                ibuf->r.midi.idx = 0;
1.3       ratchov  1197:                        }
                   1198:                }
                   1199:        }
                   1200:        abuf_rdiscard(ibuf, icount);
                   1201:        return 1;
                   1202: }
                   1203:
                   1204: int
                   1205: ctl_out(struct aproc *p, struct abuf *obuf)
                   1206: {
                   1207:        return 0;
                   1208: }
                   1209:
                   1210: void
                   1211: ctl_eof(struct aproc *p, struct abuf *ibuf)
                   1212: {
1.13      ratchov  1213:        unsigned i;
                   1214:        struct ctl_slot *s;
                   1215:
                   1216:        if (!(p->flags & APROC_QUIT))
                   1217:                return;
                   1218:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
1.27      ratchov  1219:                if (s->ops != NULL)
                   1220:                        s->ops->quit(s->arg);
1.13      ratchov  1221:        }
1.34      ratchov  1222:        if (LIST_EMPTY(&p->ins))
1.13      ratchov  1223:                aproc_del(p);
1.3       ratchov  1224: }
                   1225:
                   1226: void
                   1227: ctl_hup(struct aproc *p, struct abuf *obuf)
                   1228: {
1.13      ratchov  1229:        unsigned i;
                   1230:        struct ctl_slot *s;
                   1231:
                   1232:        if (!(p->flags & APROC_QUIT))
                   1233:                return;
                   1234:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                   1235:                if (s->ops)
                   1236:                        return;
                   1237:        }
1.34      ratchov  1238:        if (LIST_EMPTY(&p->ins))
1.13      ratchov  1239:                aproc_del(p);
1.3       ratchov  1240: }
                   1241:
                   1242: void
                   1243: ctl_newin(struct aproc *p, struct abuf *ibuf)
                   1244: {
1.10      ratchov  1245:        ibuf->r.midi.used = 0;
                   1246:        ibuf->r.midi.len = 0;
                   1247:        ibuf->r.midi.idx = 0;
                   1248:        ibuf->r.midi.st = 0;
1.3       ratchov  1249: }
                   1250:
                   1251: void
                   1252: ctl_done(struct aproc *p)
                   1253: {
1.14      ratchov  1254:        unsigned i;
                   1255:        struct ctl_slot *s;
                   1256:
                   1257:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
1.25      ratchov  1258:                if (s->ops != NULL)
                   1259:                        s->ops->quit(s->arg);
1.14      ratchov  1260:        }
1.3       ratchov  1261: }
                   1262:
                   1263: struct aproc_ops ctl_ops = {
                   1264:        "ctl",
                   1265:        ctl_in,
                   1266:        ctl_out,
                   1267:        ctl_eof,
                   1268:        ctl_hup,
                   1269:        ctl_newin,
                   1270:        NULL, /* newout */
                   1271:        NULL, /* ipos */
                   1272:        NULL, /* opos */
                   1273:        ctl_done
                   1274: };
                   1275:
                   1276: struct aproc *
1.25      ratchov  1277: ctl_new(char *name, struct dev *dev)
1.3       ratchov  1278: {
                   1279:        struct aproc *p;
1.5       ratchov  1280:        struct ctl_slot *s;
1.3       ratchov  1281:        unsigned i;
                   1282:
                   1283:        p = aproc_new(&ctl_ops, name);
1.25      ratchov  1284:        p->u.ctl.dev = dev;
1.6       ratchov  1285:        p->u.ctl.serial = 0;
1.13      ratchov  1286:        p->u.ctl.tstate = CTL_STOP;
1.5       ratchov  1287:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
1.3       ratchov  1288:                p->u.ctl.slot[i].unit = i;
1.12      ratchov  1289:                p->u.ctl.slot[i].ops = NULL;
1.7       ratchov  1290:                p->u.ctl.slot[i].vol = MIDI_MAXCTL;
1.13      ratchov  1291:                p->u.ctl.slot[i].tstate = CTL_OFF;
1.6       ratchov  1292:                p->u.ctl.slot[i].serial = p->u.ctl.serial++;
1.22      ratchov  1293:                p->u.ctl.slot[i].name[0] = '\0';
1.3       ratchov  1294:        }
1.1       ratchov  1295:        return p;
                   1296: }