Annotation of src/usr.bin/sndiod/dev.c, Revision 1.85
1.85 ! ratchov 1: /* $OpenBSD: dev.c,v 1.84 2021/01/29 10:51:24 ratchov Exp $ */
1.1 ratchov 2: /*
3: * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <stdio.h>
18: #include <string.h>
19:
20: #include "abuf.h"
21: #include "defs.h"
22: #include "dev.h"
23: #include "dsp.h"
24: #include "siofile.h"
25: #include "midi.h"
1.36 ratchov 26: #include "opt.h"
1.1 ratchov 27: #include "sysex.h"
28: #include "utils.h"
29:
1.14 ratchov 30: void zomb_onmove(void *);
1.25 ratchov 31: void zomb_onvol(void *);
1.1 ratchov 32: void zomb_fill(void *);
33: void zomb_flush(void *);
34: void zomb_eof(void *);
35: void zomb_exit(void *);
36:
1.7 ratchov 37: void dev_log(struct dev *);
38: void dev_midi_qfr(struct dev *, int);
39: void dev_midi_full(struct dev *);
40: void dev_midi_vol(struct dev *, struct slot *);
41: void dev_midi_master(struct dev *);
42: void dev_midi_slotdesc(struct dev *, struct slot *);
43: void dev_midi_dump(struct dev *);
1.1 ratchov 44: void dev_midi_imsg(void *, unsigned char *, int);
45: void dev_midi_omsg(void *, unsigned char *, int);
46: void dev_midi_fill(void *, int);
47: void dev_midi_exit(void *);
1.7 ratchov 48:
49: void dev_mix_badd(struct dev *, struct slot *);
50: void dev_mix_adjvol(struct dev *);
51: void dev_sub_bcopy(struct dev *, struct slot *);
52:
53: void dev_onmove(struct dev *, int);
54: void dev_master(struct dev *, unsigned int);
55: void dev_cycle(struct dev *);
56: int dev_getpos(struct dev *);
57: struct dev *dev_new(char *, struct aparams *, unsigned int, unsigned int,
58: unsigned int, unsigned int, unsigned int, unsigned int);
1.25 ratchov 59: void dev_adjpar(struct dev *, int, int, int);
1.59 ratchov 60: int dev_allocbufs(struct dev *);
1.7 ratchov 61: int dev_open(struct dev *);
1.59 ratchov 62: void dev_freebufs(struct dev *);
1.7 ratchov 63: void dev_close(struct dev *);
64: int dev_ref(struct dev *);
65: void dev_unref(struct dev *);
66: int dev_init(struct dev *);
67: void dev_done(struct dev *);
68: struct dev *dev_bynum(int);
69: void dev_del(struct dev *);
1.75 ratchov 70: void dev_setalt(struct dev *, unsigned int);
1.7 ratchov 71: unsigned int dev_roundof(struct dev *, unsigned int);
72: void dev_wakeup(struct dev *);
73: void dev_sync_attach(struct dev *);
74: void dev_mmcstart(struct dev *);
75: void dev_mmcstop(struct dev *);
76: void dev_mmcloc(struct dev *, unsigned int);
77:
1.64 ratchov 78: void slot_ctlname(struct slot *, char *, size_t);
1.7 ratchov 79: void slot_log(struct slot *);
80: void slot_del(struct slot *);
81: void slot_setvol(struct slot *, unsigned int);
82: void slot_ready(struct slot *);
1.35 ratchov 83: void slot_allocbufs(struct slot *);
84: void slot_freebufs(struct slot *);
1.12 ratchov 85: void slot_skip_update(struct slot *);
1.7 ratchov 86: void slot_write(struct slot *);
87: void slot_read(struct slot *);
1.12 ratchov 88: int slot_skip(struct slot *);
1.1 ratchov 89:
1.64 ratchov 90: void ctl_node_log(struct ctl_node *);
91: void ctl_log(struct ctl *);
92:
1.1 ratchov 93: struct midiops dev_midiops = {
94: dev_midi_imsg,
95: dev_midi_omsg,
96: dev_midi_fill,
97: dev_midi_exit
98: };
99:
100: struct slotops zomb_slotops = {
101: zomb_onmove,
102: zomb_onvol,
103: zomb_fill,
104: zomb_flush,
105: zomb_eof,
106: zomb_exit
107: };
108:
109: struct dev *dev_list = NULL;
110: unsigned int dev_sndnum = 0;
111:
1.85 ! ratchov 112: struct ctlslot ctlslot_array[DEV_NCTLSLOT];
1.84 ratchov 113: struct slot slot_array[DEV_NSLOT];
114: unsigned int slot_serial; /* for slot allocation */
115:
116: void
117: slot_array_init(void)
118: {
119: unsigned int i;
120:
121: for (i = 0; i < DEV_NSLOT; i++) {
122: slot_array[i].unit = i;
123: slot_array[i].ops = NULL;
124: slot_array[i].vol = MIDI_MAXCTL;
125: slot_array[i].dev = NULL;
126: slot_array[i].serial = slot_serial++;
127: memset(slot_array[i].name, 0, SLOT_NAMEMAX);
128: }
129: }
130:
1.1 ratchov 131: void
132: dev_log(struct dev *d)
133: {
1.3 ratchov 134: #ifdef DEBUG
135: static char *pstates[] = {
136: "cfg", "ini", "run"
137: };
138: #endif
1.1 ratchov 139: log_puts("snd");
140: log_putu(d->num);
1.3 ratchov 141: #ifdef DEBUG
142: if (log_level >= 3) {
143: log_puts(" pst=");
144: log_puts(pstates[d->pstate]);
145: }
146: #endif
1.1 ratchov 147: }
148:
149: void
1.64 ratchov 150: slot_ctlname(struct slot *s, char *name, size_t size)
151: {
152: snprintf(name, size, "%s%u", s->name, s->unit);
153: }
154:
155: void
1.1 ratchov 156: slot_log(struct slot *s)
1.3 ratchov 157: {
1.64 ratchov 158: char name[CTL_NAMEMAX];
1.1 ratchov 159: #ifdef DEBUG
160: static char *pstates[] = {
161: "ini", "sta", "rdy", "run", "stp", "mid"
162: };
163: #endif
1.64 ratchov 164: slot_ctlname(s, name, CTL_NAMEMAX);
165: log_puts(name);
1.1 ratchov 166: #ifdef DEBUG
167: if (log_level >= 3) {
168: log_puts(" vol=");
169: log_putu(s->vol);
170: if (s->ops) {
171: log_puts(",pst=");
172: log_puts(pstates[s->pstate]);
173: }
174: }
175: #endif
176: }
177:
178: void
1.14 ratchov 179: zomb_onmove(void *arg)
1.1 ratchov 180: {
181: }
182:
183: void
1.25 ratchov 184: zomb_onvol(void *arg)
1.1 ratchov 185: {
186: }
187:
188: void
189: zomb_fill(void *arg)
190: {
191: }
192:
193: void
194: zomb_flush(void *arg)
195: {
196: }
197:
198: void
199: zomb_eof(void *arg)
200: {
201: struct slot *s = arg;
202:
203: #ifdef DEBUG
204: if (log_level >= 3) {
205: slot_log(s);
206: log_puts(": zomb_eof\n");
207: }
208: #endif
209: s->ops = NULL;
210: }
211:
212: void
213: zomb_exit(void *arg)
214: {
215: #ifdef DEBUG
216: struct slot *s = arg;
217:
218: if (log_level >= 3) {
219: slot_log(s);
220: log_puts(": zomb_exit\n");
221: }
222: #endif
223: }
224:
225: /*
226: * send a quarter frame MTC message
227: */
228: void
229: dev_midi_qfr(struct dev *d, int delta)
230: {
231: unsigned char buf[2];
232: unsigned int data;
233: int qfrlen;
234:
235: d->mtc.delta += delta * MTC_SEC;
236: qfrlen = d->rate * (MTC_SEC / (4 * d->mtc.fps));
237: while (d->mtc.delta >= qfrlen) {
238: switch (d->mtc.qfr) {
239: case 0:
240: data = d->mtc.fr & 0xf;
241: break;
242: case 1:
243: data = d->mtc.fr >> 4;
244: break;
245: case 2:
246: data = d->mtc.sec & 0xf;
247: break;
248: case 3:
249: data = d->mtc.sec >> 4;
250: break;
251: case 4:
252: data = d->mtc.min & 0xf;
253: break;
254: case 5:
255: data = d->mtc.min >> 4;
256: break;
257: case 6:
258: data = d->mtc.hr & 0xf;
259: break;
260: case 7:
261: data = (d->mtc.hr >> 4) | (d->mtc.fps_id << 1);
262: /*
263: * tick messages are sent 2 frames ahead
264: */
265: d->mtc.fr += 2;
266: if (d->mtc.fr < d->mtc.fps)
267: break;
268: d->mtc.fr -= d->mtc.fps;
269: d->mtc.sec++;
270: if (d->mtc.sec < 60)
271: break;
272: d->mtc.sec = 0;
273: d->mtc.min++;
274: if (d->mtc.min < 60)
275: break;
276: d->mtc.min = 0;
277: d->mtc.hr++;
278: if (d->mtc.hr < 24)
279: break;
280: d->mtc.hr = 0;
281: break;
282: default:
283: /* NOTREACHED */
284: data = 0;
285: }
286: buf[0] = 0xf1;
287: buf[1] = (d->mtc.qfr << 4) | data;
288: d->mtc.qfr++;
289: d->mtc.qfr &= 7;
290: midi_send(d->midi, buf, 2);
291: d->mtc.delta -= qfrlen;
292: }
293: }
294:
295: /*
296: * send a full frame MTC message
297: */
298: void
299: dev_midi_full(struct dev *d)
300: {
301: struct sysex x;
302: unsigned int fps;
303:
304: d->mtc.delta = MTC_SEC * dev_getpos(d);
305: if (d->rate % (30 * 4 * d->round) == 0) {
306: d->mtc.fps_id = MTC_FPS_30;
307: d->mtc.fps = 30;
308: } else if (d->rate % (25 * 4 * d->round) == 0) {
309: d->mtc.fps_id = MTC_FPS_25;
310: d->mtc.fps = 25;
311: } else {
312: d->mtc.fps_id = MTC_FPS_24;
313: d->mtc.fps = 24;
314: }
315: #ifdef DEBUG
316: if (log_level >= 3) {
317: dev_log(d);
318: log_puts(": mtc full frame at ");
319: log_puti(d->mtc.delta);
320: log_puts(", ");
321: log_puti(d->mtc.fps);
322: log_puts(" fps\n");
323: }
324: #endif
325: fps = d->mtc.fps;
326: d->mtc.hr = (d->mtc.origin / (MTC_SEC * 3600)) % 24;
327: d->mtc.min = (d->mtc.origin / (MTC_SEC * 60)) % 60;
328: d->mtc.sec = (d->mtc.origin / (MTC_SEC)) % 60;
329: d->mtc.fr = (d->mtc.origin / (MTC_SEC / fps)) % fps;
330:
331: x.start = SYSEX_START;
332: x.type = SYSEX_TYPE_RT;
1.8 ratchov 333: x.dev = SYSEX_DEV_ANY;
1.1 ratchov 334: x.id0 = SYSEX_MTC;
335: x.id1 = SYSEX_MTC_FULL;
336: x.u.full.hr = d->mtc.hr | (d->mtc.fps_id << 5);
337: x.u.full.min = d->mtc.min;
338: x.u.full.sec = d->mtc.sec;
339: x.u.full.fr = d->mtc.fr;
340: x.u.full.end = SYSEX_END;
341: d->mtc.qfr = 0;
342: midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(full));
343: }
344:
345: /*
346: * send a volume change MIDI message
347: */
348: void
349: dev_midi_vol(struct dev *d, struct slot *s)
350: {
351: unsigned char msg[3];
352:
1.84 ratchov 353: msg[0] = MIDI_CTL | (s - slot_array);
1.1 ratchov 354: msg[1] = MIDI_CTL_VOL;
355: msg[2] = s->vol;
356: midi_send(d->midi, msg, 3);
357: }
358:
359: /*
360: * send a master volume MIDI message
361: */
362: void
363: dev_midi_master(struct dev *d)
364: {
1.70 ratchov 365: struct ctl *c;
366: unsigned int master, v;
1.1 ratchov 367: struct sysex x;
368:
1.70 ratchov 369: if (d->master_enabled)
370: master = d->master;
371: else {
372: master = 0;
373: for (c = d->ctl_list; c != NULL; c = c->next) {
374: if (c->type != CTL_NUM ||
375: strcmp(c->group, "") != 0 ||
376: strcmp(c->node0.name, "output") != 0 ||
377: strcmp(c->func, "level") != 0)
378: continue;
379: v = (c->curval * 127 + c->maxval / 2) / c->maxval;
380: if (master < v)
381: master = v;
382: }
383: }
384:
1.1 ratchov 385: memset(&x, 0, sizeof(struct sysex));
386: x.start = SYSEX_START;
387: x.type = SYSEX_TYPE_RT;
1.8 ratchov 388: x.dev = SYSEX_DEV_ANY;
1.1 ratchov 389: x.id0 = SYSEX_CONTROL;
390: x.id1 = SYSEX_MASTER;
391: x.u.master.fine = 0;
1.70 ratchov 392: x.u.master.coarse = master;
1.1 ratchov 393: x.u.master.end = SYSEX_END;
394: midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(master));
395: }
396:
397: /*
398: * send a sndiod-specific slot description MIDI message
399: */
400: void
401: dev_midi_slotdesc(struct dev *d, struct slot *s)
402: {
403: struct sysex x;
404:
405: memset(&x, 0, sizeof(struct sysex));
406: x.start = SYSEX_START;
407: x.type = SYSEX_TYPE_EDU;
1.8 ratchov 408: x.dev = SYSEX_DEV_ANY;
1.1 ratchov 409: x.id0 = SYSEX_AUCAT;
410: x.id1 = SYSEX_AUCAT_SLOTDESC;
1.64 ratchov 411: if (*s->name != '\0')
412: slot_ctlname(s, (char *)x.u.slotdesc.name, SYSEX_NAMELEN);
1.84 ratchov 413: x.u.slotdesc.chan = (s - slot_array);
1.1 ratchov 414: x.u.slotdesc.end = SYSEX_END;
415: midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(slotdesc));
416: }
417:
418: void
419: dev_midi_dump(struct dev *d)
420: {
421: struct sysex x;
422: struct slot *s;
423: int i;
424:
425: dev_midi_master(d);
1.84 ratchov 426: for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) {
427: if (s->dev != d)
428: continue;
1.1 ratchov 429: dev_midi_slotdesc(d, s);
430: dev_midi_vol(d, s);
431: }
432: x.start = SYSEX_START;
433: x.type = SYSEX_TYPE_EDU;
1.8 ratchov 434: x.dev = SYSEX_DEV_ANY;
1.1 ratchov 435: x.id0 = SYSEX_AUCAT;
436: x.id1 = SYSEX_AUCAT_DUMPEND;
437: x.u.dumpend.end = SYSEX_END;
438: midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(dumpend));
439: }
440:
441: void
442: dev_midi_imsg(void *arg, unsigned char *msg, int len)
443: {
444: #ifdef DEBUG
445: struct dev *d = arg;
446:
447: dev_log(d);
448: log_puts(": can't receive midi messages\n");
449: panic();
450: #endif
451: }
452:
453: void
454: dev_midi_omsg(void *arg, unsigned char *msg, int len)
455: {
456: struct dev *d = arg;
457: struct sysex *x;
458: unsigned int fps, chan;
459:
460: if ((msg[0] & MIDI_CMDMASK) == MIDI_CTL && msg[1] == MIDI_CTL_VOL) {
461: chan = msg[0] & MIDI_CHANMASK;
462: if (chan >= DEV_NSLOT)
463: return;
1.84 ratchov 464: if (slot_array[chan].dev != d)
465: return;
466: slot_setvol(slot_array + chan, msg[2]);
1.64 ratchov 467: dev_onval(d, CTLADDR_SLOT_LEVEL(chan), msg[2]);
1.1 ratchov 468: return;
469: }
470: x = (struct sysex *)msg;
471: if (x->start != SYSEX_START)
472: return;
473: if (len < SYSEX_SIZE(empty))
474: return;
475: switch (x->type) {
476: case SYSEX_TYPE_RT:
477: if (x->id0 == SYSEX_CONTROL && x->id1 == SYSEX_MASTER) {
1.64 ratchov 478: if (len == SYSEX_SIZE(master)) {
1.1 ratchov 479: dev_master(d, x->u.master.coarse);
1.70 ratchov 480: if (d->master_enabled) {
481: dev_onval(d, CTLADDR_MASTER,
482: x->u.master.coarse);
483: }
1.64 ratchov 484: }
1.1 ratchov 485: return;
486: }
487: if (x->id0 != SYSEX_MMC)
488: return;
489: switch (x->id1) {
490: case SYSEX_MMC_STOP:
491: if (len != SYSEX_SIZE(stop))
492: return;
493: if (log_level >= 2) {
494: dev_log(d);
495: log_puts(": mmc stop\n");
496: }
497: dev_mmcstop(d);
498: break;
499: case SYSEX_MMC_START:
500: if (len != SYSEX_SIZE(start))
501: return;
502: if (log_level >= 2) {
503: dev_log(d);
504: log_puts(": mmc start\n");
505: }
506: dev_mmcstart(d);
507: break;
508: case SYSEX_MMC_LOC:
509: if (len != SYSEX_SIZE(loc) ||
510: x->u.loc.len != SYSEX_MMC_LOC_LEN ||
511: x->u.loc.cmd != SYSEX_MMC_LOC_CMD)
512: return;
513: switch (x->u.loc.hr >> 5) {
514: case MTC_FPS_24:
515: fps = 24;
516: break;
517: case MTC_FPS_25:
518: fps = 25;
519: break;
520: case MTC_FPS_30:
521: fps = 30;
522: break;
523: default:
524: dev_mmcstop(d);
525: return;
526: }
527: dev_mmcloc(d,
528: (x->u.loc.hr & 0x1f) * 3600 * MTC_SEC +
529: x->u.loc.min * 60 * MTC_SEC +
530: x->u.loc.sec * MTC_SEC +
1.48 ratchov 531: x->u.loc.fr * (MTC_SEC / fps));
1.1 ratchov 532: break;
533: }
534: break;
535: case SYSEX_TYPE_EDU:
536: if (x->id0 != SYSEX_AUCAT || x->id1 != SYSEX_AUCAT_DUMPREQ)
537: return;
538: if (len != SYSEX_SIZE(dumpreq))
539: return;
540: dev_midi_dump(d);
541: break;
542: }
543: }
544:
545: void
546: dev_midi_fill(void *arg, int count)
547: {
1.2 ratchov 548: /* nothing to do */
1.1 ratchov 549: }
550:
551: void
552: dev_midi_exit(void *arg)
553: {
554: struct dev *d = arg;
555:
556: if (log_level >= 1) {
557: dev_log(d);
558: log_puts(": midi end point died\n");
559: }
560: if (d->pstate != DEV_CFG)
561: dev_close(d);
562: }
563:
1.12 ratchov 564: int
565: slot_skip(struct slot *s)
1.1 ratchov 566: {
1.12 ratchov 567: unsigned char *data = (unsigned char *)0xdeadbeef; /* please gcc */
568: int max, count;
569:
570: max = s->skip;
571: while (s->skip > 0) {
572: if (s->pstate != SLOT_STOP && (s->mode & MODE_RECMASK)) {
573: data = abuf_wgetblk(&s->sub.buf, &count);
574: if (count < s->round * s->sub.bpf)
575: break;
576: }
577: if (s->mode & MODE_PLAY) {
578: if (s->mix.buf.used < s->round * s->mix.bpf)
579: break;
580: }
1.1 ratchov 581: #ifdef DEBUG
582: if (log_level >= 4) {
583: slot_log(s);
1.12 ratchov 584: log_puts(": skipped a cycle\n");
1.1 ratchov 585: }
586: #endif
1.12 ratchov 587: if (s->pstate != SLOT_STOP && (s->mode & MODE_RECMASK)) {
588: if (s->sub.encbuf)
589: enc_sil_do(&s->sub.enc, data, s->round);
590: else
591: memset(data, 0, s->round * s->sub.bpf);
592: abuf_wcommit(&s->sub.buf, s->round * s->sub.bpf);
593: }
594: if (s->mode & MODE_PLAY) {
595: abuf_rdiscard(&s->mix.buf, s->round * s->mix.bpf);
1.1 ratchov 596: }
1.23 ratchov 597: s->skip--;
1.1 ratchov 598: }
1.12 ratchov 599: return max - s->skip;
1.1 ratchov 600: }
601:
1.32 ratchov 602: /*
603: * Mix the slot input block over the output block
604: */
605: void
606: dev_mix_badd(struct dev *d, struct slot *s)
1.1 ratchov 607: {
1.32 ratchov 608: adata_t *idata, *odata, *in;
609: int icount, i, offs, vol, nch;
610:
611: odata = DEV_PBUF(d);
612: idata = (adata_t *)abuf_rgetblk(&s->mix.buf, &icount);
613: #ifdef DEBUG
614: if (icount < s->round * s->mix.bpf) {
615: slot_log(s);
616: log_puts(": not enough data to mix (");
617: log_putu(icount);
618: log_puts("bytes)\n");
619: panic();
620: }
621: #endif
622:
623: /*
624: * Apply the following processing chain:
625: *
626: * dec -> resamp-> cmap
627: *
628: * where the first two are optional.
629: */
630:
631: in = idata;
632:
633: if (s->mix.decbuf) {
634: dec_do(&s->mix.dec, (void *)in, s->mix.decbuf, s->round);
635: in = s->mix.decbuf;
636: }
1.1 ratchov 637:
638: if (s->mix.resampbuf) {
1.32 ratchov 639: resamp_do(&s->mix.resamp, in, s->mix.resampbuf, s->round);
1.1 ratchov 640: in = s->mix.resampbuf;
1.32 ratchov 641: }
1.1 ratchov 642:
1.10 ratchov 643: nch = s->mix.cmap.nch;
1.1 ratchov 644: vol = ADATA_MUL(s->mix.weight, s->mix.vol) / s->mix.join;
1.32 ratchov 645: cmap_add(&s->mix.cmap, in, odata, vol, d->round);
1.1 ratchov 646:
647: offs = 0;
648: for (i = s->mix.join - 1; i > 0; i--) {
649: offs += nch;
1.32 ratchov 650: cmap_add(&s->mix.cmap, in + offs, odata, vol, d->round);
1.1 ratchov 651: }
1.32 ratchov 652:
1.1 ratchov 653: offs = 0;
654: for (i = s->mix.expand - 1; i > 0; i--) {
655: offs += nch;
1.32 ratchov 656: cmap_add(&s->mix.cmap, in, odata + offs, vol, d->round);
1.1 ratchov 657: }
658:
659: abuf_rdiscard(&s->mix.buf, s->round * s->mix.bpf);
660: }
661:
662: /*
663: * Normalize input levels.
664: */
665: void
666: dev_mix_adjvol(struct dev *d)
667: {
668: unsigned int n;
669: struct slot *i, *j;
1.42 ratchov 670: int jcmax, icmax, weight;
1.1 ratchov 671:
672: for (i = d->slot_list; i != NULL; i = i->next) {
673: if (!(i->mode & MODE_PLAY))
674: continue;
1.42 ratchov 675: icmax = i->opt->pmin + i->mix.nch - 1;
1.1 ratchov 676: weight = ADATA_UNIT;
677: if (d->autovol) {
678: /*
679: * count the number of inputs that have
680: * overlapping channel sets
681: */
682: n = 0;
683: for (j = d->slot_list; j != NULL; j = j->next) {
684: if (!(j->mode & MODE_PLAY))
685: continue;
1.42 ratchov 686: jcmax = j->opt->pmin + j->mix.nch - 1;
687: if (i->opt->pmin <= jcmax &&
688: icmax >= j->opt->pmin)
1.1 ratchov 689: n++;
690: }
691: weight /= n;
692: }
1.38 ratchov 693: if (weight > i->opt->maxweight)
694: weight = i->opt->maxweight;
1.70 ratchov 695: i->mix.weight = d->master_enabled ?
696: ADATA_MUL(weight, MIDI_TO_ADATA(d->master)) : weight;
1.1 ratchov 697: #ifdef DEBUG
698: if (log_level >= 3) {
699: slot_log(i);
700: log_puts(": set weight: ");
701: log_puti(i->mix.weight);
702: log_puts("/");
1.38 ratchov 703: log_puti(i->opt->maxweight);
1.1 ratchov 704: log_puts("\n");
705: }
706: #endif
707: }
708: }
709:
710: /*
711: * Copy data from slot to device
712: */
713: void
714: dev_sub_bcopy(struct dev *d, struct slot *s)
715: {
1.32 ratchov 716: adata_t *idata, *enc_out, *resamp_out, *cmap_out;
717: void *odata;
1.15 ratchov 718: int ocount, moffs;
1.1 ratchov 719:
1.32 ratchov 720: int i, vol, offs, nch;
721:
722:
1.15 ratchov 723: if (s->mode & MODE_MON) {
724: moffs = d->poffs + d->round;
725: if (moffs == d->psize)
726: moffs = 0;
727: idata = d->pbuf + moffs * d->pchan;
728: } else
729: idata = d->rbuf;
1.1 ratchov 730: odata = (adata_t *)abuf_wgetblk(&s->sub.buf, &ocount);
731: #ifdef DEBUG
732: if (ocount < s->round * s->sub.bpf) {
733: log_puts("dev_sub_bcopy: not enough space\n");
734: panic();
735: }
736: #endif
1.32 ratchov 737:
738: /*
739: * Apply the following processing chain:
740: *
741: * cmap -> resamp -> enc
742: *
743: * where the last two are optional.
744: */
745:
746: enc_out = odata;
747: resamp_out = s->sub.encbuf ? s->sub.encbuf : enc_out;
748: cmap_out = s->sub.resampbuf ? s->sub.resampbuf : resamp_out;
749:
750: nch = s->sub.cmap.nch;
751: vol = ADATA_UNIT / s->sub.join;
752: cmap_copy(&s->sub.cmap, idata, cmap_out, vol, d->round);
753:
754: offs = 0;
755: for (i = s->sub.join - 1; i > 0; i--) {
756: offs += nch;
757: cmap_add(&s->sub.cmap, idata + offs, cmap_out, vol, d->round);
758: }
759:
760: offs = 0;
761: for (i = s->sub.expand - 1; i > 0; i--) {
762: offs += nch;
763: cmap_copy(&s->sub.cmap, idata, cmap_out + offs, vol, d->round);
764: }
765:
766: if (s->sub.resampbuf) {
767: resamp_do(&s->sub.resamp,
768: s->sub.resampbuf, resamp_out, d->round);
769: }
770:
771: if (s->sub.encbuf)
772: enc_do(&s->sub.enc, s->sub.encbuf, (void *)enc_out, s->round);
773:
774: abuf_wcommit(&s->sub.buf, s->round * s->sub.bpf);
1.1 ratchov 775: }
776:
1.16 ratchov 777: /*
778: * run a one block cycle: consume one recorded block from
779: * rbuf and produce one play block in pbuf
780: */
1.1 ratchov 781: void
1.16 ratchov 782: dev_cycle(struct dev *d)
1.1 ratchov 783: {
784: struct slot *s, **ps;
1.12 ratchov 785: unsigned char *base;
786: int nsamp;
1.1 ratchov 787:
1.16 ratchov 788: /*
789: * check if the device is actually used. If it isn't,
790: * then close it
791: */
792: if (d->slot_list == NULL && d->tstate != MMC_RUN) {
793: if (log_level >= 2) {
794: dev_log(d);
795: log_puts(": device stopped\n");
796: }
797: dev_sio_stop(d);
798: d->pstate = DEV_INIT;
799: if (d->refcnt == 0)
800: dev_close(d);
801: return;
802: }
803:
804: if (d->prime > 0) {
805: #ifdef DEBUG
806: if (log_level >= 4) {
807: dev_log(d);
808: log_puts(": empty cycle, prime = ");
809: log_putu(d->prime);
810: log_puts("\n");
811: }
812: #endif
813: base = (unsigned char *)DEV_PBUF(d);
814: nsamp = d->round * d->pchan;
815: memset(base, 0, nsamp * sizeof(adata_t));
816: if (d->encbuf) {
817: enc_do(&d->enc, (unsigned char *)DEV_PBUF(d),
818: d->encbuf, d->round);
819: }
820: d->prime -= d->round;
821: return;
822: }
823:
1.12 ratchov 824: d->delta -= d->round;
1.1 ratchov 825: #ifdef DEBUG
826: if (log_level >= 4) {
827: dev_log(d);
1.16 ratchov 828: log_puts(": full cycle: delta = ");
1.12 ratchov 829: log_puti(d->delta);
830: if (d->mode & MODE_PLAY) {
831: log_puts(", poffs = ");
832: log_puti(d->poffs);
833: }
834: log_puts("\n");
1.1 ratchov 835: }
836: #endif
1.12 ratchov 837: if (d->mode & MODE_PLAY) {
838: base = (unsigned char *)DEV_PBUF(d);
839: nsamp = d->round * d->pchan;
840: memset(base, 0, nsamp * sizeof(adata_t));
841: }
1.1 ratchov 842: if ((d->mode & MODE_REC) && d->decbuf)
843: dec_do(&d->dec, d->decbuf, (unsigned char *)d->rbuf, d->round);
844: ps = &d->slot_list;
845: while ((s = *ps) != NULL) {
1.12 ratchov 846: #ifdef DEBUG
847: if (log_level >= 4) {
848: slot_log(s);
849: log_puts(": running");
850: log_puts(", skip = ");
851: log_puti(s->skip);
852: log_puts("\n");
853: }
854: #endif
855: /*
856: * skip cycles for XRUN_SYNC correction
857: */
858: slot_skip(s);
859: if (s->skip < 0) {
860: s->skip++;
1.1 ratchov 861: ps = &s->next;
862: continue;
863: }
1.12 ratchov 864:
865: #ifdef DEBUG
866: if (s->pstate == SLOT_STOP && !(s->mode & MODE_PLAY)) {
867: slot_log(s);
868: log_puts(": rec-only slots can't be drained\n");
869: panic();
870: }
871: #endif
872: /*
873: * check if stopped stream finished draining
874: */
875: if (s->pstate == SLOT_STOP &&
876: s->mix.buf.used < s->round * s->mix.bpf) {
877: /*
878: * partial blocks are zero-filled by socket
879: * layer, so s->mix.buf.used == 0 and we can
880: * destroy the buffer
881: */
1.35 ratchov 882: *ps = s->next;
1.12 ratchov 883: s->pstate = SLOT_INIT;
884: s->ops->eof(s->arg);
1.35 ratchov 885: slot_freebufs(s);
1.12 ratchov 886: dev_mix_adjvol(d);
1.35 ratchov 887: #ifdef DEBUG
888: if (log_level >= 3) {
889: slot_log(s);
890: log_puts(": drained\n");
891: }
892: #endif
1.1 ratchov 893: continue;
894: }
1.23 ratchov 895:
1.12 ratchov 896: /*
897: * check for xruns
898: */
1.23 ratchov 899: if (((s->mode & MODE_PLAY) &&
1.12 ratchov 900: s->mix.buf.used < s->round * s->mix.bpf) ||
901: ((s->mode & MODE_RECMASK) &&
902: s->sub.buf.len - s->sub.buf.used <
903: s->round * s->sub.bpf)) {
904:
905: #ifdef DEBUG
906: if (log_level >= 3) {
907: slot_log(s);
908: log_puts(": xrun, pause cycle\n");
909: }
910: #endif
1.1 ratchov 911: if (s->xrun == XRUN_IGNORE) {
912: s->delta -= s->round;
1.12 ratchov 913: ps = &s->next;
914: } else if (s->xrun == XRUN_SYNC) {
915: s->skip++;
916: ps = &s->next;
917: } else if (s->xrun == XRUN_ERROR) {
918: s->ops->exit(s->arg);
919: *ps = s->next;
920: } else {
921: #ifdef DEBUG
922: slot_log(s);
923: log_puts(": bad xrun mode\n");
924: panic();
925: #endif
926: }
927: continue;
928: }
929: if ((s->mode & MODE_RECMASK) && !(s->pstate == SLOT_STOP)) {
930: if (s->sub.prime == 0) {
931: dev_sub_bcopy(d, s);
932: s->ops->flush(s->arg);
933: } else {
1.1 ratchov 934: #ifdef DEBUG
935: if (log_level >= 3) {
936: slot_log(s);
1.12 ratchov 937: log_puts(": prime = ");
938: log_puti(s->sub.prime);
939: log_puts("\n");
1.1 ratchov 940: }
941: #endif
1.12 ratchov 942: s->sub.prime--;
1.1 ratchov 943: }
944: }
1.15 ratchov 945: if (s->mode & MODE_PLAY) {
946: dev_mix_badd(d, s);
947: if (s->pstate != SLOT_STOP)
948: s->ops->fill(s->arg);
949: }
1.1 ratchov 950: ps = &s->next;
951: }
1.12 ratchov 952: if ((d->mode & MODE_PLAY) && d->encbuf) {
953: enc_do(&d->enc, (unsigned char *)DEV_PBUF(d),
954: d->encbuf, d->round);
955: }
1.1 ratchov 956: }
957:
958: /*
959: * called at every clock tick by the device
960: */
961: void
962: dev_onmove(struct dev *d, int delta)
963: {
964: long long pos;
1.23 ratchov 965: struct slot *s, *snext;
1.12 ratchov 966:
967: d->delta += delta;
1.1 ratchov 968:
969: for (s = d->slot_list; s != NULL; s = snext) {
1.12 ratchov 970: /*
971: * s->ops->onmove() may remove the slot
972: */
1.1 ratchov 973: snext = s->next;
1.80 ratchov 974: pos = s->delta_rem +
975: (long long)s->delta * d->round +
976: (long long)delta * s->round;
977: s->delta = pos / (int)d->round;
1.1 ratchov 978: s->delta_rem = pos % d->round;
1.80 ratchov 979: if (s->delta_rem < 0) {
980: s->delta_rem += d->round;
981: s->delta--;
982: }
1.1 ratchov 983: if (s->delta >= 0)
1.14 ratchov 984: s->ops->onmove(s->arg);
1.1 ratchov 985: }
986: if (d->tstate == MMC_RUN)
987: dev_midi_qfr(d, delta);
988: }
989:
990: void
991: dev_master(struct dev *d, unsigned int master)
992: {
1.70 ratchov 993: struct ctl *c;
994: unsigned int v;
995:
1.1 ratchov 996: if (log_level >= 2) {
997: dev_log(d);
998: log_puts(": master volume set to ");
999: log_putu(master);
1000: log_puts("\n");
1001: }
1.70 ratchov 1002: if (d->master_enabled) {
1003: d->master = master;
1004: if (d->mode & MODE_PLAY)
1005: dev_mix_adjvol(d);
1006: } else {
1007: for (c = d->ctl_list; c != NULL; c = c->next) {
1008: if (c->type != CTL_NUM ||
1009: strcmp(c->group, "") != 0 ||
1010: strcmp(c->node0.name, "output") != 0 ||
1011: strcmp(c->func, "level") != 0)
1012: continue;
1013: v = (master * c->maxval + 64) / 127;
1014: dev_setctl(d, c->addr, v);
1015: }
1016: }
1.1 ratchov 1017: }
1018:
1019: /*
1020: * return the latency that a stream would have if it's attached
1021: */
1022: int
1023: dev_getpos(struct dev *d)
1024: {
1025: return (d->mode & MODE_PLAY) ? -d->bufsz : 0;
1026: }
1027:
1028: /*
1029: * Create a sndio device
1030: */
1031: struct dev *
1032: dev_new(char *path, struct aparams *par,
1033: unsigned int mode, unsigned int bufsz, unsigned int round,
1034: unsigned int rate, unsigned int hold, unsigned int autovol)
1035: {
1036: struct dev *d;
1037:
1038: if (dev_sndnum == DEV_NMAX) {
1039: if (log_level >= 1)
1040: log_puts("too many devices\n");
1041: return NULL;
1042: }
1043: d = xmalloc(sizeof(struct dev));
1.73 ratchov 1044: d->alt_list = NULL;
1045: dev_addname(d,path);
1.1 ratchov 1046: d->num = dev_sndnum++;
1.36 ratchov 1047: d->opt_list = NULL;
1.76 ratchov 1048: d->alt_num = -1;
1.1 ratchov 1049:
1050: /*
1051: * XXX: below, we allocate a midi input buffer, since we don't
1052: * receive raw midi data, so no need to allocate a input
1053: * ibuf. Possibly set imsg & fill callbacks to NULL and
1054: * use this to in midi_new() to check if buffers need to be
1055: * allocated
1056: */
1057: d->midi = midi_new(&dev_midiops, d, MODE_MIDIIN | MODE_MIDIOUT);
1058: midi_tag(d->midi, d->num);
1059: d->reqpar = *par;
1060: d->reqmode = mode;
1061: d->reqpchan = d->reqrchan = 0;
1062: d->reqbufsz = bufsz;
1063: d->reqround = round;
1064: d->reqrate = rate;
1065: d->hold = hold;
1066: d->autovol = autovol;
1067: d->refcnt = 0;
1068: d->pstate = DEV_CFG;
1069: d->slot_list = NULL;
1070: d->master = MIDI_MAXCTL;
1071: d->mtc.origin = 0;
1072: d->tstate = MMC_STOP;
1.64 ratchov 1073: d->ctl_list = NULL;
1.1 ratchov 1074: d->next = dev_list;
1075: dev_list = d;
1076: return d;
1077: }
1078:
1079: /*
1.73 ratchov 1080: * add a alternate name
1081: */
1082: int
1083: dev_addname(struct dev *d, char *name)
1084: {
1085: struct dev_alt *a;
1086:
1087: if (d->alt_list != NULL && d->alt_list->idx == DEV_NMAX - 1) {
1088: log_puts(name);
1089: log_puts(": too many alternate names\n");
1090: return 0;
1091: }
1092: a = xmalloc(sizeof(struct dev_alt));
1093: a->name = name;
1094: a->idx = (d->alt_list == NULL) ? 0 : d->alt_list->idx + 1;
1095: a->next = d->alt_list;
1096: d->alt_list = a;
1097: return 1;
1098: }
1099:
1100: /*
1.75 ratchov 1101: * set prefered alt device name
1102: */
1103: void
1104: dev_setalt(struct dev *d, unsigned int idx)
1105: {
1106: struct dev_alt **pa, *a;
1107:
1108: /* find alt with given index */
1109: for (pa = &d->alt_list; (a = *pa)->idx != idx; pa = &a->next)
1110: ;
1111:
1112: /* detach from list */
1113: *pa = a->next;
1114:
1115: /* attach at head */
1116: a->next = d->alt_list;
1117: d->alt_list = a;
1118:
1119: /* reopen device with the new alt */
1120: if (idx != d->alt_num)
1121: dev_reopen(d);
1122: }
1123:
1124: /*
1.1 ratchov 1125: * adjust device parameters and mode
1126: */
1127: void
1128: dev_adjpar(struct dev *d, int mode,
1.25 ratchov 1129: int pmax, int rmax)
1.1 ratchov 1130: {
1131: d->reqmode |= mode & MODE_AUDIOMASK;
1132: if (mode & MODE_PLAY) {
1133: if (d->reqpchan < pmax + 1)
1134: d->reqpchan = pmax + 1;
1135: }
1136: if (mode & MODE_REC) {
1137: if (d->reqrchan < rmax + 1)
1138: d->reqrchan = rmax + 1;
1139: }
1140: }
1141:
1142: /*
1143: * Open the device with the dev_reqxxx capabilities. Setup a mixer, demuxer,
1144: * monitor, midi control, and any necessary conversions.
1145: */
1146: int
1.59 ratchov 1147: dev_allocbufs(struct dev *d)
1.1 ratchov 1148: {
1149: if (d->mode & MODE_REC) {
1150: /*
1151: * Create device <-> demuxer buffer
1152: */
1153: d->rbuf = xmalloc(d->round * d->rchan * sizeof(adata_t));
1154:
1155: /*
1156: * Insert a converter, if needed.
1157: */
1158: if (!aparams_native(&d->par)) {
1159: dec_init(&d->dec, &d->par, d->rchan);
1160: d->decbuf = xmalloc(d->round * d->rchan * d->par.bps);
1161: } else
1162: d->decbuf = NULL;
1163: }
1164: if (d->mode & MODE_PLAY) {
1165: /*
1166: * Create device <-> mixer buffer
1167: */
1168: d->poffs = 0;
1.15 ratchov 1169: d->psize = d->bufsz + d->round;
1170: d->pbuf = xmalloc(d->psize * d->pchan * sizeof(adata_t));
1.1 ratchov 1171: d->mode |= MODE_MON;
1172:
1173: /*
1174: * Append a converter, if needed.
1175: */
1176: if (!aparams_native(&d->par)) {
1177: enc_init(&d->enc, &d->par, d->pchan);
1178: d->encbuf = xmalloc(d->round * d->pchan * d->par.bps);
1179: } else
1180: d->encbuf = NULL;
1181: }
1182: if (log_level >= 2) {
1183: dev_log(d);
1184: log_puts(": ");
1185: log_putu(d->rate);
1186: log_puts("Hz, ");
1187: aparams_log(&d->par);
1188: if (d->mode & MODE_PLAY) {
1189: log_puts(", play 0:");
1190: log_puti(d->pchan - 1);
1191: }
1192: if (d->mode & MODE_REC) {
1193: log_puts(", rec 0:");
1194: log_puti(d->rchan - 1);
1195: }
1196: log_puts(", ");
1197: log_putu(d->bufsz / d->round);
1198: log_puts(" blocks of ");
1199: log_putu(d->round);
1200: log_puts(" frames\n");
1201: }
1.57 ratchov 1202: return 1;
1203: }
1204:
1205: /*
1206: * Reset parameters and open the device.
1207: */
1208: int
1209: dev_open(struct dev *d)
1210: {
1.64 ratchov 1211: int i;
1212: char name[CTL_NAMEMAX];
1.75 ratchov 1213: struct dev_alt *a;
1.84 ratchov 1214: struct slot *s;
1.64 ratchov 1215:
1.70 ratchov 1216: d->master_enabled = 0;
1.57 ratchov 1217: d->mode = d->reqmode;
1218: d->round = d->reqround;
1219: d->bufsz = d->reqbufsz;
1220: d->rate = d->reqrate;
1221: d->pchan = d->reqpchan;
1222: d->rchan = d->reqrchan;
1223: d->par = d->reqpar;
1224: if (d->pchan == 0)
1225: d->pchan = 2;
1226: if (d->rchan == 0)
1227: d->rchan = 2;
1.59 ratchov 1228: if (!dev_sio_open(d)) {
1229: if (log_level >= 1) {
1230: dev_log(d);
1231: log_puts(": failed to open audio device\n");
1232: }
1233: return 0;
1234: }
1235: if (!dev_allocbufs(d))
1.57 ratchov 1236: return 0;
1.64 ratchov 1237:
1.84 ratchov 1238: for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) {
1239: if (s->dev != d || s->name[0] == 0)
1.69 ratchov 1240: continue;
1.84 ratchov 1241: slot_ctlname(s, name, CTL_NAMEMAX);
1.64 ratchov 1242: dev_addctl(d, "app", CTL_NUM,
1243: CTLADDR_SLOT_LEVEL(i),
1244: name, -1, "level",
1.84 ratchov 1245: NULL, -1, 127, s->vol);
1.64 ratchov 1246: }
1247:
1.77 ratchov 1248: /* if there are multiple alt devs, add server.device knob */
1249: if (d->alt_list->next != NULL) {
1250: for (a = d->alt_list; a != NULL; a = a->next) {
1251: snprintf(name, sizeof(name), "%d", a->idx);
1252: dev_addctl(d, "", CTL_SEL,
1253: CTLADDR_ALT_SEL + a->idx,
1254: "server", -1, "device",
1255: name, -1, 1, a->idx == d->alt_num);
1256: }
1.75 ratchov 1257: }
1258:
1.59 ratchov 1259: d->pstate = DEV_INIT;
1.1 ratchov 1260: return 1;
1261: }
1262:
1263: /*
1.72 ratchov 1264: * Force all slots to exit and close device, called after an error
1.55 ratchov 1265: */
1266: void
1.72 ratchov 1267: dev_abort(struct dev *d)
1.55 ratchov 1268: {
1269: int i;
1270: struct slot *s;
1.64 ratchov 1271: struct ctlslot *c;
1.55 ratchov 1272:
1.84 ratchov 1273: for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) {
1274: if (s->dev != d)
1275: continue;
1.55 ratchov 1276: if (s->ops)
1277: s->ops->exit(s->arg);
1278: s->ops = NULL;
1279: }
1280: d->slot_list = NULL;
1.64 ratchov 1281:
1.85 ! ratchov 1282: for (c = ctlslot_array, i = DEV_NCTLSLOT; i > 0; i--, c++) {
! 1283: if (c->dev != d)
! 1284: continue;
1.64 ratchov 1285: if (c->ops)
1286: c->ops->exit(c->arg);
1287: c->ops = NULL;
1288: }
1.83 ratchov 1289:
1290: midi_abort(d->midi);
1.72 ratchov 1291:
1292: if (d->pstate != DEV_CFG)
1293: dev_close(d);
1.55 ratchov 1294: }
1295:
1296: /*
1.1 ratchov 1297: * force the device to go in DEV_CFG state, the caller is supposed to
1298: * ensure buffers are drained
1299: */
1300: void
1.59 ratchov 1301: dev_freebufs(struct dev *d)
1.1 ratchov 1302: {
1303: #ifdef DEBUG
1304: if (log_level >= 3) {
1305: dev_log(d);
1306: log_puts(": closing\n");
1307: }
1308: #endif
1309: if (d->mode & MODE_PLAY) {
1310: if (d->encbuf != NULL)
1311: xfree(d->encbuf);
1312: xfree(d->pbuf);
1313: }
1314: if (d->mode & MODE_REC) {
1315: if (d->decbuf != NULL)
1316: xfree(d->decbuf);
1317: xfree(d->rbuf);
1318: }
1.58 ratchov 1319: }
1320:
1321: /*
1322: * Close the device and exit all slots
1323: */
1324: void
1325: dev_close(struct dev *d)
1326: {
1.64 ratchov 1327: struct ctl *c;
1328:
1.59 ratchov 1329: d->pstate = DEV_CFG;
1330: dev_sio_close(d);
1331: dev_freebufs(d);
1.64 ratchov 1332:
1333: /* there are no clients, just free remaining local controls */
1334: while ((c = d->ctl_list) != NULL) {
1335: d->ctl_list = c->next;
1336: xfree(c);
1337: }
1.1 ratchov 1338: }
1339:
1.62 ratchov 1340: /*
1341: * Close the device, but attempt to migrate everything to a new sndio
1342: * device.
1343: */
1344: int
1345: dev_reopen(struct dev *d)
1346: {
1347: struct slot *s;
1348: long long pos;
1349: unsigned int pstate;
1350: int delta;
1351:
1352: /* not opened */
1353: if (d->pstate == DEV_CFG)
1354: return 1;
1355:
1356: /* save state */
1357: delta = d->delta;
1358: pstate = d->pstate;
1359:
1360: if (!dev_sio_reopen(d))
1361: return 0;
1362:
1363: /* reopen returns a stopped device */
1364: d->pstate = DEV_INIT;
1365:
1366: /* reallocate new buffers, with new parameters */
1367: dev_freebufs(d);
1368: dev_allocbufs(d);
1369:
1370: /*
1371: * adjust time positions, make anything go back delta ticks, so
1372: * that the new device can start at zero
1373: */
1374: for (s = d->slot_list; s != NULL; s = s->next) {
1375: pos = (long long)s->delta * d->round + s->delta_rem;
1376: pos -= (long long)delta * s->round;
1377: s->delta_rem = pos % (int)d->round;
1378: s->delta = pos / (int)d->round;
1379: if (log_level >= 3) {
1380: slot_log(s);
1381: log_puts(": adjusted: delta -> ");
1382: log_puti(s->delta);
1383: log_puts(", delta_rem -> ");
1384: log_puti(s->delta_rem);
1385: log_puts("\n");
1386: }
1387:
1388: /* reinitilize the format conversion chain */
1389: slot_initconv(s);
1390: }
1391: if (d->tstate == MMC_RUN) {
1392: d->mtc.delta -= delta * MTC_SEC;
1393: if (log_level >= 2) {
1394: dev_log(d);
1395: log_puts(": adjusted mtc: delta ->");
1396: log_puti(d->mtc.delta);
1397: log_puts("\n");
1398: }
1399: }
1400:
1.71 ratchov 1401: /* remove old controls and add new ones */
1402: dev_sioctl_close(d);
1.64 ratchov 1403: dev_sioctl_open(d);
1404:
1.62 ratchov 1405: /* start the device if needed */
1406: if (pstate == DEV_RUN)
1407: dev_wakeup(d);
1408:
1409: return 1;
1410: }
1411:
1.1 ratchov 1412: int
1413: dev_ref(struct dev *d)
1414: {
1415: #ifdef DEBUG
1416: if (log_level >= 3) {
1417: dev_log(d);
1418: log_puts(": device requested\n");
1419: }
1420: #endif
1421: if (d->pstate == DEV_CFG && !dev_open(d))
1422: return 0;
1423: d->refcnt++;
1424: return 1;
1425: }
1426:
1427: void
1428: dev_unref(struct dev *d)
1429: {
1430: #ifdef DEBUG
1431: if (log_level >= 3) {
1432: dev_log(d);
1433: log_puts(": device released\n");
1434: }
1435: #endif
1436: d->refcnt--;
1437: if (d->refcnt == 0 && d->pstate == DEV_INIT)
1438: dev_close(d);
1439: }
1440:
1441: /*
1442: * initialize the device with the current parameters
1443: */
1444: int
1445: dev_init(struct dev *d)
1446: {
1447: if ((d->reqmode & MODE_AUDIOMASK) == 0) {
1448: #ifdef DEBUG
1449: dev_log(d);
1450: log_puts(": has no streams\n");
1451: #endif
1452: return 0;
1453: }
1454: if (d->hold && !dev_ref(d))
1455: return 0;
1456: return 1;
1457: }
1458:
1459: /*
1460: * Unless the device is already in process of closing, request it to close
1461: */
1462: void
1463: dev_done(struct dev *d)
1464: {
1465: #ifdef DEBUG
1466: if (log_level >= 3) {
1467: dev_log(d);
1468: log_puts(": draining\n");
1469: }
1470: #endif
1.20 ratchov 1471: if (d->tstate != MMC_STOP)
1472: dev_mmcstop(d);
1.1 ratchov 1473: if (d->hold)
1474: dev_unref(d);
1475: }
1476:
1477: struct dev *
1478: dev_bynum(int num)
1479: {
1480: struct dev *d;
1481:
1482: for (d = dev_list; d != NULL; d = d->next) {
1.19 ratchov 1483: if (d->num == num)
1.1 ratchov 1484: return d;
1485: }
1486: return NULL;
1487: }
1488:
1489: /*
1490: * Free the device
1491: */
1492: void
1493: dev_del(struct dev *d)
1494: {
1495: struct dev **p;
1.73 ratchov 1496: struct dev_alt *a;
1.1 ratchov 1497:
1498: #ifdef DEBUG
1499: if (log_level >= 3) {
1500: dev_log(d);
1501: log_puts(": deleting\n");
1502: }
1503: #endif
1.36 ratchov 1504: while (d->opt_list != NULL)
1505: opt_del(d, d->opt_list);
1.1 ratchov 1506: if (d->pstate != DEV_CFG)
1507: dev_close(d);
1508: for (p = &dev_list; *p != d; p = &(*p)->next) {
1509: #ifdef DEBUG
1510: if (*p == NULL) {
1511: dev_log(d);
1512: log_puts(": device to delete not on the list\n");
1513: panic();
1514: }
1515: #endif
1516: }
1517: midi_del(d->midi);
1518: *p = d->next;
1.73 ratchov 1519: while ((a = d->alt_list) != NULL) {
1520: d->alt_list = a->next;
1521: xfree(a);
1522: }
1.1 ratchov 1523: xfree(d);
1524: }
1525:
1526: unsigned int
1527: dev_roundof(struct dev *d, unsigned int newrate)
1528: {
1529: return (d->round * newrate + d->rate / 2) / d->rate;
1530: }
1531:
1532: /*
1533: * If the device is paused, then resume it.
1534: */
1535: void
1536: dev_wakeup(struct dev *d)
1537: {
1538: if (d->pstate == DEV_INIT) {
1539: if (log_level >= 2) {
1540: dev_log(d);
1541: log_puts(": device started\n");
1542: }
1543: if (d->mode & MODE_PLAY) {
1544: d->prime = d->bufsz;
1545: } else {
1546: d->prime = 0;
1547: }
1.16 ratchov 1548: d->poffs = 0;
1.12 ratchov 1549:
1.23 ratchov 1550: /*
1.16 ratchov 1551: * empty cycles don't increment delta, so it's ok to
1552: * start at 0
1553: **/
1.23 ratchov 1554: d->delta = 0;
1.12 ratchov 1555:
1.1 ratchov 1556: d->pstate = DEV_RUN;
1557: dev_sio_start(d);
1558: }
1559: }
1560:
1561: /*
1562: * check that all clients controlled by MMC are ready to start, if so,
1563: * attach them all at the same position
1564: */
1565: void
1566: dev_sync_attach(struct dev *d)
1567: {
1568: int i;
1569: struct slot *s;
1570:
1571: if (d->tstate != MMC_START) {
1572: if (log_level >= 2) {
1573: dev_log(d);
1574: log_puts(": not started by mmc yet, waiting...\n");
1575: }
1576: return;
1577: }
1.84 ratchov 1578:
1579: for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) {
1580: if (s->dev != d || !s->ops || !s->opt->mmc)
1.1 ratchov 1581: continue;
1.46 ratchov 1582: if (s->pstate != SLOT_READY) {
1.1 ratchov 1583: #ifdef DEBUG
1584: if (log_level >= 3) {
1585: slot_log(s);
1586: log_puts(": not ready, start delayed\n");
1587: }
1588: #endif
1589: return;
1590: }
1591: }
1592: if (!dev_ref(d))
1593: return;
1.84 ratchov 1594:
1595: for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) {
1596: if (s->dev != d || !s->ops || !s->opt->mmc)
1.1 ratchov 1597: continue;
1.46 ratchov 1598: slot_attach(s);
1.79 ratchov 1599: s->pstate = SLOT_RUN;
1.1 ratchov 1600: }
1601: d->tstate = MMC_RUN;
1602: dev_midi_full(d);
1603: dev_wakeup(d);
1604: }
1605:
1606: /*
1607: * start all slots simultaneously
1608: */
1609: void
1610: dev_mmcstart(struct dev *d)
1611: {
1612: if (d->tstate == MMC_STOP) {
1613: d->tstate = MMC_START;
1614: dev_sync_attach(d);
1615: #ifdef DEBUG
1616: } else {
1617: if (log_level >= 3) {
1618: dev_log(d);
1619: log_puts(": ignoring mmc start\n");
1620: }
1621: #endif
1622: }
1623: }
1624:
1625: /*
1626: * stop all slots simultaneously
1627: */
1628: void
1629: dev_mmcstop(struct dev *d)
1630: {
1631: switch (d->tstate) {
1632: case MMC_START:
1633: d->tstate = MMC_STOP;
1634: return;
1635: case MMC_RUN:
1636: d->tstate = MMC_STOP;
1637: dev_unref(d);
1638: break;
1639: default:
1640: #ifdef DEBUG
1641: if (log_level >= 3) {
1642: dev_log(d);
1643: log_puts(": ignored mmc stop\n");
1644: }
1645: #endif
1646: return;
1647: }
1648: }
1649:
1650: /*
1651: * relocate all slots simultaneously
1652: */
1653: void
1654: dev_mmcloc(struct dev *d, unsigned int origin)
1655: {
1656: if (log_level >= 2) {
1657: dev_log(d);
1658: log_puts(": relocated to ");
1659: log_putu(origin);
1660: log_puts("\n");
1661: }
1662: if (d->tstate == MMC_RUN)
1663: dev_mmcstop(d);
1664: d->mtc.origin = origin;
1665: if (d->tstate == MMC_RUN)
1666: dev_mmcstart(d);
1667: }
1668:
1.35 ratchov 1669: /*
1670: * allocate buffers & conversion chain
1671: */
1672: void
1.60 ratchov 1673: slot_initconv(struct slot *s)
1.35 ratchov 1674: {
1.63 ratchov 1675: unsigned int dev_nch;
1.35 ratchov 1676: struct dev *d = s->dev;
1677:
1678: if (s->mode & MODE_PLAY) {
1679: cmap_init(&s->mix.cmap,
1.42 ratchov 1680: s->opt->pmin, s->opt->pmin + s->mix.nch - 1,
1681: s->opt->pmin, s->opt->pmin + s->mix.nch - 1,
1.35 ratchov 1682: 0, d->pchan - 1,
1.40 ratchov 1683: s->opt->pmin, s->opt->pmax);
1.35 ratchov 1684: if (!aparams_native(&s->par)) {
1.42 ratchov 1685: dec_init(&s->mix.dec, &s->par, s->mix.nch);
1.35 ratchov 1686: }
1687: if (s->rate != d->rate) {
1688: resamp_init(&s->mix.resamp, s->round, d->round,
1.42 ratchov 1689: s->mix.nch);
1.35 ratchov 1690: }
1.61 ratchov 1691: s->mix.join = 1;
1692: s->mix.expand = 1;
1.63 ratchov 1693: if (s->opt->dup && s->mix.cmap.nch > 0) {
1694: dev_nch = d->pchan < (s->opt->pmax + 1) ?
1695: d->pchan - s->opt->pmin :
1696: s->opt->pmax - s->opt->pmin + 1;
1697: if (dev_nch > s->mix.nch)
1698: s->mix.expand = dev_nch / s->mix.nch;
1699: else if (s->mix.nch > dev_nch)
1700: s->mix.join = s->mix.nch / dev_nch;
1.61 ratchov 1701: }
1.35 ratchov 1702: }
1703:
1704: if (s->mode & MODE_RECMASK) {
1.63 ratchov 1705: unsigned int outchan = (s->mode & MODE_MON) ?
1706: d->pchan : d->rchan;
1707:
1.35 ratchov 1708: cmap_init(&s->sub.cmap,
1.63 ratchov 1709: 0, outchan - 1,
1.40 ratchov 1710: s->opt->rmin, s->opt->rmax,
1.42 ratchov 1711: s->opt->rmin, s->opt->rmin + s->sub.nch - 1,
1712: s->opt->rmin, s->opt->rmin + s->sub.nch - 1);
1.35 ratchov 1713: if (s->rate != d->rate) {
1714: resamp_init(&s->sub.resamp, d->round, s->round,
1.42 ratchov 1715: s->sub.nch);
1.35 ratchov 1716: }
1717: if (!aparams_native(&s->par)) {
1.42 ratchov 1718: enc_init(&s->sub.enc, &s->par, s->sub.nch);
1.61 ratchov 1719: }
1720: s->sub.join = 1;
1721: s->sub.expand = 1;
1.63 ratchov 1722: if (s->opt->dup && s->sub.cmap.nch > 0) {
1723: dev_nch = outchan < (s->opt->rmax + 1) ?
1724: outchan - s->opt->rmin :
1725: s->opt->rmax - s->opt->rmin + 1;
1726: if (dev_nch > s->sub.nch)
1727: s->sub.join = dev_nch / s->sub.nch;
1728: else if (s->sub.nch > dev_nch)
1729: s->sub.expand = s->sub.nch / dev_nch;
1.35 ratchov 1730: }
1731:
1732: /*
1733: * cmap_copy() doesn't write samples in all channels,
1734: * for instance when mono->stereo conversion is
1735: * disabled. So we have to prefill cmap_copy() output
1736: * with silence.
1737: */
1738: if (s->sub.resampbuf) {
1739: memset(s->sub.resampbuf, 0,
1.42 ratchov 1740: d->round * s->sub.nch * sizeof(adata_t));
1.35 ratchov 1741: } else if (s->sub.encbuf) {
1742: memset(s->sub.encbuf, 0,
1.42 ratchov 1743: s->round * s->sub.nch * sizeof(adata_t));
1.35 ratchov 1744: } else {
1745: memset(s->sub.buf.data, 0,
1.42 ratchov 1746: s->appbufsz * s->sub.nch * sizeof(adata_t));
1.35 ratchov 1747: }
1748: }
1.60 ratchov 1749: }
1750:
1751: /*
1752: * allocate buffers & conversion chain
1753: */
1754: void
1755: slot_allocbufs(struct slot *s)
1756: {
1757: struct dev *d = s->dev;
1758:
1759: if (s->mode & MODE_PLAY) {
1760: s->mix.bpf = s->par.bps * s->mix.nch;
1761: abuf_init(&s->mix.buf, s->appbufsz * s->mix.bpf);
1762:
1763: s->mix.decbuf = NULL;
1764: s->mix.resampbuf = NULL;
1765: if (!aparams_native(&s->par)) {
1766: s->mix.decbuf =
1767: xmalloc(s->round * s->mix.nch * sizeof(adata_t));
1768: }
1769: if (s->rate != d->rate) {
1770: s->mix.resampbuf =
1771: xmalloc(d->round * s->mix.nch * sizeof(adata_t));
1772: }
1773: }
1774:
1775: if (s->mode & MODE_RECMASK) {
1776: s->sub.bpf = s->par.bps * s->sub.nch;
1777: abuf_init(&s->sub.buf, s->appbufsz * s->sub.bpf);
1778:
1779: s->sub.encbuf = NULL;
1780: s->sub.resampbuf = NULL;
1781: if (s->rate != d->rate) {
1782: s->sub.resampbuf =
1783: xmalloc(d->round * s->sub.nch * sizeof(adata_t));
1784: }
1785: if (!aparams_native(&s->par)) {
1786: s->sub.encbuf =
1787: xmalloc(s->round * s->sub.nch * sizeof(adata_t));
1788: }
1789: }
1790:
1791: slot_initconv(s);
1.35 ratchov 1792:
1793: #ifdef DEBUG
1794: if (log_level >= 3) {
1795: slot_log(s);
1796: log_puts(": allocated ");
1797: log_putu(s->appbufsz);
1798: log_puts("/");
1799: log_putu(SLOT_BUFSZ(s));
1800: log_puts(" fr buffers\n");
1801: }
1802: #endif
1803: }
1804:
1805: /*
1806: * free buffers & conversion chain
1807: */
1808: void
1809: slot_freebufs(struct slot *s)
1810: {
1811: if (s->mode & MODE_RECMASK) {
1812: abuf_done(&s->sub.buf);
1813: if (s->sub.encbuf)
1814: xfree(s->sub.encbuf);
1815: if (s->sub.resampbuf)
1816: xfree(s->sub.resampbuf);
1817: }
1818:
1819: if (s->mode & MODE_PLAY) {
1820: abuf_done(&s->mix.buf);
1821: if (s->mix.decbuf)
1822: xfree(s->mix.decbuf);
1823: if (s->mix.resampbuf)
1824: xfree(s->mix.resampbuf);
1825: }
1826: }
1827:
1.1 ratchov 1828: /*
1829: * allocate a new slot and register the given call-backs
1830: */
1831: struct slot *
1.54 ratchov 1832: slot_new(struct dev *d, struct opt *opt, unsigned int id, char *who,
1.37 ratchov 1833: struct slotops *ops, void *arg, int mode)
1.1 ratchov 1834: {
1835: char *p;
1836: char name[SLOT_NAMEMAX];
1.52 ratchov 1837: unsigned int i, ser, bestser, bestidx;
1838: struct slot *unit[DEV_NSLOT];
1.1 ratchov 1839: struct slot *s;
1840:
1841: /*
1.27 ratchov 1842: * create a ``valid'' control name (lowcase, remove [^a-z], truncate)
1.1 ratchov 1843: */
1844: for (i = 0, p = who; ; p++) {
1845: if (i == SLOT_NAMEMAX - 1 || *p == '\0') {
1846: name[i] = '\0';
1847: break;
1848: } else if (*p >= 'A' && *p <= 'Z') {
1849: name[i++] = *p + 'a' - 'A';
1850: } else if (*p >= 'a' && *p <= 'z')
1851: name[i++] = *p;
1852: }
1853: if (i == 0)
1854: strlcpy(name, "noname", SLOT_NAMEMAX);
1855:
1856: /*
1.52 ratchov 1857: * build a unit-to-slot map for this name
1.1 ratchov 1858: */
1.52 ratchov 1859: for (i = 0; i < DEV_NSLOT; i++)
1860: unit[i] = NULL;
1.84 ratchov 1861: for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) {
1.1 ratchov 1862: if (strcmp(s->name, name) == 0)
1.52 ratchov 1863: unit[s->unit] = s;
1.1 ratchov 1864: }
1865:
1866: /*
1.54 ratchov 1867: * find the free slot with the least unit number and same id
1868: */
1869: for (i = 0; i < DEV_NSLOT; i++) {
1870: s = unit[i];
1871: if (s != NULL && s->ops == NULL && s->id == id)
1872: goto found;
1873: }
1874:
1875: /*
1.52 ratchov 1876: * find the free slot with the least unit number
1.1 ratchov 1877: */
1.52 ratchov 1878: for (i = 0; i < DEV_NSLOT; i++) {
1879: s = unit[i];
1.54 ratchov 1880: if (s != NULL && s->ops == NULL) {
1881: s->id = id;
1.1 ratchov 1882: goto found;
1.54 ratchov 1883: }
1.1 ratchov 1884: }
1885:
1886: /*
1.18 ratchov 1887: * couldn't find a matching slot, pick oldest free slot
1.1 ratchov 1888: * and set its name/unit
1889: */
1890: bestser = 0;
1891: bestidx = DEV_NSLOT;
1.84 ratchov 1892: for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) {
1.1 ratchov 1893: if (s->ops != NULL)
1894: continue;
1.84 ratchov 1895: ser = slot_serial - s->serial;
1.1 ratchov 1896: if (ser > bestser) {
1897: bestser = ser;
1898: bestidx = i;
1899: }
1900: }
1.51 ratchov 1901: if (bestidx != DEV_NSLOT) {
1.84 ratchov 1902: s = slot_array + bestidx;
1.51 ratchov 1903: s->vol = MIDI_MAXCTL;
1904: strlcpy(s->name, name, SLOT_NAMEMAX);
1.84 ratchov 1905: s->serial = slot_serial++;
1.52 ratchov 1906: for (i = 0; unit[i] != NULL; i++)
1907: ; /* nothing */
1908: s->unit = i;
1.54 ratchov 1909: s->id = id;
1.51 ratchov 1910: goto found;
1.1 ratchov 1911: }
1.53 ratchov 1912:
1.51 ratchov 1913: if (log_level >= 1) {
1.1 ratchov 1914: log_puts(name);
1.51 ratchov 1915: log_puts(": out of sub-device slots\n");
1.1 ratchov 1916: }
1.51 ratchov 1917: return NULL;
1.1 ratchov 1918:
1919: found:
1.37 ratchov 1920: if ((mode & MODE_REC) && (opt->mode & MODE_MON)) {
1921: mode |= MODE_MON;
1922: mode &= ~MODE_REC;
1923: }
1924: if ((mode & opt->mode) != mode) {
1925: if (log_level >= 1) {
1926: slot_log(s);
1927: log_puts(": requested mode not allowed\n");
1928: }
1929: return 0;
1930: }
1.1 ratchov 1931: if (!dev_ref(d))
1932: return NULL;
1.84 ratchov 1933: dev_label(d, s - slot_array);
1.31 ratchov 1934: if ((mode & d->mode) != mode) {
1.1 ratchov 1935: if (log_level >= 1) {
1936: slot_log(s);
1937: log_puts(": requested mode not supported\n");
1938: }
1.31 ratchov 1939: dev_unref(d);
1.49 ratchov 1940: return NULL;
1.1 ratchov 1941: }
1.31 ratchov 1942: s->dev = d;
1.37 ratchov 1943: s->opt = opt;
1.31 ratchov 1944: s->ops = ops;
1945: s->arg = arg;
1946: s->pstate = SLOT_INIT;
1.1 ratchov 1947: s->mode = mode;
1.6 ratchov 1948: aparams_init(&s->par);
1.41 ratchov 1949: if (s->mode & MODE_PLAY)
1.42 ratchov 1950: s->mix.nch = s->opt->pmax - s->opt->pmin + 1;
1.41 ratchov 1951: if (s->mode & MODE_RECMASK)
1.42 ratchov 1952: s->sub.nch = s->opt->rmax - s->opt->rmin + 1;
1.46 ratchov 1953: s->xrun = s->opt->mmc ? XRUN_SYNC : XRUN_IGNORE;
1.1 ratchov 1954: s->appbufsz = d->bufsz;
1955: s->round = d->round;
1.5 ratchov 1956: s->rate = d->rate;
1.1 ratchov 1957: dev_midi_slotdesc(d, s);
1958: dev_midi_vol(d, s);
1.43 ratchov 1959: #ifdef DEBUG
1960: if (log_level >= 3) {
1961: slot_log(s);
1962: log_puts(": using ");
1963: dev_log(d);
1964: log_puts(".");
1965: log_puts(opt->name);
1966: log_puts(", mode = ");
1967: log_putx(mode);
1968: log_puts("\n");
1969: }
1970: #endif
1.1 ratchov 1971: return s;
1972: }
1973:
1974: /*
1975: * release the given slot
1976: */
1977: void
1978: slot_del(struct slot *s)
1979: {
1980: s->arg = s;
1981: s->ops = &zomb_slotops;
1982: switch (s->pstate) {
1983: case SLOT_INIT:
1984: s->ops = NULL;
1985: break;
1986: case SLOT_START:
1987: case SLOT_READY:
1988: case SLOT_RUN:
1989: case SLOT_STOP:
1.82 ratchov 1990: slot_stop(s, 0);
1.1 ratchov 1991: break;
1992: }
1993: dev_unref(s->dev);
1994: s->dev = NULL;
1995: }
1996:
1997: /*
1998: * change the slot play volume; called either by the slot or by MIDI
1999: */
2000: void
2001: slot_setvol(struct slot *s, unsigned int vol)
2002: {
2003: #ifdef DEBUG
2004: if (log_level >= 3) {
2005: slot_log(s);
2006: log_puts(": setting volume ");
2007: log_putu(vol);
2008: log_puts("\n");
2009: }
2010: #endif
2011: s->vol = vol;
2012: s->mix.vol = MIDI_TO_ADATA(s->vol);
2013: }
2014:
2015: /*
2016: * attach the slot to the device (ie start playing & recording
2017: */
2018: void
2019: slot_attach(struct slot *s)
2020: {
2021: struct dev *d = s->dev;
1.12 ratchov 2022: long long pos;
1.1 ratchov 2023:
2024: /*
2025: * start the device if not started
2026: */
2027: dev_wakeup(d);
1.23 ratchov 2028:
1.1 ratchov 2029: /*
1.12 ratchov 2030: * adjust initial clock
2031: */
1.79 ratchov 2032: pos = s->delta_rem +
2033: (long long)s->delta * d->round +
2034: (long long)d->delta * s->round;
2035: s->delta = pos / (int)d->round;
1.12 ratchov 2036: s->delta_rem = pos % d->round;
1.79 ratchov 2037: if (s->delta_rem < 0) {
2038: s->delta_rem += d->round;
2039: s->delta--;
2040: }
1.12 ratchov 2041:
1.1 ratchov 2042: #ifdef DEBUG
1.17 ratchov 2043: if (log_level >= 2) {
1.1 ratchov 2044: slot_log(s);
2045: log_puts(": attached at ");
1.79 ratchov 2046: log_puti(s->delta);
2047: log_puts(" + ");
2048: log_puti(s->delta_rem);
2049: log_puts("/");
2050: log_puti(s->round);
1.1 ratchov 2051: log_puts("\n");
2052: }
2053: #endif
2054:
2055: /*
2056: * We dont check whether the device is dying,
2057: * because dev_xxx() functions are supposed to
2058: * work (i.e., not to crash)
2059: */
2060: #ifdef DEBUG
2061: if ((s->mode & d->mode) != s->mode) {
2062: slot_log(s);
1.24 ratchov 2063: log_puts(": mode beyond device mode, not attaching\n");
1.1 ratchov 2064: panic();
2065: }
2066: #endif
2067: s->next = d->slot_list;
2068: d->slot_list = s;
2069: if (s->mode & MODE_PLAY) {
2070: s->mix.vol = MIDI_TO_ADATA(s->vol);
2071: dev_mix_adjvol(d);
2072: }
2073: }
2074:
2075: /*
2076: * if MMC is enabled, and try to attach all slots synchronously, else
2077: * simply attach the slot
2078: */
2079: void
2080: slot_ready(struct slot *s)
2081: {
1.3 ratchov 2082: /*
2083: * device may be disconnected, and if so we're called from
2084: * slot->ops->exit() on a closed device
1.23 ratchov 2085: */
1.3 ratchov 2086: if (s->dev->pstate == DEV_CFG)
2087: return;
1.79 ratchov 2088: if (!s->opt->mmc) {
1.1 ratchov 2089: slot_attach(s);
1.79 ratchov 2090: s->pstate = SLOT_RUN;
2091: } else
1.1 ratchov 2092: dev_sync_attach(s->dev);
2093: }
2094:
2095: /*
2096: * setup buffers & conversion layers, prepare the slot to receive data
2097: * (for playback) or start (recording).
2098: */
2099: void
2100: slot_start(struct slot *s)
2101: {
1.81 ratchov 2102: struct dev *d = s->dev;
1.1 ratchov 2103: #ifdef DEBUG
2104: if (s->pstate != SLOT_INIT) {
2105: slot_log(s);
2106: log_puts(": slot_start: wrong state\n");
2107: panic();
2108: }
2109: if (s->mode & MODE_PLAY) {
2110: if (log_level >= 3) {
2111: slot_log(s);
2112: log_puts(": playing ");
2113: aparams_log(&s->par);
2114: log_puts(" -> ");
1.81 ratchov 2115: aparams_log(&d->par);
1.1 ratchov 2116: log_puts("\n");
2117: }
2118: }
2119: if (s->mode & MODE_RECMASK) {
2120: if (log_level >= 3) {
2121: slot_log(s);
2122: log_puts(": recording ");
2123: aparams_log(&s->par);
2124: log_puts(" <- ");
1.81 ratchov 2125: aparams_log(&d->par);
1.1 ratchov 2126: log_puts("\n");
1.35 ratchov 2127: }
1.1 ratchov 2128: }
2129: #endif
1.35 ratchov 2130: slot_allocbufs(s);
1.47 ratchov 2131:
2132: if (s->mode & MODE_RECMASK) {
2133: /*
2134: * N-th recorded block is the N-th played block
2135: */
1.81 ratchov 2136: s->sub.prime = -dev_getpos(d) / d->round;
1.47 ratchov 2137: }
2138: s->skip = 0;
2139:
1.79 ratchov 2140: /*
2141: * get the current position, the origin is when the first sample
2142: * played and/or recorded
2143: */
1.81 ratchov 2144: s->delta = dev_getpos(d) * (int)s->round / (int)d->round;
1.79 ratchov 2145: s->delta_rem = 0;
2146:
1.1 ratchov 2147: if (s->mode & MODE_PLAY) {
2148: s->pstate = SLOT_START;
2149: } else {
2150: s->pstate = SLOT_READY;
2151: slot_ready(s);
2152: }
2153: }
2154:
2155: /*
2156: * stop playback and recording, and free conversion layers
2157: */
2158: void
2159: slot_detach(struct slot *s)
2160: {
2161: struct slot **ps;
1.81 ratchov 2162: struct dev *d = s->dev;
1.79 ratchov 2163: long long pos;
1.1 ratchov 2164:
1.81 ratchov 2165: for (ps = &d->slot_list; *ps != s; ps = &(*ps)->next) {
1.1 ratchov 2166: #ifdef DEBUG
1.28 ratchov 2167: if (*ps == NULL) {
1.1 ratchov 2168: slot_log(s);
2169: log_puts(": can't detach, not on list\n");
2170: panic();
2171: }
2172: #endif
1.23 ratchov 2173: }
1.1 ratchov 2174: *ps = s->next;
1.79 ratchov 2175:
2176: /*
2177: * adjust clock, go back d->delta ticks so that slot_attach()
2178: * could be called with the resulting state
2179: */
2180: pos = s->delta_rem +
2181: (long long)s->delta * d->round -
2182: (long long)d->delta * s->round;
2183: s->delta = pos / (int)d->round;
2184: s->delta_rem = pos % d->round;
2185: if (s->delta_rem < 0) {
2186: s->delta_rem += d->round;
2187: s->delta--;
2188: }
2189:
2190: #ifdef DEBUG
2191: if (log_level >= 2) {
2192: slot_log(s);
2193: log_puts(": detached at ");
2194: log_puti(s->delta);
2195: log_puts(" + ");
2196: log_puti(s->delta_rem);
2197: log_puts("/");
2198: log_puti(d->round);
2199: log_puts("\n");
2200: }
2201: #endif
1.35 ratchov 2202: if (s->mode & MODE_PLAY)
1.81 ratchov 2203: dev_mix_adjvol(d);
1.1 ratchov 2204: }
2205:
2206: /*
2207: * put the slot in stopping state (draining play buffers) or
2208: * stop & detach if no data to drain.
2209: */
2210: void
1.82 ratchov 2211: slot_stop(struct slot *s, int drain)
1.1 ratchov 2212: {
2213: #ifdef DEBUG
2214: if (log_level >= 3) {
2215: slot_log(s);
2216: log_puts(": stopping\n");
2217: }
2218: #endif
2219: if (s->pstate == SLOT_START) {
1.33 ratchov 2220: /*
2221: * If in rec-only mode, we're already in the READY or
2222: * RUN states. We're here because the play buffer was
2223: * not full enough, try to start so it's drained.
2224: */
2225: s->pstate = SLOT_READY;
2226: slot_ready(s);
1.1 ratchov 2227: }
1.34 ratchov 2228:
2229: if (s->pstate == SLOT_RUN) {
1.82 ratchov 2230: if ((s->mode & MODE_PLAY) && drain) {
1.34 ratchov 2231: /*
2232: * Don't detach, dev_cycle() will do it for us
2233: * when the buffer is drained.
2234: */
2235: s->pstate = SLOT_STOP;
2236: return;
2237: }
1.82 ratchov 2238: slot_detach(s);
2239: } else if (s->pstate == SLOT_STOP) {
1.34 ratchov 2240: slot_detach(s);
2241: } else {
1.1 ratchov 2242: #ifdef DEBUG
2243: if (log_level >= 3) {
2244: slot_log(s);
2245: log_puts(": not drained (blocked by mmc)\n");
2246: }
2247: #endif
2248: }
1.35 ratchov 2249:
1.34 ratchov 2250: s->pstate = SLOT_INIT;
2251: s->ops->eof(s->arg);
1.35 ratchov 2252: slot_freebufs(s);
1.1 ratchov 2253: }
2254:
1.12 ratchov 2255: void
2256: slot_skip_update(struct slot *s)
2257: {
2258: int skip;
2259:
2260: skip = slot_skip(s);
2261: while (skip > 0) {
2262: #ifdef DEBUG
2263: if (log_level >= 4) {
2264: slot_log(s);
2265: log_puts(": catching skipped block\n");
2266: }
2267: #endif
2268: if (s->mode & MODE_RECMASK)
2269: s->ops->flush(s->arg);
2270: if (s->mode & MODE_PLAY)
2271: s->ops->fill(s->arg);
2272: skip--;
2273: }
2274: }
2275:
1.1 ratchov 2276: /*
2277: * notify the slot that we just wrote in the play buffer, must be called
2278: * after each write
2279: */
2280: void
2281: slot_write(struct slot *s)
2282: {
2283: if (s->pstate == SLOT_START && s->mix.buf.used == s->mix.buf.len) {
2284: #ifdef DEBUG
2285: if (log_level >= 4) {
2286: slot_log(s);
2287: log_puts(": switching to READY state\n");
2288: }
2289: #endif
2290: s->pstate = SLOT_READY;
2291: slot_ready(s);
2292: }
1.12 ratchov 2293: slot_skip_update(s);
1.1 ratchov 2294: }
2295:
2296: /*
2297: * notify the slot that we freed some space in the rec buffer
2298: */
2299: void
2300: slot_read(struct slot *s)
2301: {
1.12 ratchov 2302: slot_skip_update(s);
1.64 ratchov 2303: }
2304:
2305: /*
2306: * allocate at control slot
2307: */
2308: struct ctlslot *
2309: ctlslot_new(struct dev *d, struct ctlops *ops, void *arg)
2310: {
2311: struct ctlslot *s;
2312: struct ctl *c;
2313: int i;
2314:
2315: i = 0;
2316: for (;;) {
2317: if (i == DEV_NCTLSLOT)
2318: return NULL;
1.85 ! ratchov 2319: s = ctlslot_array + i;
1.64 ratchov 2320: if (s->ops == NULL)
2321: break;
2322: i++;
2323: }
2324: s->dev = d;
2325: s->mask = 1 << i;
2326: if (!dev_ref(d))
2327: return NULL;
2328: s->ops = ops;
2329: s->arg = arg;
2330: for (c = d->ctl_list; c != NULL; c = c->next)
2331: c->refs_mask |= s->mask;
2332: return s;
2333: }
2334:
2335: /*
2336: * free control slot
2337: */
2338: void
2339: ctlslot_del(struct ctlslot *s)
2340: {
2341: struct ctl *c, **pc;
2342:
2343: pc = &s->dev->ctl_list;
2344: while ((c = *pc) != NULL) {
2345: c->refs_mask &= ~s->mask;
2346: if (c->refs_mask == 0) {
2347: *pc = c->next;
2348: xfree(c);
2349: } else
2350: pc = &c->next;
2351: }
2352: s->ops = NULL;
2353: dev_unref(s->dev);
2354: }
2355:
2356: void
2357: ctl_node_log(struct ctl_node *c)
2358: {
2359: log_puts(c->name);
2360: if (c->unit >= 0)
2361: log_putu(c->unit);
2362: }
2363:
2364: void
2365: ctl_log(struct ctl *c)
2366: {
2367: if (c->group[0] != 0) {
2368: log_puts(c->group);
2369: log_puts("/");
2370: }
2371: ctl_node_log(&c->node0);
2372: log_puts(".");
2373: log_puts(c->func);
2374: log_puts("=");
2375: switch (c->type) {
1.67 ratchov 2376: case CTL_NONE:
2377: log_puts("none");
2378: break;
1.64 ratchov 2379: case CTL_NUM:
2380: case CTL_SW:
2381: log_putu(c->curval);
2382: break;
2383: case CTL_VEC:
2384: case CTL_LIST:
1.74 ratchov 2385: case CTL_SEL:
1.64 ratchov 2386: ctl_node_log(&c->node1);
2387: log_puts(":");
2388: log_putu(c->curval);
2389: }
2390: log_puts(" at ");
2391: log_putu(c->addr);
2392: }
2393:
2394: /*
2395: * add a ctl
2396: */
2397: struct ctl *
2398: dev_addctl(struct dev *d, char *gstr, int type, int addr,
2399: char *str0, int unit0, char *func, char *str1, int unit1, int maxval, int val)
2400: {
2401: struct ctl *c, **pc;
2402: int i;
2403:
2404: c = xmalloc(sizeof(struct ctl));
2405: c->type = type;
2406: strlcpy(c->func, func, CTL_NAMEMAX);
2407: strlcpy(c->group, gstr, CTL_NAMEMAX);
2408: strlcpy(c->node0.name, str0, CTL_NAMEMAX);
2409: c->node0.unit = unit0;
1.74 ratchov 2410: if (c->type == CTL_VEC || c->type == CTL_LIST || c->type == CTL_SEL) {
1.64 ratchov 2411: strlcpy(c->node1.name, str1, CTL_NAMEMAX);
2412: c->node1.unit = unit1;
2413: } else
2414: memset(&c->node1, 0, sizeof(struct ctl_node));
2415: c->addr = addr;
2416: c->maxval = maxval;
2417: c->val_mask = ~0;
2418: c->desc_mask = ~0;
2419: c->curval = val;
2420: c->dirty = 0;
2421: c->refs_mask = 0;
2422: for (i = 0; i < DEV_NCTLSLOT; i++) {
2423: c->refs_mask |= CTL_DEVMASK;
1.85 ! ratchov 2424: if (ctlslot_array[i].ops != NULL)
1.64 ratchov 2425: c->refs_mask |= 1 << i;
2426: }
2427: for (pc = &d->ctl_list; *pc != NULL; pc = &(*pc)->next)
2428: ; /* nothing */
2429: c->next = NULL;
2430: *pc = c;
2431: #ifdef DEBUG
2432: if (log_level >= 3) {
2433: dev_log(d);
2434: log_puts(": adding ");
2435: ctl_log(c);
2436: log_puts("\n");
2437: }
2438: #endif
2439: return c;
2440: }
2441:
2442: void
2443: dev_rmctl(struct dev *d, int addr)
2444: {
2445: struct ctl *c, **pc;
2446:
2447: pc = &d->ctl_list;
2448: for (;;) {
2449: c = *pc;
2450: if (c == NULL)
2451: return;
2452: if (c->type != CTL_NONE && c->addr == addr)
2453: break;
2454: pc = &c->next;
2455: }
2456: c->type = CTL_NONE;
2457: #ifdef DEBUG
2458: if (log_level >= 3) {
2459: dev_log(d);
2460: log_puts(": removing ");
2461: ctl_log(c);
2462: log_puts(", refs_mask = 0x");
2463: log_putx(c->refs_mask);
2464: log_puts("\n");
2465: }
2466: #endif
2467: c->refs_mask &= ~CTL_DEVMASK;
1.68 ratchov 2468: if (c->refs_mask == 0) {
2469: *pc = c->next;
2470: xfree(c);
1.64 ratchov 2471: return;
1.68 ratchov 2472: }
2473: c->desc_mask = ~0;
1.65 ratchov 2474: }
2475:
2476: void
2477: dev_ctlsync(struct dev *d)
2478: {
1.70 ratchov 2479: struct ctl *c;
1.65 ratchov 2480: struct ctlslot *s;
1.70 ratchov 2481: int found, i;
2482:
2483: found = 0;
2484: for (c = d->ctl_list; c != NULL; c = c->next) {
2485: if (c->addr != CTLADDR_MASTER &&
2486: c->type == CTL_NUM &&
2487: strcmp(c->group, "") == 0 &&
2488: strcmp(c->node0.name, "output") == 0 &&
2489: strcmp(c->func, "level") == 0)
2490: found = 1;
2491: }
2492:
2493: if (d->master_enabled && found) {
2494: if (log_level >= 2) {
2495: dev_log(d);
2496: log_puts(": software master level control disabled\n");
2497: }
2498: d->master_enabled = 0;
2499: dev_rmctl(d, CTLADDR_MASTER);
2500: } else if (!d->master_enabled && !found) {
2501: if (log_level >= 2) {
2502: dev_log(d);
2503: log_puts(": software master level control enabled\n");
2504: }
2505: d->master_enabled = 1;
2506: dev_addctl(d, "", CTL_NUM, CTLADDR_MASTER,
2507: "output", -1, "level", NULL, -1, 127, d->master);
2508: }
1.65 ratchov 2509:
1.85 ! ratchov 2510: for (s = ctlslot_array, i = DEV_NCTLSLOT; i > 0; i--, s++) {
! 2511: if (s->dev == d && s->ops)
1.65 ratchov 2512: s->ops->sync(s->arg);
2513: }
1.64 ratchov 2514: }
2515:
2516: int
2517: dev_setctl(struct dev *d, int addr, int val)
2518: {
2519: struct ctl *c;
1.84 ratchov 2520: struct slot *s;
1.64 ratchov 2521: int num;
2522:
2523: c = d->ctl_list;
2524: for (;;) {
2525: if (c == NULL) {
2526: if (log_level >= 3) {
2527: dev_log(d);
2528: log_puts(": ");
2529: log_putu(addr);
2530: log_puts(": no such ctl address\n");
2531: }
2532: return 0;
2533: }
2534: if (c->type != CTL_NONE && c->addr == addr)
2535: break;
2536: c = c->next;
2537: }
2538: if (c->curval == val) {
2539: if (log_level >= 3) {
2540: ctl_log(c);
2541: log_puts(": already set\n");
2542: }
2543: return 1;
2544: }
2545: if (val < 0 || val > c->maxval) {
2546: if (log_level >= 3) {
2547: dev_log(d);
2548: log_puts(": ");
2549: log_putu(val);
2550: log_puts(": ctl val out of bounds\n");
2551: }
2552: return 0;
2553: }
2554: if (addr >= CTLADDR_END) {
2555: if (log_level >= 3) {
2556: ctl_log(c);
2557: log_puts(": marked as dirty\n");
2558: }
2559: c->dirty = 1;
2560: dev_ref(d);
2561: } else {
1.75 ratchov 2562: if (addr >= CTLADDR_ALT_SEL) {
2563: if (val) {
2564: num = addr - CTLADDR_ALT_SEL;
2565: dev_setalt(d, num);
2566: }
2567: return 1;
2568: } else if (addr == CTLADDR_MASTER) {
1.70 ratchov 2569: if (d->master_enabled) {
2570: dev_master(d, val);
2571: dev_midi_master(d);
2572: }
1.64 ratchov 2573: } else {
2574: num = addr - CTLADDR_SLOT_LEVEL(0);
1.84 ratchov 2575: s = slot_array + num;
2576: if (s->dev != d)
2577: return 1;
2578: slot_setvol(s, val);
2579: dev_midi_vol(d, s);
1.64 ratchov 2580: }
1.66 ratchov 2581: c->val_mask = ~0U;
1.64 ratchov 2582: }
2583: c->curval = val;
2584: return 1;
2585: }
2586:
2587: int
2588: dev_onval(struct dev *d, int addr, int val)
2589: {
2590: struct ctl *c;
2591:
2592: c = d->ctl_list;
2593: for (;;) {
2594: if (c == NULL)
2595: return 0;
2596: if (c->type != CTL_NONE && c->addr == addr)
2597: break;
2598: c = c->next;
2599: }
2600: c->curval = val;
2601: c->val_mask = ~0U;
2602: return 1;
2603: }
2604:
2605: void
2606: dev_label(struct dev *d, int i)
2607: {
2608: struct ctl *c;
2609: char name[CTL_NAMEMAX];
2610:
1.84 ratchov 2611: slot_ctlname(&slot_array[i], name, CTL_NAMEMAX);
1.69 ratchov 2612:
1.64 ratchov 2613: c = d->ctl_list;
2614: for (;;) {
1.69 ratchov 2615: if (c == NULL) {
2616: dev_addctl(d, "app", CTL_NUM,
2617: CTLADDR_SLOT_LEVEL(i),
2618: name, -1, "level",
1.84 ratchov 2619: NULL, -1, 127, slot_array[i].vol);
1.64 ratchov 2620: return;
1.69 ratchov 2621: }
1.64 ratchov 2622: if (c->addr == CTLADDR_SLOT_LEVEL(i))
2623: break;
2624: c = c->next;
2625: }
2626: if (strcmp(c->node0.name, name) == 0)
2627: return;
2628: strlcpy(c->node0.name, name, CTL_NAMEMAX);
2629: c->desc_mask = ~0;
1.1 ratchov 2630: }