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