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

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