Annotation of src/usr.bin/sndiod/dev.c, Revision 1.21
1.21 ! ratchov 1: /* $OpenBSD$ */
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"
26: #include "sysex.h"
27: #include "utils.h"
28:
1.14 ratchov 29: void zomb_onmove(void *);
1.1 ratchov 30: void zomb_onvol(void *, unsigned int);
31: void zomb_fill(void *);
32: void zomb_flush(void *);
33: void zomb_eof(void *);
34: void zomb_exit(void *);
35:
1.7 ratchov 36: void dev_log(struct dev *);
37: void dev_midi_qfr(struct dev *, int);
38: void dev_midi_full(struct dev *);
39: void dev_midi_vol(struct dev *, struct slot *);
40: void dev_midi_master(struct dev *);
41: void dev_midi_slotdesc(struct dev *, struct slot *);
42: void dev_midi_dump(struct dev *);
1.1 ratchov 43: void dev_midi_imsg(void *, unsigned char *, int);
44: void dev_midi_omsg(void *, unsigned char *, int);
45: void dev_midi_fill(void *, int);
46: void dev_midi_exit(void *);
1.7 ratchov 47:
48: int play_filt_resamp(struct slot *, void *, void *, int);
49: int play_filt_dec(struct slot *, void *, void *, int);
50: void dev_mix_badd(struct dev *, struct slot *);
51: void dev_mix_adjvol(struct dev *);
52: int rec_filt_resamp(struct slot *, void *, void *, int);
53: int rec_filt_enc(struct slot *, void *, void *, int);
54: void dev_sub_bcopy(struct dev *, struct slot *);
55:
56: void dev_onmove(struct dev *, int);
57: void dev_master(struct dev *, unsigned int);
58: void dev_cycle(struct dev *);
59: int dev_getpos(struct dev *);
60: struct dev *dev_new(char *, struct aparams *, unsigned int, unsigned int,
61: unsigned int, unsigned int, unsigned int, unsigned int);
62: void dev_adjpar(struct dev *, int, int, int, int, int);
63: int dev_open(struct dev *);
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:
78: void slot_log(struct slot *);
79: struct slot *slot_new(struct dev *, char *, struct slotops *, void *, int);
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 *);
84: void slot_start(struct slot *);
85: void slot_detach(struct slot *);
86: void slot_stop(struct slot *);
1.12 ratchov 87: void slot_skip_update(struct slot *);
1.7 ratchov 88: void slot_write(struct slot *);
89: void slot_read(struct slot *);
1.12 ratchov 90: int slot_skip(struct slot *);
1.1 ratchov 91:
92: struct midiops dev_midiops = {
93: dev_midi_imsg,
94: dev_midi_omsg,
95: dev_midi_fill,
96: dev_midi_exit
97: };
98:
99: struct slotops zomb_slotops = {
100: zomb_onmove,
101: zomb_onvol,
102: zomb_fill,
103: zomb_flush,
104: zomb_eof,
105: zomb_exit
106: };
107:
108: struct dev *dev_list = NULL;
109: unsigned int dev_sndnum = 0;
110:
111: void
112: dev_log(struct dev *d)
113: {
1.3 ratchov 114: #ifdef DEBUG
115: static char *pstates[] = {
116: "cfg", "ini", "run"
117: };
118: #endif
1.1 ratchov 119: log_puts("snd");
120: log_putu(d->num);
1.3 ratchov 121: #ifdef DEBUG
122: if (log_level >= 3) {
123: log_puts(" pst=");
124: log_puts(pstates[d->pstate]);
125: }
126: #endif
1.1 ratchov 127: }
128:
129: void
130: slot_log(struct slot *s)
1.3 ratchov 131: {
1.1 ratchov 132: #ifdef DEBUG
133: static char *pstates[] = {
134: "ini", "sta", "rdy", "run", "stp", "mid"
135: };
136: static char *tstates[] = {
137: "off", "sta", "run", "stp"
138: };
139: #endif
140: log_puts(s->name);
141: log_putu(s->unit);
142: #ifdef DEBUG
143: if (log_level >= 3) {
144: log_puts(" vol=");
145: log_putu(s->vol);
146: if (s->ops) {
147: log_puts(",pst=");
148: log_puts(pstates[s->pstate]);
149: log_puts(",mmc=");
150: log_puts(tstates[s->tstate]);
151: }
152: }
153: #endif
154: }
155:
156: void
1.14 ratchov 157: zomb_onmove(void *arg)
1.1 ratchov 158: {
159: }
160:
161: void
162: zomb_onvol(void *arg, unsigned int vol)
163: {
164: }
165:
166: void
167: zomb_fill(void *arg)
168: {
169: }
170:
171: void
172: zomb_flush(void *arg)
173: {
174: }
175:
176: void
177: zomb_eof(void *arg)
178: {
179: struct slot *s = arg;
180:
181: #ifdef DEBUG
182: if (log_level >= 3) {
183: slot_log(s);
184: log_puts(": zomb_eof\n");
185: }
186: #endif
187: s->ops = NULL;
188: }
189:
190: void
191: zomb_exit(void *arg)
192: {
193: #ifdef DEBUG
194: struct slot *s = arg;
195:
196: if (log_level >= 3) {
197: slot_log(s);
198: log_puts(": zomb_exit\n");
199: }
200: #endif
201: }
202:
203: /*
204: * send a quarter frame MTC message
205: */
206: void
207: dev_midi_qfr(struct dev *d, int delta)
208: {
209: unsigned char buf[2];
210: unsigned int data;
211: int qfrlen;
212:
213: d->mtc.delta += delta * MTC_SEC;
214: qfrlen = d->rate * (MTC_SEC / (4 * d->mtc.fps));
215: while (d->mtc.delta >= qfrlen) {
216: switch (d->mtc.qfr) {
217: case 0:
218: data = d->mtc.fr & 0xf;
219: break;
220: case 1:
221: data = d->mtc.fr >> 4;
222: break;
223: case 2:
224: data = d->mtc.sec & 0xf;
225: break;
226: case 3:
227: data = d->mtc.sec >> 4;
228: break;
229: case 4:
230: data = d->mtc.min & 0xf;
231: break;
232: case 5:
233: data = d->mtc.min >> 4;
234: break;
235: case 6:
236: data = d->mtc.hr & 0xf;
237: break;
238: case 7:
239: data = (d->mtc.hr >> 4) | (d->mtc.fps_id << 1);
240: /*
241: * tick messages are sent 2 frames ahead
242: */
243: d->mtc.fr += 2;
244: if (d->mtc.fr < d->mtc.fps)
245: break;
246: d->mtc.fr -= d->mtc.fps;
247: d->mtc.sec++;
248: if (d->mtc.sec < 60)
249: break;
250: d->mtc.sec = 0;
251: d->mtc.min++;
252: if (d->mtc.min < 60)
253: break;
254: d->mtc.min = 0;
255: d->mtc.hr++;
256: if (d->mtc.hr < 24)
257: break;
258: d->mtc.hr = 0;
259: break;
260: default:
261: /* NOTREACHED */
262: data = 0;
263: }
264: buf[0] = 0xf1;
265: buf[1] = (d->mtc.qfr << 4) | data;
266: d->mtc.qfr++;
267: d->mtc.qfr &= 7;
268: midi_send(d->midi, buf, 2);
269: d->mtc.delta -= qfrlen;
270: }
271: }
272:
273: /*
274: * send a full frame MTC message
275: */
276: void
277: dev_midi_full(struct dev *d)
278: {
279: struct sysex x;
280: unsigned int fps;
281:
282: d->mtc.delta = MTC_SEC * dev_getpos(d);
283: if (d->rate % (30 * 4 * d->round) == 0) {
284: d->mtc.fps_id = MTC_FPS_30;
285: d->mtc.fps = 30;
286: } else if (d->rate % (25 * 4 * d->round) == 0) {
287: d->mtc.fps_id = MTC_FPS_25;
288: d->mtc.fps = 25;
289: } else {
290: d->mtc.fps_id = MTC_FPS_24;
291: d->mtc.fps = 24;
292: }
293: #ifdef DEBUG
294: if (log_level >= 3) {
295: dev_log(d);
296: log_puts(": mtc full frame at ");
297: log_puti(d->mtc.delta);
298: log_puts(", ");
299: log_puti(d->mtc.fps);
300: log_puts(" fps\n");
301: }
302: #endif
303: fps = d->mtc.fps;
304: d->mtc.hr = (d->mtc.origin / (MTC_SEC * 3600)) % 24;
305: d->mtc.min = (d->mtc.origin / (MTC_SEC * 60)) % 60;
306: d->mtc.sec = (d->mtc.origin / (MTC_SEC)) % 60;
307: d->mtc.fr = (d->mtc.origin / (MTC_SEC / fps)) % fps;
308:
309: x.start = SYSEX_START;
310: x.type = SYSEX_TYPE_RT;
1.8 ratchov 311: x.dev = SYSEX_DEV_ANY;
1.1 ratchov 312: x.id0 = SYSEX_MTC;
313: x.id1 = SYSEX_MTC_FULL;
314: x.u.full.hr = d->mtc.hr | (d->mtc.fps_id << 5);
315: x.u.full.min = d->mtc.min;
316: x.u.full.sec = d->mtc.sec;
317: x.u.full.fr = d->mtc.fr;
318: x.u.full.end = SYSEX_END;
319: d->mtc.qfr = 0;
320: midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(full));
321: }
322:
323: /*
324: * send a volume change MIDI message
325: */
326: void
327: dev_midi_vol(struct dev *d, struct slot *s)
328: {
329: unsigned char msg[3];
330:
331: msg[0] = MIDI_CTL | (s - d->slot);
332: msg[1] = MIDI_CTL_VOL;
333: msg[2] = s->vol;
334: midi_send(d->midi, msg, 3);
335: }
336:
337: /*
338: * send a master volume MIDI message
339: */
340: void
341: dev_midi_master(struct dev *d)
342: {
343: struct sysex x;
344:
345: memset(&x, 0, sizeof(struct sysex));
346: x.start = SYSEX_START;
347: x.type = SYSEX_TYPE_RT;
1.8 ratchov 348: x.dev = SYSEX_DEV_ANY;
1.1 ratchov 349: x.id0 = SYSEX_CONTROL;
350: x.id1 = SYSEX_MASTER;
351: x.u.master.fine = 0;
352: x.u.master.coarse = d->master;
353: x.u.master.end = SYSEX_END;
354: midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(master));
355: }
356:
357: /*
358: * send a sndiod-specific slot description MIDI message
359: */
360: void
361: dev_midi_slotdesc(struct dev *d, struct slot *s)
362: {
363: struct sysex x;
364:
365: memset(&x, 0, sizeof(struct sysex));
366: x.start = SYSEX_START;
367: x.type = SYSEX_TYPE_EDU;
1.8 ratchov 368: x.dev = SYSEX_DEV_ANY;
1.1 ratchov 369: x.id0 = SYSEX_AUCAT;
370: x.id1 = SYSEX_AUCAT_SLOTDESC;
371: if (*s->name != '\0') {
372: snprintf((char *)x.u.slotdesc.name, SYSEX_NAMELEN,
373: "%s%u", s->name, s->unit);
374: }
375: x.u.slotdesc.chan = s - d->slot;
376: x.u.slotdesc.end = SYSEX_END;
377: midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(slotdesc));
378: }
379:
380: void
381: dev_midi_dump(struct dev *d)
382: {
383: struct sysex x;
384: struct slot *s;
385: int i;
386:
387: dev_midi_master(d);
388: for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) {
389: dev_midi_slotdesc(d, s);
390: dev_midi_vol(d, s);
391: }
392: x.start = SYSEX_START;
393: x.type = SYSEX_TYPE_EDU;
1.8 ratchov 394: x.dev = SYSEX_DEV_ANY;
1.1 ratchov 395: x.id0 = SYSEX_AUCAT;
396: x.id1 = SYSEX_AUCAT_DUMPEND;
397: x.u.dumpend.end = SYSEX_END;
398: midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(dumpend));
399: }
400:
401: void
402: dev_midi_imsg(void *arg, unsigned char *msg, int len)
403: {
404: #ifdef DEBUG
405: struct dev *d = arg;
406:
407: dev_log(d);
408: log_puts(": can't receive midi messages\n");
409: panic();
410: #endif
411: }
412:
413: void
414: dev_midi_omsg(void *arg, unsigned char *msg, int len)
415: {
416: struct dev *d = arg;
417: struct sysex *x;
418: unsigned int fps, chan;
419:
420: if ((msg[0] & MIDI_CMDMASK) == MIDI_CTL && msg[1] == MIDI_CTL_VOL) {
421: chan = msg[0] & MIDI_CHANMASK;
422: if (chan >= DEV_NSLOT)
423: return;
424: slot_setvol(d->slot + chan, msg[2]);
425: return;
426: }
427: x = (struct sysex *)msg;
428: if (x->start != SYSEX_START)
429: return;
430: if (len < SYSEX_SIZE(empty))
431: return;
432: switch (x->type) {
433: case SYSEX_TYPE_RT:
434: if (x->id0 == SYSEX_CONTROL && x->id1 == SYSEX_MASTER) {
435: if (len == SYSEX_SIZE(master))
436: dev_master(d, x->u.master.coarse);
437: return;
438: }
439: if (x->id0 != SYSEX_MMC)
440: return;
441: switch (x->id1) {
442: case SYSEX_MMC_STOP:
443: if (len != SYSEX_SIZE(stop))
444: return;
445: if (log_level >= 2) {
446: dev_log(d);
447: log_puts(": mmc stop\n");
448: }
449: dev_mmcstop(d);
450: break;
451: case SYSEX_MMC_START:
452: if (len != SYSEX_SIZE(start))
453: return;
454: if (log_level >= 2) {
455: dev_log(d);
456: log_puts(": mmc start\n");
457: }
458: dev_mmcstart(d);
459: break;
460: case SYSEX_MMC_LOC:
461: if (len != SYSEX_SIZE(loc) ||
462: x->u.loc.len != SYSEX_MMC_LOC_LEN ||
463: x->u.loc.cmd != SYSEX_MMC_LOC_CMD)
464: return;
465: switch (x->u.loc.hr >> 5) {
466: case MTC_FPS_24:
467: fps = 24;
468: break;
469: case MTC_FPS_25:
470: fps = 25;
471: break;
472: case MTC_FPS_30:
473: fps = 30;
474: break;
475: default:
476: dev_mmcstop(d);
477: return;
478: }
479: dev_mmcloc(d,
480: (x->u.loc.hr & 0x1f) * 3600 * MTC_SEC +
481: x->u.loc.min * 60 * MTC_SEC +
482: x->u.loc.sec * MTC_SEC +
483: x->u.loc.fr * (MTC_SEC / fps) +
484: x->u.loc.cent * (MTC_SEC / 100 / fps));
485: break;
486: }
487: break;
488: case SYSEX_TYPE_EDU:
489: if (x->id0 != SYSEX_AUCAT || x->id1 != SYSEX_AUCAT_DUMPREQ)
490: return;
491: if (len != SYSEX_SIZE(dumpreq))
492: return;
493: dev_midi_dump(d);
494: break;
495: }
496: }
497:
498: void
499: dev_midi_fill(void *arg, int count)
500: {
1.2 ratchov 501: /* nothing to do */
1.1 ratchov 502: }
503:
504: void
505: dev_midi_exit(void *arg)
506: {
507: struct dev *d = arg;
508:
509: if (log_level >= 1) {
510: dev_log(d);
511: log_puts(": midi end point died\n");
512: }
513: if (d->pstate != DEV_CFG)
514: dev_close(d);
515: }
516:
1.12 ratchov 517: int
518: slot_skip(struct slot *s)
1.1 ratchov 519: {
1.12 ratchov 520: unsigned char *data = (unsigned char *)0xdeadbeef; /* please gcc */
521: int max, count;
522:
523: max = s->skip;
524: while (s->skip > 0) {
525: if (s->pstate != SLOT_STOP && (s->mode & MODE_RECMASK)) {
526: data = abuf_wgetblk(&s->sub.buf, &count);
527: if (count < s->round * s->sub.bpf)
528: break;
529: }
530: if (s->mode & MODE_PLAY) {
531: if (s->mix.buf.used < s->round * s->mix.bpf)
532: break;
533: }
1.1 ratchov 534: #ifdef DEBUG
535: if (log_level >= 4) {
536: slot_log(s);
1.12 ratchov 537: log_puts(": skipped a cycle\n");
1.1 ratchov 538: }
539: #endif
1.12 ratchov 540: if (s->pstate != SLOT_STOP && (s->mode & MODE_RECMASK)) {
541: if (s->sub.encbuf)
542: enc_sil_do(&s->sub.enc, data, s->round);
543: else
544: memset(data, 0, s->round * s->sub.bpf);
545: abuf_wcommit(&s->sub.buf, s->round * s->sub.bpf);
546: }
547: if (s->mode & MODE_PLAY) {
548: abuf_rdiscard(&s->mix.buf, s->round * s->mix.bpf);
1.1 ratchov 549: }
1.12 ratchov 550: s->skip--;
1.1 ratchov 551: }
1.12 ratchov 552: return max - s->skip;
1.1 ratchov 553: }
554:
555: int
556: play_filt_resamp(struct slot *s, void *res_in, void *out, int todo)
557: {
558: int i, offs, vol, nch;
559: void *in;
560:
561: if (s->mix.resampbuf) {
562: todo = resamp_do(&s->mix.resamp,
563: res_in, s->mix.resampbuf, todo);
564: in = s->mix.resampbuf;
565: } else
566: in = res_in;
567:
1.10 ratchov 568: nch = s->mix.cmap.nch;
1.1 ratchov 569: vol = ADATA_MUL(s->mix.weight, s->mix.vol) / s->mix.join;
570: cmap_add(&s->mix.cmap, in, out, vol, todo);
571:
572: offs = 0;
573: for (i = s->mix.join - 1; i > 0; i--) {
574: offs += nch;
575: cmap_add(&s->mix.cmap, (adata_t *)in + offs, out, vol, todo);
576: }
577: offs = 0;
578: for (i = s->mix.expand - 1; i > 0; i--) {
579: offs += nch;
580: cmap_add(&s->mix.cmap, in, (adata_t *)out + offs, vol, todo);
581: }
582: return todo;
583: }
584:
585: int
586: play_filt_dec(struct slot *s, void *in, void *out, int todo)
587: {
588: void *tmp;
589:
590: tmp = s->mix.decbuf;
591: if (tmp)
592: dec_do(&s->mix.dec, in, tmp, todo);
593: return play_filt_resamp(s, tmp ? tmp : in, out, todo);
594: }
595:
596: /*
597: * mix "todo" frames from the input block over the output block; if
598: * there are frames to drop, less frames are consumed from the input
599: */
600: void
601: dev_mix_badd(struct dev *d, struct slot *s)
602: {
603: adata_t *idata, *odata;
604: int icount;
605:
606: odata = DEV_PBUF(d);
607: idata = (adata_t *)abuf_rgetblk(&s->mix.buf, &icount);
608: #ifdef DEBUG
609: if (icount < s->round * s->mix.bpf) {
610: slot_log(s);
611: log_puts(": not enough data to mix (");
612: log_putu(icount);
613: log_puts("bytes)\n");
614: panic();
615: }
616: #endif
617: play_filt_dec(s, idata, odata, s->round);
618: abuf_rdiscard(&s->mix.buf, s->round * s->mix.bpf);
619: }
620:
621: /*
622: * Normalize input levels.
623: */
624: void
625: dev_mix_adjvol(struct dev *d)
626: {
627: unsigned int n;
628: struct slot *i, *j;
629: int weight;
630:
631: for (i = d->slot_list; i != NULL; i = i->next) {
632: if (!(i->mode & MODE_PLAY))
633: continue;
634: weight = ADATA_UNIT;
635: if (d->autovol) {
636: /*
637: * count the number of inputs that have
638: * overlapping channel sets
639: */
640: n = 0;
641: for (j = d->slot_list; j != NULL; j = j->next) {
642: if (!(j->mode & MODE_PLAY))
643: continue;
644: if (i->mix.slot_cmin <= j->mix.slot_cmax &&
645: i->mix.slot_cmax >= j->mix.slot_cmin)
646: n++;
647: }
648: weight /= n;
649: }
650: if (weight > i->mix.maxweight)
651: weight = i->mix.maxweight;
652: i->mix.weight = ADATA_MUL(weight, MIDI_TO_ADATA(d->master));
653: #ifdef DEBUG
654: if (log_level >= 3) {
655: slot_log(i);
656: log_puts(": set weight: ");
657: log_puti(i->mix.weight);
658: log_puts("/");
659: log_puti(i->mix.maxweight);
660: log_puts("\n");
661: }
662: #endif
663: }
664: }
665:
666: int
667: rec_filt_resamp(struct slot *s, void *in, void *res_out, int todo)
668: {
669: int i, vol, offs, nch;
670: void *out = res_out;
671:
672: out = (s->sub.resampbuf) ? s->sub.resampbuf : res_out;
673:
1.10 ratchov 674: nch = s->sub.cmap.nch;
1.1 ratchov 675: vol = ADATA_UNIT / s->sub.join;
676: cmap_copy(&s->sub.cmap, in, out, vol, todo);
677:
678: offs = 0;
679: for (i = s->sub.join - 1; i > 0; i--) {
680: offs += nch;
681: cmap_add(&s->sub.cmap, (adata_t *)in + offs, out, vol, todo);
682: }
683: offs = 0;
684: for (i = s->sub.expand - 1; i > 0; i--) {
685: offs += nch;
686: cmap_copy(&s->sub.cmap, in, (adata_t *)out + offs, vol, todo);
687: }
688: if (s->sub.resampbuf) {
689: todo = resamp_do(&s->sub.resamp,
690: s->sub.resampbuf, res_out, todo);
691: }
692: return todo;
693: }
694:
695: int
696: rec_filt_enc(struct slot *s, void *in, void *out, int todo)
697: {
698: void *tmp;
699:
700: tmp = s->sub.encbuf;
701: todo = rec_filt_resamp(s, in, tmp ? tmp : out, todo);
702: if (tmp)
703: enc_do(&s->sub.enc, tmp, out, todo);
704: return todo;
705: }
706:
707: /*
708: * Copy data from slot to device
709: */
710: void
711: dev_sub_bcopy(struct dev *d, struct slot *s)
712: {
713: adata_t *idata, *odata;
1.15 ratchov 714: int ocount, moffs;
1.1 ratchov 715:
1.15 ratchov 716: if (s->mode & MODE_MON) {
717: moffs = d->poffs + d->round;
718: if (moffs == d->psize)
719: moffs = 0;
720: idata = d->pbuf + moffs * d->pchan;
721: } else
722: idata = d->rbuf;
1.1 ratchov 723: odata = (adata_t *)abuf_wgetblk(&s->sub.buf, &ocount);
724: #ifdef DEBUG
725: if (ocount < s->round * s->sub.bpf) {
726: log_puts("dev_sub_bcopy: not enough space\n");
727: panic();
728: }
729: #endif
730: ocount = rec_filt_enc(s, idata, odata, d->round);
731: abuf_wcommit(&s->sub.buf, ocount * s->sub.bpf);
732: }
733:
1.16 ratchov 734: /*
735: * run a one block cycle: consume one recorded block from
736: * rbuf and produce one play block in pbuf
737: */
1.1 ratchov 738: void
1.16 ratchov 739: dev_cycle(struct dev *d)
1.1 ratchov 740: {
741: struct slot *s, **ps;
1.12 ratchov 742: unsigned char *base;
743: int nsamp;
1.1 ratchov 744:
1.16 ratchov 745: /*
746: * check if the device is actually used. If it isn't,
747: * then close it
748: */
749: if (d->slot_list == NULL && d->tstate != MMC_RUN) {
750: if (log_level >= 2) {
751: dev_log(d);
752: log_puts(": device stopped\n");
753: }
754: dev_sio_stop(d);
755: d->pstate = DEV_INIT;
756: if (d->refcnt == 0)
757: dev_close(d);
758: return;
759: }
760:
761: if (d->prime > 0) {
762: #ifdef DEBUG
763: if (log_level >= 4) {
764: dev_log(d);
765: log_puts(": empty cycle, prime = ");
766: log_putu(d->prime);
767: log_puts("\n");
768: }
769: #endif
770: base = (unsigned char *)DEV_PBUF(d);
771: nsamp = d->round * d->pchan;
772: memset(base, 0, nsamp * sizeof(adata_t));
773: if (d->encbuf) {
774: enc_do(&d->enc, (unsigned char *)DEV_PBUF(d),
775: d->encbuf, d->round);
776: }
777: d->prime -= d->round;
778: return;
779: }
780:
1.12 ratchov 781: d->delta -= d->round;
1.1 ratchov 782: #ifdef DEBUG
783: if (log_level >= 4) {
784: dev_log(d);
1.16 ratchov 785: log_puts(": full cycle: delta = ");
1.12 ratchov 786: log_puti(d->delta);
787: if (d->mode & MODE_PLAY) {
788: log_puts(", poffs = ");
789: log_puti(d->poffs);
790: }
791: log_puts("\n");
1.1 ratchov 792: }
793: #endif
1.12 ratchov 794: if (d->mode & MODE_PLAY) {
795: base = (unsigned char *)DEV_PBUF(d);
796: nsamp = d->round * d->pchan;
797: memset(base, 0, nsamp * sizeof(adata_t));
798: }
1.1 ratchov 799: if ((d->mode & MODE_REC) && d->decbuf)
800: dec_do(&d->dec, d->decbuf, (unsigned char *)d->rbuf, d->round);
801: ps = &d->slot_list;
802: while ((s = *ps) != NULL) {
1.12 ratchov 803: #ifdef DEBUG
804: if (log_level >= 4) {
805: slot_log(s);
806: log_puts(": running");
807: log_puts(", skip = ");
808: log_puti(s->skip);
809: log_puts("\n");
810: }
811: #endif
812: /*
813: * skip cycles for XRUN_SYNC correction
814: */
815: slot_skip(s);
816: if (s->skip < 0) {
817: s->skip++;
1.1 ratchov 818: ps = &s->next;
819: continue;
820: }
1.12 ratchov 821:
822: #ifdef DEBUG
823: if (s->pstate == SLOT_STOP && !(s->mode & MODE_PLAY)) {
824: slot_log(s);
825: log_puts(": rec-only slots can't be drained\n");
826: panic();
827: }
828: #endif
829: /*
830: * check if stopped stream finished draining
831: */
832: if (s->pstate == SLOT_STOP &&
833: s->mix.buf.used < s->round * s->mix.bpf) {
834: /*
835: * partial blocks are zero-filled by socket
836: * layer, so s->mix.buf.used == 0 and we can
837: * destroy the buffer
838: */
839: s->pstate = SLOT_INIT;
840: abuf_done(&s->mix.buf);
841: if (s->mix.decbuf)
842: xfree(s->mix.decbuf);
843: if (s->mix.resampbuf)
844: xfree(s->mix.resampbuf);
845: s->ops->eof(s->arg);
846: *ps = s->next;
847: dev_mix_adjvol(d);
1.1 ratchov 848: continue;
849: }
1.12 ratchov 850:
851: /*
852: * check for xruns
853: */
854: if (((s->mode & MODE_PLAY) &&
855: s->mix.buf.used < s->round * s->mix.bpf) ||
856: ((s->mode & MODE_RECMASK) &&
857: s->sub.buf.len - s->sub.buf.used <
858: s->round * s->sub.bpf)) {
859:
860: #ifdef DEBUG
861: if (log_level >= 3) {
862: slot_log(s);
863: log_puts(": xrun, pause cycle\n");
864: }
865: #endif
1.1 ratchov 866: if (s->xrun == XRUN_IGNORE) {
867: s->delta -= s->round;
1.12 ratchov 868: ps = &s->next;
869: } else if (s->xrun == XRUN_SYNC) {
870: s->skip++;
871: ps = &s->next;
872: } else if (s->xrun == XRUN_ERROR) {
873: s->ops->exit(s->arg);
874: *ps = s->next;
875: } else {
876: #ifdef DEBUG
877: slot_log(s);
878: log_puts(": bad xrun mode\n");
879: panic();
880: #endif
881: }
882: continue;
883: }
884: if ((s->mode & MODE_RECMASK) && !(s->pstate == SLOT_STOP)) {
885: if (s->sub.prime == 0) {
886: dev_sub_bcopy(d, s);
887: s->ops->flush(s->arg);
888: } else {
1.1 ratchov 889: #ifdef DEBUG
890: if (log_level >= 3) {
891: slot_log(s);
1.12 ratchov 892: log_puts(": prime = ");
893: log_puti(s->sub.prime);
894: log_puts("\n");
1.1 ratchov 895: }
896: #endif
1.12 ratchov 897: s->sub.prime--;
1.1 ratchov 898: }
899: }
1.15 ratchov 900: if (s->mode & MODE_PLAY) {
901: dev_mix_badd(d, s);
902: if (s->pstate != SLOT_STOP)
903: s->ops->fill(s->arg);
904: }
1.1 ratchov 905: ps = &s->next;
906: }
1.12 ratchov 907: if ((d->mode & MODE_PLAY) && d->encbuf) {
908: enc_do(&d->enc, (unsigned char *)DEV_PBUF(d),
909: d->encbuf, d->round);
910: }
1.1 ratchov 911: }
912:
913: /*
914: * called at every clock tick by the device
915: */
916: void
917: dev_onmove(struct dev *d, int delta)
918: {
919: long long pos;
1.12 ratchov 920: struct slot *s, *snext;
921:
922: d->delta += delta;
1.1 ratchov 923:
924: for (s = d->slot_list; s != NULL; s = snext) {
1.12 ratchov 925: /*
926: * s->ops->onmove() may remove the slot
927: */
1.1 ratchov 928: snext = s->next;
929: pos = (long long)delta * s->round + s->delta_rem;
930: s->delta_rem = pos % d->round;
931: s->delta += pos / (int)d->round;
932: if (s->delta >= 0)
1.14 ratchov 933: s->ops->onmove(s->arg);
1.1 ratchov 934: }
935: if (d->tstate == MMC_RUN)
936: dev_midi_qfr(d, delta);
937: }
938:
939: void
940: dev_master(struct dev *d, unsigned int master)
941: {
942: if (log_level >= 2) {
943: dev_log(d);
944: log_puts(": master volume set to ");
945: log_putu(master);
946: log_puts("\n");
947: }
948: d->master = master;
949: if (d->mode & MODE_PLAY)
950: dev_mix_adjvol(d);
951: }
952:
953: /*
954: * return the latency that a stream would have if it's attached
955: */
956: int
957: dev_getpos(struct dev *d)
958: {
959: return (d->mode & MODE_PLAY) ? -d->bufsz : 0;
960: }
961:
962: /*
963: * Create a sndio device
964: */
965: struct dev *
966: dev_new(char *path, struct aparams *par,
967: unsigned int mode, unsigned int bufsz, unsigned int round,
968: unsigned int rate, unsigned int hold, unsigned int autovol)
969: {
970: struct dev *d;
971: unsigned int i;
972:
973: if (dev_sndnum == DEV_NMAX) {
974: if (log_level >= 1)
975: log_puts("too many devices\n");
976: return NULL;
977: }
978: d = xmalloc(sizeof(struct dev));
1.21 ! ratchov 979: d->path = xstrdup(path);
1.1 ratchov 980: d->num = dev_sndnum++;
981:
982: /*
983: * XXX: below, we allocate a midi input buffer, since we don't
984: * receive raw midi data, so no need to allocate a input
985: * ibuf. Possibly set imsg & fill callbacks to NULL and
986: * use this to in midi_new() to check if buffers need to be
987: * allocated
988: */
989: d->midi = midi_new(&dev_midiops, d, MODE_MIDIIN | MODE_MIDIOUT);
990: midi_tag(d->midi, d->num);
991: d->path = path;
992: d->reqpar = *par;
993: d->reqmode = mode;
994: d->reqpchan = d->reqrchan = 0;
995: d->reqbufsz = bufsz;
996: d->reqround = round;
997: d->reqrate = rate;
998: d->hold = hold;
999: d->autovol = autovol;
1000: d->refcnt = 0;
1001: d->pstate = DEV_CFG;
1002: d->serial = 0;
1003: for (i = 0; i < DEV_NSLOT; i++) {
1004: d->slot[i].unit = i;
1005: d->slot[i].ops = NULL;
1006: d->slot[i].vol = MIDI_MAXCTL;
1007: d->slot[i].tstate = MMC_OFF;
1008: d->slot[i].serial = d->serial++;
1009: d->slot[i].name[0] = '\0';
1010: }
1011: d->slot_list = NULL;
1012: d->master = MIDI_MAXCTL;
1013: d->mtc.origin = 0;
1014: d->tstate = MMC_STOP;
1015: d->next = dev_list;
1016: dev_list = d;
1017: return d;
1018: }
1019:
1020: /*
1021: * adjust device parameters and mode
1022: */
1023: void
1024: dev_adjpar(struct dev *d, int mode,
1025: int pmin, int pmax, int rmin, int rmax)
1026: {
1027: d->reqmode |= mode & MODE_AUDIOMASK;
1028: if (mode & MODE_PLAY) {
1029: if (d->reqpchan < pmax + 1)
1030: d->reqpchan = pmax + 1;
1031: }
1032: if (mode & MODE_REC) {
1033: if (d->reqrchan < rmax + 1)
1034: d->reqrchan = rmax + 1;
1035: }
1036: }
1037:
1038: /*
1039: * Open the device with the dev_reqxxx capabilities. Setup a mixer, demuxer,
1040: * monitor, midi control, and any necessary conversions.
1041: */
1042: int
1043: dev_open(struct dev *d)
1044: {
1045: d->mode = d->reqmode;
1046: d->round = d->reqround;
1047: d->bufsz = d->reqbufsz;
1048: d->rate = d->reqrate;
1049: d->pchan = d->reqpchan;
1050: d->rchan = d->reqrchan;
1051: d->par = d->reqpar;
1052: if (d->pchan == 0)
1053: d->pchan = 2;
1054: if (d->rchan == 0)
1055: d->rchan = 2;
1056: if (!dev_sio_open(d)) {
1057: if (log_level >= 1) {
1058: dev_log(d);
1059: log_puts(": ");
1060: log_puts(d->path);
1061: log_puts(": failed to open audio device\n");
1062: }
1063: return 0;
1064: }
1065: if (d->mode & MODE_REC) {
1066: /*
1067: * Create device <-> demuxer buffer
1068: */
1069: d->rbuf = xmalloc(d->round * d->rchan * sizeof(adata_t));
1070:
1071: /*
1072: * Insert a converter, if needed.
1073: */
1074: if (!aparams_native(&d->par)) {
1075: dec_init(&d->dec, &d->par, d->rchan);
1076: d->decbuf = xmalloc(d->round * d->rchan * d->par.bps);
1077: } else
1078: d->decbuf = NULL;
1079: }
1080: if (d->mode & MODE_PLAY) {
1081: /*
1082: * Create device <-> mixer buffer
1083: */
1084: d->poffs = 0;
1.15 ratchov 1085: d->psize = d->bufsz + d->round;
1086: d->pbuf = xmalloc(d->psize * d->pchan * sizeof(adata_t));
1.1 ratchov 1087: d->mode |= MODE_MON;
1088:
1089: /*
1090: * Append a converter, if needed.
1091: */
1092: if (!aparams_native(&d->par)) {
1093: enc_init(&d->enc, &d->par, d->pchan);
1094: d->encbuf = xmalloc(d->round * d->pchan * d->par.bps);
1095: } else
1096: d->encbuf = NULL;
1097: }
1098: d->pstate = DEV_INIT;
1099: if (log_level >= 2) {
1100: dev_log(d);
1101: log_puts(": ");
1102: log_putu(d->rate);
1103: log_puts("Hz, ");
1104: aparams_log(&d->par);
1105: if (d->mode & MODE_PLAY) {
1106: log_puts(", play 0:");
1107: log_puti(d->pchan - 1);
1108: }
1109: if (d->mode & MODE_REC) {
1110: log_puts(", rec 0:");
1111: log_puti(d->rchan - 1);
1112: }
1113: log_puts(", ");
1114: log_putu(d->bufsz / d->round);
1115: log_puts(" blocks of ");
1116: log_putu(d->round);
1117: log_puts(" frames\n");
1118: }
1119: return 1;
1120: }
1121:
1122: /*
1123: * force the device to go in DEV_CFG state, the caller is supposed to
1124: * ensure buffers are drained
1125: */
1126: void
1127: dev_close(struct dev *d)
1128: {
1.3 ratchov 1129: int i;
1130: struct slot *s;
1.1 ratchov 1131:
1132: #ifdef DEBUG
1133: if (log_level >= 3) {
1134: dev_log(d);
1135: log_puts(": closing\n");
1136: }
1137: #endif
1.3 ratchov 1138: d->pstate = DEV_CFG;
1139: for (s = d->slot, i = DEV_NSLOT; i > 0; i--, s++) {
1.1 ratchov 1140: if (s->ops)
1141: s->ops->exit(s->arg);
1142: s->ops = NULL;
1143: }
1.3 ratchov 1144: d->slot_list = NULL;
1.1 ratchov 1145: dev_sio_close(d);
1146: if (d->mode & MODE_PLAY) {
1147: if (d->encbuf != NULL)
1148: xfree(d->encbuf);
1149: xfree(d->pbuf);
1150: }
1151: if (d->mode & MODE_REC) {
1152: if (d->decbuf != NULL)
1153: xfree(d->decbuf);
1154: xfree(d->rbuf);
1155: }
1156: }
1157:
1158: int
1159: dev_ref(struct dev *d)
1160: {
1161: #ifdef DEBUG
1162: if (log_level >= 3) {
1163: dev_log(d);
1164: log_puts(": device requested\n");
1165: }
1166: #endif
1167: if (d->pstate == DEV_CFG && !dev_open(d))
1168: return 0;
1169: d->refcnt++;
1170: return 1;
1171: }
1172:
1173: void
1174: dev_unref(struct dev *d)
1175: {
1176: #ifdef DEBUG
1177: if (log_level >= 3) {
1178: dev_log(d);
1179: log_puts(": device released\n");
1180: }
1181: #endif
1182: d->refcnt--;
1183: if (d->refcnt == 0 && d->pstate == DEV_INIT)
1184: dev_close(d);
1185: }
1186:
1187: /*
1188: * initialize the device with the current parameters
1189: */
1190: int
1191: dev_init(struct dev *d)
1192: {
1193: if ((d->reqmode & MODE_AUDIOMASK) == 0) {
1194: #ifdef DEBUG
1195: dev_log(d);
1196: log_puts(": has no streams\n");
1197: #endif
1198: return 0;
1199: }
1200: if (d->hold && !dev_ref(d))
1201: return 0;
1202: return 1;
1203: }
1204:
1205: /*
1206: * Unless the device is already in process of closing, request it to close
1207: */
1208: void
1209: dev_done(struct dev *d)
1210: {
1211: #ifdef DEBUG
1212: if (log_level >= 3) {
1213: dev_log(d);
1214: log_puts(": draining\n");
1215: }
1216: #endif
1.20 ratchov 1217: if (d->tstate != MMC_STOP)
1218: dev_mmcstop(d);
1.1 ratchov 1219: if (d->hold)
1220: dev_unref(d);
1221: }
1222:
1223: struct dev *
1224: dev_bynum(int num)
1225: {
1226: struct dev *d;
1227:
1228: for (d = dev_list; d != NULL; d = d->next) {
1.19 ratchov 1229: if (d->num == num)
1.1 ratchov 1230: return d;
1231: }
1232: return NULL;
1233: }
1234:
1235: /*
1236: * Free the device
1237: */
1238: void
1239: dev_del(struct dev *d)
1240: {
1241: struct dev **p;
1242:
1243: #ifdef DEBUG
1244: if (log_level >= 3) {
1245: dev_log(d);
1246: log_puts(": deleting\n");
1247: }
1248: #endif
1249: if (d->pstate != DEV_CFG)
1250: dev_close(d);
1251: for (p = &dev_list; *p != d; p = &(*p)->next) {
1252: #ifdef DEBUG
1253: if (*p == NULL) {
1254: dev_log(d);
1255: log_puts(": device to delete not on the list\n");
1256: panic();
1257: }
1258: #endif
1259: }
1260: midi_del(d->midi);
1261: *p = d->next;
1.21 ! ratchov 1262: xfree(d->path);
1.1 ratchov 1263: xfree(d);
1264: }
1265:
1266: unsigned int
1267: dev_roundof(struct dev *d, unsigned int newrate)
1268: {
1269: return (d->round * newrate + d->rate / 2) / d->rate;
1270: }
1271:
1272: /*
1273: * If the device is paused, then resume it.
1274: */
1275: void
1276: dev_wakeup(struct dev *d)
1277: {
1278: if (d->pstate == DEV_INIT) {
1279: if (log_level >= 2) {
1280: dev_log(d);
1281: log_puts(": device started\n");
1282: }
1283: if (d->mode & MODE_PLAY) {
1284: d->prime = d->bufsz;
1285: } else {
1286: d->prime = 0;
1287: }
1.16 ratchov 1288: d->poffs = 0;
1.12 ratchov 1289:
1.16 ratchov 1290: /*
1291: * empty cycles don't increment delta, so it's ok to
1292: * start at 0
1293: **/
1.12 ratchov 1294: d->delta = 0;
1295:
1.1 ratchov 1296: d->pstate = DEV_RUN;
1297: dev_sio_start(d);
1298: }
1299: }
1300:
1301: /*
1302: * check that all clients controlled by MMC are ready to start, if so,
1303: * attach them all at the same position
1304: */
1305: void
1306: dev_sync_attach(struct dev *d)
1307: {
1308: int i;
1309: struct slot *s;
1310:
1311: if (d->tstate != MMC_START) {
1312: if (log_level >= 2) {
1313: dev_log(d);
1314: log_puts(": not started by mmc yet, waiting...\n");
1315: }
1316: return;
1317: }
1318: for (i = 0; i < DEV_NSLOT; i++) {
1319: s = d->slot + i;
1320: if (!s->ops || s->tstate == MMC_OFF)
1321: continue;
1322: if (s->tstate != MMC_START || s->pstate != SLOT_READY) {
1323: #ifdef DEBUG
1324: if (log_level >= 3) {
1325: slot_log(s);
1326: log_puts(": not ready, start delayed\n");
1327: }
1328: #endif
1329: return;
1330: }
1331: }
1332: if (!dev_ref(d))
1333: return;
1334: for (i = 0; i < DEV_NSLOT; i++) {
1335: s = d->slot + i;
1336: if (!s->ops)
1337: continue;
1338: if (s->tstate == MMC_START) {
1339: #ifdef DEBUG
1340: if (log_level >= 3) {
1341: slot_log(s);
1342: log_puts(": started\n");
1343: }
1344: #endif
1345: s->tstate = MMC_RUN;
1346: slot_attach(s);
1347: }
1348: }
1349: d->tstate = MMC_RUN;
1350: dev_midi_full(d);
1351: dev_wakeup(d);
1352: }
1353:
1354: /*
1355: * start all slots simultaneously
1356: */
1357: void
1358: dev_mmcstart(struct dev *d)
1359: {
1360: if (d->tstate == MMC_STOP) {
1361: d->tstate = MMC_START;
1362: dev_sync_attach(d);
1363: #ifdef DEBUG
1364: } else {
1365: if (log_level >= 3) {
1366: dev_log(d);
1367: log_puts(": ignoring mmc start\n");
1368: }
1369: #endif
1370: }
1371: }
1372:
1373: /*
1374: * stop all slots simultaneously
1375: */
1376: void
1377: dev_mmcstop(struct dev *d)
1378: {
1379: switch (d->tstate) {
1380: case MMC_START:
1381: d->tstate = MMC_STOP;
1382: return;
1383: case MMC_RUN:
1384: d->tstate = MMC_STOP;
1385: dev_unref(d);
1386: break;
1387: default:
1388: #ifdef DEBUG
1389: if (log_level >= 3) {
1390: dev_log(d);
1391: log_puts(": ignored mmc stop\n");
1392: }
1393: #endif
1394: return;
1395: }
1396: }
1397:
1398: /*
1399: * relocate all slots simultaneously
1400: */
1401: void
1402: dev_mmcloc(struct dev *d, unsigned int origin)
1403: {
1404: if (log_level >= 2) {
1405: dev_log(d);
1406: log_puts(": relocated to ");
1407: log_putu(origin);
1408: log_puts("\n");
1409: }
1410: if (d->tstate == MMC_RUN)
1411: dev_mmcstop(d);
1412: d->mtc.origin = origin;
1413: if (d->tstate == MMC_RUN)
1414: dev_mmcstart(d);
1415: }
1416:
1417: /*
1418: * allocate a new slot and register the given call-backs
1419: */
1420: struct slot *
1421: slot_new(struct dev *d, char *who, struct slotops *ops, void *arg, int mode)
1422: {
1423: char *p;
1424: char name[SLOT_NAMEMAX];
1425: unsigned int i, unit, umap = 0;
1426: unsigned int ser, bestser, bestidx;
1427: struct slot *s;
1428:
1429: /*
1430: * create a ``valid'' control name (lowcase, remove [^a-z], trucate)
1431: */
1432: for (i = 0, p = who; ; p++) {
1433: if (i == SLOT_NAMEMAX - 1 || *p == '\0') {
1434: name[i] = '\0';
1435: break;
1436: } else if (*p >= 'A' && *p <= 'Z') {
1437: name[i++] = *p + 'a' - 'A';
1438: } else if (*p >= 'a' && *p <= 'z')
1439: name[i++] = *p;
1440: }
1441: if (i == 0)
1442: strlcpy(name, "noname", SLOT_NAMEMAX);
1443:
1444: /*
1445: * find the first unused "unit" number for this name
1446: */
1447: for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) {
1448: if (s->ops == NULL)
1449: continue;
1450: if (strcmp(s->name, name) == 0)
1451: umap |= (1 << s->unit);
1452: }
1453: for (unit = 0; ; unit++) {
1454: if ((umap & (1 << unit)) == 0)
1455: break;
1456: }
1457:
1458: /*
1.18 ratchov 1459: * find a free controller slot with the same name/unit
1.1 ratchov 1460: */
1461: for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) {
1462: if (s->ops == NULL &&
1463: strcmp(s->name, name) == 0 &&
1464: s->unit == unit) {
1465: #ifdef DEBUG
1466: if (log_level >= 3) {
1467: log_puts(name);
1468: log_putu(unit);
1469: log_puts(": reused\n");
1470: }
1471: #endif
1472: goto found;
1473: }
1474: }
1475:
1476: /*
1.18 ratchov 1477: * couldn't find a matching slot, pick oldest free slot
1.1 ratchov 1478: * and set its name/unit
1479: */
1480: bestser = 0;
1481: bestidx = DEV_NSLOT;
1482: for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) {
1483: if (s->ops != NULL)
1484: continue;
1485: ser = d->serial - s->serial;
1486: if (ser > bestser) {
1487: bestser = ser;
1488: bestidx = i;
1489: }
1490: }
1491: if (bestidx == DEV_NSLOT) {
1492: if (log_level >= 1) {
1493: log_puts(name);
1494: log_putu(unit);
1495: log_puts(": out of sub-device slots\n");
1496: }
1497: return NULL;
1498: }
1499: s = d->slot + bestidx;
1500: if (s->name[0] != '\0')
1501: s->vol = MIDI_MAXCTL;
1502: strlcpy(s->name, name, SLOT_NAMEMAX);
1503: s->serial = d->serial++;
1504: s->unit = unit;
1505: #ifdef DEBUG
1506: if (log_level >= 3) {
1507: log_puts(name);
1508: log_putu(unit);
1509: log_puts(": overwritten slot ");
1510: log_putu(bestidx);
1511: log_puts("\n");
1512: }
1513: #endif
1514:
1515: found:
1516: if (!dev_ref(d))
1517: return NULL;
1518: s->dev = d;
1519: s->ops = ops;
1520: s->arg = arg;
1521: s->pstate = SLOT_INIT;
1522: s->tstate = MMC_OFF;
1523:
1524: if ((mode & s->dev->mode) != mode) {
1525: if (log_level >= 1) {
1526: slot_log(s);
1527: log_puts(": requested mode not supported\n");
1528: }
1529: return 0;
1530: }
1531: s->mode = mode;
1.6 ratchov 1532: aparams_init(&s->par);
1.1 ratchov 1533: if (s->mode & MODE_PLAY) {
1.6 ratchov 1534: s->mix.slot_cmin = s->mix.dev_cmin = 0;
1535: s->mix.slot_cmax = s->mix.dev_cmax = d->pchan - 1;
1.1 ratchov 1536: }
1537: if (s->mode & MODE_RECMASK) {
1.6 ratchov 1538: s->sub.slot_cmin = s->sub.dev_cmin = 0;
1539: s->sub.slot_cmax = s->sub.dev_cmax =
1540: ((s->mode & MODE_MON) ? d->pchan : d->rchan) - 1;
1.1 ratchov 1541: }
1542: s->xrun = XRUN_IGNORE;
1543: s->dup = 0;
1544: s->appbufsz = d->bufsz;
1545: s->round = d->round;
1.5 ratchov 1546: s->rate = d->rate;
1.6 ratchov 1547: s->mix.maxweight = ADATA_UNIT;
1.1 ratchov 1548: dev_midi_slotdesc(d, s);
1549: dev_midi_vol(d, s);
1550: return s;
1551: }
1552:
1553: /*
1554: * release the given slot
1555: */
1556: void
1557: slot_del(struct slot *s)
1558: {
1559: s->arg = s;
1560: s->ops = &zomb_slotops;
1561: switch (s->pstate) {
1562: case SLOT_INIT:
1563: s->ops = NULL;
1564: break;
1565: case SLOT_START:
1566: case SLOT_READY:
1567: case SLOT_RUN:
1568: slot_stop(s);
1569: /* PASSTHROUGH */
1570: case SLOT_STOP:
1571: break;
1572: }
1573: dev_unref(s->dev);
1574: s->dev = NULL;
1575: }
1576:
1577: /*
1578: * change the slot play volume; called either by the slot or by MIDI
1579: */
1580: void
1581: slot_setvol(struct slot *s, unsigned int vol)
1582: {
1583: #ifdef DEBUG
1584: if (log_level >= 3) {
1585: slot_log(s);
1586: log_puts(": setting volume ");
1587: log_putu(vol);
1588: log_puts("\n");
1589: }
1590: #endif
1591: s->vol = vol;
1592: if (s->ops == NULL)
1593: return;
1594: s->mix.vol = MIDI_TO_ADATA(s->vol);
1595: }
1596:
1597: /*
1598: * attach the slot to the device (ie start playing & recording
1599: */
1600: void
1601: slot_attach(struct slot *s)
1602: {
1603: struct dev *d = s->dev;
1604: unsigned int slot_nch, dev_nch;
1.12 ratchov 1605: long long pos;
1.1 ratchov 1606: int startpos;
1607:
1608: /*
1609: * start the device if not started
1610: */
1611: dev_wakeup(d);
1612:
1613: /*
1614: * get the current position, the origin is when the first sample
1615: * played and/or recorded
1616: */
1617: startpos = dev_getpos(d) * (int)s->round / (int)d->round;
1.12 ratchov 1618:
1619: /*
1620: * adjust initial clock
1621: */
1622: pos = (long long)d->delta * s->round;
1623: s->delta = startpos + pos / (int)d->round;
1624: s->delta_rem = pos % d->round;
1625:
1.1 ratchov 1626: s->pstate = SLOT_RUN;
1627: #ifdef DEBUG
1.17 ratchov 1628: if (log_level >= 2) {
1.1 ratchov 1629: slot_log(s);
1630: log_puts(": attached at ");
1631: log_puti(startpos);
1.12 ratchov 1632: log_puts(", delta = ");
1633: log_puti(d->delta);
1.1 ratchov 1634: log_puts("\n");
1635: }
1636: #endif
1637:
1638: /*
1639: * We dont check whether the device is dying,
1640: * because dev_xxx() functions are supposed to
1641: * work (i.e., not to crash)
1642: */
1643: #ifdef DEBUG
1644: if ((s->mode & d->mode) != s->mode) {
1645: slot_log(s);
1646: log_puts(": mode beyond device mode, not attaching\n");
1647: panic();
1648: }
1649: #endif
1650: s->next = d->slot_list;
1651: d->slot_list = s;
1.12 ratchov 1652: s->skip = 0;
1.1 ratchov 1653: if (s->mode & MODE_PLAY) {
1654: slot_nch = s->mix.slot_cmax - s->mix.slot_cmin + 1;
1655: dev_nch = s->mix.dev_cmax - s->mix.dev_cmin + 1;
1656: s->mix.decbuf = NULL;
1657: s->mix.resampbuf = NULL;
1658: s->mix.join = 1;
1659: s->mix.expand = 1;
1660: if (s->dup) {
1661: if (dev_nch > slot_nch)
1662: s->mix.expand = dev_nch / slot_nch;
1663: else if (dev_nch < slot_nch)
1664: s->mix.join = slot_nch / dev_nch;
1665: }
1666: cmap_init(&s->mix.cmap,
1667: s->mix.slot_cmin, s->mix.slot_cmax,
1668: s->mix.slot_cmin, s->mix.slot_cmax,
1669: 0, d->pchan - 1,
1670: s->mix.dev_cmin, s->mix.dev_cmax);
1671: if (!aparams_native(&s->par)) {
1672: dec_init(&s->mix.dec, &s->par, slot_nch);
1673: s->mix.decbuf =
1.4 ratchov 1674: xmalloc(s->round * slot_nch * sizeof(adata_t));
1.1 ratchov 1675: }
1676: if (s->rate != d->rate) {
1677: resamp_init(&s->mix.resamp, s->round, d->round,
1678: slot_nch);
1679: s->mix.resampbuf =
1680: xmalloc(d->round * slot_nch * sizeof(adata_t));
1681: }
1682: s->mix.vol = MIDI_TO_ADATA(s->vol);
1683: dev_mix_adjvol(d);
1684: }
1685: if (s->mode & MODE_RECMASK) {
1686: slot_nch = s->sub.slot_cmax - s->sub.slot_cmin + 1;
1687: dev_nch = s->sub.dev_cmax - s->sub.dev_cmin + 1;
1688: s->sub.encbuf = NULL;
1689: s->sub.resampbuf = NULL;
1690: s->sub.join = 1;
1691: s->sub.expand = 1;
1692: if (s->dup) {
1693: if (dev_nch > slot_nch)
1694: s->sub.join = dev_nch / slot_nch;
1695: else if (dev_nch < slot_nch)
1696: s->sub.expand = slot_nch / dev_nch;
1697: }
1698: cmap_init(&s->sub.cmap,
1699: 0, ((s->mode & MODE_MON) ? d->pchan : d->rchan) - 1,
1700: s->sub.dev_cmin, s->sub.dev_cmax,
1701: s->sub.slot_cmin, s->sub.slot_cmax,
1702: s->sub.slot_cmin, s->sub.slot_cmax);
1703: if (s->rate != d->rate) {
1704: resamp_init(&s->sub.resamp, d->round, s->round,
1705: slot_nch);
1706: s->sub.resampbuf =
1707: xmalloc(d->round * slot_nch * sizeof(adata_t));
1708: }
1709: if (!aparams_native(&s->par)) {
1710: enc_init(&s->sub.enc, &s->par, slot_nch);
1711: s->sub.encbuf =
1.4 ratchov 1712: xmalloc(s->round * slot_nch * sizeof(adata_t));
1.1 ratchov 1713: }
1714:
1715: /*
1716: * N-th recorded block is the N-th played block
1717: */
1.12 ratchov 1718: s->sub.prime = -startpos / (int)s->round;
1.1 ratchov 1719: }
1720: }
1721:
1722: /*
1723: * if MMC is enabled, and try to attach all slots synchronously, else
1724: * simply attach the slot
1725: */
1726: void
1727: slot_ready(struct slot *s)
1728: {
1.3 ratchov 1729: /*
1730: * device may be disconnected, and if so we're called from
1731: * slot->ops->exit() on a closed device
1732: */
1733: if (s->dev->pstate == DEV_CFG)
1734: return;
1.1 ratchov 1735: if (s->tstate == MMC_OFF)
1736: slot_attach(s);
1737: else {
1738: s->tstate = MMC_START;
1739: dev_sync_attach(s->dev);
1740: }
1741: }
1742:
1743: /*
1744: * setup buffers & conversion layers, prepare the slot to receive data
1745: * (for playback) or start (recording).
1746: */
1747: void
1748: slot_start(struct slot *s)
1749: {
1750: unsigned int bufsz;
1751: #ifdef DEBUG
1752: struct dev *d = s->dev;
1753:
1754:
1755: if (s->pstate != SLOT_INIT) {
1756: slot_log(s);
1757: log_puts(": slot_start: wrong state\n");
1758: panic();
1759: }
1760: #endif
1761: bufsz = s->appbufsz;
1762: if (s->mode & MODE_PLAY) {
1763: #ifdef DEBUG
1764: if (log_level >= 3) {
1765: slot_log(s);
1766: log_puts(": playing ");
1767: aparams_log(&s->par);
1768: log_puts(" -> ");
1769: aparams_log(&d->par);
1770: log_puts("\n");
1771: }
1772: #endif
1773: s->mix.bpf = s->par.bps *
1774: (s->mix.slot_cmax - s->mix.slot_cmin + 1);
1775: abuf_init(&s->mix.buf, bufsz * s->mix.bpf);
1776: }
1777: if (s->mode & MODE_RECMASK) {
1778: #ifdef DEBUG
1779: if (log_level >= 3) {
1780: slot_log(s);
1781: log_puts(": recording ");
1782: aparams_log(&s->par);
1783: log_puts(" <- ");
1784: aparams_log(&d->par);
1785: log_puts("\n");
1786: }
1787: #endif
1788: s->sub.bpf = s->par.bps *
1789: (s->sub.slot_cmax - s->sub.slot_cmin + 1);
1790: abuf_init(&s->sub.buf, bufsz * s->sub.bpf);
1791: }
1792: s->mix.weight = MIDI_TO_ADATA(MIDI_MAXCTL);
1793: #ifdef DEBUG
1794: if (log_level >= 3) {
1795: slot_log(s);
1796: log_puts(": allocated ");
1797: log_putu(s->appbufsz);
1798: log_puts("/");
1799: log_putu(SLOT_BUFSZ(s));
1800: log_puts(" fr buffers\n");
1801: }
1802: #endif
1803: if (s->mode & MODE_PLAY) {
1804: s->pstate = SLOT_START;
1805: } else {
1806: s->pstate = SLOT_READY;
1807: slot_ready(s);
1808: }
1809: }
1810:
1811: /*
1812: * stop playback and recording, and free conversion layers
1813: */
1814: void
1815: slot_detach(struct slot *s)
1816: {
1817: struct slot **ps;
1818:
1819: #ifdef DEBUG
1820: if (log_level >= 3) {
1821: slot_log(s);
1822: log_puts(": detaching\n");
1823: }
1824: #endif
1825: for (ps = &s->dev->slot_list; *ps != s; ps = &(*ps)->next) {
1826: #ifdef DEBUG
1827: if (s == NULL) {
1828: slot_log(s);
1829: log_puts(": can't detach, not on list\n");
1830: panic();
1831: }
1832: #endif
1833: }
1834: *ps = s->next;
1835: if (s->mode & MODE_RECMASK) {
1836: if (s->sub.encbuf)
1837: xfree(s->sub.encbuf);
1838: if (s->sub.resampbuf)
1839: xfree(s->sub.resampbuf);
1840: }
1841: if (s->mode & MODE_PLAY) {
1842: if (s->mix.decbuf)
1843: xfree(s->mix.decbuf);
1844: if (s->mix.resampbuf)
1845: xfree(s->mix.resampbuf);
1846: dev_mix_adjvol(s->dev);
1847: }
1848: }
1849:
1850: /*
1851: * put the slot in stopping state (draining play buffers) or
1852: * stop & detach if no data to drain.
1853: */
1854: void
1855: slot_stop(struct slot *s)
1856: {
1857: #ifdef DEBUG
1858: if (log_level >= 3) {
1859: slot_log(s);
1860: log_puts(": stopping\n");
1861: }
1862: #endif
1863: if (s->pstate == SLOT_START) {
1864: if (s->mode & MODE_PLAY) {
1865: s->pstate = SLOT_READY;
1866: slot_ready(s);
1867: } else
1868: s->pstate = SLOT_INIT;
1869: }
1870: if (s->mode & MODE_RECMASK)
1871: abuf_done(&s->sub.buf);
1872: if (s->pstate == SLOT_READY) {
1873: #ifdef DEBUG
1874: if (log_level >= 3) {
1875: slot_log(s);
1876: log_puts(": not drained (blocked by mmc)\n");
1877: }
1878: #endif
1879: if (s->mode & MODE_PLAY)
1880: abuf_done(&s->mix.buf);
1881: s->ops->eof(s->arg);
1882: s->pstate = SLOT_INIT;
1883: } else {
1884: /* s->pstate == SLOT_RUN */
1885: if (s->mode & MODE_PLAY)
1886: s->pstate = SLOT_STOP;
1887: else {
1888: slot_detach(s);
1889: s->pstate = SLOT_INIT;
1890: s->ops->eof(s->arg);
1891: }
1892: }
1893: if (s->tstate != MMC_OFF)
1894: s->tstate = MMC_STOP;
1895: }
1896:
1.12 ratchov 1897: void
1898: slot_skip_update(struct slot *s)
1899: {
1900: int skip;
1901:
1902: skip = slot_skip(s);
1903: while (skip > 0) {
1904: #ifdef DEBUG
1905: if (log_level >= 4) {
1906: slot_log(s);
1907: log_puts(": catching skipped block\n");
1908: }
1909: #endif
1910: if (s->mode & MODE_RECMASK)
1911: s->ops->flush(s->arg);
1912: if (s->mode & MODE_PLAY)
1913: s->ops->fill(s->arg);
1914: skip--;
1915: }
1916: }
1917:
1.1 ratchov 1918: /*
1919: * notify the slot that we just wrote in the play buffer, must be called
1920: * after each write
1921: */
1922: void
1923: slot_write(struct slot *s)
1924: {
1925: if (s->pstate == SLOT_START && s->mix.buf.used == s->mix.buf.len) {
1926: #ifdef DEBUG
1927: if (log_level >= 4) {
1928: slot_log(s);
1929: log_puts(": switching to READY state\n");
1930: }
1931: #endif
1932: s->pstate = SLOT_READY;
1933: slot_ready(s);
1934: }
1.12 ratchov 1935: slot_skip_update(s);
1.1 ratchov 1936: }
1937:
1938: /*
1939: * notify the slot that we freed some space in the rec buffer
1940: */
1941: void
1942: slot_read(struct slot *s)
1943: {
1.12 ratchov 1944: slot_skip_update(s);
1.1 ratchov 1945: }