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