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