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