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