forked from michaelforney/oscmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oscmix_ff802.c
1846 lines (1695 loc) · 48.3 KB
/
oscmix_ff802.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 <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 oscnode {
const char *name;
int reg;
int (*set)(const struct oscnode *path[], int reg, struct oscmsg *msg);
int (*new)(const struct oscnode *path[], const char *addr, int reg, 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;
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;
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 %#.4x\n", reg, 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 int
setint(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
int_least32_t val;
val = oscgetint(msg);
if (oscend(msg) != 0)
return -1;
setreg(reg, val);
return 0;
}
static int
newint(const struct oscnode *path[], const char *addr, int reg, int val)
{
oscsend(addr, ",i", val);
return 0;
}
static int
setfixed(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
const struct oscnode *node;
float val;
node = *path;
val = oscgetfloat(msg);
if (oscend(msg) != 0)
return -1;
setreg(reg, val / node->scale);
return 0;
}
static int
newfixed(const struct oscnode *path[], const char *addr, int reg, int val)
{
const struct oscnode *node;
node = *path;
oscsend(addr, ",f", val * node->scale);
return 0;
}
static int
setenum(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
const struct oscnode *node;
const char *str;
int val;
node = *path;
switch (*msg->type) {
case 's':
str = oscgetstr(msg);
if (str) {
for (val = 0; val < node->nameslen; ++val) {
if (strcasecmp(str, node->names[val]) == 0)
break;
}
if (val == node->nameslen)
return -1;
}
break;
default:
val = oscgetint(msg);
break;
}
if (oscend(msg) != 0)
return -1;
setreg(reg, val);
return 0;
}
static int
newenum(const struct oscnode *path[], const char *addr, int reg, int val)
{
const struct oscnode *node;
node = *path;
oscsendenum(addr, val, node->names, node->nameslen);
return 0;
}
static int
setbool(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
bool val;
val = oscgetint(msg);
if (oscend(msg) != 0)
return -1;
setreg(reg, val);
return 0;
}
static int
newbool(const struct oscnode *path[], const char *addr, int reg, int val)
{
oscsend(addr, ",i", val != 0);
return 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 int
setinputmute(const struct oscnode *path[], int reg, 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 -1;
inidx = path[-1] - path[-2]->child;
assert(inidx < device->inputslen);
/* mutex */
in = &inputs[inidx];
if (inidx % 2 == 1 && in[-1].stereo)
--in, --inidx;
setreg(reg, 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;
}
}
}
return 0;
}
static int
setinputstereo(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
int idx;
bool val;
val = oscgetint(msg);
if (oscend(msg) != 0)
return -1;
idx = (path[-1] - path[-2]->child) & -2;
assert(idx < device->inputslen);
inputs[idx].stereo = val;
inputs[idx + 1].stereo = val;
setreg(idx << 6 | 2, val);
setreg((idx + 1) << 6 | 2, val);
return 0;
}
static int
newinputstereo(const struct oscnode *path[], const char *addr, int reg, int val)
{
int idx;
char addrbuf[256];
idx = (path[-1] - path[-2]->child) & -2;
assert(idx < device->inputslen);
inputs[idx].stereo = val;
inputs[idx + 1].stereo = val;
addr = addrbuf;
snprintf(addrbuf, sizeof addrbuf, "/input/%d/stereo", idx + 1);
oscsend(addr, ",i", val != 0);
snprintf(addrbuf, sizeof addrbuf, "/input/%d/stereo", idx + 2);
oscsend(addr, ",i", val != 0);
return 0;
}
static int
newoutputstereo(const struct oscnode *path[], const char *addr, int reg, int val)
{
int idx;
char addrbuf[256];
idx = (path[-1] - path[-2]->child) & -2;
assert(idx < device->outputslen);
outputs[idx].stereo = val;
outputs[idx + 1].stereo = val;
addr = addrbuf;
snprintf(addrbuf, sizeof addrbuf, "/output/%d/stereo", idx + 1);
oscsend(addr, ",i", val != 0);
snprintf(addrbuf, sizeof addrbuf, "/output/%d/stereo", idx + 2);
oscsend(addr, ",i", val != 0);
return 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 >= 20)
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 int
setinputgain(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
float val;
bool mic;
val = oscgetfloat(msg);
if (oscend(msg) != 0)
return -1;
mic = (path[-1] - path[-2]->child) <= 1;
if (val < 0 || val > 75 || (!mic && val > 24))
return -1;
setreg(reg, val * 10);
return 0;
}
static int
newinputgain(const struct oscnode *path[], const char *addr, int reg, int val)
{
oscsend(addr, ",f", val / 10.0);
return 0;
}
static int
setinput48v(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
int idx;
idx = path[-1] - path[-2]->child;
assert(idx < device->inputslen);
if (device->inputs[idx].flags & INPUT_48V)
return setbool(path, reg, msg);
return -1;
}
static int
newinput48v_reflevel(const struct oscnode *path[], const char *addr, int reg, int val)
{
static const char *const names[] = {"+7dBu", "+13dBu", "+19dBu"};
int idx;
const struct inputinfo *info;
idx = path[-1] - path[-2]->child;
assert(idx < device->inputslen);
info = &device->inputs[idx];
if (info->flags & INPUT_48V) {
char addrbuf[256];
snprintf(addrbuf, sizeof addrbuf, "/input/%d/48v", idx + 1);
return newbool(path, addrbuf, reg, val);
} else if (info->flags & INPUT_HIZ) {
oscsendenum(addr, val & 0xf, names, 2);
return 0;
} else if (info->flags & INPUT_REFLEVEL) {
oscsendenum(addr, val & 0xf, names + 1, 2);
return 0;
}
return -1;
}
static int
setinputhiz(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
int idx;
idx = path[-1] - path[-2]->child;
assert(idx < device->inputslen);
if (device->inputs[idx].flags & INPUT_HIZ)
return setbool(path, reg, msg);
return -1;
}
static int
newinputhiz(const struct oscnode *path[], const char *addr, int reg, int val)
{
int idx;
idx = path[-1] - path[-2]->child;
assert(idx < device->inputslen);
if (device->inputs[idx].flags & INPUT_HIZ)
return newbool(path, addr, reg, val);
return -1;
}
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 int
seteqdrecord(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
bool val;
unsigned char buf[4], sysexbuf[7 + 5];
val = oscgetint(msg);
if (oscend(msg) != 0)
return -1;
putle32(buf, val);
writesysex(4, buf, sizeof buf, sysexbuf);
return 0;
}
static int
newdspload(const struct oscnode *path[], const char *addr, int reg, 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);
}
return 0;
}
static int
newdspavail(const struct oscnode *path[], const char *addr, int reg, int val)
{
return 0;
}
static int
newdspactive(const struct oscnode *path[], const char *addr, int reg, int val)
{
return 0;
}
static int
newarcencoder(const struct oscnode *path[], const char *addr, int reg, int val)
{
return 0;
}
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 int
newsamplerate(const struct oscnode *path[], const char *addr, int reg, int val)
{
uint_least32_t rate;
rate = getsamplerate(val);
if (rate != 0)
oscsend(addr, ",i", rate);
return 0;
}
static int
newdynlevel(const struct oscnode *path[], const char *unused, int reg, int val)
{
/*
char addr[256];
int ch;
ch = (reg - 0x3180) * 2;
if (ch < 20) {
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 < 40) {
ch -= 20;
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 int
newdurecstatus(const struct oscnode *path[], const char *addr, int reg, 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);
}
return 0;
}
static int
newdurectime(const struct oscnode *path[], const char *addr, int reg, int val)
{
if (val != durec.time) {
durec.time = val;
oscsend(addr, ",i", val);
}
return 0;
}
static int
newdurecusbstatus(const struct oscnode *path[], const char *addr, int reg, 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);
}
return 0;
}
static int
newdurectotalspace(const struct oscnode *path[], const char *addr, int reg, int val)
{
float totalspace;
totalspace = val / 16.f;
if (totalspace != durec.totalspace) {
durec.totalspace = totalspace;
oscsend(addr, ",f", totalspace);
}
return 0;
}
static int
newdurecfreespace(const struct oscnode *path[], const char *addr, int reg, int val)
{
float freespace;
freespace = val / 16.f;
if (freespace != durec.freespace) {
durec.freespace = freespace;
oscsend(addr, ",f", freespace);
}
return 0;
}
static int
newdurecfileslen(const struct oscnode *path[], const char *addr, int reg, int val)
{
if (val < 0 || val == durec.fileslen)
return 0;
durec.files = realloc(durec.files, val * sizeof *durec.files);
if (!durec.files)
fatal(NULL);
if (val > durec.fileslen)
memset(durec.files + durec.fileslen, 0, (val - durec.fileslen) * sizeof *durec.files);
durec.fileslen = val;
if (durec.index >= durec.fileslen)
durec.index = -1;
oscsend(addr, ",i", val);
return 0;
}
static int
setdurecfile(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
int val;
val = oscgetint(msg);
if (oscend(msg) != 0)
return -1;
setreg(0x3e9c, val | 0x8000);
return 0;
}
static int
newdurecfile(const struct oscnode *path[], const char *addr, int reg, int val)
{
if (val != durec.file) {
durec.file = val;
oscsend(addr, ",i", val);
}
return 0;
}
static int
newdurecnext(const struct oscnode *path[], const char *addr, int reg, 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(addr, ",i", ((val & 0xfff) ^ 0x800) - 0x800);
}
playmode = val >> 12;
if (playmode != durec.playmode) {
durec.playmode = playmode;
oscsendenum("/durec/playmode", val >> 12, names, LEN(names));
}
return 0;
}
static int
newdurecrecordtime(const struct oscnode *path[], const char *addr, int reg, int val)
{
if (val != durec.recordtime) {
durec.recordtime = val;
oscsend(addr, ",i", val);
}
return 0;
}
static int
newdurecindex(const struct oscnode *path[], const char *addr, int reg, int val)
{
if (val + 1 > durec.fileslen)
newdurecfileslen(NULL, "/durec/numfiles", -1, val + 1);
durec.index = val;
return 0;
}
static int
newdurecname(const struct oscnode *path[], const char *addr, int reg, int val)
{
struct durecfile *f;
char *pos, old[2];
if (durec.index == -1)
return 0;
assert(durec.index < durec.fileslen);
f = &durec.files[durec.index];
reg -= 0x358b;
assert(reg < sizeof f->name / 2);
pos = f->name + reg * 2;
memcpy(old, pos, sizeof old);
putle16(pos, val);
if (memcmp(old, pos, sizeof old) != 0)
oscsend("/durec/name", ",is", durec.index, f->name);
return 0;
}
static int
newdurecinfo(const struct oscnode *path[], const char *unused, int reg, int val)
{
struct durecfile *f;
unsigned long samplerate;
int channels;
if (durec.index == -1)
return 0;
f = &durec.files[durec.index];
samplerate = getsamplerate(val & 0xff);
if (samplerate != f->samplerate) {
f->samplerate = samplerate;
oscsend("/durec/samplerate", ",ii", durec.index, samplerate);
}
channels = val >> 8;
if (channels != f->channels) {
f->channels = channels;
oscsend("/durec/channels", ",ii", durec.index, channels);
}
return 0;
}
static int
newdureclength(const struct oscnode *path[], const char *unused, int reg, int val)
{
struct durecfile *f;
if (durec.index == -1)
return 0;
f = &durec.files[durec.index];
if (val != f->length) {
f->length = val;
oscsend("/durec/length", ",ii", durec.index, val);
}
return 0;
}
static int
setdurecstop(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
if (oscend(msg) != 0)
return -1;
setreg(0x3e9a, 0x8120);
return 0;
}
static int
setdurecplay(const struct oscnode *path[], int reg, struct oscmsg *msg)
{
if (oscend(msg) != 0)
return -1;
setreg(0x3e9a, 0x8123);
return 0;
}