forked from michaelforney/oscmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oscmix.c
1936 lines (1768 loc) · 50.6 KB
/
oscmix.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
#define _XOPEN_SOURCE 700 /* for memccpy */
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h> /* for strcasecmp */
#include "device.h"
#include "intpack.h"
#include "oscmix.h"
#include "osc.h"
#include "sysex.h"
#include "util.h"
#define LEN(a) (sizeof (a) / sizeof *(a))
#define PI 3.14159265358979323846
struct oscctx {
const struct oscnode *node;
const char *addr;
unsigned char ctl[4];
int depth;
};
struct oscnode {
const char *name;
void (*set)(struct oscctx *ctx, struct oscmsg *msg);
void (*new)(struct oscctx *ctx, int val);
union {
struct {
const char *const *const names;
size_t nameslen;
};
struct {
short min;
short max;
float scale;
};
};
const struct oscnode *child;
};
struct mix {
signed char pan;
short vol;
};
struct input {
bool stereo;
bool mute;
float width;
};
struct output {
bool stereo;
struct mix *mix;
};
struct durecfile {
short reg[6];
char name[9];
unsigned long samplerate;
unsigned channels;
unsigned length;
};
int dflag;
int iflag;
static const struct device *device;
static struct input *inputs;
static struct input *playbacks;
static struct output *outputs;
static struct {
int status;
int position;
int time;
int usberrors;
int usbload;
float totalspace;
float freespace;
struct durecfile *files;
size_t fileslen;
int file;
int recordtime;
int index;
int next;
int playmode;
} durec = {.index = -1};
static struct {
int vers;
int load;
} dsp;
static bool refreshing=false;
static void oscsend(const char *addr, const char *type, ...);
static void oscflush(void);
static void oscsendenum(const char *addr, int val, const char *const names[], size_t nameslen);
static void
dump(const char *name, const void *ptr, size_t len)
{
size_t i;
const unsigned char *buf;
buf = ptr;
if (name)
fputs(name, stdout);
if (len > 0) {
if (name)
putchar('\t');
printf("%.2x", buf[0]);
}
for (i = 1; i < len; ++i)
printf(" %.2x", buf[i]);
putchar('\n');
}
static void
writesysex(int subid, const unsigned char *buf, size_t len, unsigned char *sysexbuf)
{
struct sysex sysex;
size_t sysexlen;
sysex.mfrid = 0x200d;
sysex.devid = 0x10;
sysex.data = NULL;
sysex.datalen = len * 5 / 4;
sysex.subid = subid;
sysexlen = sysexenc(&sysex, sysexbuf, SYSEX_MFRID | SYSEX_DEVID | SYSEX_SUBID);
base128enc(sysex.data, buf, len);
writemidi(sysexbuf, sysexlen);
}
static int
setreg(unsigned reg, unsigned val)
{
unsigned long regval;
unsigned char buf[4], sysexbuf[7 + 5];
unsigned par;
if (dflag && reg != 0x3f00)
fprintf(stderr, "setreg %.4X %.4hX\n", reg, (unsigned short)val);
regval = (reg & 0x7fff) << 16 | (val & 0xffff);
par = regval >> 16 ^ regval;
par ^= par >> 8;
par ^= par >> 4;
par ^= par >> 2;
par ^= par >> 1;
regval |= (~par & 1) << 31;
putle32(buf, regval);
writesysex(0, buf, sizeof buf, sysexbuf);
return 0;
}
static void
setval(struct oscctx *ctx, int val)
{
int reg;
reg = device->ctltoreg(getle32(ctx->ctl));
if (reg == -1) {
fprintf(stderr, "no reg for reg: %.4X %.4hX\n", reg, (unsigned short)val);
fprintf(stderr, "no reg for ctl: %d %d %d %d\n", ctx->ctl[0], ctx->ctl[1], ctx->ctl[2], ctx->ctl[3]);
return;
}
setreg(reg, val);
}
static void
setint(struct oscctx *ctx, struct oscmsg *msg)
{
int_least32_t val;
val = oscgetint(msg);
if (oscend(msg) != 0)
return;
setval(ctx, val);
}
static void
newint(struct oscctx *ctx, int val)
{
oscsend(ctx->addr, ",i", val);
}
static void
setfixed(struct oscctx *ctx, struct oscmsg *msg)
{
float val;
val = oscgetfloat(msg);
if (oscend(msg) != 0)
return;
setval(ctx, val / ctx->node->scale);
}
static void
newfixed(struct oscctx *ctx, int val)
{
oscsend(ctx->addr, ",f", val * ctx->node->scale);
}
static void
setenum(struct oscctx *ctx, struct oscmsg *msg)
{
const char *str;
int val;
switch (*msg->type) {
case 's':
str = oscgetstr(msg);
if (str) {
for (val = 0; val < ctx->node->nameslen; ++val) {
if (strcasecmp(str, ctx->node->names[val]) == 0)
break;
}
if (val == ctx->node->nameslen)
return;
}
break;
default:
val = oscgetint(msg);
break;
}
if (oscend(msg) != 0)
return;
setval(ctx, val);
}
static void
newenum(struct oscctx *ctx, int val)
{
oscsendenum(ctx->addr, val, ctx->node->names, ctx->node->nameslen);
}
static void
setbool(struct oscctx *ctx, struct oscmsg *msg)
{
bool val;
fprintf(stderr, "setbool2\n");
val = oscgetint(msg);
if (oscend(msg) != 0)
return;
setval(ctx, val);
}
static void
newbool(struct oscctx *ctx, int val)
{
oscsend(ctx->addr, ",i", val != 0);
}
static int
setlevel(int reg, float level)
{
long val;
val = level * 0x8000l;
assert(val >= 0);
assert(val <= 0x10000);
if (val > 0x4000)
val = (val >> 3) - 0x8000;
return setreg(reg, val);
}
static void
setlevels(struct output *out, struct input *in, struct mix *mix)
{
int reg;
float level, theta;
reg = 0x4000 | (out - outputs) << 6 | (in - inputs);
level = in->mute ? 0 : mix->vol <= -650 ? 0 : powf(10, mix->vol / 200.f);
if (out->stereo) {
theta = (mix->pan + 100) / 400.f * PI;
setlevel(reg, level * cosf(theta));
setlevel(reg + 0x40, level * sinf(theta));
} else {
setlevel(reg, level);
}
}
static const char *
match(const char *pat, const char *str)
{
assert(*pat == '/');
++pat;
for (;;) {
if (*pat == '/' || *pat == '\0')
return *str == '\0' ? pat : NULL;
if (*pat != *str)
return NULL;
++pat;
++str;
}
}
static void
setinput(struct oscctx *ctx, struct oscmsg *msg)
{
const char *next;
long ch;
char buf[(sizeof ch * CHAR_BIT + 2) / 3];
if (!ctx->addr[0])
return;
assert(ctx->addr[0] == '/');
ch = strtol(ctx->addr + 1, (char **)&next, 10);
if (next == ctx->addr + 1 || (*next != '\0' && *next != '/')) {
for (ch = 1; ch <= device->inputslen; ++ch) {
snprintf(buf, sizeof buf, "%ld", ch);
next = match(ctx->addr, buf);
if (next)
break;
}
}
if (ch < 1 || ch > device->inputslen)
return;
ctx->addr = next;
fprintf(stderr, "input ch=%ld\n", ch);
ctx->ctl[0] = INPUT;
ctx->ctl[1] = ch - 1;
ctx->depth = 2;
}
static void
setoutput(struct oscctx *ctx, struct oscmsg *msg)
{
const char *next;
long ch;
char buf[(sizeof ch * CHAR_BIT + 2) / 3];
if (!ctx->addr[0])
return;
assert(ctx->addr[0] == '/');
ch = strtol(ctx->addr + 1, (char **)&next, 10);
if (next == ctx->addr + 1 || (*next != '\0' && *next != '/')) {
for (ch = 1; ch <= device->inputslen; ++ch) {
snprintf(buf, sizeof buf, "%ld", ch);
next = match(ctx->addr, buf);
if (next)
break;
}
if (ch > device->inputslen)
return;
}
ctx->addr = next;
fprintf(stderr, "output ch=%ld\n", ch);
ctx->ctl[0] = OUTPUT;
ctx->ctl[1] = ch -1;
ctx->depth = 2;
}
static void
setinputmute(struct oscctx *ctx, struct oscmsg *msg)
{
struct input *in;
struct output *out;
struct mix *mix;
int inidx, outidx;
bool val;
val = oscgetint(msg);
if (oscend(msg) != 0)
return;
inidx = ctx->ctl[1];
assert(inidx < device->inputslen);
/* mutex */
in = &inputs[inidx];
if (inidx % 2 == 1 && in[-1].stereo)
--in, --inidx;
setval(ctx, val);
if (in->mute != val) {
in->mute = val;
if (in->stereo)
in[1].mute = val;
for (outidx = 0; outidx < device->outputslen; ++outidx) {
out = &outputs[outidx];
mix = &out->mix[inidx];
if (mix->vol > -650)
setlevels(out, in, mix);
if (in->stereo && (++mix)->vol > -650)
setlevels(out, in + 1, mix);
if (out->stereo) {
assert(outidx % 2 == 0);
++outidx;
}
}
}
}
static void
setinputstereo(struct oscctx *ctx, struct oscmsg *msg)
{
bool val;
val = oscgetint(msg);
if (oscend(msg) != 0)
return;
inputs[ctx->ctl[1]].stereo = val;
setval(ctx, val);
ctx->ctl[1] ^= 1;
inputs[ctx->ctl[1]].stereo = val;
setval(ctx, val);
}
static void
newinputstereo(struct oscctx *ctx, int val)
{
int idx;
char addr[256];
idx = ctx->ctl[1];
assert(idx < device->inputslen);
inputs[idx].stereo = val;
inputs[idx + 1].stereo = val;
snprintf(addr, sizeof addr, "/input/%d/stereo", idx + 1);
oscsend(addr, ",i", val != 0);
snprintf(addr, sizeof addr, "/input/%d/stereo", idx + 2);
oscsend(addr, ",i", val != 0);
}
static void
newoutputstereo(struct oscctx *ctx, int val)
{
int idx;
char addr[256];
idx = ctx->ctl[1];
assert(idx < device->outputslen);
outputs[idx].stereo = val;
outputs[idx + 1].stereo = val;
snprintf(addr, sizeof addr, "/output/%d/stereo", idx + 1);
oscsend(addr, ",i", val != 0);
snprintf(addr, sizeof addr, "/output/%d/stereo", idx + 2);
oscsend(addr, ",i", val != 0);
}
static int
setinputname(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
const char *name;
char namebuf[12];
int i, ch, val;
ch = path[-1] - path[-2]->child;
if (ch >= 30)
return -1;
name = oscgetstr(msg);
if (oscend(msg) != 0)
return -1;
strncpy(namebuf, name, sizeof namebuf);
namebuf[sizeof namebuf - 1] = '\0';
reg = 0x3200 + ch * 8;
for (i = 0; i < sizeof namebuf; i += 2, ++reg) {
val = getle16(namebuf + i);
setreg(reg, val);
}
return 0;
}
static void
setinputgain(struct oscctx *ctx, struct oscmsg *msg)
{
const struct inputinfo *info;
float val;
val = oscgetfloat(msg);
if (oscend(msg) != 0)
return;
info = &device->inputs[ctx->ctl[1]];
if (info->flags & INPUT_HAS_GAIN) {
if (val < info->gain.min)
val = info->gain.min;
if (val > info->gain.max)
val = info->gain.max;
setval(ctx, val * 10);
}
}
static void
newinputgain(struct oscctx *ctx, int val)
{
oscsend(ctx->addr, ",f", val / 10.0);
}
static void
setinput48v(struct oscctx *ctx, struct oscmsg *msg)
{
if (device->inputs[ctx->ctl[1]].flags & INPUT_HAS_48V)
setbool(ctx, msg);
}
static void
newinput48v_reflevel(struct oscctx *ctx, int val)
{
int idx;
const struct inputinfo *info;
idx = ctx->ctl[1];
assert(idx < device->inputslen);
info = &device->inputs[idx];
if (info->flags & INPUT_HAS_48V) {
char addrbuf[256];
snprintf(addrbuf, sizeof addrbuf, "/input/%d/48v", idx + 1);
ctx->addr = addrbuf;
newbool(ctx, val);
} else if (info->flags & INPUT_HAS_REFLEVEL) {
oscsendenum(ctx->addr, val & 0xf, info->reflevel.names, info->reflevel.nameslen);
}
}
static void
setinputhiz(struct oscctx *ctx, struct oscmsg *msg)
{
if (device->inputs[ctx->ctl[1]].flags & INPUT_HAS_HIZ)
setbool(ctx, msg);
}
static void
newinputhiz(struct oscctx *ctx, int val)
{
int idx;
idx = ctx->ctl[1];
assert(idx < device->inputslen);
if (device->inputs[idx].flags & INPUT_HAS_HIZ)
newbool(ctx, val);
}
static int
setoutputloopback(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
bool val;
unsigned char buf[4], sysexbuf[7 + 5];
int idx;
val = oscgetint(msg);
if (oscend(msg) != 0)
return -1;
idx = path[-1] - path[-2]->child;
if (val)
idx |= 0x80;
putle32(buf, idx);
writesysex(3, buf, sizeof buf, sysexbuf);
return 0;
}
static void
seteqdrecord(struct oscctx *ctx, struct oscmsg *msg)
{
bool val;
unsigned char buf[4], sysexbuf[7 + 5];
val = oscgetint(msg);
if (oscend(msg) != 0)
return;
putle32(buf, val);
writesysex(4, buf, sizeof buf, sysexbuf);
}
static void
newdspload(struct oscctx *ctx, int val)
{
if (dsp.load != (val & 0xff)) {
dsp.load = val & 0xff;
oscsend("/hardware/dspload", ",i", dsp.load);
}
if (dsp.vers != val >> 8) {
dsp.vers = val >> 8;
oscsend("/hardware/dspvers", ",i", dsp.vers);
}
}
static void
newdspavail(struct oscctx *ctx, int val)
{
}
static void
newdspstatus(struct oscctx *ctx, int val)
{
}
static void
newarcdelta(struct oscctx *ctx, int val)
{
}
static int
setdb(int reg, float db)
{
int val;
val = (isinf(db) && db < 0 ? -650 : (int)(db * 10)) & 0x7fff;
return setreg(reg, val);
}
static int
setpan(int reg, int pan)
{
int val;
val = (pan & 0x7fff) | 0x8000;
return setreg(reg, val);
}
static int
setmix(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
int outidx, inidx, pan;
float vol, level, theta, width;
struct output *out;
struct input *in;
outidx = path[-2] - path[-3]->child;
assert(outidx < device->outputslen);
out = &outputs[outidx];
inidx = path[0] - path[-1]->child;
if (reg & 0x20) {
assert(inidx < device->outputslen);
in = &playbacks[inidx];
} else {
assert(inidx < device->inputslen);
in = &inputs[inidx];
}
vol = oscgetfloat(msg);
if (vol <= -65)
vol = -INFINITY;
printf("setmix %d %d %f\n", (reg >> 6) & 0x3f, reg & 0x3f, vol);
pan = 0;
width = 1;
if (*msg->type) {
pan = oscgetint(msg);
if (*msg->type && in->stereo && out->stereo)
width = oscgetfloat(msg);
}
if (oscend(msg) != 0)
return -1;
level = pow(10, vol / 20);
if (in->stereo) {
float level0, level1, level00, level10, level01, level11;
level0 = (100 - (pan > 0 ? pan : 0)) / 200.f * level;
level1 = (100 + (pan < 0 ? pan : 0)) / 200.f * level;
if (out->stereo) {
level00 = level0 * (1 + width);
level10 = level0 * (1 - width);
level01 = level1 * (1 - width);
level11 = level1 * (1 + width);
setlevel(reg + 0x2000, level00);
setlevel(reg + 0x2001, level10);
setlevel(reg + 0x2040, level01);
setlevel(reg + 0x2041, level11);
level00 = level00 * level00;
level0 = level00 + level01 * level01;
/*
L0 = level0^2 * (1 + width)^2 + level1^2 * (1 - width)^2
L1 = level0^2 * (1 - width)^2 + level1^2 * (1 + width)^2
*/
setdb(reg, 10 * log10(level0));
setpan(reg, acos(2 * level00 / level0 - 1) * 200 / PI - 100);
level10 = level10 * level10;
level1 = level10 + level11 * level11;
setdb(reg + 1, 10 * log10(level1));
setpan(reg + 1, acos(2 * level10 / level1 - 1) * 200 / PI - 100);
} else {
setlevel(reg + 0x2000, level0);
setlevel(reg + 0x2001, level1);
setdb(reg, 20 * log10(level0));
setpan(reg, 0);
setdb(reg + 1, 20 * log10(level1));
setpan(reg + 1, 0);
}
} else {
if (out->stereo) {
theta = (pan + 100) * PI / 400;
setlevel(reg + 0x2000, level * cos(theta));
setlevel(reg + 0x2040, level * sin(theta));
} else {
setlevel(reg + 0x2000, level);
}
setdb(reg, vol);
setpan(reg, pan);
}
return 0;
}
static int
newmix(const struct oscnode *path[], const char *addr, int reg, int val)
{
struct output *out;
struct input *in;
struct mix *mix;
int outidx, inidx;
bool newpan;
char addrbuf[256];
float vol;
int pan;
outidx = (reg & 0xfff) >> 6;
inidx = reg & 0x3f;
if (outidx >= device->outputslen || inidx >= device->inputslen)
return -1;
out = &outputs[outidx];
in = &inputs[inidx];
mix = &out->mix[inidx];
newpan = val & 0x8000;
val = ((val & 0x7fff) ^ 0x4000) - 0x4000;
if (newpan)
mix->pan = val;
else
mix->vol = val;
if (outidx & 1 && out[-1].stereo)
--out, --outidx;
if (inidx & 1 && in[-1].stereo)
--in, --inidx;
mix = &out->mix[inidx];
if (in->stereo) {
float level0, level1, scale;
level0 = mix[0].vol <= -650 ? 0 : powf(10, mix[0].vol / 200.f);
level1 = mix[1].vol <= -650 ? 0 : powf(10, mix[1].vol / 200.f);
if (out->stereo) {
//scale = sqrtf(2.f / (1 + in->width * in->width));
scale = 1;
} else {
scale = 2;
}
level0 *= scale;
level1 *= scale;
if (level0 == 0 && level1 == 0) {
vol = -INFINITY;
pan = 0;
} else if (level0 >= level1) {
vol = 20 * log10f(level0);
pan = 100 * (level1 / level0 - 1);
} else {
vol = 20 * log10f(level1);
pan = -100 * (level0 / level1 - 1);
}
} else {
vol = mix->vol <= -650 ? -65.f : mix->vol / 10.f;
pan = mix->pan;
}
snprintf(addrbuf, sizeof addrbuf, "/mix/%d/input/%d", outidx + 1, inidx + 1);
oscsend(addrbuf, ",f", vol);
snprintf(addrbuf, sizeof addrbuf, "/mix/%d/input/%d/pan", outidx + 1, inidx + 1);
oscsend(addrbuf, ",i", pan);
return 0;
}
static long
getsamplerate(int val)
{
static const long samplerate[] = {
32000,
44100,
48000,
64000,
88200,
96000,
128000,
176400,
192000,
};
return val > 0 && val < LEN(samplerate) ? samplerate[val] : 0;
}
static void
newsamplerate(struct oscctx *ctx, int val)
{
uint_least32_t rate;
rate = getsamplerate(val);
if (rate != 0)
oscsend(ctx->addr, ",i", rate);
}
static int
newdynlevel(const struct oscnode *path[], const char *unused, int reg, int val)
{
/*
char addr[256];
int ch;
//fixme for ff802
ch = (reg - 0x3180) * 2;
if (ch < 30) {
snprintf(addr, sizeof addr, "/input/%d/dynamics/level", ch + 1);
oscsend(addr, ",i", val >> 8 & 0xff);
snprintf(addr, sizeof addr, "/input/%d/dynamics/level", ch + 2);
oscsend(addr, ",i", val & 0xff);
} else if (ch < 60) {
ch -= 60;
snprintf(addr, sizeof addr, "/output/%d/dynamics/level", ch + 1);
oscsend(addr, ",i", val >> 8 & 0xff);
snprintf(addr, sizeof addr, "/output/%d/dynamics/level", ch + 2);
oscsend(addr, ",i", val & 0xff);
}
*/
return 0;
}
static void
newdurecstatus(struct oscctx *ctx, int val)
{
static const char *const names[] = {
"No Media", "Filesystem Error", "Initializing", "Reinitializing",
[5] = "Stopped", "Recording",
[10] = "Playing", "Paused",
};
int status;
int position;
status = val & 0xf;
if (status != durec.status) {
durec.status = status;
oscsendenum("/durec/status", val & 0xf, names, LEN(names));
}
position = (val >> 8) * 100 / 65;
if (position != durec.position) {
durec.position = position;
oscsend("/durec/position", ",i", (val >> 8) * 100 / 65);
}
}
static void
newdurectime(struct oscctx *ctx, int val)
{
if (val != durec.time) {
durec.time = val;
oscsend(ctx->addr, ",i", val);
}
}
static void
newdurecusbstatus(struct oscctx *ctx, int val)
{
int usbload, usberrors;
usbload = val >> 8;
if (usbload != durec.usbload) {
durec.usbload = usbload;
oscsend("/durec/usbload", ",i", val >> 8);
}
usberrors = val & 0xff;
if (usberrors != durec.usberrors) {
durec.usberrors = usberrors;
oscsend("/durec/usberrors", ",i", val & 0xff);
}
}
static void
newdurectotalspace(struct oscctx *ctx, int val)
{
float totalspace;
totalspace = val / 16.f;
if (totalspace != durec.totalspace) {
durec.totalspace = totalspace;
oscsend(ctx->addr, ",f", totalspace);
}
}
static void
newdurecfreespace(struct oscctx *ctx, int val)
{
float freespace;
freespace = val / 16.f;
if (freespace != durec.freespace) {
durec.freespace = freespace;
oscsend(ctx->addr, ",f", freespace);
}
}
static void
resizedurecfiles(size_t len)
{
if (len < 0 || len == durec.fileslen)
return;
durec.files = realloc(durec.files, len * sizeof *durec.files);
if (!durec.files)
fatal(NULL); /* XXX: probably shouldn't exit */
if (len > durec.fileslen)
memset(durec.files + durec.fileslen, 0, (len - durec.fileslen) * sizeof *durec.files);
durec.fileslen = len;
if (durec.index >= durec.fileslen)
durec.index = -1;
oscsend("/durec/numfiles", ",i", len);
}
static void
newdurecfileslen(struct oscctx *ctx, int val)
{
resizedurecfiles(val);
}
static void
setdurecfile(struct oscctx *ctx, struct oscmsg *msg)
{
int val;
val = oscgetint(msg);
if (oscend(msg) != 0)
return;
setval(ctx, val | 0x8000);
}
static void
newdurecfile(struct oscctx *ctx, int val)
{
if (val != durec.file) {
durec.file = val;
oscsend(ctx->addr, ",i", val);
}
}
static void
newdurecnext(struct oscctx *ctx, int val)
{
static const char *const names[] = {
"Single", "UFX Single", "Continuous", "Single Next", "Repeat Single", "Repeat All",
};
int next, playmode;
next = ((val & 0xfff) ^ 0x800) - 0x800;
if (next != durec.next) {
durec.next = next;
oscsend(ctx->addr, ",i", ((val & 0xfff) ^ 0x800) - 0x800);
}
playmode = val >> 12;
if (playmode != durec.playmode) {
durec.playmode = playmode;
oscsendenum("/durec/playmode", val >> 12, names, LEN(names));
}
}
static void
newdurecrecordtime(struct oscctx *ctx, int val)
{
if (val != durec.recordtime) {
durec.recordtime = val;
oscsend(ctx->addr, ",i", val);
}
}
static void
newdurecindex(struct oscctx *ctx, int val)
{
if (val + 1 > durec.fileslen)
resizedurecfiles(val + 1);
durec.index = val;
}
static void
newdurecname(struct oscctx *ctx, int val)
{
struct durecfile *f;
char *pos, old[2];
int off;
if (durec.index == -1)
return;
assert(durec.index < durec.fileslen);
f = &durec.files[durec.index];
off = (ctx->ctl[1] - DUREC_NAME0) * 2;
assert(off >= 0 && off < sizeof f->name);
pos = f->name + off * 2;
memcpy(old, pos, sizeof old);
putle16(pos, val);
if (memcmp(old, pos, sizeof old) != 0)
oscsend("/durec/name", ",is", durec.index, f->name);
}
static void
newdurecinfo(struct oscctx *ctx, int val)
{
struct durecfile *f;
unsigned long samplerate;
int channels;
if (durec.index == -1)
return;
f = &durec.files[durec.index];