-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdev.c
2480 lines (2230 loc) · 50.4 KB
/
dev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $OpenBSD$ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include "bsd-compat.h"
#include "abuf.h"
#include "defs.h"
#include "dev.h"
#include "dsp.h"
#include "siofile.h"
#include "midi.h"
#include "opt.h"
#include "sysex.h"
#include "utils.h"
void zomb_onmove(void *);
void zomb_onvol(void *);
void zomb_fill(void *);
void zomb_flush(void *);
void zomb_eof(void *);
void zomb_exit(void *);
void dev_mix_badd(struct dev *, struct slot *);
void dev_mix_adjvol(struct dev *);
void dev_sub_bcopy(struct dev *, struct slot *);
void dev_onmove(struct dev *, int);
void dev_master(struct dev *, unsigned int);
void dev_cycle(struct dev *);
void dev_adjpar(struct dev *, int, int, int);
int dev_allocbufs(struct dev *);
void dev_freebufs(struct dev *);
int dev_ref(struct dev *);
void dev_unref(struct dev *);
int dev_init(struct dev *);
void dev_done(struct dev *);
struct dev *dev_bynum(int);
void dev_del(struct dev *);
unsigned int dev_roundof(struct dev *, unsigned int);
void dev_wakeup(struct dev *);
void slot_ctlname(struct slot *, char *, size_t);
void slot_del(struct slot *);
void slot_setvol(struct slot *, unsigned int);
void slot_ready(struct slot *);
void slot_allocbufs(struct slot *);
void slot_freebufs(struct slot *);
void slot_skip_update(struct slot *);
void slot_write(struct slot *);
void slot_read(struct slot *);
int slot_skip(struct slot *);
struct slotops zomb_slotops = {
zomb_onmove,
zomb_onvol,
zomb_fill,
zomb_flush,
zomb_eof,
zomb_exit
};
struct ctl *ctl_list = NULL;
struct dev *dev_list = NULL;
unsigned int dev_sndnum = 0;
struct ctlslot ctlslot_array[DEV_NCTLSLOT];
struct slot slot_array[DEV_NSLOT];
unsigned int slot_serial; /* for slot allocation */
/*
* we support/need a single MTC clock source only
*/
struct mtc mtc_array[1] = {
{.dev = NULL, .tstate = MTC_STOP}
};
void
slot_array_init(void)
{
unsigned int i;
for (i = 0; i < DEV_NSLOT; i++) {
slot_array[i].unit = i;
slot_array[i].ops = NULL;
slot_array[i].vol = MIDI_MAXCTL;
slot_array[i].opt = NULL;
slot_array[i].serial = slot_serial++;
memset(slot_array[i].name, 0, SLOT_NAMEMAX);
}
}
void
slot_ctlname(struct slot *s, char *name, size_t size)
{
snprintf(name, size, "%s%u", s->name, s->unit);
}
void
zomb_onmove(void *arg)
{
}
void
zomb_onvol(void *arg)
{
}
void
zomb_fill(void *arg)
{
}
void
zomb_flush(void *arg)
{
}
void
zomb_eof(void *arg)
{
struct slot *s = arg;
#ifdef DEBUG
logx(3, "%s%u: %s", s->name, s->unit, __func__);
#endif
s->ops = NULL;
}
void
zomb_exit(void *arg)
{
#ifdef DEBUG
struct slot *s = arg;
logx(3, "%s%u: %s", s->name, s->unit, __func__);
#endif
}
size_t
chans_fmt(char *buf, size_t size, int mode, int pmin, int pmax, int rmin, int rmax)
{
const char *sep = "";
char *end = buf + size;
char *p = buf;
if (mode & MODE_PLAY) {
p += snprintf(p, p < end ? end - p : 0, "play %d:%d", pmin, pmax);
sep = ", ";
}
if (mode & MODE_RECMASK) {
p += snprintf(p, p < end ? end - p : 0, "%s%s %d:%d", sep,
(mode & MODE_MON) ? "mon" : "rec", rmin, rmax);
}
return p - buf;
}
/*
* Broadcast MIDI data to all opts using this device
*/
void
dev_midi_send(struct dev *d, void *msg, int msglen)
{
struct opt *o;
for (o = opt_list; o != NULL; o = o->next) {
if (o->dev != d)
continue;
midi_send(o->midi, msg, msglen);
}
}
/*
* send a quarter frame MTC message
*/
void
mtc_midi_qfr(struct mtc *mtc, int delta)
{
unsigned char buf[2];
unsigned int data;
int qfrlen;
mtc->delta += delta * MTC_SEC;
qfrlen = mtc->dev->rate * (MTC_SEC / (4 * mtc->fps));
while (mtc->delta >= qfrlen) {
switch (mtc->qfr) {
case 0:
data = mtc->fr & 0xf;
break;
case 1:
data = mtc->fr >> 4;
break;
case 2:
data = mtc->sec & 0xf;
break;
case 3:
data = mtc->sec >> 4;
break;
case 4:
data = mtc->min & 0xf;
break;
case 5:
data = mtc->min >> 4;
break;
case 6:
data = mtc->hr & 0xf;
break;
case 7:
data = (mtc->hr >> 4) | (mtc->fps_id << 1);
/*
* tick messages are sent 2 frames ahead
*/
mtc->fr += 2;
if (mtc->fr < mtc->fps)
break;
mtc->fr -= mtc->fps;
mtc->sec++;
if (mtc->sec < 60)
break;
mtc->sec = 0;
mtc->min++;
if (mtc->min < 60)
break;
mtc->min = 0;
mtc->hr++;
if (mtc->hr < 24)
break;
mtc->hr = 0;
break;
default:
/* NOTREACHED */
data = 0;
}
buf[0] = 0xf1;
buf[1] = (mtc->qfr << 4) | data;
mtc->qfr++;
mtc->qfr &= 7;
dev_midi_send(mtc->dev, buf, 2);
mtc->delta -= qfrlen;
}
}
/*
* send a full frame MTC message
*/
void
mtc_midi_full(struct mtc *mtc)
{
struct sysex x;
unsigned int fps;
mtc->delta = -MTC_SEC * (int)mtc->dev->bufsz;
if (mtc->dev->rate % (30 * 4 * mtc->dev->round) == 0) {
mtc->fps_id = MTC_FPS_30;
mtc->fps = 30;
} else if (mtc->dev->rate % (25 * 4 * mtc->dev->round) == 0) {
mtc->fps_id = MTC_FPS_25;
mtc->fps = 25;
} else {
mtc->fps_id = MTC_FPS_24;
mtc->fps = 24;
}
#ifdef DEBUG
logx(3, "%s: mtc full frame at %d, %d fps", mtc->dev->path, mtc->delta, mtc->fps);
#endif
fps = mtc->fps;
mtc->hr = (mtc->origin / (MTC_SEC * 3600)) % 24;
mtc->min = (mtc->origin / (MTC_SEC * 60)) % 60;
mtc->sec = (mtc->origin / (MTC_SEC)) % 60;
mtc->fr = (mtc->origin / (MTC_SEC / fps)) % fps;
x.start = SYSEX_START;
x.type = SYSEX_TYPE_RT;
x.dev = SYSEX_DEV_ANY;
x.id0 = SYSEX_MTC;
x.id1 = SYSEX_MTC_FULL;
x.u.full.hr = mtc->hr | (mtc->fps_id << 5);
x.u.full.min = mtc->min;
x.u.full.sec = mtc->sec;
x.u.full.fr = mtc->fr;
x.u.full.end = SYSEX_END;
mtc->qfr = 0;
dev_midi_send(mtc->dev, (unsigned char *)&x, SYSEX_SIZE(full));
}
/*
* send a volume change MIDI message
*/
void
dev_midi_vol(struct dev *d, struct slot *s)
{
unsigned char msg[3];
msg[0] = MIDI_CTL | (s - slot_array);
msg[1] = MIDI_CTL_VOL;
msg[2] = s->vol;
dev_midi_send(d, msg, 3);
}
/*
* send a master volume MIDI message
*/
void
dev_midi_master(struct dev *d)
{
struct ctl *c;
unsigned int master, v;
struct sysex x;
if (d->master_enabled)
master = d->master;
else {
master = 0;
for (c = ctl_list; c != NULL; c = c->next) {
if (c->type != CTL_NUM ||
strcmp(c->group, d->name) != 0 ||
strcmp(c->node0.name, "output") != 0 ||
strcmp(c->func, "level") != 0)
continue;
if (c->u.any.arg0 != d)
continue;
v = (c->curval * 127 + c->maxval / 2) / c->maxval;
if (master < v)
master = v;
}
}
memset(&x, 0, sizeof(struct sysex));
x.start = SYSEX_START;
x.type = SYSEX_TYPE_RT;
x.dev = SYSEX_DEV_ANY;
x.id0 = SYSEX_CONTROL;
x.id1 = SYSEX_MASTER;
x.u.master.fine = 0;
x.u.master.coarse = master;
x.u.master.end = SYSEX_END;
dev_midi_send(d, (unsigned char *)&x, SYSEX_SIZE(master));
}
/*
* send a sndiod-specific slot description MIDI message
*/
void
dev_midi_slotdesc(struct dev *d, struct slot *s)
{
struct sysex x;
memset(&x, 0, sizeof(struct sysex));
x.start = SYSEX_START;
x.type = SYSEX_TYPE_EDU;
x.dev = SYSEX_DEV_ANY;
x.id0 = SYSEX_AUCAT;
x.id1 = SYSEX_AUCAT_SLOTDESC;
if (s->opt != NULL && s->opt->dev == d)
slot_ctlname(s, (char *)x.u.slotdesc.name, SYSEX_NAMELEN);
x.u.slotdesc.chan = (s - slot_array);
x.u.slotdesc.end = SYSEX_END;
dev_midi_send(d, (unsigned char *)&x, SYSEX_SIZE(slotdesc));
}
void
dev_midi_dump(struct dev *d)
{
struct sysex x;
struct slot *s;
int i;
dev_midi_master(d);
for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) {
if (s->opt != NULL && s->opt->dev != d)
continue;
dev_midi_slotdesc(d, s);
dev_midi_vol(d, s);
}
x.start = SYSEX_START;
x.type = SYSEX_TYPE_EDU;
x.dev = SYSEX_DEV_ANY;
x.id0 = SYSEX_AUCAT;
x.id1 = SYSEX_AUCAT_DUMPEND;
x.u.dumpend.end = SYSEX_END;
dev_midi_send(d, (unsigned char *)&x, SYSEX_SIZE(dumpend));
}
int
slot_skip(struct slot *s)
{
unsigned char *data = (unsigned char *)0xdeadbeef; /* please gcc */
int max, count;
max = s->skip;
while (s->skip > 0) {
if (s->pstate != SLOT_STOP && (s->mode & MODE_RECMASK)) {
data = abuf_wgetblk(&s->sub.buf, &count);
if (count < s->round * s->sub.bpf)
break;
}
if (s->mode & MODE_PLAY) {
if (s->mix.buf.used < s->round * s->mix.bpf)
break;
}
#ifdef DEBUG
logx(4, "%s%u: skipped a cycle", s->name, s->unit);
#endif
if (s->pstate != SLOT_STOP && (s->mode & MODE_RECMASK)) {
if (s->sub.encbuf)
enc_sil_do(&s->sub.enc, data, s->round);
else
memset(data, 0, s->round * s->sub.bpf);
abuf_wcommit(&s->sub.buf, s->round * s->sub.bpf);
}
if (s->mode & MODE_PLAY) {
abuf_rdiscard(&s->mix.buf, s->round * s->mix.bpf);
}
s->skip--;
}
return max - s->skip;
}
/*
* Mix the slot input block over the output block
*/
void
dev_mix_badd(struct dev *d, struct slot *s)
{
adata_t *idata, *odata, *in;
int icount, i, offs, vol, nch;
odata = DEV_PBUF(d);
idata = (adata_t *)abuf_rgetblk(&s->mix.buf, &icount);
#ifdef DEBUG
if (icount < s->round * s->mix.bpf) {
logx(0, "%s%u: not enough data to mix (%u bytes)",
s->name, s->unit, icount);
panic();
}
#endif
if (!(s->opt->mode & MODE_PLAY)) {
/*
* playback not allowed in opt structure, produce silence
*/
abuf_rdiscard(&s->mix.buf, s->round * s->mix.bpf);
return;
}
/*
* Apply the following processing chain:
*
* dec -> resamp-> cmap
*
* where the first two are optional.
*/
in = idata;
if (s->mix.decbuf) {
dec_do(&s->mix.dec, (void *)in, s->mix.decbuf, s->round);
in = s->mix.decbuf;
}
if (s->mix.resampbuf) {
resamp_do(&s->mix.resamp,
in, s->mix.resampbuf, s->round, d->round);
in = s->mix.resampbuf;
}
nch = s->mix.cmap.nch;
vol = ADATA_MUL(s->mix.weight, s->mix.vol) / s->mix.join;
cmap_add(&s->mix.cmap, in, odata, vol, d->round);
offs = 0;
for (i = s->mix.join - 1; i > 0; i--) {
offs += nch;
cmap_add(&s->mix.cmap, in + offs, odata, vol, d->round);
}
offs = 0;
for (i = s->mix.expand - 1; i > 0; i--) {
offs += nch;
cmap_add(&s->mix.cmap, in, odata + offs, vol, d->round);
}
abuf_rdiscard(&s->mix.buf, s->round * s->mix.bpf);
}
/*
* Normalize input levels.
*/
void
dev_mix_adjvol(struct dev *d)
{
unsigned int n;
struct slot *i, *j;
int jcmax, icmax, weight;
for (i = d->slot_list; i != NULL; i = i->next) {
if (!(i->mode & MODE_PLAY))
continue;
icmax = i->opt->pmin + i->mix.nch - 1;
weight = ADATA_UNIT;
if (d->autovol) {
/*
* count the number of inputs that have
* overlapping channel sets
*/
n = 0;
for (j = d->slot_list; j != NULL; j = j->next) {
if (!(j->mode & MODE_PLAY))
continue;
jcmax = j->opt->pmin + j->mix.nch - 1;
if (i->opt->pmin <= jcmax &&
icmax >= j->opt->pmin)
n++;
}
weight /= n;
}
if (weight > i->opt->maxweight)
weight = i->opt->maxweight;
i->mix.weight = d->master_enabled ?
ADATA_MUL(weight, MIDI_TO_ADATA(d->master)) : weight;
#ifdef DEBUG
logx(3, "%s%u: set weight: %d / %d", i->name, i->unit, i->mix.weight,
i->opt->maxweight);
#endif
}
}
/*
* Copy data from slot to device
*/
void
dev_sub_bcopy(struct dev *d, struct slot *s)
{
adata_t *idata, *enc_out, *resamp_out, *cmap_out;
void *odata;
int ocount, moffs;
int i, vol, offs, nch;
odata = (adata_t *)abuf_wgetblk(&s->sub.buf, &ocount);
#ifdef DEBUG
if (ocount < s->round * s->sub.bpf) {
logx(0, "dev_sub_bcopy: not enough space");
panic();
}
#endif
if (s->opt->mode & MODE_MON) {
moffs = d->poffs + d->round;
if (moffs == d->psize)
moffs = 0;
idata = d->pbuf + moffs * d->pchan;
} else if (s->opt->mode & MODE_REC) {
idata = d->rbuf;
} else {
/*
* recording not allowed in opt structure, produce silence
*/
enc_sil_do(&s->sub.enc, odata, s->round);
abuf_wcommit(&s->sub.buf, s->round * s->sub.bpf);
return;
}
/*
* Apply the following processing chain:
*
* cmap -> resamp -> enc
*
* where the last two are optional.
*/
enc_out = odata;
resamp_out = s->sub.encbuf ? s->sub.encbuf : enc_out;
cmap_out = s->sub.resampbuf ? s->sub.resampbuf : resamp_out;
nch = s->sub.cmap.nch;
vol = ADATA_UNIT / s->sub.join;
cmap_copy(&s->sub.cmap, idata, cmap_out, vol, d->round);
offs = 0;
for (i = s->sub.join - 1; i > 0; i--) {
offs += nch;
cmap_add(&s->sub.cmap, idata + offs, cmap_out, vol, d->round);
}
offs = 0;
for (i = s->sub.expand - 1; i > 0; i--) {
offs += nch;
cmap_copy(&s->sub.cmap, idata, cmap_out + offs, vol, d->round);
}
if (s->sub.resampbuf) {
resamp_do(&s->sub.resamp,
s->sub.resampbuf, resamp_out, d->round, s->round);
}
if (s->sub.encbuf)
enc_do(&s->sub.enc, s->sub.encbuf, (void *)enc_out, s->round);
abuf_wcommit(&s->sub.buf, s->round * s->sub.bpf);
}
/*
* run a one block cycle: consume one recorded block from
* rbuf and produce one play block in pbuf
*/
void
dev_cycle(struct dev *d)
{
struct slot *s, **ps;
unsigned char *base;
int nsamp;
/*
* check if the device is actually used. If it isn't,
* then close it
*/
if (d->slot_list == NULL && d->idle >= d->bufsz &&
(mtc_array[0].dev != d || mtc_array[0].tstate != MTC_RUN)) {
logx(2, "%s: device stopped", d->path);
dev_sio_stop(d);
d->pstate = DEV_INIT;
if (d->refcnt == 0)
dev_close(d);
return;
}
if (d->prime > 0) {
#ifdef DEBUG
logx(4, "%s: empty cycle, prime = %u", d->path, d->prime);
#endif
base = (unsigned char *)DEV_PBUF(d);
nsamp = d->round * d->pchan;
memset(base, 0, nsamp * sizeof(adata_t));
if (d->encbuf) {
enc_do(&d->enc, (unsigned char *)DEV_PBUF(d),
d->encbuf, d->round);
}
d->prime -= d->round;
return;
}
d->delta -= d->round;
#ifdef DEBUG
logx(4, "%s: full cycle: delta = %d", d->path, d->delta);
#endif
if (d->mode & MODE_PLAY) {
base = (unsigned char *)DEV_PBUF(d);
nsamp = d->round * d->pchan;
memset(base, 0, nsamp * sizeof(adata_t));
}
if ((d->mode & MODE_REC) && d->decbuf)
dec_do(&d->dec, d->decbuf, (unsigned char *)d->rbuf, d->round);
ps = &d->slot_list;
while ((s = *ps) != NULL) {
#ifdef DEBUG
logx(4, "%s%u: running, skip = %d", s->name, s->unit, s->skip);
#endif
d->idle = 0;
/*
* skip cycles for XRUN_SYNC correction
*/
slot_skip(s);
if (s->skip < 0) {
s->skip++;
ps = &s->next;
continue;
}
#ifdef DEBUG
if (s->pstate == SLOT_STOP && !(s->mode & MODE_PLAY)) {
logx(0, "%s%u: rec-only slots can't be drained",
s->name, s->unit);
panic();
}
#endif
/*
* check if stopped stream finished draining
*/
if (s->pstate == SLOT_STOP &&
s->mix.buf.used < s->round * s->mix.bpf) {
/*
* partial blocks are zero-filled by socket
* layer, so s->mix.buf.used == 0 and we can
* destroy the buffer
*/
*ps = s->next;
s->pstate = SLOT_INIT;
s->ops->eof(s->arg);
slot_freebufs(s);
dev_mix_adjvol(d);
#ifdef DEBUG
logx(3, "%s%u: drained", s->name, s->unit);
#endif
continue;
}
/*
* check for xruns
*/
if (((s->mode & MODE_PLAY) &&
s->mix.buf.used < s->round * s->mix.bpf) ||
((s->mode & MODE_RECMASK) &&
s->sub.buf.len - s->sub.buf.used <
s->round * s->sub.bpf)) {
#ifdef DEBUG
logx(3, "%s%u: xrun, pause cycle", s->name, s->unit);
#endif
if (s->xrun == XRUN_IGNORE) {
s->delta -= s->round;
ps = &s->next;
} else if (s->xrun == XRUN_SYNC) {
s->skip++;
ps = &s->next;
} else if (s->xrun == XRUN_ERROR) {
s->ops->exit(s->arg);
*ps = s->next;
} else {
#ifdef DEBUG
logx(0, "%s%u: bad xrun mode", s->name, s->unit);
panic();
#endif
}
continue;
}
if ((s->mode & MODE_RECMASK) && !(s->pstate == SLOT_STOP)) {
if (s->sub.prime == 0) {
dev_sub_bcopy(d, s);
s->ops->flush(s->arg);
} else {
#ifdef DEBUG
logx(3, "%s%u: prime = %d", s->name, s->unit,
s->sub.prime);
#endif
s->sub.prime--;
}
}
if (s->mode & MODE_PLAY) {
dev_mix_badd(d, s);
if (s->pstate != SLOT_STOP)
s->ops->fill(s->arg);
}
ps = &s->next;
}
if ((d->mode & MODE_PLAY) && d->encbuf) {
enc_do(&d->enc, (unsigned char *)DEV_PBUF(d),
d->encbuf, d->round);
}
}
/*
* called at every clock tick by the device
*/
void
dev_onmove(struct dev *d, int delta)
{
long long pos;
struct slot *s, *snext;
d->delta += delta;
if (d->slot_list == NULL)
d->idle += delta;
for (s = d->slot_list; s != NULL; s = snext) {
/*
* s->ops->onmove() may remove the slot
*/
snext = s->next;
pos = s->delta_rem +
(long long)s->delta * d->round +
(long long)delta * s->round;
s->delta = pos / (int)d->round;
s->delta_rem = pos % d->round;
if (s->delta_rem < 0) {
s->delta_rem += d->round;
s->delta--;
}
if (s->delta >= 0)
s->ops->onmove(s->arg);
}
if (mtc_array[0].dev == d && mtc_array[0].tstate == MTC_RUN)
mtc_midi_qfr(&mtc_array[0], delta);
}
void
dev_master(struct dev *d, unsigned int master)
{
struct ctl *c;
unsigned int v;
logx(2, "%s: master volume set to %u", d->path, master);
if (d->master_enabled) {
d->master = master;
if (d->mode & MODE_PLAY)
dev_mix_adjvol(d);
} else {
for (c = ctl_list; c != NULL; c = c->next) {
if (c->scope != CTL_HW || c->u.hw.dev != d)
continue;
if (c->type != CTL_NUM ||
strcmp(c->group, d->name) != 0 ||
strcmp(c->node0.name, "output") != 0 ||
strcmp(c->func, "level") != 0)
continue;
v = (master * c->maxval + 64) / 127;
ctl_setval(c, v);
}
}
}
/*
* Create a sndio device
*/
struct dev *
dev_new(char *path, struct aparams *par,
unsigned int mode, unsigned int bufsz, unsigned int round,
unsigned int rate, unsigned int hold, unsigned int autovol)
{
struct dev *d, **pd;
if (dev_sndnum == DEV_NMAX) {
logx(1, "too many devices");
return NULL;
}
d = xmalloc(sizeof(struct dev));
d->path = path;
d->num = dev_sndnum++;
d->reqpar = *par;
d->reqmode = mode;
d->reqpchan = d->reqrchan = 0;
d->reqbufsz = bufsz;
d->reqround = round;
d->reqrate = rate;
d->hold = hold;
d->autovol = autovol;
d->refcnt = 0;
d->pstate = DEV_CFG;
d->slot_list = NULL;
d->master = MIDI_MAXCTL;
d->master_enabled = 0;
d->alt_next = d;
snprintf(d->name, CTL_NAMEMAX, "%u", d->num);
for (pd = &dev_list; *pd != NULL; pd = &(*pd)->next)
;
d->next = *pd;
*pd = d;
return d;
}
/*
* adjust device parameters and mode
*/
void
dev_adjpar(struct dev *d, int mode,
int pmax, int rmax)
{
d->reqmode |= mode & MODE_AUDIOMASK;
if (mode & MODE_PLAY) {
if (d->reqpchan < pmax + 1)
d->reqpchan = pmax + 1;
}
if (mode & MODE_REC) {
if (d->reqrchan < rmax + 1)
d->reqrchan = rmax + 1;
}
}
/*
* Open the device with the dev_reqxxx capabilities. Setup a mixer, demuxer,
* monitor, midi control, and any necessary conversions.
*
* Note that record and play buffers are always allocated, even if the
* underlying device doesn't support both modes.
*/
int
dev_allocbufs(struct dev *d)
{
char enc_str[ENCMAX], chans_str[64];
/*
* Create record buffer.
*/
/* Create device <-> demuxer buffer */
d->rbuf = xmalloc(d->round * d->rchan * sizeof(adata_t));
/* Insert a converter, if needed. */
if (!aparams_native(&d->par)) {
dec_init(&d->dec, &d->par, d->rchan);
d->decbuf = xmalloc(d->round * d->rchan * d->par.bps);
} else
d->decbuf = NULL;
/*
* Create play buffer
*/
/* Create device <-> mixer buffer */
d->poffs = 0;
d->psize = d->bufsz + d->round;
d->pbuf = xmalloc(d->psize * d->pchan * sizeof(adata_t));
d->mode |= MODE_MON;
/* Append a converter, if needed. */
if (!aparams_native(&d->par)) {
enc_init(&d->enc, &d->par, d->pchan);
d->encbuf = xmalloc(d->round * d->pchan * d->par.bps);
} else
d->encbuf = NULL;
/*
* Initially fill the record buffer with zeroed samples. This ensures
* that when a client records from a play-only device the client just
* gets silence.
*/
memset(d->rbuf, 0, d->round * d->rchan * sizeof(adata_t));
logx(2, "%s: %dHz, %s, %s, %d blocks of %d frames",
d->path, d->rate,
(aparams_enctostr(&d->par, enc_str), enc_str),
(chans_fmt(chans_str, sizeof(chans_str),
d->mode & (MODE_PLAY | MODE_REC),
0, d->pchan - 1, 0, d->rchan - 1), chans_str),
d->bufsz / d->round, d->round);
return 1;
}
/*
* Reset parameters and open the device.
*/
int
dev_open(struct dev *d)
{
d->mode = d->reqmode;
d->round = d->reqround;
d->bufsz = d->reqbufsz;
d->rate = d->reqrate;
d->pchan = d->reqpchan;
d->rchan = d->reqrchan;
d->par = d->reqpar;
if (d->pchan == 0)
d->pchan = 2;
if (d->rchan == 0)
d->rchan = 2;
if (!dev_sio_open(d)) {
logx(1, "%s: failed to open audio device", d->path);
return 0;
}
if (!dev_allocbufs(d))
return 0;
d->pstate = DEV_INIT;
return 1;
}
/*
* Force all slots to exit and close device, called after an error
*/
void
dev_abort(struct dev *d)
{
int i;
struct slot *s;
struct ctlslot *c;
struct opt *o;
for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) {
if (s->opt == NULL || s->opt->dev != d)
continue;
if (s->ops) {
s->ops->exit(s->arg);
s->ops = NULL;
}
}
d->slot_list = NULL;
for (o = opt_list; o != NULL; o = o->next) {
if (o->dev != d)