Annotation of src/usr.bin/aucat/aucat.c, Revision 1.158
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);
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
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;
1.156 ratchov 978: if ((s->mode & SIO_PLAY) &&
979: (s->buf.used < s->round * s->bpf))
1.146 ratchov 980: slot_fill(s);
1.156 ratchov 981: if ((s->mode & SIO_REC) &&
982: (s->buf.len - s->buf.used < s->round * s->bpf))
1.146 ratchov 983: slot_flush(s);
984: }
1.1 kstailey 985: }
986:
1.146 ratchov 987: static int
988: offline(void)
1.74 ratchov 989: {
1.146 ratchov 990: unsigned int todo;
991: int rate, cmax;
992: struct slot *s;
993:
994: rate = cmax = 0;
995: for (s = slot_list; s != NULL; s = s->next) {
996: if (s->afile.rate > rate)
997: rate = s->afile.rate;
998: if (s->cmax > cmax)
999: cmax = s->cmax;
1000: }
1001: dev_sh = NULL;
1002: dev_name = "offline";
1003: dev_mode = SIO_PLAY | SIO_REC;
1004: dev_rate = rate;
1005: dev_bufsz = rate;
1006: dev_round = rate;
1007: dev_pchan = dev_rchan = cmax + 1;
1008: dev_pbuf = dev_rbuf = xmalloc(sizeof(adata_t) * dev_pchan * dev_round);
1009: dev_pstate = DEV_STOP;
1010: dev_mmcpos = 0;
1011: for (s = slot_list; s != NULL; s = s->next)
1012: slot_init(s);
1013: for (s = slot_list; s != NULL; s = s->next)
1014: slot_start(s, 0);
1015: for (;;) {
1016: todo = slot_list_mix(dev_round, dev_pchan, dev_pbuf);
1017: if (todo == 0)
1018: break;
1019: slot_list_copy(todo, dev_pchan, dev_pbuf);
1020: slot_list_iodo();
1021: }
1.150 mmcc 1022: free(dev_pbuf);
1.146 ratchov 1023: while (slot_list)
1024: slot_del(slot_list);
1025: return 1;
1.74 ratchov 1026: }
1027:
1.146 ratchov 1028: static int
1029: playrec_cycle(void)
1.84 ratchov 1030: {
1.146 ratchov 1031: unsigned int n, todo;
1032: unsigned char *p;
1033: int pcnt, rcnt;
1034:
1035: #ifdef DEBUG
1036: if (log_level >= 4) {
1037: log_puts(dev_name);
1038: log_puts(": cycle, prime = ");
1039: log_putu(dev_prime);
1040: log_puts("\n");
1041: }
1042: #endif
1043: pcnt = rcnt = 0;
1044: if (dev_mode & SIO_REC) {
1045: if (dev_prime > 0)
1046: dev_prime--;
1047: else {
1048: todo = dev_round * dev_rchan * sizeof(adata_t);
1049: p = (unsigned char *)dev_rbuf;
1050: while (todo > 0) {
1051: n = sio_read(dev_sh, p, todo);
1052: if (n == 0) {
1053: log_puts(dev_name);
1.151 ratchov 1054: log_puts(": failed to read "
1055: "from device\n");
1.146 ratchov 1056: return 0;
1057: }
1058: p += n;
1059: todo -= n;
1060: }
1061: rcnt = slot_list_copy(dev_round, dev_rchan, dev_rbuf);
1062: }
1063: }
1064: if (dev_mode & SIO_PLAY) {
1065: pcnt = slot_list_mix(dev_round, dev_pchan, dev_pbuf);
1066: todo = sizeof(adata_t) * dev_pchan * dev_round;
1067: n = sio_write(dev_sh, dev_pbuf, todo);
1068: if (n == 0) {
1069: log_puts(dev_name);
1070: log_puts(": failed to write to device\n");
1071: return 0;
1072: }
1073: }
1074: slot_list_iodo();
1075: return pcnt > 0 || rcnt > 0;
1.84 ratchov 1076: }
1077:
1.146 ratchov 1078: static void
1079: sigint(int s)
1.22 ratchov 1080: {
1.146 ratchov 1081: if (quit_flag)
1082: _exit(1);
1083: quit_flag = 1;
1.22 ratchov 1084: }
1085:
1.146 ratchov 1086: static int
1087: playrec(char *dev, int mode, int bufsz, char *port)
1.61 ratchov 1088: {
1.146 ratchov 1089: #define MIDIBUFSZ 0x100
1090: unsigned char mbuf[MIDIBUFSZ];
1.61 ratchov 1091: struct sigaction sa;
1.146 ratchov 1092: struct pollfd *pfds;
1093: struct slot *s;
1094: int n, ns, nm, ev;
1095:
1096: if (!dev_open(dev, mode, bufsz, port))
1097: return 0;
1098: n = sio_nfds(dev_sh);
1099: if (dev_mh)
1100: n += mio_nfds(dev_mh);
1101: pfds = xmalloc(n * sizeof(struct pollfd));
1102: for (s = slot_list; s != NULL; s = s->next)
1103: slot_init(s);
1104: if (dev_mh == NULL)
1105: dev_mmcstart();
1106: else {
1107: if (log_level >= 2)
1108: log_puts("ready, waiting for mmc messages\n");
1109: }
1.61 ratchov 1110:
1111: quit_flag = 0;
1112: sigfillset(&sa.sa_mask);
1113: sa.sa_flags = SA_RESTART;
1114: sa.sa_handler = sigint;
1.146 ratchov 1115: sigaction(SIGINT, &sa, NULL);
1116: sigaction(SIGTERM, &sa, NULL);
1117: sigaction(SIGHUP, &sa, NULL);
1118: while (!quit_flag) {
1119: if (dev_pstate == DEV_START) {
1120: ev = 0;
1121: if (mode & SIO_PLAY)
1122: ev |= POLLOUT;
1123: if (mode & SIO_REC)
1124: ev |= POLLIN;
1125: ns = sio_pollfd(dev_sh, pfds, ev);
1126: } else
1127: ns = 0;
1128: if (dev_mh)
1129: nm = mio_pollfd(dev_mh, pfds + ns, POLLIN);
1130: else
1131: nm = 0;
1132: if (poll(pfds, ns + nm, -1) < 0) {
1133: if (errno == EINTR)
1134: continue;
1135: log_puts("poll failed\n");
1136: panic();
1.151 ratchov 1137: }
1.146 ratchov 1138: if (dev_pstate == DEV_START) {
1139: ev = sio_revents(dev_sh, pfds);
1140: if (ev & POLLHUP) {
1141: log_puts(dev);
1142: log_puts(": audio device gone, stopping\n");
1143: break;
1144: }
1145: if (ev & (POLLIN | POLLOUT)) {
1146: if (!playrec_cycle() && dev_mh == NULL)
1147: break;
1148: }
1149: }
1150: if (dev_mh) {
1151: ev = mio_revents(dev_mh, pfds + ns);
1152: if (ev & POLLHUP) {
1153: log_puts(dev_port);
1154: log_puts(": midi port gone, stopping\n");
1155: break;
1156: }
1157: if (ev & POLLIN) {
1158: n = mio_read(dev_mh, mbuf, MIDIBUFSZ);
1159: midi_in(mbuf, n);
1160: }
1161: }
1162: }
1163: sigfillset(&sa.sa_mask);
1164: sa.sa_flags = SA_RESTART;
1165: sa.sa_handler = SIG_DFL;
1166: sigaction(SIGINT, &sa, NULL);
1167: sigaction(SIGTERM, &sa, NULL);
1168: sigaction(SIGHUP, &sa, NULL);
1169:
1170: if (dev_pstate == DEV_START)
1171: dev_mmcstop();
1.150 mmcc 1172: free(pfds);
1.146 ratchov 1173: dev_close();
1174: while (slot_list)
1175: slot_del(slot_list);
1176: return 1;
1177: }
1178:
1179: static int
1180: opt_onoff(char *s, int *flag)
1181: {
1182: if (strcmp("off", s) == 0) {
1183: *flag = 0;
1184: return 1;
1185: }
1186: if (strcmp("on", s) == 0) {
1187: *flag = 1;
1188: return 1;
1189: }
1190: log_puts(s);
1191: log_puts(": on/off expected\n");
1192: return 0;
1193: }
1194:
1195: static int
1196: opt_enc(char *s, struct aparams *par)
1197: {
1198: int len;
1199:
1200: len = aparams_strtoenc(par, s);
1201: if (len == 0 || s[len] != '\0') {
1202: log_puts(s);
1203: log_puts(": bad encoding\n");
1204: return 0;
1205: }
1206: return 1;
1207: }
1208:
1209: static int
1210: opt_hdr(char *s, int *hdr)
1211: {
1212: if (strcmp("auto", s) == 0) {
1213: *hdr = AFILE_HDR_AUTO;
1214: return 1;
1215: }
1216: if (strcmp("raw", s) == 0) {
1217: *hdr = AFILE_HDR_RAW;
1218: return 1;
1219: }
1220: if (strcmp("wav", s) == 0) {
1221: *hdr = AFILE_HDR_WAV;
1222: return 1;
1223: }
1224: if (strcmp("aiff", s) == 0) {
1225: *hdr = AFILE_HDR_AIFF;
1226: return 1;
1227: }
1228: if (strcmp("au", s) == 0) {
1229: *hdr = AFILE_HDR_AU;
1230: return 1;
1231: }
1232: log_puts(s);
1233: log_puts(": bad header type\n");
1234: return 0;
1235: }
1236:
1237: static int
1238: opt_ch(char *s, int *rcmin, int *rcmax)
1239: {
1240: char *next, *end;
1241: long cmin, cmax;
1242:
1243: errno = 0;
1244: cmin = strtol(s, &next, 10);
1245: if (next == s || *next != ':')
1246: goto failed;
1247: cmax = strtol(++next, &end, 10);
1248: if (end == next || *end != '\0')
1249: goto failed;
1250: if (cmin < 0 || cmax < cmin || cmax >= NCHAN_MAX)
1251: goto failed;
1252: *rcmin = cmin;
1253: *rcmax = cmax;
1254: return 1;
1255: failed:
1256: log_puts(s);
1257: log_puts(": channel range expected\n");
1258: return 0;
1.61 ratchov 1259: }
1260:
1.146 ratchov 1261: static int
1262: opt_num(char *s, int min, int max, int *num)
1.61 ratchov 1263: {
1.146 ratchov 1264: const char *errstr;
1.61 ratchov 1265:
1.146 ratchov 1266: *num = strtonum(s, min, max, &errstr);
1267: if (errstr) {
1268: log_puts(s);
1269: log_puts(": expected integer between ");
1270: log_puti(min);
1271: log_puts(" and ");
1272: log_puti(max);
1273: log_puts("\n");
1274: return 0;
1275: }
1276: return 1;
1.120 ratchov 1277: }
1278:
1.1 kstailey 1279: int
1.120 ratchov 1280: main(int argc, char **argv)
1.1 kstailey 1281: {
1.146 ratchov 1282: int dup, cmin, cmax, rate, vol, bufsz, hdr, mode;
1283: char *port, *dev;
1284: struct aparams par;
1285: int n_flag, c;
1286:
1287: vol = 127;
1288: dup = 0;
1.120 ratchov 1289: bufsz = 0;
1.146 ratchov 1290: rate = DEFAULT_RATE;
1291: cmin = 0;
1292: cmax = 1;
1293: aparams_init(&par);
1294: hdr = AFILE_HDR_AUTO;
1295: n_flag = 0;
1296: port = NULL;
1297: dev = NULL;
1298: mode = 0;
1.151 ratchov 1299:
1.146 ratchov 1300: while ((c = getopt(argc, argv, "b:c:de:f:h:i:j:no:q:r:t:v:")) != -1) {
1.15 ratchov 1301: switch (c) {
1.146 ratchov 1302: case 'b':
1303: if (!opt_num(optarg, 1, RATE_MAX, &bufsz))
1304: return 1;
1.74 ratchov 1305: break;
1.15 ratchov 1306: case 'c':
1.146 ratchov 1307: if (!opt_ch(optarg, &cmin, &cmax))
1308: return 1;
1.15 ratchov 1309: break;
1.146 ratchov 1310: case 'd':
1311: log_level++;
1.15 ratchov 1312: break;
1313: case 'e':
1.146 ratchov 1314: if (!opt_enc(optarg, &par))
1315: return 1;
1.15 ratchov 1316: break;
1.146 ratchov 1317: case 'f':
1318: dev = optarg;
1.15 ratchov 1319: break;
1.146 ratchov 1320: case 'h':
1321: if (!opt_hdr(optarg, &hdr))
1322: return 1;
1.35 ratchov 1323: break;
1.15 ratchov 1324: case 'i':
1.146 ratchov 1325: if (!slot_new(optarg, SIO_PLAY,
1326: &par, hdr, cmin, cmax, rate, dup, vol))
1327: return 1;
1328: mode |= SIO_PLAY;
1.15 ratchov 1329: break;
1.146 ratchov 1330: case 'j':
1331: if (!opt_onoff(optarg, &dup))
1332: return 1;
1.92 ratchov 1333: break;
1.146 ratchov 1334: case 'n':
1335: n_flag = 1;
1.4 millert 1336: break;
1.146 ratchov 1337: case 'o':
1338: if (!slot_new(optarg, SIO_REC,
1339: &par, hdr, cmin, cmax, rate, dup, 0))
1340: return 1;
1341: mode |= SIO_REC;
1.74 ratchov 1342: break;
1.146 ratchov 1343: case 'q':
1344: port = optarg;
1.120 ratchov 1345: break;
1.146 ratchov 1346: case 'r':
1347: if (!opt_num(optarg, RATE_MIN, RATE_MAX, &rate))
1348: return 1;
1.120 ratchov 1349: break;
1.146 ratchov 1350: case 'v':
1351: if (!opt_num(optarg, 0, MIDI_MAXCTL, &vol))
1352: return 1;
1.92 ratchov 1353: break;
1.11 jaredy 1354: default:
1.146 ratchov 1355: goto bad_usage;
1.4 millert 1356: }
1357: }
1358: argc -= optind;
1359: argv += optind;
1.146 ratchov 1360: if (argc != 0) {
1361: bad_usage:
1362: log_puts(usagestr);
1363: return 1;
1364: }
1365: if (n_flag) {
1366: if (dev != NULL || port != NULL) {
1367: log_puts("-f and -q make no sense in off-line mode\n");
1368: return 1;
1.125 ratchov 1369: }
1.146 ratchov 1370: if (mode != (SIO_PLAY | SIO_REC)) {
1371: log_puts("both -i and -o required\n");
1372: return 0;
1373: }
1374: if (!offline())
1375: return 1;
1.125 ratchov 1376: } else {
1.146 ratchov 1377: if (dev == NULL)
1378: dev = SIO_DEVANY;
1379: if (mode == 0) {
1.149 ratchov 1380: log_puts("at least -i or -o required\n");
1.146 ratchov 1381: return 1;
1.151 ratchov 1382: }
1.146 ratchov 1383: if (!playrec(dev, mode, bufsz, port))
1384: return 1;
1.120 ratchov 1385: }
1.61 ratchov 1386: return 0;
1.1 kstailey 1387: }