/
sampler_layer.c
2225 lines (2055 loc) · 94.3 KB
/
sampler_layer.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
/*
Calf Box, an open source musical instrument.
Copyright (C) 2010-2011 Krzysztof Foltman
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config-api.h"
#include "dspmath.h"
#include "errors.h"
#include "midi.h"
#include "module.h"
#include "rt.h"
#include "sampler.h"
#include "sampler_impl.h"
#include "sfzloader.h"
#include <assert.h>
#include <errno.h>
#include <glib.h>
#include <math.h>
#include <memory.h>
#include <sndfile.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
static inline gboolean sampler_modulation_key_equal(const struct sampler_modulation_key *k1, const struct sampler_modulation_key *k2)
{
return (k1->src == k2->src && k1->src2 == k2->src2 && k1->dest == k2->dest);
}
static inline void sampler_modulation_dump_one(const struct sampler_modulation *sm)
{
printf("%d x %d -> %d : %f : %d\n", sm->key.src, sm->key.src2, sm->key.dest, sm->value.amount, sm->value.curve_id);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
static inline gboolean sampler_noteinitfunc_key_equal(const struct sampler_noteinitfunc_key *k1, const struct sampler_noteinitfunc_key *k2)
{
return (k1->notefunc_voice == k2->notefunc_voice && k1->variant == k2->variant);
}
static inline void sampler_noteinitfunc_dump_one(const struct sampler_noteinitfunc *nif)
{
printf("%p(%d) = %f\n", nif->key.notefunc_voice, nif->key.variant, nif->value.value);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
static inline gboolean sampler_cc_range_key_equal(const struct sampler_cc_range_key *k1, const struct sampler_cc_range_key *k2)
{
return k1->cc_number == k2->cc_number;
}
static inline void sampler_cc_range_dump_one(const struct sampler_cc_range *ccrange)
{
printf("CC%d in [%c%d, %c%d]\n", (int)ccrange->key.cc_number, ccrange->value.has_locc ? '!' : '.', (int)ccrange->value.locc, ccrange->value.has_hicc ? '!' : '.', (int)ccrange->value.hicc);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
static inline gboolean sampler_flex_lfo_key_equal(const struct sampler_flex_lfo_key *k1, const struct sampler_flex_lfo_key *k2)
{
return k1->id == k2->id;
}
static inline void sampler_flex_lfo_dump_one(const struct sampler_flex_lfo *lfo)
{
printf("LFO%d (freq %s %f, delay %s %f, fade %s %f, wave %s %d)\n",
(int)lfo->key.id,
lfo->value.has_freq ? "(local)" : "(inherited)", lfo->value.freq,
lfo->value.has_delay ? "(local)" : "(inherited)", lfo->value.delay,
lfo->value.has_fade ? "(local)" : "(inherited)", lfo->value.fade,
lfo->value.has_wave ? "(local)" : "(inherited)", lfo->value.wave
);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
#define SAMPLER_COLL_FUNC_DUMP(sname) \
void sname##_dump(const struct sname *p) \
{ \
for(; p; p = p->next) \
sname##_dump_one(p); \
}
SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_DUMP)
#define SAMPLER_COLL_FUNC_FIND(sname) \
static struct sname *sname##_find(struct sname *list, const struct sname##_key *key) \
{ \
for(struct sname *p = list; p; p = p->next) \
{ \
struct sname##_key *dkey = &p->key; \
if (sname##_key_equal(dkey, key)) \
return p; \
} \
return NULL; \
} \
static struct sname *sname##_find2(struct sname **list_ptr, const struct sname##_key *key, struct sname ***link_ptr) \
{ \
for(struct sname **pp = list_ptr; *pp; pp = &(*pp)->next) \
{ \
struct sname##_key *dkey = &(*pp)->key; \
if (sname##_key_equal(dkey, key)) \
{ \
if (link_ptr) \
*link_ptr = pp; \
return *pp; \
} \
} \
return NULL; \
}
SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_FIND)
#define SAMPLER_COLL_FIELD_INIT(name, has_name, type, init_value) \
d->value.name = init_value; \
d->value.has_name = FALSE;
#define SAMPLER_COLL_FUNC_ADD(sname) \
static struct sname *sname##_add(struct sname **list_ptr, const struct sname##_key *key) \
{ \
struct sname *d = sname##_find(*list_ptr, key); \
if (d) \
return d; \
d = g_malloc0(sizeof(struct sname)); \
d->key = *key; \
SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_INIT)\
d->next = *list_ptr; \
*list_ptr = d; \
return d; \
}
SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_ADD)
#define SAMPLER_COLL_FUNC_DESTROY(sname) \
static void sname##s_destroy(struct sname *list_ptr) \
{ \
while(list_ptr) \
{ \
struct sname *p = list_ptr->next; \
g_free(list_ptr); \
list_ptr = p; \
} \
}
SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_DESTROY)
#define SAMPLER_COLL_FIELD_ISNULL(name, has_name, type, init_value) \
if (d->name != init_value || d->has_name) \
return FALSE;
#define SAMPLER_COLL_FUNC_ISNULL(sname) \
static inline gboolean sname##_is_null(const struct sname##_value *d) \
{ \
SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_ISNULL) \
return TRUE; \
}
SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_ISNULL)
// sampler_modulation_set_amount etc.
#define SAMPLER_COLL_FIELD_SETTER(name, has_name, type, init_value, sname) \
struct sname *sname##_set_##name##_by_offset(struct sampler_layer *l, uint32_t offset, const struct sname##_key *key, gboolean set_local_value, type value) \
{ \
void *vl = &l->data; \
struct sname **list_ptr = vl + offset; \
struct sname *dstm = sname##_add(list_ptr, key); \
if (!set_local_value && dstm->value.has_name) \
return dstm; \
dstm->value.has_name = set_local_value; \
dstm->value.name = value; \
return dstm; \
}
#define SAMPLER_COLL_FUNC_SETTERS(sname) \
SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_SETTER, sname)
SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_SETTERS)
#define SAMPLER_COLL_FIELD_UNSET(name, has_name, type, init_value, sname) \
if ((unset_mask & (1 << sname##_value_field_##name)) && d->value.has_name == remove_local) \
{ \
d->value.name = parent ? parent->value.name : init_value; \
d->value.has_name = FALSE; \
} \
#define SAMPLER_COLL_FIELD_KEY_ENUM_VALUE(name, has_name, type, init_value, sname) \
sname##_value_field_##name,
#define SAMPLER_COLL_FUNC_UNSET(sname) \
enum { \
SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_KEY_ENUM_VALUE, sname) \
}; \
static gboolean sname##_unset_by_offset(struct sampler_layer *l, uint32_t offset, const struct sname##_key *key, gboolean remove_local, uint32_t unset_mask) \
{ \
void *vl = &l->data, *vp = l->parent ? &l->parent->data : NULL; \
struct sname **list_ptr = vl + offset; \
struct sname **parent_list_ptr = vp ? vp + offset : NULL; \
struct sname **link_ptr = NULL; \
struct sname *d = sname##_find2(list_ptr, key, &link_ptr); \
if (!d) \
return FALSE; \
struct sname *parent = remove_local && *parent_list_ptr != NULL ? sname##_find(*parent_list_ptr, key) : NULL; \
SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_UNSET, sname) \
/* Delete if it's all default values and it's not overriding anything */ \
if (sname##_is_null(&d->value)) {\
*link_ptr = d->next; \
g_free(d); \
} \
return TRUE; \
}
SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_UNSET)
#define SAMPLER_COLL_FIELD_ZEROHASATTR(name, has_name, type, init_value) \
dstv->value.has_name = FALSE;
#define SAMPLER_COLL_FUNC_CLONE(sname) \
static struct sname *sname##_clone(struct sname *src, gboolean copy_hasattr) \
{ \
struct sname *dst = NULL, **last = &dst;\
for(const struct sname *srcv = src; srcv; srcv = srcv->next) \
{ \
struct sname *dstv = g_malloc(sizeof(struct sname)); \
memcpy(dstv, srcv, sizeof(struct sname)); \
if (!copy_hasattr) \
{ \
SAMPLER_COLL_FIELD_LIST_##sname(SAMPLER_COLL_FIELD_ZEROHASATTR) \
} \
*last = dstv; \
dstv->next = NULL; \
last = &dstv->next; \
} \
return dst; \
}
SAMPLER_COLL_LIST(SAMPLER_COLL_FUNC_CLONE)
//////////////////////////////////////////////////////////////////////////////////////////////////
enum sampler_layer_param_type
{
slpt_invalid,
slpt_alias,
slpt_int,
slpt_uint32_t,
slpt_float,
slpt_dBamp,
slpt_midi_note_t,
slpt_enum,
slpt_string,
slpt_midicurve,
slpt_ccrange,
// modulation matrix
slpt_mod_amount, // src (or CC) * src2 (or CC) -> dest
slpt_mod_curveid,
slpt_mod_smooth,
slpt_mod_step,
slpt_generic_modulation,
// note init functions
slpt_voice_nif,
slpt_prevoice_nif,
slpt_flex_lfo,
slpt_flex_lfo_dest,
slpt_flex_lfo_dest_cc,
slpt_nonfunctional,
slpt_reserved,
};
struct sampler_layer_param_entry
{
const char *name;
size_t offset;
enum sampler_layer_param_type type;
double def_value;
uint64_t extra_int;
void *extra_ptr;
void (*set_has_value)(struct sampler_layer_data *, gboolean);
gboolean (*get_has_value)(struct sampler_layer_data *);
};
#define MODSRC_CC 0xFFF
#define smsrc_CC MODSRC_CC
#define ENCODE_MOD(src, src2, dst) ((((uint32_t)(src) & 0xFFFU) | (((uint32_t)(src2) & 0xFFFU) << 12U) | ((uint32_t)(dst) << 24U)))
#define PROC_SUBSTRUCT_FIELD_SETHASFUNC(name, index, def_value, parent) \
static void sampler_layer_data_##parent##_set_has_##name(struct sampler_layer_data *l, gboolean value) { l->has_##parent.name = value; } \
static gboolean sampler_layer_data_##parent##_get_has_##name(struct sampler_layer_data *l) { return l->has_##parent.name; }
#define PROC_FIELD_SETHASFUNC(type, name, default_value) \
static void sampler_layer_data_set_has_##name(struct sampler_layer_data *l, gboolean value) { l->has_##name = value; } \
static gboolean sampler_layer_data_get_has_##name(struct sampler_layer_data *l) { return l->has_##name; }
#define PROC_FIELD_SETHASFUNC_string(name) \
static void sampler_layer_data_set_has_##name(struct sampler_layer_data *l, gboolean value) { l->has_##name = value; } \
static gboolean sampler_layer_data_get_has_##name(struct sampler_layer_data *l) { return l->has_##name; }
#define PROC_FIELD_SETHASFUNC_dBamp(type, name, default_value) \
PROC_FIELD_SETHASFUNC(type, name, default_value)
#define PROC_FIELD_SETHASFUNC_enum(type, name, default_value) \
PROC_FIELD_SETHASFUNC(type, name, default_value)
#define PROC_FIELD_SETHASFUNC_dahdsr(field, name, default_value) \
DAHDSR_FIELDS(PROC_SUBSTRUCT_FIELD_SETHASFUNC, field)
#define PROC_FIELD_SETHASFUNC_lfo(field, name, default_value) \
LFO_FIELDS(PROC_SUBSTRUCT_FIELD_SETHASFUNC, field)
#define PROC_FIELD_SETHASFUNC_eq(field, name, default_value) \
EQ_FIELDS(PROC_SUBSTRUCT_FIELD_SETHASFUNC, field)
#define PROC_FIELD_SETHASFUNC_ccrange(name, parname)
#define PROC_FIELD_SETHASFUNC_midicurve(name) \
static gboolean sampler_layer_data_set_get_has_##name(struct sampler_layer_data *l, uint32_t index, int value) \
{ \
if (value != -1) \
l->name.has_values[index] = value; \
return l->name.has_values[index]; \
}
SAMPLER_FIXED_FIELDS(PROC_FIELD_SETHASFUNC)
#define LOFS(field) offsetof(struct sampler_layer_data, field)
#define FIELD_MOD(name, param, src, src2, dest) \
{ name, LOFS(modulations), slpt_mod_##param, 0, ENCODE_MOD(smsrc_##src, smsrc_##src2, smdest_##dest), NULL, NULL, NULL },
#define FIELD_AMOUNT(name, src, dest) \
FIELD_MOD(name, amount, src, none, dest)
#define FIELD_AMOUNT_CC(name, dest) \
FIELD_ALIAS(name "cc#", name "_oncc#") \
FIELD_MOD(name "_oncc#", amount, CC, none, dest) \
FIELD_MOD(name "_curvecc#", curveid, CC, none, dest) \
FIELD_MOD(name "_smoothcc#", smooth, CC, none, dest) \
FIELD_MOD(name "_stepcc#", step, CC, none, dest)
#define FIELD_AMOUNT_CC_(name, dest) \
FIELD_ALIAS(name "_cc#", name "_oncc#") \
FIELD_MOD(name "_oncc#", amount, CC, none, dest) \
FIELD_MOD(name "_curvecc#", curveid, CC, none, dest) \
FIELD_MOD(name "_smoothcc#", smooth, CC, none, dest) \
FIELD_MOD(name "_stepcc#", step, CC, none, dest)
#define FIELD_VOICE_NIF(name, nif, variant) \
{ name, LOFS(voice_nifs), slpt_voice_nif, 0, variant, nif, NULL, NULL },
#define FIELD_PREVOICE_NIF(name, nif, variant) \
{ name, LOFS(prevoice_nifs), slpt_prevoice_nif, 0, variant, nif, NULL, NULL },
#define FIELD_ALIAS(alias, name) \
{ alias, -1, slpt_alias, 0, 0, name, NULL, NULL },
#define FIELD_NONFUNCTIONAL(name) \
{ name, -1, slpt_nonfunctional, 0, 0, NULL, NULL, NULL },
#define PROC_SUBSTRUCT_FIELD_DESCRIPTOR(name, index, def_value, parent, parent_name, parent_index, parent_struct) \
{ #parent_name "_" #name, offsetof(struct sampler_layer_data, parent) + offsetof(struct parent_struct, name), slpt_float, def_value, parent_index * 100 + index, NULL, sampler_layer_data_##parent##_set_has_##name, sampler_layer_data_##parent##_get_has_##name }, \
#define PROC_SUBSTRUCT_FIELD_DESCRIPTOR_DAHDSR(name, index, def_value, parent, parent_name, parent_index, parent_struct) \
{ #parent_name "_" #name, offsetof(struct sampler_layer_data, parent) + offsetof(struct parent_struct, name), slpt_float, def_value, parent_index * 100 + index, NULL, sampler_layer_data_##parent##_set_has_##name, sampler_layer_data_##parent##_get_has_##name }, \
FIELD_VOICE_NIF(#parent_name "_vel2" #name, sampler_nif_vel2env, (parent_index << 4) + snif_env_##name) \
FIELD_AMOUNT_CC(#parent_name "_" #name, ampeg_stage + (parent_index << 4) + snif_env_##name) \
#define PROC_FIELD_DESCRIPTOR(type, name, default_value) \
{ #name, LOFS(name), slpt_##type, default_value, 0, NULL, sampler_layer_data_set_has_##name, sampler_layer_data_get_has_##name },
#define PROC_FIELD_DESCRIPTOR_dBamp(type, name, default_value) \
{ #name, LOFS(name), slpt_##type, default_value, 0, NULL, sampler_layer_data_set_has_##name, sampler_layer_data_get_has_##name },
#define PROC_FIELD_DESCRIPTOR_string(name) \
{ #name, LOFS(name), slpt_string, 0, LOFS(name##_changed), NULL, sampler_layer_data_set_has_##name, sampler_layer_data_get_has_##name },
#define PROC_FIELD_DESCRIPTOR_enum(enumtype, name, default_value) \
{ #name, LOFS(name), slpt_enum, (double)(enum enumtype)default_value, 0, enumtype##_from_string, sampler_layer_data_set_has_##name, sampler_layer_data_get_has_##name },
#define PROC_FIELD_DESCRIPTOR_midicurve(name) \
{ #name "_#", LOFS(name), slpt_midicurve, 0, 0, (void *)sampler_layer_data_set_get_has_##name, NULL, NULL },
#define FIELD_DEPTH_SET(name, dest, attrib) \
FIELD_ALIAS(#name attrib "cc#", #name attrib "_oncc#") \
FIELD_MOD(#name attrib "_oncc#", amount, name, CC, dest) \
FIELD_MOD(#name attrib "_curvecc#", curveid, name, CC, dest) \
FIELD_MOD(#name attrib "_smoothcc#", smooth, name, CC, dest) \
FIELD_MOD(#name attrib "_stepcc#", step, name, CC, dest) \
FIELD_MOD(#name attrib "polyaft", amount, name, polyaft, dest) \
FIELD_MOD(#name attrib "chanaft", amount, name, chanaft, dest) \
FIELD_MOD(#name attrib, amount, name, none, dest) \
#define PROC_FIELD_DESCRIPTOR_dahdsr(field, name, index) \
DAHDSR_FIELDS(PROC_SUBSTRUCT_FIELD_DESCRIPTOR_DAHDSR, field, name, index, cbox_dahdsr) \
FIELD_DEPTH_SET(name, from_##name, "_depth") \
FIELD_MOD(#name "_vel2depth", amount, name, vel, from_##name)
#define PROC_FIELD_DESCRIPTOR_lfo(field, name, index) \
LFO_FIELDS(PROC_SUBSTRUCT_FIELD_DESCRIPTOR, field, name, index, sampler_lfo_params) \
FIELD_AMOUNT(#name "_freqpolyaft", polyaft, name##_freq) \
FIELD_AMOUNT(#name "_freqchanaft", chanaft, name##_freq) \
FIELD_AMOUNT_CC(#name "_freq", name##_freq) \
FIELD_DEPTH_SET(name, from_##name, "_depth")
#define PROC_FIELD_DESCRIPTOR_eq(field, name, index) \
EQ_FIELDS(PROC_SUBSTRUCT_FIELD_DESCRIPTOR, field, name, index, sampler_eq_params) \
FIELD_AMOUNT_CC(#name "_freq", name##_freq) \
FIELD_AMOUNT_CC(#name "_bw", name##_bw) \
FIELD_AMOUNT_CC(#name "_gain", name##_gain)
#define PROC_FIELD_DESCRIPTOR_ccrange(field, parname) \
{ #parname "locc#", LOFS(field), slpt_ccrange, 0, 0, NULL, NULL, NULL }, \
{ #parname "hicc#", LOFS(field), slpt_ccrange, 127, 1, NULL, NULL, NULL },
#define FLEX_LFO_MOD(dest,param) (smdest_##dest | ((uint64_t)sampler_modulation_value_field_##param << 32))
#define FIELD_FLEX_LFO(name, field) \
{ name, LOFS(flex_lfos), slpt_flex_lfo, 0, sampler_flex_lfo_value_field_##field, NULL, NULL, NULL },
#define FIELD_FLEX_LFO_DEST(name, dest) \
{ name, LOFS(modulations), slpt_flex_lfo_dest, 0, smdest_##dest, NULL, NULL, NULL }, \
{ name "_oncc#", LOFS(modulations), slpt_flex_lfo_dest_cc, 0, FLEX_LFO_MOD(dest, amount), NULL, NULL, NULL }, \
{ name "_curvecc#", LOFS(modulations), slpt_flex_lfo_dest_cc, 0, FLEX_LFO_MOD(dest, curve_id), NULL, NULL, NULL }, \
{ name "_smoothcc#", LOFS(modulations), slpt_flex_lfo_dest_cc, 0, FLEX_LFO_MOD(dest, smooth), NULL, NULL, NULL }, \
{ name "_stepcc#", LOFS(modulations), slpt_flex_lfo_dest_cc, 0, FLEX_LFO_MOD(dest, step), NULL, NULL, NULL }, \
#define NIF_VARIANT_CC 0x01000000
#define NIF_VARIANT_CURVECC 0x02000000
#define NIF_VARIANT_STEPCC 0x03000000
#define NIF_VARIANT_MASK 0xFF000000
struct sampler_layer_param_entry sampler_layer_params[] = {
SAMPLER_FIXED_FIELDS(PROC_FIELD_DESCRIPTOR)
FIELD_AMOUNT("cutoff_chanaft", chanaft, cutoff)
FIELD_AMOUNT("resonance_chanaft", chanaft, resonance)
FIELD_AMOUNT("cutoff_polyaft", polyaft, cutoff)
FIELD_AMOUNT("resonance_polyaft", polyaft, resonance)
FIELD_DEPTH_SET(fileg, cutoff2, "_depth2")
FIELD_MOD("fileg_vel2depth2", amount, fileg, vel, cutoff2)
FIELD_DEPTH_SET(fillfo, cutoff2, "_depth2")
FIELD_AMOUNT("cutoff2_chanaft", chanaft, cutoff2)
FIELD_AMOUNT("resonance2_chanaft", chanaft, resonance2)
FIELD_AMOUNT("cutoff2_polyaft", polyaft, cutoff2)
FIELD_AMOUNT("resonance2_polyaft", polyaft, resonance2)
FIELD_AMOUNT_CC_("gain", gain)
FIELD_AMOUNT_CC_("cutoff", cutoff)
FIELD_AMOUNT_CC_("resonance", resonance)
FIELD_AMOUNT_CC_("cutoff2", cutoff2)
FIELD_AMOUNT_CC_("resonance2", resonance2)
FIELD_AMOUNT_CC_("pitch", pitch)
FIELD_AMOUNT_CC_("tune", pitch)
FIELD_AMOUNT_CC_("tonectl", tonectl)
FIELD_AMOUNT_CC_("pan", pan)
FIELD_AMOUNT_CC_("amplitude", amplitude)
FIELD_VOICE_NIF("amp_random", sampler_nif_addrandom, 0)
FIELD_VOICE_NIF("fil_random", sampler_nif_addrandom, 1)
FIELD_VOICE_NIF("pitch_random", sampler_nif_addrandom, 2)
FIELD_VOICE_NIF("pitch_veltrack", sampler_nif_vel2pitch, 0)
FIELD_VOICE_NIF("offset_veltrack", sampler_nif_vel2offset, 0)
FIELD_VOICE_NIF("reloffset_veltrack", sampler_nif_vel2reloffset, 0)
FIELD_PREVOICE_NIF("delay_random", sampler_nif_addrandomdelay, 0)
FIELD_PREVOICE_NIF("sync_beats", sampler_nif_syncbeats, 0)
FIELD_PREVOICE_NIF("delay_cc#", sampler_nif_cc2delay, NIF_VARIANT_CC)
FIELD_PREVOICE_NIF("delay_curvecc#", sampler_nif_cc2delay, NIF_VARIANT_CURVECC)
FIELD_PREVOICE_NIF("delay_stepcc#", sampler_nif_cc2delay, NIF_VARIANT_STEPCC)
FIELD_VOICE_NIF("reloffset_cc#", sampler_nif_cc2reloffset, NIF_VARIANT_CC)
FIELD_VOICE_NIF("reloffset_curvecc#", sampler_nif_cc2reloffset, NIF_VARIANT_CURVECC)
FIELD_VOICE_NIF("reloffset_stepcc#", sampler_nif_cc2reloffset, NIF_VARIANT_STEPCC)
FIELD_VOICE_NIF("offset_cc#", sampler_nif_cc2offset, NIF_VARIANT_CC)
FIELD_VOICE_NIF("offset_curvecc#", sampler_nif_cc2offset, NIF_VARIANT_CURVECC)
FIELD_VOICE_NIF("offset_stepcc#", sampler_nif_cc2offset, NIF_VARIANT_STEPCC)
FIELD_FLEX_LFO("lfo#_freq", freq)
FIELD_FLEX_LFO("lfo#_delay", delay)
FIELD_FLEX_LFO("lfo#_fade", fade)
FIELD_FLEX_LFO("lfo#_wave", wave)
FIELD_FLEX_LFO("lfo#_phase", phase)
FIELD_FLEX_LFO("lfo#_count", count)
FIELD_FLEX_LFO_DEST("lfo#_cutoff", cutoff)
FIELD_FLEX_LFO_DEST("lfo#_resonance", resonance)
FIELD_FLEX_LFO_DEST("lfo#_cutoff2", cutoff2)
FIELD_FLEX_LFO_DEST("lfo#_resonance2", resonance2)
FIELD_FLEX_LFO_DEST("lfo#_pitch", pitch)
FIELD_FLEX_LFO_DEST("lfo#_tune", pitch)
FIELD_FLEX_LFO_DEST("lfo#_tonectl", tonectl)
FIELD_FLEX_LFO_DEST("lfo#_pan", pan)
FIELD_FLEX_LFO_DEST("lfo#_amplitude", amplitude)
FIELD_FLEX_LFO_DEST("lfo#_gain", gain)
FIELD_ALIAS("hilev", "hivel")
FIELD_ALIAS("lolev", "lovel")
FIELD_ALIAS("loopstart", "loop_start")
FIELD_ALIAS("loopend", "loop_end")
FIELD_ALIAS("loopmode", "loop_mode")
FIELD_ALIAS("bendup", "bend_up")
FIELD_ALIAS("benddown", "bend_down")
FIELD_ALIAS("offby", "off_by")
FIELD_ALIAS("offset_oncc#", "offset_cc#")
FIELD_ALIAS("reloffset_oncc#", "reloffset_cc#")
FIELD_ALIAS("delay_oncc#", "delay_cc#")
//NONFUNCTIONAL Opcodes can still be looked up and used as strings in a GUI
FIELD_NONFUNCTIONAL("region_label") //ARIA
FIELD_NONFUNCTIONAL("group_label") //ARIA
FIELD_NONFUNCTIONAL("master_label") //ARIA
FIELD_NONFUNCTIONAL("global_label") //ARIA
FIELD_NONFUNCTIONAL("sw_label") //Keyswitch. ARIA
//label_cc and label_key are in sfzloader.c because they are under <control>
//Ignore List #TODO
FIELD_NONFUNCTIONAL("ampeg_dynamic") //ARIA
{ "genericmod_#_#_#_#", -1, slpt_generic_modulation, 0, 0, NULL, NULL, NULL },
};
#define NPARAMS (sizeof(sampler_layer_params) / sizeof(sampler_layer_params[0]))
static void sampler_layer_apply_unknown(struct sampler_layer *l, const char *key, const char *value);
static int compare_entries(const void *p1, const void *p2)
{
const struct sampler_layer_param_entry *e1 = p1, *e2 = p2;
return strcmp(e1->name, e2->name);
}
void sampler_layer_prepare_params(void)
{
qsort(sampler_layer_params, NPARAMS, sizeof(struct sampler_layer_param_entry), compare_entries);
for (size_t i = 0; i < NPARAMS; ++i)
{
struct sampler_layer_param_entry *e = &sampler_layer_params[i];
if (e->type == slpt_alias)
{
struct sampler_layer_param_entry prototype;
prototype.name = e->extra_ptr;
void *found = bsearch(&prototype, sampler_layer_params, NPARAMS, sizeof(sampler_layer_params[0]), compare_entries);
if (!found)
printf("Alias %s redirects to non-existent name (%s)\n", e->name, prototype.name);
assert(found);
e->extra_ptr = found;
}
if (i)
{
struct sampler_layer_param_entry *prev_e = &sampler_layer_params[i - 1];
if (!strcmp(e->name, prev_e->name))
{
printf("Duplicate parameter %s\n", e->name);
assert(FALSE);
}
}
}
}
// This only works for setting. Unsetting is slightly different.
static gboolean override_logic(gboolean is_equal, gboolean has_value, gboolean set_local_value)
{
if (!set_local_value && has_value) // Do not override locally set values
return FALSE;
// Override if a value or a inherited value is replaced with a local setting.
return (!is_equal) || (set_local_value != has_value);
}
static inline void mod_key_decode(uint64_t extra_int, const uint32_t *args, struct sampler_modulation_key *mod_key)
{
uint32_t modsrc = (extra_int & 0xFFF);
uint32_t modsrc2 = ((extra_int >> 12) & 0xFFF);
if (modsrc == MODSRC_CC)
modsrc = args[0];
if (modsrc2 == MODSRC_CC)
modsrc2 = args[0];
mod_key->src = modsrc;
mod_key->src2 = modsrc2;
mod_key->dest = ((extra_int >> 24) & 0xFF);
}
static inline void nif_key_decode(uint64_t extra_int, void *extra_ptr, const uint32_t *args, struct sampler_noteinitfunc_key *nif_key)
{
uint32_t variant = extra_int &~ NIF_VARIANT_MASK;
nif_key->notefunc_voice = extra_ptr;
if (extra_int & NIF_VARIANT_MASK)
{
int cc = args[0] & 255;
variant = cc + (variant << 8);
}
nif_key->variant = variant;
}
static inline void flex_lfo_key_decode(const uint32_t *args, struct sampler_flex_lfo_key *flex_lfo_key)
{
flex_lfo_key->id = args[0];
}
static inline void flex_lfo_key_and_cc_decode(const uint32_t *args, struct sampler_flex_lfo_key *flex_lfo_key, uint32_t *cc)
{
flex_lfo_key->id = args[0];
*cc = args[1];
}
#define OVERRIDE_LOGIC(type) override_logic(!memcmp(p, data_ptr, sizeof(type)), e->get_has_value(&l->data), set_local_value)
#define CAST_FLOAT_VALUE fvalue = *(double *)data_ptr
gboolean sampler_layer_param_entry_set_from_ptr(const struct sampler_layer_param_entry *e, struct sampler_layer *l, gboolean set_local_value, const void *data_ptr, const uint32_t *args, GError **error)
{
void *p = ((uint8_t *)&l->data) + e->offset;
uint32_t cc = 0;
double fvalue = 0;
struct sampler_modulation_key mod_key = {0, 0, 0};
struct sampler_noteinitfunc_key nif_key = {{NULL}, 0};
struct sampler_flex_lfo_key flex_lfo_key = {0};
switch(e->type)
{
case slpt_midi_note_t:
if (!OVERRIDE_LOGIC(midi_note_t))
return TRUE;
memcpy(p, data_ptr, sizeof(midi_note_t));
break;
case slpt_int:
if (!OVERRIDE_LOGIC(int))
return TRUE;
memcpy(p, data_ptr, sizeof(int));
break;
case slpt_enum:
case slpt_uint32_t:
if (!OVERRIDE_LOGIC(uint32_t))
return TRUE;
memcpy(p, data_ptr, sizeof(uint32_t));
break;
case slpt_string:
{
char **pc = p;
gboolean str_differs = (!*pc != !data_ptr) || strcmp(*pc, data_ptr);
if (!override_logic(!str_differs, e->get_has_value(&l->data), set_local_value))
return TRUE;
if (str_differs)
{
free(*pc);
*pc = g_strdup(data_ptr);
gboolean *changed_ptr = (gboolean *)(((uint8_t *)&l->data) + e->extra_int);
*changed_ptr = 1;
}
}
break;
case slpt_float:
case slpt_dBamp:
fvalue = *(double *)data_ptr;
if (!override_logic((float)fvalue == *(float *)p, e->get_has_value(&l->data), set_local_value))
return TRUE;
*(float *)p = fvalue;
break;
case slpt_midicurve:
CAST_FLOAT_VALUE;
if (args[0] >= 0 && args[0] <= 127)
{
gboolean (*setgethasfunc)(struct sampler_layer_data *, uint32_t, int) = e->extra_ptr;
float *dst = &((struct sampler_midi_curve *)p)->values[args[0]];
if (!override_logic(*dst == fvalue, setgethasfunc(&l->data, args[0], -1), set_local_value))
return TRUE;
*dst = fvalue;
setgethasfunc(&l->data, args[0], set_local_value);
}
else
{
g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Curve entry index (%u) is out of range for %s", (unsigned)args[0], e->name);
return FALSE;
}
break;
case slpt_ccrange:
{
int number = *(int *)data_ptr;
cc = args[0];
switch(e->extra_int) {
case 0:
sampler_cc_range_set_locc_by_offset(l, e->offset, &(struct sampler_cc_range_key){cc}, set_local_value, number);
break;
case 1:
sampler_cc_range_set_hicc_by_offset(l, e->offset, &(struct sampler_cc_range_key){cc}, set_local_value, number);
break;
default: assert(0);
}
break;
}
case slpt_mod_amount:
CAST_FLOAT_VALUE;
mod_key_decode(e->extra_int, args, &mod_key);
sampler_modulation_set_amount_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
break;
case slpt_mod_curveid:
CAST_FLOAT_VALUE;
mod_key_decode(e->extra_int, args, &mod_key);
sampler_modulation_set_curve_id_by_offset(l, e->offset, &mod_key, set_local_value, (int)fvalue);
break;
case slpt_mod_smooth:
CAST_FLOAT_VALUE;
mod_key_decode(e->extra_int, args, &mod_key);
sampler_modulation_set_smooth_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
break;
case slpt_mod_step:
CAST_FLOAT_VALUE;
mod_key_decode(e->extra_int, args, &mod_key);
sampler_modulation_set_step_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
break;
case slpt_generic_modulation:
CAST_FLOAT_VALUE;
sampler_modulation_set_amount_by_offset(l, e->offset, &(struct sampler_modulation_key){args[0], args[1], args[2]}, set_local_value, fvalue);
sampler_modulation_set_curve_id_by_offset(l, e->offset, &(struct sampler_modulation_key){args[0], args[1], args[2]}, set_local_value, (int)args[3]);
break;
case slpt_voice_nif:
case slpt_prevoice_nif:
CAST_FLOAT_VALUE;
nif_key_decode(e->extra_int, e->extra_ptr, args, &nif_key);
switch(e->extra_int & NIF_VARIANT_MASK)
{
case 0:
case NIF_VARIANT_CC:
sampler_noteinitfunc_set_value_by_offset(l, e->offset, &nif_key, set_local_value, fvalue);
break;
case NIF_VARIANT_CURVECC:
sampler_noteinitfunc_set_curve_id_by_offset(l, e->offset, &nif_key, set_local_value, (int)fvalue);
break;
case NIF_VARIANT_STEPCC:
sampler_noteinitfunc_set_step_by_offset(l, e->offset, &nif_key, set_local_value, fvalue);
break;
}
break;
case slpt_flex_lfo:
CAST_FLOAT_VALUE;
flex_lfo_key_decode(args, &flex_lfo_key);
switch(e->extra_int)
{
case sampler_flex_lfo_value_field_freq:
sampler_flex_lfo_set_freq_by_offset(l, e->offset, &flex_lfo_key, set_local_value, fvalue);
break;
case sampler_flex_lfo_value_field_delay:
sampler_flex_lfo_set_delay_by_offset(l, e->offset, &flex_lfo_key, set_local_value, fvalue);
break;
case sampler_flex_lfo_value_field_fade:
sampler_flex_lfo_set_fade_by_offset(l, e->offset, &flex_lfo_key, set_local_value, fvalue);
break;
case sampler_flex_lfo_value_field_wave:
sampler_flex_lfo_set_wave_by_offset(l, e->offset, &flex_lfo_key, set_local_value, (int)fvalue);
break;
case sampler_flex_lfo_value_field_phase:
sampler_flex_lfo_set_phase_by_offset(l, e->offset, &flex_lfo_key, set_local_value, fvalue);
break;
case sampler_flex_lfo_value_field_count:
sampler_flex_lfo_set_count_by_offset(l, e->offset, &flex_lfo_key, set_local_value, (int)fvalue);
break;
}
break;
case slpt_flex_lfo_dest:
CAST_FLOAT_VALUE;
flex_lfo_key_decode(args, &flex_lfo_key);
mod_key.src = SMSRC_FLEXLFO_BY_NUM(flex_lfo_key.id);
mod_key.src2 = smsrc_none;
mod_key.dest = e->extra_int;
sampler_modulation_set_amount_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
break;
case slpt_flex_lfo_dest_cc:
CAST_FLOAT_VALUE;
flex_lfo_key_and_cc_decode(args, &flex_lfo_key, &cc);
mod_key.src = SMSRC_FLEXLFO_BY_NUM(flex_lfo_key.id);
mod_key.src2 = cc;
mod_key.dest = e->extra_int & 0xFFFFFFFF;
switch(e->extra_int >> 32) {
case sampler_modulation_value_field_amount:
sampler_modulation_set_amount_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
break;
case sampler_modulation_value_field_smooth:
sampler_modulation_set_smooth_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
break;
case sampler_modulation_value_field_step:
sampler_modulation_set_step_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
break;
case sampler_modulation_value_field_curve_id:
sampler_modulation_set_curve_id_by_offset(l, e->offset, &mod_key, set_local_value, fvalue);
break;
default:
assert(false);
break;
}
break;
case slpt_reserved:
case slpt_invalid:
case slpt_alias:
case slpt_nonfunctional:
printf("Unhandled parameter type of parameter %s\n", e->name);
assert(0);
return FALSE;
}
if (e->set_has_value)
e->set_has_value(&l->data, set_local_value);
if (l->child_layers) {
/* Propagate to children */
GHashTableIter iter;
g_hash_table_iter_init(&iter, l->child_layers);
gpointer key, value;
while(g_hash_table_iter_next(&iter, &key, &value))
{
struct sampler_layer *child = value;
if (!sampler_layer_param_entry_set_from_ptr(e, child, FALSE, data_ptr, args, error))
return FALSE;
}
}
return TRUE;
}
#define VERIFY_FLOAT_VALUE do { if (!atof_C_verify(e->name, value, &fvalue, error)) return FALSE; } while(0)
gboolean sampler_layer_param_entry_set_from_string(const struct sampler_layer_param_entry *e, struct sampler_layer *l, gboolean set_local_value, const char *value, const uint32_t *args, GError **error)
{
double fvalue;
switch(e->type)
{
case slpt_midi_note_t:
{
midi_note_t note = sfz_note_from_string(value);
if (note < -1)
{
g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "'%s' is not a valid note name for %s", value, e->name);
return FALSE;
}
return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, ¬e, args, error);
}
case slpt_int:
{
char *endptr;
errno = 0;
int number = strtol(value, &endptr, 10);
if (errno || *endptr || endptr == value)
{
g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "'%s' is not a correct integer value for %s", value, e->name);
return FALSE;
}
return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &number, args, error);
}
case slpt_enum:
{
gboolean (*func)(const char *, uint32_t *value);
func = e->extra_ptr;
uint32_t data = 0;
if (!func(value, &data))
{
g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "'%s' is not a correct value for %s", value, e->name);
return FALSE;
}
return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &data, args, error);
}
case slpt_uint32_t:
{
char *endptr;
errno = 0;
uint32_t number = (uint32_t)strtoul(value, &endptr, 10);
if (errno || *endptr || endptr == value || value[0] == '-')
{
g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "'%s' is not a correct unsigned integer value for %s", value, e->name);
return FALSE;
}
return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &number, args, error);
}
case slpt_string:
return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, value, args, error);
case slpt_ccrange:
{
char *endptr;
errno = 0;
int number = strtol(value, &endptr, 10);
if (errno || *endptr || endptr == value || number < 0 || number > 127)
{
g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "'%s' is not a correct control change value for %s", value, e->name);
return FALSE;
}
return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &number, args, error);
}
case slpt_nonfunctional:
{
char *argptr = strchr(e->name, '#');
if (argptr)
{
int rootlen = argptr - e->name;
char buf[128];
strncpy(buf, e->name, rootlen);
sprintf(buf + rootlen, "%d", args[0]);
sampler_layer_apply_unknown(l, buf, value);
}
else
sampler_layer_apply_unknown(l, e->name, value);
return TRUE;
}
case slpt_float:
case slpt_dBamp:
default:
VERIFY_FLOAT_VALUE;
return sampler_layer_param_entry_set_from_ptr(e, l, set_local_value, &fvalue, args, error);
}
}
#define COPY_NUM_FROM_PARENT(case_value, type) \
case case_value: \
if (!unset_local_value && e->get_has_value(&l->data)) \
return TRUE; \
*(type *)p = pp ? *(type *)pp : (type)e->def_value; \
e->set_has_value(&l->data, 0); \
break;
gboolean sampler_layer_param_entry_unset(const struct sampler_layer_param_entry *e, struct sampler_layer *l, gboolean unset_local_value, const uint32_t *args, GError **error)
{
void *p = ((uint8_t *)&l->data) + e->offset;
void *pp = l->parent ? ((uint8_t *)&l->parent->data) + e->offset : NULL;
uint32_t cc;
struct sampler_modulation_key mod_key = {0, 0, 0};
struct sampler_noteinitfunc_key nif_key = {{NULL}, 0};
struct sampler_flex_lfo_key flex_lfo_key = {0};
switch(e->type)
{
COPY_NUM_FROM_PARENT(slpt_midi_note_t, midi_note_t)
COPY_NUM_FROM_PARENT(slpt_int, int)
COPY_NUM_FROM_PARENT(slpt_enum, uint32_t) // XXXKF that's a kludge, enums are not guaranteed to be uint32_t (but they should be on common platforms)
COPY_NUM_FROM_PARENT(slpt_uint32_t, uint32_t)
COPY_NUM_FROM_PARENT(slpt_float, float)
COPY_NUM_FROM_PARENT(slpt_dBamp, float)
case slpt_string:
{
if (!unset_local_value && e->get_has_value(&l->data))
return TRUE;
char **pc = p;
free(*pc);
*pc = pp ? g_strdup(*(const char **)pp) : NULL;
e->set_has_value(&l->data, 0);
gboolean *changed_ptr = (gboolean *)(((uint8_t *)&l->data) + e->extra_int);
*changed_ptr = 1;
}
return TRUE;
case slpt_midicurve:
if (args[0] >= 0 && args[0] <= 127)
{
struct sampler_midi_curve *curve = p, *parent_curve = pp;
gboolean (*setgethasfunc)(struct sampler_layer_data *, uint32_t, gboolean value) = e->extra_ptr;
if (setgethasfunc(&l->data, args[0], -1) && !unset_local_value)
return TRUE;
curve->values[args[0]] = parent_curve ? parent_curve->values[args[0]] : SAMPLER_CURVE_GAP;
setgethasfunc(&l->data, args[0], 0);
break;
}
else
{
g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Curve entry index (%u) is out of range for %s", (unsigned)args[0], e->name);
return FALSE;
}
case slpt_ccrange:
cc = args[0];
if (!sampler_cc_range_unset_by_offset(l, e->offset, &(struct sampler_cc_range_key){cc}, unset_local_value, 1 << e->extra_int))
{
if (!unset_local_value)
return TRUE;
g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Controller number %d not used for %s", cc, e->name);
return FALSE;
}
break;
case slpt_mod_amount:
mod_key_decode(e->extra_int, args, &mod_key);
sampler_modulation_unset_by_offset(l, e->offset, &mod_key, unset_local_value, 1 << sampler_modulation_value_field_amount);
break;
case slpt_mod_curveid:
mod_key_decode(e->extra_int, args, &mod_key);
sampler_modulation_unset_by_offset(l, e->offset, &mod_key, unset_local_value, 1 << sampler_modulation_value_field_curve_id);
break;
case slpt_mod_smooth:
mod_key_decode(e->extra_int, args, &mod_key);
sampler_modulation_unset_by_offset(l, e->offset, &mod_key, unset_local_value, 1 << sampler_modulation_value_field_smooth);
break;
case slpt_mod_step:
mod_key_decode(e->extra_int, args, &mod_key);
sampler_modulation_unset_by_offset(l, e->offset, &mod_key, unset_local_value, 1 << sampler_modulation_value_field_step);
break;
case slpt_generic_modulation:
mod_key = (struct sampler_modulation_key){args[0], args[1], args[2]};
sampler_modulation_unset_by_offset(l, e->offset, &mod_key, unset_local_value, (1 << sampler_modulation_value_field_amount) | (1 << sampler_modulation_value_field_curve_id));
break;
case slpt_voice_nif:
case slpt_prevoice_nif:
{
nif_key_decode(e->extra_int, e->extra_ptr, args, &nif_key);
static const uint32_t value_fields[] = {
sampler_noteinitfunc_value_field_value, sampler_noteinitfunc_value_field_value,
sampler_noteinitfunc_value_field_curve_id, sampler_noteinitfunc_value_field_step,
};
if (!sampler_noteinitfunc_unset_by_offset(l, e->offset, &nif_key, unset_local_value, 1 << value_fields[e->extra_int >> 24]))
{
if (!unset_local_value)
return TRUE;
if (e->extra_int & NIF_VARIANT_MASK)
g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Controller number %d not used for %s", args[0], e->name);
else
g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "%s not set", e->name);
return FALSE;
}
break;
}