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