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

Annotation of src/usr.bin/aucat/aucat.c, Revision 1.173

1.1       kstailey    1: /*
1.146     ratchov     2:  * Copyright (c) 2008-2014 Alexandre Ratchov <alex@caoua.org>
1.1       kstailey    3:  *
1.15      ratchov     4:  * Permission to use, copy, modify, and distribute this software for any
                      5:  * purpose with or without fee is hereby granted, provided that the above
                      6:  * copyright notice and this permission notice appear in all copies.
                      7:  *
                      8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                      9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     15:  */
1.145     deraadt    16:
1.173   ! ratchov    17: #include <err.h>
1.55      ratchov    18: #include <errno.h>
1.164     ratchov    19: #include <limits.h>
1.146     ratchov    20: #include <poll.h>
1.15      ratchov    21: #include <signal.h>
1.135     ratchov    22: #include <sndio.h>
1.4       millert    23: #include <stdlib.h>
1.8       david      24: #include <string.h>
1.1       kstailey   25: #include <unistd.h>
1.146     ratchov    26: #include "abuf.h"
                     27: #include "afile.h"
                     28: #include "dsp.h"
                     29: #include "sysex.h"
                     30: #include "utils.h"
                     31:
                     32: /*
                     33:  * masks to extract command and channel of status byte
                     34:  */
                     35: #define MIDI_CMDMASK   0xf0
                     36: #define MIDI_CHANMASK  0x0f
                     37:
                     38: /*
                     39:  * MIDI status bytes of voice messages
                     40:  */
                     41: #define MIDI_NOFF      0x80            /* note off */
                     42: #define MIDI_NON       0x90            /* note on */
                     43: #define MIDI_KAT       0xa0            /* key after touch */
                     44: #define MIDI_CTL       0xb0            /* controller */
                     45: #define MIDI_PC                0xc0            /* program change */
                     46: #define MIDI_CAT       0xd0            /* channel after touch */
                     47: #define MIDI_BEND      0xe0            /* pitch bend */
                     48: #define MIDI_ACK       0xfe            /* active sensing message */
                     49:
                     50: /*
                     51:  * MIDI controller numbers
                     52:  */
                     53: #define MIDI_CTL_VOL   7
                     54:
                     55: /*
                     56:  * Max coarse value
                     57:  */
                     58: #define MIDI_MAXCTL    127
                     59:
                     60: /*
                     61:  * MIDI status bytes for sysex
                     62:  */
                     63: #define MIDI_SX_START  0xf0
                     64: #define MIDI_SX_STOP   0xf7
                     65:
                     66: /*
                     67:  * audio device defaults
                     68:  */
                     69: #define DEFAULT_RATE           48000
                     70: #define DEFAULT_BUFSZ_MS       200
                     71:
                     72: struct slot {
1.149     ratchov    73:        struct slot *next;              /* next on the play/rec list */
1.146     ratchov    74:        int vol;                        /* dynamic range */
                     75:        int volctl;                     /* volume in the 0..127 range */
                     76:        struct abuf buf;                /* file i/o buffer */
                     77:        int bpf;                        /* bytes per frame */
                     78:        int cmin, cmax;                 /* file channel range */
                     79:        struct cmap cmap;               /* channel mapper state */
                     80:        struct resamp resamp;           /* resampler state */
                     81:        struct conv conv;               /* format encoder state */
                     82:        int join;                       /* channel join factor */
                     83:        int expand;                     /* channel expand factor */
                     84:        void *resampbuf, *convbuf;      /* conversion tmp buffers */
                     85:        int dup;                        /* mono-to-stereo and alike */
                     86:        int round;                      /* slot-side block size */
                     87:        int mode;                       /* MODE_{PLAY,REC} */
                     88: #define SLOT_CFG       0               /* buffers not allocated yet */
                     89: #define SLOT_INIT      1               /* not trying to do anything */
                     90: #define SLOT_RUN       2               /* playing/recording */
                     91: #define SLOT_STOP      3               /* draining (play only) */
                     92:        int pstate;                     /* one of above */
1.164     ratchov    93:        long long skip;                 /* frames to skip at the beginning */
                     94:        long long pos;                  /* start position (at device rate) */
1.149     ratchov    95:        struct afile afile;             /* file desc & friends */
1.146     ratchov    96: };
                     97:
                     98: /*
                     99:  * device properties
                    100:  */
                    101: unsigned int dev_mode;                 /* bitmap of SIO_{PLAY,REC} */
                    102: unsigned int dev_bufsz;                        /* device buffer size */
                    103: unsigned int dev_round;                        /* device block size */
                    104: int dev_rate;                          /* device sample rate (Hz) */
                    105: unsigned int dev_pchan, dev_rchan;     /* play & rec channels count */
                    106: adata_t *dev_pbuf, *dev_rbuf;          /* play & rec buffers */
1.159     ratchov   107: long long dev_pos;                     /* last MMC position in frames */
1.146     ratchov   108: #define DEV_STOP       0               /* stopped */
                    109: #define DEV_START      1               /* started */
                    110: unsigned int dev_pstate;               /* one of above */
                    111: char *dev_name;                                /* device sndio(7) name */
                    112: char *dev_port;                                /* control port sndio(7) name */
                    113: struct sio_hdl *dev_sh;                        /* device handle */
                    114: struct mio_hdl *dev_mh;                        /* MIDI control port handle */
                    115: unsigned int dev_volctl = MIDI_MAXCTL; /* master volume */
                    116:
                    117: /*
                    118:  * MIDI parser state
                    119:  */
                    120: #define MIDI_MSGMAX    32              /* max size of MIDI msg */
                    121: unsigned char dev_msg[MIDI_MSGMAX];    /* parsed input message */
                    122: unsigned int dev_mst;                  /* input MIDI running status */
                    123: unsigned int dev_mused;                        /* bytes used in ``msg'' */
                    124: unsigned int dev_midx;                 /* current ``msg'' size */
                    125: unsigned int dev_mlen;                 /* expected ``msg'' length */
                    126: unsigned int dev_prime;                        /* blocks to write to start */
                    127:
                    128: unsigned int log_level = 1;
                    129: volatile sig_atomic_t quit_flag = 0;
                    130: struct slot *slot_list = NULL;
                    131:
                    132: /*
                    133:  * length of voice and common MIDI messages (status byte included)
                    134:  */
                    135: unsigned int voice_len[] = { 3, 3, 3, 3, 2, 2, 3 };
                    136: unsigned int common_len[] = { 0, 2, 3, 2, 0, 0, 1, 1 };
                    137:
1.147     jmc       138: char usagestr[] = "usage: aucat [-dn] [-b size] "
1.164     ratchov   139:     "[-c min:max] [-e enc] [-f device] [-g position]\n\t"
                    140:     "[-h fmt] [-i file] [-j flag] [-o file] [-p position] [-q port]\n\t"
                    141:     "[-r rate] [-v volume]\n";
1.146     ratchov   142:
                    143: static void
                    144: slot_log(struct slot *s)
                    145: {
                    146: #ifdef DEBUG
                    147:        static char *pstates[] = {
                    148:                "cfg", "ini", "run", "stp"
                    149:        };
                    150: #endif
                    151:        log_puts(s->afile.path);
                    152: #ifdef DEBUG
                    153:        if (log_level >= 3) {
                    154:                log_puts(",pst=");
                    155:                log_puts(pstates[s->pstate]);
                    156:        }
                    157: #endif
                    158: }
                    159:
                    160: static void
                    161: slot_flush(struct slot *s)
                    162: {
1.155     ratchov   163:        int count, n;
1.146     ratchov   164:        unsigned char *data;
                    165:
1.155     ratchov   166:        for (;;) {
1.146     ratchov   167:                data = abuf_rgetblk(&s->buf, &count);
1.155     ratchov   168:                if (count == 0)
                    169:                        break;
1.146     ratchov   170:                n = afile_write(&s->afile, data, count);
                    171:                if (n == 0) {
                    172:                        slot_log(s);
                    173:                        log_puts(": can't write, disabled\n");
                    174:                        s->pstate = SLOT_INIT;
                    175:                        return;
                    176:                }
                    177:                abuf_rdiscard(&s->buf, n);
                    178:        }
                    179: }
                    180:
                    181: static void
                    182: slot_fill(struct slot *s)
                    183: {
1.155     ratchov   184:        int count, n;
1.146     ratchov   185:        unsigned char *data;
                    186:
1.155     ratchov   187:        for (;;) {
1.146     ratchov   188:                data = abuf_wgetblk(&s->buf, &count);
1.155     ratchov   189:                if (count == 0)
                    190:                        break;
1.146     ratchov   191:                n = afile_read(&s->afile, data, count);
                    192:                if (n == 0) {
                    193: #ifdef DEBUG
                    194:                        if (log_level >= 3) {
                    195:                                slot_log(s);
                    196:                                log_puts(": eof reached, stopping\n");
                    197:                        }
                    198: #endif
                    199:                        s->pstate = SLOT_STOP;
                    200:                        break;
                    201:                }
                    202:                abuf_wcommit(&s->buf, n);
                    203:        }
                    204: }
                    205:
                    206: static int
                    207: slot_new(char *path, int mode, struct aparams *par, int hdr,
1.164     ratchov   208:     int cmin, int cmax, int rate, int dup, int vol, long long pos)
1.146     ratchov   209: {
                    210:        struct slot *s;
                    211:
                    212:        s = xmalloc(sizeof(struct slot));
                    213:        if (!afile_open(&s->afile, path, hdr,
                    214:                mode == SIO_PLAY ? AFILE_FREAD : AFILE_FWRITE,
                    215:                par, rate, cmax - cmin + 1)) {
1.169     ratchov   216:                xfree(s);
1.146     ratchov   217:                return 0;
                    218:        }
                    219:        s->cmin = cmin;
                    220:        s->cmax = cmin + s->afile.nch - 1;
                    221:        s->dup = dup;
                    222:        s->vol = MIDI_TO_ADATA(vol);
                    223:        s->mode = mode;
                    224:        s->pstate = SLOT_CFG;
1.164     ratchov   225:        s->pos = pos;
1.146     ratchov   226:        if (log_level >= 2) {
                    227:                slot_log(s);
                    228:                log_puts(": ");
                    229:                log_puts(s->mode == SIO_PLAY ? "play" : "rec");
                    230:                log_puts(", chan ");
                    231:                log_putu(s->cmin);
                    232:                log_puts(":");
                    233:                log_putu(s->cmax);
                    234:                log_puts(", ");
                    235:                log_putu(s->afile.rate);
                    236:                log_puts("Hz, ");
                    237:                switch (s->afile.fmt) {
                    238:                case AFILE_FMT_PCM:
                    239:                        aparams_log(&s->afile.par);
                    240:                        break;
                    241:                case AFILE_FMT_ULAW:
                    242:                        log_puts("ulaw");
                    243:                        break;
                    244:                case AFILE_FMT_ALAW:
                    245:                        log_puts("alaw");
                    246:                        break;
                    247:                case AFILE_FMT_FLOAT:
                    248:                        log_puts("f32le");
                    249:                        break;
                    250:                }
                    251:                if (s->mode == SIO_PLAY && s->afile.endpos >= 0) {
                    252:                        log_puts(", bytes ");
                    253:                        log_puti(s->afile.startpos);
                    254:                        log_puts("..");
                    255:                        log_puti(s->afile.endpos);
                    256:                }
1.168     ratchov   257:                if (s->mode == SIO_PLAY) {
                    258:                        log_puts(", vol ");
                    259:                        log_puti(s->vol);
                    260:                }
1.146     ratchov   261:                log_puts("\n");
                    262:        }
                    263:        s->next = slot_list;
                    264:        slot_list = s;
                    265:        return 1;
                    266: }
                    267:
                    268: static void
                    269: slot_init(struct slot *s)
                    270: {
                    271:        unsigned int slot_nch, bufsz;
                    272:
                    273: #ifdef DEBUG
                    274:        if (s->pstate != SLOT_CFG) {
                    275:                slot_log(s);
                    276:                log_puts(": slot_init: wrong state\n");
                    277:                panic();
                    278:        }
                    279: #endif
                    280:        s->bpf = s->afile.par.bps * (s->cmax - s->cmin + 1);
1.158     ratchov   281:        s->round = (dev_round * s->afile.rate + dev_rate - 1) / dev_rate;
1.146     ratchov   282:
                    283:        bufsz = s->round * (dev_bufsz / dev_round);
                    284:        bufsz -= bufsz % s->round;
                    285:        if (bufsz == 0)
                    286:                bufsz = s->round;
                    287:        abuf_init(&s->buf, bufsz * s->bpf);
                    288: #ifdef DEBUG
                    289:        if (log_level >= 3) {
                    290:                slot_log(s);
                    291:                log_puts(": allocated ");
                    292:                log_putu(bufsz);
                    293:                log_puts(" frame buffer\n");
                    294:        }
                    295: #endif
                    296:
                    297:        slot_nch = s->cmax - s->cmin + 1;
                    298:        s->convbuf = NULL;
                    299:        s->resampbuf = NULL;
                    300:        s->join = 1;
                    301:        s->expand = 1;
                    302:        if (s->mode & SIO_PLAY) {
                    303:                if (s->dup) {
                    304:                        if (dev_pchan > slot_nch)
                    305:                                s->expand = dev_pchan / slot_nch;
                    306:                        else if (dev_pchan < slot_nch)
                    307:                                s->join = slot_nch / dev_pchan;
                    308:                }
                    309:                cmap_init(&s->cmap,
                    310:                    s->cmin, s->cmax,
                    311:                    s->cmin, s->cmax,
                    312:                    0, dev_pchan - 1,
                    313:                    0, dev_pchan - 1);
1.151     ratchov   314:                if (s->afile.fmt != AFILE_FMT_PCM ||
                    315:                    !aparams_native(&s->afile.par)) {
1.146     ratchov   316:                        dec_init(&s->conv, &s->afile.par, slot_nch);
                    317:                        s->convbuf =
                    318:                            xmalloc(s->round * slot_nch * sizeof(adata_t));
                    319:                }
                    320:                if (s->afile.rate != dev_rate) {
1.157     ratchov   321:                        resamp_init(&s->resamp, s->afile.rate, dev_rate,
1.146     ratchov   322:                            slot_nch);
                    323:                        s->resampbuf =
                    324:                            xmalloc(dev_round * slot_nch * sizeof(adata_t));
                    325:                }
                    326:        }
                    327:        if (s->mode & SIO_REC) {
                    328:                if (s->dup) {
                    329:                        if (dev_rchan > slot_nch)
                    330:                                s->join = dev_rchan / slot_nch;
                    331:                        else if (dev_rchan < slot_nch)
                    332:                                s->expand = slot_nch / dev_rchan;
                    333:                }
                    334:                cmap_init(&s->cmap,
                    335:                    0, dev_rchan - 1,
                    336:                    0, dev_rchan - 1,
                    337:                    s->cmin, s->cmax,
                    338:                    s->cmin, s->cmax);
                    339:                if (s->afile.rate != dev_rate) {
1.157     ratchov   340:                        resamp_init(&s->resamp, dev_rate, s->afile.rate,
1.146     ratchov   341:                            slot_nch);
                    342:                        s->resampbuf =
                    343:                            xmalloc(dev_round * slot_nch * sizeof(adata_t));
                    344:                }
                    345:                if (!aparams_native(&s->afile.par)) {
                    346:                        enc_init(&s->conv, &s->afile.par, slot_nch);
                    347:                        s->convbuf =
                    348:                            xmalloc(s->round * slot_nch * sizeof(adata_t));
1.172     ratchov   349:                }
                    350:
                    351:                /*
                    352:                 * cmap_copy() doesn't write samples in all channels,
                    353:                 * for instance when mono->stereo conversion is
                    354:                 * disabled. So we have to prefill cmap_copy() output
                    355:                 * with silence.
                    356:                 */
                    357:                if (s->resampbuf) {
                    358:                        memset(s->resampbuf, 0,
                    359:                            dev_round * slot_nch * sizeof(adata_t));
                    360:                } else if (s->convbuf) {
                    361:                        memset(s->convbuf, 0,
                    362:                            s->round * slot_nch * sizeof(adata_t));
1.171     ratchov   363:                } else {
                    364:                        memset(s->buf.data, 0,
                    365:                            bufsz * slot_nch * sizeof(adata_t));
1.146     ratchov   366:                }
                    367:        }
                    368:        s->pstate = SLOT_INIT;
                    369: #ifdef DEBUG
                    370:        if (log_level >= 3) {
                    371:                slot_log(s);
                    372:                log_puts(": chain initialized\n");
                    373:        }
                    374: #endif
                    375: }
                    376:
                    377: static void
1.159     ratchov   378: slot_start(struct slot *s, long long pos)
1.146     ratchov   379: {
                    380: #ifdef DEBUG
                    381:        if (s->pstate != SLOT_INIT) {
                    382:                slot_log(s);
                    383:                log_puts(": slot_start: wrong state\n");
                    384:                panic();
                    385:        }
                    386: #endif
1.164     ratchov   387:        pos -= s->pos;
                    388:        if (pos < 0) {
                    389:                s->skip = -pos;
                    390:                pos = 0;
                    391:        } else
                    392:                s->skip = 0;
                    393:
1.159     ratchov   394:        /*
                    395:         * convert pos to slot sample rate
                    396:         *
                    397:         * At this stage, we could adjust s->resamp.diff to get
                    398:         * sub-frame accuracy.
                    399:         */
                    400:        pos = pos * s->afile.rate / dev_rate;
                    401:
                    402:        if (!afile_seek(&s->afile, pos * s->bpf)) {
1.146     ratchov   403:                s->pstate = SLOT_INIT;
                    404:                return;
                    405:        }
                    406:        s->pstate = SLOT_RUN;
                    407:        if (s->mode & SIO_PLAY)
                    408:                slot_fill(s);
                    409: #ifdef DEBUG
                    410:        if (log_level >= 2) {
                    411:                slot_log(s);
                    412:                log_puts(": started\n");
                    413:        }
                    414: #endif
                    415: }
1.1       kstailey  416:
1.146     ratchov   417: static void
                    418: slot_stop(struct slot *s)
                    419: {
                    420:        if (s->pstate == SLOT_INIT)
                    421:                return;
                    422:        if (s->mode & SIO_REC)
                    423:                slot_flush(s);
                    424:        if (s->mode & SIO_PLAY)
                    425:                s->buf.used = s->buf.start = 0;
                    426:        s->pstate = SLOT_INIT;
1.78      ratchov   427: #ifdef DEBUG
1.146     ratchov   428:        if (log_level >= 2) {
                    429:                slot_log(s);
                    430:                log_puts(": stopped\n");
                    431:        }
1.78      ratchov   432: #endif
1.146     ratchov   433: }
1.11      jaredy    434:
1.146     ratchov   435: static void
                    436: slot_del(struct slot *s)
                    437: {
                    438:        struct slot **ps;
1.61      ratchov   439:
1.146     ratchov   440:        if (s->pstate != SLOT_CFG) {
                    441:                slot_stop(s);
                    442:                afile_close(&s->afile);
                    443: #ifdef DEBUG
                    444:                if (log_level >= 3) {
                    445:                        slot_log(s);
                    446:                        log_puts(": closed\n");
                    447:                }
1.108     ratchov   448: #endif
1.146     ratchov   449:                abuf_done(&s->buf);
1.164     ratchov   450:                if (s->resampbuf)
1.169     ratchov   451:                        xfree(s->resampbuf);
1.164     ratchov   452:                if (s->convbuf)
1.169     ratchov   453:                        xfree(s->convbuf);
1.146     ratchov   454:        }
                    455:        for (ps = &slot_list; *ps != s; ps = &(*ps)->next)
                    456:                ; /* nothing */
                    457:        *ps = s->next;
1.169     ratchov   458:        xfree(s);
1.146     ratchov   459: }
                    460:
1.163     ratchov   461: static void
                    462: slot_getcnt(struct slot *s, int *icnt, int *ocnt)
1.154     ratchov   463: {
1.163     ratchov   464:        int cnt;
1.154     ratchov   465:
1.163     ratchov   466:        if (s->resampbuf)
                    467:                resamp_getcnt(&s->resamp, icnt, ocnt);
                    468:        else {
                    469:                cnt = (*icnt < *ocnt) ? *icnt : *ocnt;
                    470:                *icnt = cnt;
                    471:                *ocnt = cnt;
                    472:        }
1.154     ratchov   473: }
                    474:
1.153     ratchov   475: static void
1.154     ratchov   476: play_filt_resamp(struct slot *s, void *res_in, void *out, int icnt, int ocnt)
1.146     ratchov   477: {
                    478:        int i, offs, vol, nch;
                    479:        void *in;
                    480:
                    481:        if (s->resampbuf) {
1.153     ratchov   482:                resamp_do(&s->resamp, res_in, s->resampbuf, icnt, ocnt);
1.146     ratchov   483:                in = s->resampbuf;
                    484:        } else
                    485:                in = res_in;
                    486:
                    487:        nch = s->cmap.nch;
                    488:        vol = s->vol / s->join; /* XXX */
1.154     ratchov   489:        cmap_add(&s->cmap, in, out, vol, ocnt);
1.146     ratchov   490:
                    491:        offs = 0;
                    492:        for (i = s->join - 1; i > 0; i--) {
                    493:                offs += nch;
1.154     ratchov   494:                cmap_add(&s->cmap, (adata_t *)in + offs, out, vol, ocnt);
1.146     ratchov   495:        }
                    496:        offs = 0;
                    497:        for (i = s->expand - 1; i > 0; i--) {
                    498:                offs += nch;
1.154     ratchov   499:                cmap_add(&s->cmap, in, (adata_t *)out + offs, vol, ocnt);
1.146     ratchov   500:        }
                    501: }
                    502:
1.153     ratchov   503: static void
1.154     ratchov   504: play_filt_dec(struct slot *s, void *in, void *out, int icnt, int ocnt)
1.146     ratchov   505: {
                    506:        void *tmp;
                    507:
                    508:        tmp = s->convbuf;
                    509:        if (tmp) {
                    510:                switch (s->afile.fmt) {
                    511:                case AFILE_FMT_PCM:
1.154     ratchov   512:                        dec_do(&s->conv, in, tmp, icnt);
1.146     ratchov   513:                        break;
                    514:                case AFILE_FMT_ULAW:
1.154     ratchov   515:                        dec_do_ulaw(&s->conv, in, tmp, icnt, 0);
1.146     ratchov   516:                        break;
                    517:                case AFILE_FMT_ALAW:
1.154     ratchov   518:                        dec_do_ulaw(&s->conv, in, tmp, icnt, 1);
1.146     ratchov   519:                        break;
                    520:                case AFILE_FMT_FLOAT:
1.154     ratchov   521:                        dec_do_float(&s->conv, in, tmp, icnt);
1.146     ratchov   522:                        break;
                    523:                }
1.153     ratchov   524:        } else
                    525:                tmp = in;
                    526:        play_filt_resamp(s, tmp, out, icnt, ocnt);
1.146     ratchov   527: }
1.108     ratchov   528:
1.120     ratchov   529: /*
1.146     ratchov   530:  * Mix as many as possible frames (but not more than a block) from the
                    531:  * slot buffer to the given location. Return the number of frames mixed
                    532:  * in the output buffer
1.120     ratchov   533:  */
1.146     ratchov   534: static int
                    535: slot_mix_badd(struct slot *s, adata_t *odata)
                    536: {
                    537:        adata_t *idata;
1.154     ratchov   538:        int len, icnt, ocnt, otodo, odone;
1.146     ratchov   539:
1.154     ratchov   540:        odone = 0;
                    541:        otodo = dev_round;
1.164     ratchov   542:        if (s->skip > 0) {
                    543:                ocnt = otodo;
                    544:                if (ocnt > s->skip)
                    545:                        ocnt = s->skip;
                    546:                s->skip -= ocnt;
                    547:                odata += dev_pchan * ocnt;
                    548:                otodo -= ocnt;
                    549:                odone += ocnt;
                    550:        }
1.154     ratchov   551:        while (otodo > 0) {
                    552:                idata = (adata_t *)abuf_rgetblk(&s->buf, &len);
                    553:                icnt = len / s->bpf;
1.165     ratchov   554:                if (icnt > s->round)
                    555:                        icnt = s->round;
1.163     ratchov   556:                ocnt = otodo;
                    557:                slot_getcnt(s, &icnt, &ocnt);
1.154     ratchov   558:                if (icnt == 0)
                    559:                        break;
                    560:                play_filt_dec(s, idata, odata, icnt, ocnt);
                    561:                abuf_rdiscard(&s->buf, icnt * s->bpf);
                    562:                otodo -= ocnt;
                    563:                odone += ocnt;
                    564:                odata += ocnt * dev_pchan;
                    565:        }
                    566:        return odone;
1.146     ratchov   567: }
                    568:
1.153     ratchov   569: static void
1.154     ratchov   570: rec_filt_resamp(struct slot *s, void *in, void *res_out, int icnt, int ocnt)
1.146     ratchov   571: {
                    572:        int i, vol, offs, nch;
                    573:        void *out = res_out;
                    574:
                    575:        out = (s->resampbuf) ? s->resampbuf : res_out;
                    576:
                    577:        nch = s->cmap.nch;
                    578:        vol = ADATA_UNIT / s->join;
1.154     ratchov   579:        cmap_copy(&s->cmap, in, out, vol, icnt);
1.146     ratchov   580:
                    581:        offs = 0;
                    582:        for (i = s->join - 1; i > 0; i--) {
                    583:                offs += nch;
1.154     ratchov   584:                cmap_add(&s->cmap, (adata_t *)in + offs, out, vol, icnt);
1.146     ratchov   585:        }
                    586:        offs = 0;
                    587:        for (i = s->expand - 1; i > 0; i--) {
                    588:                offs += nch;
1.154     ratchov   589:                cmap_copy(&s->cmap, in, (adata_t *)out + offs, vol, icnt);
1.146     ratchov   590:        }
1.153     ratchov   591:        if (s->resampbuf)
                    592:                resamp_do(&s->resamp, s->resampbuf, res_out, icnt, ocnt);
                    593:        else
1.154     ratchov   594:                ocnt = icnt;
1.146     ratchov   595: }
                    596:
1.153     ratchov   597: static void
1.154     ratchov   598: rec_filt_enc(struct slot *s, void *in, void *out, int icnt, int ocnt)
1.146     ratchov   599: {
                    600:        void *tmp;
                    601:
                    602:        tmp = s->convbuf;
1.153     ratchov   603:        rec_filt_resamp(s, in, tmp ? tmp : out, icnt, ocnt);
1.146     ratchov   604:        if (tmp)
1.154     ratchov   605:                enc_do(&s->conv, tmp, out, ocnt);
1.146     ratchov   606: }
1.133     ratchov   607:
                    608: /*
1.146     ratchov   609:  * Copy "todo" frames from the given buffer to the slot buffer,
                    610:  * but not more than a block.
1.133     ratchov   611:  */
1.146     ratchov   612: static void
1.154     ratchov   613: slot_sub_bcopy(struct slot *s, adata_t *idata, int itodo)
1.146     ratchov   614: {
                    615:        adata_t *odata;
1.153     ratchov   616:        int len, icnt, ocnt;
1.146     ratchov   617:
1.164     ratchov   618:        if (s->skip > 0) {
                    619:                icnt = itodo;
                    620:                if (icnt > s->skip)
                    621:                        icnt = s->skip;
                    622:                s->skip -= icnt;
                    623:                idata += dev_rchan * icnt;
                    624:                itodo -= icnt;
                    625:        }
                    626:
1.154     ratchov   627:        while (itodo > 0) {
                    628:                odata = (adata_t *)abuf_wgetblk(&s->buf, &len);
                    629:                ocnt = len / s->bpf;
1.165     ratchov   630:                if (ocnt > s->round)
                    631:                        ocnt = s->round;
1.163     ratchov   632:                icnt = itodo;
                    633:                slot_getcnt(s, &icnt, &ocnt);
1.154     ratchov   634:                if (ocnt == 0)
                    635:                        break;
                    636:                rec_filt_enc(s, idata, odata, icnt, ocnt);
                    637:                abuf_wcommit(&s->buf, ocnt * s->bpf);
                    638:                itodo -= icnt;
                    639:                idata += icnt * dev_rchan;
                    640:        }
1.146     ratchov   641: }
                    642:
                    643: static int
                    644: dev_open(char *dev, int mode, int bufsz, char *port)
                    645: {
                    646:        int rate, pmax, rmax;
                    647:        struct sio_par par;
                    648:        struct slot *s;
                    649:
                    650:        if (port) {
                    651:                dev_port = port;
                    652:                dev_mh = mio_open(dev_port, MIO_IN, 0);
                    653:                if (dev_mh == NULL) {
                    654:                        log_puts(port);
                    655:                        log_puts(": couldn't open midi port\n");
                    656:                        return 0;
                    657:                }
                    658:        } else
                    659:                dev_mh = NULL;
                    660:
                    661:        dev_name = dev;
                    662:        dev_sh = sio_open(dev, mode, 0);
                    663:        if (dev_sh == NULL) {
                    664:                log_puts(dev_name);
                    665:                log_puts(": couldn't open audio device\n");
                    666:                return 0;
                    667:        }
                    668:
                    669:        rate = pmax = rmax = 0;
                    670:        for (s = slot_list; s != NULL; s = s->next) {
                    671:                if (s->afile.rate > rate)
                    672:                        rate = s->afile.rate;
                    673:                if (s->mode == SIO_PLAY) {
                    674:                        if (s->cmax > pmax)
                    675:                                pmax = s->cmax;
                    676:                }
                    677:                if (s->mode == SIO_REC) {
                    678:                        if (s->cmax > rmax)
                    679:                                rmax = s->cmax;
                    680:                }
                    681:        }
                    682:        sio_initpar(&par);
                    683:        par.bits = ADATA_BITS;
                    684:        par.bps = sizeof(adata_t);
                    685:        par.msb = 0;
                    686:        par.le = SIO_LE_NATIVE;
1.167     ratchov   687:        par.rate = rate;
1.146     ratchov   688:        if (mode & SIO_PLAY)
                    689:                par.pchan = pmax + 1;
                    690:        if (mode & SIO_REC)
                    691:                par.rchan = rmax + 1;
                    692:        par.appbufsz = bufsz > 0 ? bufsz : rate * DEFAULT_BUFSZ_MS / 1000;
                    693:        if (!sio_setpar(dev_sh, &par) || !sio_getpar(dev_sh, &par)) {
                    694:                log_puts(dev_name);
                    695:                log_puts(": couldn't set audio params\n");
                    696:                return 0;
                    697:        }
                    698:        if (par.bits != ADATA_BITS ||
                    699:            par.bps != sizeof(adata_t) ||
1.152     ratchov   700:            (par.bps > 1 && par.le != SIO_LE_NATIVE) ||
                    701:            (par.bps * 8 > par.bits && par.msb)) {
1.146     ratchov   702:                log_puts(dev_name);
                    703:                log_puts(": unsupported audio params\n");
                    704:                return 0;
                    705:        }
                    706:        dev_mode = mode;
                    707:        dev_rate = par.rate;
                    708:        dev_bufsz = par.bufsz;
                    709:        dev_round = par.round;
                    710:        if (mode & SIO_PLAY) {
                    711:                dev_pchan = par.pchan;
                    712:                dev_pbuf = xmalloc(sizeof(adata_t) * dev_pchan * dev_round);
                    713:        }
                    714:        if (mode & SIO_REC) {
                    715:                dev_rchan = par.rchan;
                    716:                dev_rbuf = xmalloc(sizeof(adata_t) * dev_rchan * dev_round);
                    717:        }
                    718:        dev_pstate = DEV_STOP;
                    719:        if (log_level >= 2) {
                    720:                log_puts(dev_name);
                    721:                log_puts(": ");
                    722:                log_putu(dev_rate);
                    723:                log_puts("Hz");
                    724:                if (dev_mode & SIO_PLAY) {
                    725:                        log_puts(", play 0:");
                    726:                        log_puti(dev_pchan - 1);
                    727:                }
                    728:                if (dev_mode & SIO_REC) {
                    729:                        log_puts(", rec 0:");
                    730:                        log_puti(dev_rchan - 1);
                    731:                }
                    732:                log_puts(", ");
                    733:                log_putu(dev_bufsz / dev_round);
                    734:                log_puts(" blocks of ");
                    735:                log_putu(dev_round);
                    736:                log_puts(" frames\n");
                    737:        }
                    738:        return 1;
                    739: }
1.120     ratchov   740:
1.146     ratchov   741: static void
                    742: dev_close(void)
                    743: {
                    744:        sio_close(dev_sh);
                    745:        if (dev_mh)
                    746:                mio_close(dev_mh);
                    747:        if (dev_mode & SIO_PLAY)
1.169     ratchov   748:                xfree(dev_pbuf);
1.146     ratchov   749:        if (dev_mode & SIO_REC)
1.169     ratchov   750:                xfree(dev_rbuf);
1.146     ratchov   751: }
                    752:
                    753: static void
                    754: dev_master(int val)
                    755: {
                    756:        struct slot *s;
                    757:        int mastervol, slotvol;
1.143     ratchov   758:
1.146     ratchov   759:        mastervol = MIDI_TO_ADATA(dev_volctl);
                    760:        for (s = slot_list; s != NULL; s = s->next) {
                    761:                slotvol = MIDI_TO_ADATA(val);
                    762:                s->vol = ADATA_MUL(mastervol, slotvol);
                    763:        }
1.78      ratchov   764: #ifdef DEBUG
1.146     ratchov   765:        if (log_level >= 3) {
                    766:                log_puts("master volume set to ");
                    767:                log_putu(val);
                    768:                log_puts("\n");
                    769:        }
1.78      ratchov   770: #endif
1.146     ratchov   771: }
1.7       deraadt   772:
1.146     ratchov   773: static void
                    774: dev_slotvol(int midich, int val)
                    775: {
                    776:        struct slot *s;
                    777:        int mastervol, slotvol;
                    778:
                    779:        for (s = slot_list; s != NULL; s = s->next) {
                    780:                if (midich == 0) {
                    781:                        mastervol = MIDI_TO_ADATA(dev_volctl);
                    782:                        slotvol = MIDI_TO_ADATA(val);
                    783:                        s->vol = ADATA_MUL(mastervol, slotvol);
                    784: #ifdef DEBUG
                    785:                        if (log_level >= 3) {
                    786:                                slot_log(s);
                    787:                                log_puts(": volume set to ");
                    788:                                log_putu(val);
                    789:                                log_puts("\n");
                    790:                        }
                    791: #endif
                    792:                        break;
                    793:                }
                    794:        }
                    795: }
1.129     ratchov   796:
1.28      ratchov   797: /*
1.146     ratchov   798:  * start all slots simultaneously
1.28      ratchov   799:  */
1.146     ratchov   800: static void
                    801: dev_mmcstart(void)
1.28      ratchov   802: {
1.146     ratchov   803:        struct slot *s;
                    804:
                    805:        if (dev_pstate == DEV_STOP) {
                    806:                dev_pstate = DEV_START;
                    807:                for (s = slot_list; s != NULL; s = s->next)
1.159     ratchov   808:                        slot_start(s, dev_pos);
1.146     ratchov   809:                dev_prime = (dev_mode & SIO_PLAY) ? dev_bufsz / dev_round : 0;
                    810:                sio_start(dev_sh);
                    811:                if (log_level >= 2)
                    812:                        log_puts("started\n");
                    813:        } else {
                    814: #ifdef DEBUG
                    815:                if (log_level >= 3)
                    816:                        log_puts("ignoring mmc start\n");
                    817: #endif
                    818:        }
1.28      ratchov   819: }
1.22      ratchov   820:
1.146     ratchov   821: /*
                    822:  * stop all slots simultaneously
                    823:  */
                    824: static void
                    825: dev_mmcstop(void)
                    826: {
                    827:        struct slot *s;
                    828:
                    829:        if (dev_pstate == DEV_START) {
                    830:                dev_pstate = DEV_STOP;
                    831:                for (s = slot_list; s != NULL; s = s->next)
                    832:                        slot_stop(s);
                    833:                sio_stop(dev_sh);
                    834:                if (log_level >= 2)
                    835:                        log_puts("stopped\n");
                    836:        } else {
1.78      ratchov   837: #ifdef DEBUG
1.146     ratchov   838:                if (log_level >= 3)
                    839:                        log_puts("ignored mmc stop\n");
                    840: #endif
                    841:        }
                    842: }
                    843:
1.78      ratchov   844: /*
1.146     ratchov   845:  * relocate all slots simultaneously
1.78      ratchov   846:  */
1.146     ratchov   847: static void
1.160     ratchov   848: dev_mmcloc(int hr, int min, int sec, int fr, int cent, int fps)
1.78      ratchov   849: {
1.159     ratchov   850:        long long pos;
                    851:
1.170     ratchov   852:        pos = (long long)dev_rate * hr * 3600 +
                    853:            (long long)dev_rate * min * 60 +
                    854:            (long long)dev_rate * sec +
                    855:            (long long)dev_rate * fr / fps +
                    856:            (long long)dev_rate * cent / (100 * fps);
1.159     ratchov   857:        if (dev_pos == pos)
1.146     ratchov   858:                return;
1.159     ratchov   859:        dev_pos = pos;
1.146     ratchov   860:        if (log_level >= 2) {
                    861:                log_puts("relocated to ");
1.160     ratchov   862:                log_putu(hr);
1.146     ratchov   863:                log_puts(":");
1.160     ratchov   864:                log_putu(min);
1.146     ratchov   865:                log_puts(":");
1.160     ratchov   866:                log_putu(sec);
1.159     ratchov   867:                log_puts(".");
1.160     ratchov   868:                log_putu(fr);
1.146     ratchov   869:                log_puts(".");
1.160     ratchov   870:                log_putu(cent);
                    871:                log_puts(" at ");
                    872:                log_putu(fps);
                    873:                log_puts("fps\n");
1.146     ratchov   874:        }
                    875:        if (dev_pstate == DEV_START) {
                    876:                dev_mmcstop();
                    877:                dev_mmcstart();
                    878:        }
                    879: }
                    880:
                    881: static void
                    882: dev_imsg(unsigned char *msg, unsigned int len)
                    883: {
                    884:        struct sysex *x;
                    885:        unsigned int fps, chan;
                    886:
                    887:        if ((msg[0] & MIDI_CMDMASK) == MIDI_CTL && msg[1] == MIDI_CTL_VOL) {
                    888:                chan = msg[0] & MIDI_CHANMASK;
                    889:                dev_slotvol(chan, msg[2]);
                    890:                return;
                    891:        }
                    892:        x = (struct sysex *)msg;
                    893:        if (x->start != SYSEX_START)
                    894:                return;
                    895:        if (len < SYSEX_SIZE(empty))
                    896:                return;
                    897:        if (x->type != SYSEX_TYPE_RT)
                    898:                return;
                    899:        if (x->id0 == SYSEX_CONTROL && x->id1 == SYSEX_MASTER) {
                    900:                if (len == SYSEX_SIZE(master))
                    901:                        dev_master(x->u.master.coarse);
                    902:                return;
                    903:        }
                    904:        if (x->id0 != SYSEX_MMC)
                    905:                return;
                    906:        switch (x->id1) {
                    907:        case SYSEX_MMC_STOP:
                    908:                if (len != SYSEX_SIZE(stop))
                    909:                        return;
                    910:                dev_mmcstop();
                    911:                break;
                    912:        case SYSEX_MMC_START:
                    913:                if (len != SYSEX_SIZE(start))
                    914:                        return;
                    915:                dev_mmcstart();
                    916:                break;
                    917:        case SYSEX_MMC_LOC:
                    918:                if (len != SYSEX_SIZE(loc) ||
                    919:                    x->u.loc.len != SYSEX_MMC_LOC_LEN ||
                    920:                    x->u.loc.cmd != SYSEX_MMC_LOC_CMD)
                    921:                        return;
                    922:                switch (x->u.loc.hr >> 5) {
                    923:                case MTC_FPS_24:
                    924:                        fps = 24;
                    925:                        break;
                    926:                case MTC_FPS_25:
                    927:                        fps = 25;
                    928:                        break;
                    929:                case MTC_FPS_30:
                    930:                        fps = 30;
                    931:                        break;
                    932:                default:
                    933:                        dev_mmcstop();
                    934:                        return;
                    935:                }
1.160     ratchov   936:                dev_mmcloc(x->u.loc.hr & 0x1f,
                    937:                    x->u.loc.min,
                    938:                    x->u.loc.sec,
                    939:                    x->u.loc.fr,
                    940:                    x->u.loc.cent,
                    941:                    fps);
1.146     ratchov   942:                break;
                    943:        }
1.78      ratchov   944: }
                    945:
                    946: /*
1.149     ratchov   947:  * parse the given data chunk and call imsg() for each message
1.78      ratchov   948:  */
1.146     ratchov   949: static void
                    950: midi_in(unsigned char *idata, int icount)
1.78      ratchov   951: {
1.146     ratchov   952:        int i;
                    953:        unsigned char c;
                    954:
                    955:        for (i = 0; i < icount; i++) {
                    956:                c = *idata++;
                    957:                if (c >= 0xf8) {
1.149     ratchov   958:                        /* we don't use real-time events */
1.146     ratchov   959:                } else if (c == SYSEX_END) {
                    960:                        if (dev_mst == SYSEX_START) {
                    961:                                dev_msg[dev_midx++] = c;
                    962:                                dev_imsg(dev_msg, dev_midx);
                    963:                        }
                    964:                        dev_mst = 0;
                    965:                        dev_midx = 0;
                    966:                } else if (c >= 0xf0) {
                    967:                        dev_msg[0] = c;
                    968:                        dev_mlen = common_len[c & 7];
                    969:                        dev_mst = c;
                    970:                        dev_midx = 1;
                    971:                } else if (c >= 0x80) {
                    972:                        dev_msg[0] = c;
                    973:                        dev_mlen = voice_len[(c >> 4) & 7];
                    974:                        dev_mst = c;
                    975:                        dev_midx = 1;
                    976:                } else if (dev_mst) {
                    977:                        if (dev_midx == 0 && dev_mst != SYSEX_START)
                    978:                                dev_msg[dev_midx++] = dev_mst;
                    979:                        dev_msg[dev_midx++] = c;
                    980:                        if (dev_midx == dev_mlen) {
                    981:                                dev_imsg(dev_msg, dev_midx);
                    982:                                if (dev_mst >= 0xf0)
                    983:                                        dev_mst = 0;
                    984:                                dev_midx = 0;
                    985:                        } else if (dev_midx == MIDI_MSGMAX) {
                    986:                                /* sysex too long */
                    987:                                dev_mst = 0;
                    988:                        }
                    989:                }
                    990:        }
1.78      ratchov   991: }
1.15      ratchov   992:
1.146     ratchov   993: static int
                    994: slot_list_mix(unsigned int round, unsigned int pchan, adata_t *pbuf)
1.15      ratchov   995: {
1.146     ratchov   996:        unsigned int done, n;
                    997:        struct slot *s;
1.13      uwe       998:
1.146     ratchov   999:        memset(pbuf, 0, pchan * round * sizeof(adata_t));
                   1000:        done = 0;
                   1001:        for (s = slot_list; s != NULL; s = s->next) {
                   1002:                if (s->pstate == SLOT_INIT || !(s->mode & SIO_PLAY))
                   1003:                        continue;
                   1004:                if (s->pstate == SLOT_STOP && s->buf.used < s->bpf) {
                   1005: #ifdef DEBUG
                   1006:                        if (log_level >= 3) {
                   1007:                                slot_log(s);
                   1008:                                log_puts(": drained, done\n");
                   1009:                        }
                   1010: #endif
1.148     ratchov  1011:                        slot_stop(s);
1.146     ratchov  1012:                        continue;
                   1013:                }
                   1014:                n = slot_mix_badd(s, dev_pbuf);
                   1015:                if (n > done)
                   1016:                        done = n;
                   1017:        }
                   1018:        return done;
1.15      ratchov  1019: }
1.13      uwe      1020:
1.146     ratchov  1021: static int
                   1022: slot_list_copy(unsigned int count, unsigned int rchan, adata_t *rbuf)
1.15      ratchov  1023: {
1.146     ratchov  1024:        unsigned int done;
                   1025:        struct slot *s;
1.28      ratchov  1026:
1.146     ratchov  1027:        done = 0;
                   1028:        for (s = slot_list; s != NULL; s = s->next) {
                   1029:                if (s->pstate == SLOT_INIT || !(s->mode & SIO_REC))
                   1030:                        continue;
                   1031:                slot_sub_bcopy(s, rbuf, count);
                   1032:                done = count;
                   1033:        }
                   1034:        return done;
1.15      ratchov  1035: }
1.4       millert  1036:
1.146     ratchov  1037: static void
                   1038: slot_list_iodo(void)
1.15      ratchov  1039: {
1.146     ratchov  1040:        struct slot *s;
                   1041:
                   1042:        for (s = slot_list; s != NULL; s = s->next) {
                   1043:                if (s->pstate != SLOT_RUN)
                   1044:                        continue;
1.156     ratchov  1045:                if ((s->mode & SIO_PLAY) &&
                   1046:                    (s->buf.used < s->round * s->bpf))
1.146     ratchov  1047:                        slot_fill(s);
1.156     ratchov  1048:                if ((s->mode & SIO_REC) &&
                   1049:                    (s->buf.len - s->buf.used < s->round * s->bpf))
1.146     ratchov  1050:                        slot_flush(s);
                   1051:        }
1.1       kstailey 1052: }
                   1053:
1.146     ratchov  1054: static int
                   1055: offline(void)
1.74      ratchov  1056: {
1.146     ratchov  1057:        unsigned int todo;
                   1058:        int rate, cmax;
                   1059:        struct slot *s;
                   1060:
1.173   ! ratchov  1061:        if (pledge("stdio", NULL) == -1)
        !          1062:                err(1, "pledge");
        !          1063:
1.146     ratchov  1064:        rate = cmax = 0;
                   1065:        for (s = slot_list; s != NULL; s = s->next) {
                   1066:                if (s->afile.rate > rate)
                   1067:                        rate = s->afile.rate;
                   1068:                if (s->cmax > cmax)
                   1069:                        cmax = s->cmax;
                   1070:        }
                   1071:        dev_sh = NULL;
                   1072:        dev_name = "offline";
                   1073:        dev_mode = SIO_PLAY | SIO_REC;
                   1074:        dev_rate = rate;
                   1075:        dev_bufsz = rate;
                   1076:        dev_round = rate;
                   1077:        dev_pchan = dev_rchan = cmax + 1;
                   1078:        dev_pbuf = dev_rbuf = xmalloc(sizeof(adata_t) * dev_pchan * dev_round);
                   1079:        dev_pstate = DEV_STOP;
                   1080:        for (s = slot_list; s != NULL; s = s->next)
                   1081:                slot_init(s);
                   1082:        for (s = slot_list; s != NULL; s = s->next)
                   1083:                slot_start(s, 0);
                   1084:        for (;;) {
                   1085:                todo = slot_list_mix(dev_round, dev_pchan, dev_pbuf);
                   1086:                if (todo == 0)
                   1087:                        break;
                   1088:                slot_list_copy(todo, dev_pchan, dev_pbuf);
                   1089:                slot_list_iodo();
                   1090:        }
1.169     ratchov  1091:        xfree(dev_pbuf);
1.146     ratchov  1092:        while (slot_list)
                   1093:                slot_del(slot_list);
                   1094:        return 1;
1.74      ratchov  1095: }
                   1096:
1.146     ratchov  1097: static int
                   1098: playrec_cycle(void)
1.84      ratchov  1099: {
1.146     ratchov  1100:        unsigned int n, todo;
                   1101:        unsigned char *p;
                   1102:        int pcnt, rcnt;
                   1103:
                   1104: #ifdef DEBUG
                   1105:        if (log_level >= 4) {
                   1106:                log_puts(dev_name);
                   1107:                log_puts(": cycle, prime = ");
                   1108:                log_putu(dev_prime);
                   1109:                log_puts("\n");
                   1110:        }
                   1111: #endif
                   1112:        pcnt = rcnt = 0;
                   1113:        if (dev_mode & SIO_REC) {
                   1114:                if (dev_prime > 0)
                   1115:                        dev_prime--;
                   1116:                else {
                   1117:                        todo = dev_round * dev_rchan * sizeof(adata_t);
                   1118:                        p = (unsigned char *)dev_rbuf;
                   1119:                        while (todo > 0) {
                   1120:                                n = sio_read(dev_sh, p, todo);
                   1121:                                if (n == 0) {
                   1122:                                        log_puts(dev_name);
1.151     ratchov  1123:                                        log_puts(": failed to read "
                   1124:                                            "from device\n");
1.146     ratchov  1125:                                        return 0;
                   1126:                                }
                   1127:                                p += n;
                   1128:                                todo -= n;
                   1129:                        }
                   1130:                        rcnt = slot_list_copy(dev_round, dev_rchan, dev_rbuf);
                   1131:                }
                   1132:        }
                   1133:        if (dev_mode & SIO_PLAY) {
                   1134:                pcnt = slot_list_mix(dev_round, dev_pchan, dev_pbuf);
                   1135:                todo = sizeof(adata_t) * dev_pchan * dev_round;
                   1136:                n = sio_write(dev_sh, dev_pbuf, todo);
                   1137:                if (n == 0) {
                   1138:                        log_puts(dev_name);
                   1139:                        log_puts(": failed to write to device\n");
                   1140:                        return 0;
                   1141:                }
                   1142:        }
                   1143:        slot_list_iodo();
                   1144:        return pcnt > 0 || rcnt > 0;
1.84      ratchov  1145: }
                   1146:
1.146     ratchov  1147: static void
                   1148: sigint(int s)
1.22      ratchov  1149: {
1.146     ratchov  1150:        if (quit_flag)
                   1151:                _exit(1);
                   1152:        quit_flag = 1;
1.22      ratchov  1153: }
                   1154:
1.146     ratchov  1155: static int
                   1156: playrec(char *dev, int mode, int bufsz, char *port)
1.61      ratchov  1157: {
1.146     ratchov  1158: #define MIDIBUFSZ 0x100
                   1159:        unsigned char mbuf[MIDIBUFSZ];
1.61      ratchov  1160:        struct sigaction sa;
1.146     ratchov  1161:        struct pollfd *pfds;
                   1162:        struct slot *s;
                   1163:        int n, ns, nm, ev;
                   1164:
                   1165:        if (!dev_open(dev, mode, bufsz, port))
                   1166:                return 0;
1.173   ! ratchov  1167:        if (pledge("stdio audio", NULL) == -1)
        !          1168:                err(1, "pledge");
1.146     ratchov  1169:        n = sio_nfds(dev_sh);
                   1170:        if (dev_mh)
                   1171:                n += mio_nfds(dev_mh);
                   1172:        pfds = xmalloc(n * sizeof(struct pollfd));
                   1173:        for (s = slot_list; s != NULL; s = s->next)
                   1174:                slot_init(s);
                   1175:        if (dev_mh == NULL)
                   1176:                dev_mmcstart();
                   1177:        else {
                   1178:                if (log_level >= 2)
                   1179:                        log_puts("ready, waiting for mmc messages\n");
                   1180:        }
1.61      ratchov  1181:
                   1182:        quit_flag = 0;
                   1183:        sigfillset(&sa.sa_mask);
                   1184:        sa.sa_flags = SA_RESTART;
                   1185:        sa.sa_handler = sigint;
1.146     ratchov  1186:        sigaction(SIGINT, &sa, NULL);
                   1187:        sigaction(SIGTERM, &sa, NULL);
                   1188:        sigaction(SIGHUP, &sa, NULL);
                   1189:        while (!quit_flag) {
                   1190:                if (dev_pstate == DEV_START) {
                   1191:                        ev = 0;
                   1192:                        if (mode & SIO_PLAY)
                   1193:                                ev |= POLLOUT;
                   1194:                        if (mode & SIO_REC)
                   1195:                                ev |= POLLIN;
                   1196:                        ns = sio_pollfd(dev_sh, pfds, ev);
                   1197:                } else
                   1198:                        ns = 0;
                   1199:                if (dev_mh)
                   1200:                        nm = mio_pollfd(dev_mh, pfds + ns, POLLIN);
                   1201:                else
                   1202:                        nm = 0;
                   1203:                if (poll(pfds, ns + nm, -1) < 0) {
                   1204:                        if (errno == EINTR)
                   1205:                                continue;
                   1206:                        log_puts("poll failed\n");
                   1207:                        panic();
1.151     ratchov  1208:                }
1.146     ratchov  1209:                if (dev_pstate == DEV_START) {
                   1210:                        ev = sio_revents(dev_sh, pfds);
                   1211:                        if (ev & POLLHUP) {
                   1212:                                log_puts(dev);
                   1213:                                log_puts(": audio device gone, stopping\n");
                   1214:                                break;
                   1215:                        }
                   1216:                        if (ev & (POLLIN | POLLOUT)) {
                   1217:                                if (!playrec_cycle() && dev_mh == NULL)
                   1218:                                        break;
                   1219:                        }
                   1220:                }
                   1221:                if (dev_mh) {
                   1222:                        ev = mio_revents(dev_mh, pfds + ns);
                   1223:                        if (ev & POLLHUP) {
                   1224:                                log_puts(dev_port);
                   1225:                                log_puts(": midi port gone, stopping\n");
                   1226:                                break;
                   1227:                        }
                   1228:                        if (ev & POLLIN) {
                   1229:                                n = mio_read(dev_mh, mbuf, MIDIBUFSZ);
                   1230:                                midi_in(mbuf, n);
                   1231:                        }
                   1232:                }
                   1233:        }
                   1234:        sigfillset(&sa.sa_mask);
                   1235:        sa.sa_flags = SA_RESTART;
                   1236:        sa.sa_handler = SIG_DFL;
                   1237:        sigaction(SIGINT, &sa, NULL);
                   1238:        sigaction(SIGTERM, &sa, NULL);
                   1239:        sigaction(SIGHUP, &sa, NULL);
                   1240:
                   1241:        if (dev_pstate == DEV_START)
                   1242:                dev_mmcstop();
1.169     ratchov  1243:        xfree(pfds);
1.146     ratchov  1244:        dev_close();
                   1245:        while (slot_list)
                   1246:                slot_del(slot_list);
                   1247:        return 1;
                   1248: }
                   1249:
                   1250: static int
                   1251: opt_onoff(char *s, int *flag)
                   1252: {
                   1253:        if (strcmp("off", s) == 0) {
                   1254:                *flag = 0;
                   1255:                return 1;
                   1256:        }
                   1257:        if (strcmp("on", s) == 0) {
                   1258:                *flag = 1;
                   1259:                return 1;
                   1260:        }
                   1261:        log_puts(s);
                   1262:        log_puts(": on/off expected\n");
                   1263:        return 0;
                   1264: }
                   1265:
                   1266: static int
                   1267: opt_enc(char *s, struct aparams *par)
                   1268: {
                   1269:        int len;
                   1270:
                   1271:        len = aparams_strtoenc(par, s);
                   1272:        if (len == 0 || s[len] != '\0') {
                   1273:                log_puts(s);
                   1274:                log_puts(": bad encoding\n");
                   1275:                return 0;
                   1276:        }
                   1277:        return 1;
                   1278: }
                   1279:
                   1280: static int
                   1281: opt_hdr(char *s, int *hdr)
                   1282: {
                   1283:        if (strcmp("auto", s) == 0) {
                   1284:                *hdr = AFILE_HDR_AUTO;
                   1285:                return 1;
                   1286:        }
                   1287:        if (strcmp("raw", s) == 0) {
                   1288:                *hdr = AFILE_HDR_RAW;
                   1289:                return 1;
                   1290:        }
                   1291:        if (strcmp("wav", s) == 0) {
                   1292:                *hdr = AFILE_HDR_WAV;
                   1293:                return 1;
                   1294:        }
                   1295:        if (strcmp("aiff", s) == 0) {
                   1296:                *hdr = AFILE_HDR_AIFF;
                   1297:                return 1;
                   1298:        }
                   1299:        if (strcmp("au", s) == 0) {
                   1300:                *hdr = AFILE_HDR_AU;
                   1301:                return 1;
                   1302:        }
                   1303:        log_puts(s);
                   1304:        log_puts(": bad header type\n");
                   1305:        return 0;
                   1306: }
                   1307:
                   1308: static int
                   1309: opt_ch(char *s, int *rcmin, int *rcmax)
                   1310: {
                   1311:        char *next, *end;
                   1312:        long cmin, cmax;
                   1313:
                   1314:        errno = 0;
                   1315:        cmin = strtol(s, &next, 10);
                   1316:        if (next == s || *next != ':')
                   1317:                goto failed;
                   1318:        cmax = strtol(++next, &end, 10);
                   1319:        if (end == next || *end != '\0')
                   1320:                goto failed;
                   1321:        if (cmin < 0 || cmax < cmin || cmax >= NCHAN_MAX)
                   1322:                goto failed;
                   1323:        *rcmin = cmin;
                   1324:        *rcmax = cmax;
                   1325:        return 1;
                   1326: failed:
                   1327:        log_puts(s);
                   1328:        log_puts(": channel range expected\n");
                   1329:        return 0;
1.61      ratchov  1330: }
                   1331:
1.146     ratchov  1332: static int
                   1333: opt_num(char *s, int min, int max, int *num)
1.61      ratchov  1334: {
1.146     ratchov  1335:        const char *errstr;
1.61      ratchov  1336:
1.146     ratchov  1337:        *num = strtonum(s, min, max, &errstr);
                   1338:        if (errstr) {
                   1339:                log_puts(s);
                   1340:                log_puts(": expected integer between ");
                   1341:                log_puti(min);
                   1342:                log_puts(" and ");
                   1343:                log_puti(max);
                   1344:                log_puts("\n");
                   1345:                return 0;
                   1346:        }
                   1347:        return 1;
1.120     ratchov  1348: }
                   1349:
1.164     ratchov  1350: static int
                   1351: opt_pos(char *s, long long *pos)
                   1352: {
                   1353:        const char *errstr;
                   1354:
                   1355:        *pos = strtonum(s, 0, LLONG_MAX, &errstr);
                   1356:        if (errstr) {
                   1357:                log_puts(s);
                   1358:                log_puts(": positive number of samples expected\n");
                   1359:                return 0;
                   1360:        }
                   1361:        return 1;
                   1362: }
                   1363:
1.1       kstailey 1364: int
1.120     ratchov  1365: main(int argc, char **argv)
1.1       kstailey 1366: {
1.146     ratchov  1367:        int dup, cmin, cmax, rate, vol, bufsz, hdr, mode;
                   1368:        char *port, *dev;
                   1369:        struct aparams par;
                   1370:        int n_flag, c;
1.164     ratchov  1371:        long long pos;
1.173   ! ratchov  1372:
        !          1373:        if (pledge("stdio rpath wpath cpath inet unix dns audio", NULL) == -1)
        !          1374:                err(1, "pledge");
1.146     ratchov  1375:
                   1376:        vol = 127;
                   1377:        dup = 0;
1.120     ratchov  1378:        bufsz = 0;
1.146     ratchov  1379:        rate = DEFAULT_RATE;
                   1380:        cmin = 0;
                   1381:        cmax = 1;
                   1382:        aparams_init(&par);
                   1383:        hdr = AFILE_HDR_AUTO;
                   1384:        n_flag = 0;
                   1385:        port = NULL;
                   1386:        dev = NULL;
                   1387:        mode = 0;
1.164     ratchov  1388:        pos = 0;
1.151     ratchov  1389:
1.164     ratchov  1390:        while ((c = getopt(argc, argv,
                   1391:                "b:c:de:f:g:h:i:j:no:p:q:r:t:v:")) != -1) {
1.15      ratchov  1392:                switch (c) {
1.146     ratchov  1393:                case 'b':
                   1394:                        if (!opt_num(optarg, 1, RATE_MAX, &bufsz))
                   1395:                                return 1;
1.74      ratchov  1396:                        break;
1.15      ratchov  1397:                case 'c':
1.146     ratchov  1398:                        if (!opt_ch(optarg, &cmin, &cmax))
                   1399:                                return 1;
1.15      ratchov  1400:                        break;
1.146     ratchov  1401:                case 'd':
                   1402:                        log_level++;
1.15      ratchov  1403:                        break;
                   1404:                case 'e':
1.146     ratchov  1405:                        if (!opt_enc(optarg, &par))
                   1406:                                return 1;
1.15      ratchov  1407:                        break;
1.146     ratchov  1408:                case 'f':
                   1409:                        dev = optarg;
1.15      ratchov  1410:                        break;
1.164     ratchov  1411:                case 'g':
                   1412:                        if (!opt_pos(optarg, &dev_pos))
                   1413:                                return 1;
                   1414:                        break;
1.146     ratchov  1415:                case 'h':
                   1416:                        if (!opt_hdr(optarg, &hdr))
                   1417:                                return 1;
1.35      ratchov  1418:                        break;
1.15      ratchov  1419:                case 'i':
1.146     ratchov  1420:                        if (!slot_new(optarg, SIO_PLAY,
1.164     ratchov  1421:                                &par, hdr, cmin, cmax, rate, dup, vol, pos))
1.146     ratchov  1422:                                return 1;
                   1423:                        mode |= SIO_PLAY;
1.15      ratchov  1424:                        break;
1.146     ratchov  1425:                case 'j':
                   1426:                        if (!opt_onoff(optarg, &dup))
                   1427:                                return 1;
1.92      ratchov  1428:                        break;
1.146     ratchov  1429:                case 'n':
                   1430:                        n_flag = 1;
1.4       millert  1431:                        break;
1.146     ratchov  1432:                case 'o':
                   1433:                        if (!slot_new(optarg, SIO_REC,
1.164     ratchov  1434:                                &par, hdr, cmin, cmax, rate, dup, 0, pos))
1.146     ratchov  1435:                                return 1;
                   1436:                        mode |= SIO_REC;
1.164     ratchov  1437:                        break;
                   1438:                case 'p':
                   1439:                        if (!opt_pos(optarg, &pos))
                   1440:                                return 1;
1.74      ratchov  1441:                        break;
1.146     ratchov  1442:                case 'q':
                   1443:                        port = optarg;
1.120     ratchov  1444:                        break;
1.146     ratchov  1445:                case 'r':
                   1446:                        if (!opt_num(optarg, RATE_MIN, RATE_MAX, &rate))
                   1447:                                return 1;
1.120     ratchov  1448:                        break;
1.146     ratchov  1449:                case 'v':
                   1450:                        if (!opt_num(optarg, 0, MIDI_MAXCTL, &vol))
                   1451:                                return 1;
1.92      ratchov  1452:                        break;
1.11      jaredy   1453:                default:
1.146     ratchov  1454:                        goto bad_usage;
1.4       millert  1455:                }
                   1456:        }
                   1457:        argc -= optind;
                   1458:        argv += optind;
1.146     ratchov  1459:        if (argc != 0) {
                   1460:        bad_usage:
                   1461:                log_puts(usagestr);
                   1462:                return 1;
                   1463:        }
                   1464:        if (n_flag) {
                   1465:                if (dev != NULL || port != NULL) {
                   1466:                        log_puts("-f and -q make no sense in off-line mode\n");
                   1467:                        return 1;
1.125     ratchov  1468:                }
1.146     ratchov  1469:                if (mode != (SIO_PLAY | SIO_REC)) {
                   1470:                        log_puts("both -i and -o required\n");
1.161     ratchov  1471:                        return 1;
1.146     ratchov  1472:                }
                   1473:                if (!offline())
                   1474:                        return 1;
1.125     ratchov  1475:        } else {
1.146     ratchov  1476:                if (dev == NULL)
                   1477:                        dev = SIO_DEVANY;
                   1478:                if (mode == 0) {
1.149     ratchov  1479:                        log_puts("at least -i or -o required\n");
1.146     ratchov  1480:                        return 1;
1.151     ratchov  1481:                }
1.146     ratchov  1482:                if (!playrec(dev, mode, bufsz, port))
                   1483:                        return 1;
1.120     ratchov  1484:        }
1.61      ratchov  1485:        return 0;
1.1       kstailey 1486: }