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